Repository navigation
fix(Gemma4): add pad token (ID=0) to eosTokenIds to prevent infinite … - #28
Conversation
…padding loops Gemma-4 generates <pad> (token ID=0) instead of a proper EOS when context exceeds the model's 1024-token sliding window attention limit. Without this fix, the model enters an infinite padding loop taking 120+ seconds to timeout. Changes: - ModelConfiguration: expose eosTokenIds parameter in initializer - LLMModelFactory: add eosTokenIds: [0] to all Gemma-4 LLM configs (gemma4_e4b_it_4bit, gemma4_e2b_it_4bit, gemma4_26BA4B_it_4bit, gemma4_31B_it_4bit) - VLMModelFactory: same for all Gemma-4 VLM configs This terminates generation immediately when the model produces a padding token, preventing both infinite loops and false 'finish_reason: length' responses. The primary fix for prompt overflow is keeping tool schemas under the 1024-token sliding window; this is a safety net for edge cases.
There was a problem hiding this comment.
Pull request overview
Adds a safety-stop for Gemma-4 generation by treating the <pad> token (ID 0) as an EOS/stop condition, and ensures registry-provided stop IDs are preserved alongside IDs loaded from model config files.
Changes:
- Expose
eosTokenIdsas an initializer parameter onModelConfiguration. - Add
<pad>/ token ID0as stop conditions for Gemma-4 LLM + VLM registry entries. - Update LLM/VLM factories to union config-loaded EOS IDs with configuration-provided EOS IDs (instead of overwriting).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| Libraries/MLXVLM/VLMModelFactory.swift | Adds Gemma-4 stop IDs and preserves caller/registry eosTokenIds by unioning with config-loaded IDs. |
| Libraries/MLXLMCommon/ModelConfiguration.swift | Allows passing eosTokenIds via initializer so registries/callers can provide additional stop IDs. |
| Libraries/MLXLLM/LLMModelFactory.swift | Adds Gemma-4 stop IDs, adds missing Gemma-4 registry entries, and unions config-loaded + configuration-provided EOS IDs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public init( | ||
| id: String, revision: String = "main", | ||
| tokenizerSource: TokenizerSource? = nil, | ||
| defaultPrompt: String = "", | ||
| extraEOSTokens: Set<String> = [], | ||
| eosTokenIds: Set<Int> = [], | ||
| toolCallFormat: ToolCallFormat? = nil, | ||
| preparePrompt: (@Sendable (String) -> String)? = nil, | ||
| lazyLoad: Bool = false | ||
| ) { | ||
| self.id = .id(id, revision: revision) | ||
| self.tokenizerSource = tokenizerSource | ||
| self.defaultPrompt = defaultPrompt | ||
| self.extraEOSTokens = extraEOSTokens | ||
| self.eosTokenIds = eosTokenIds | ||
| self.toolCallFormat = toolCallFormat |
There was a problem hiding this comment.
ModelConfiguration.eosTokenIds is now configurable via the initializer, but the stored-property doc comment still reads as if it's only populated from config.json/generation_config.json. Consider updating the documentation to reflect that callers/registries can provide additional stop token IDs that will be combined with the IDs loaded at runtime.
| // Build a ModelConfiguration with loaded EOS token IDs and tool call format | ||
| var mutableConfiguration = configuration | ||
| mutableConfiguration.eosTokenIds = eosTokenIds | ||
| mutableConfiguration.eosTokenIds = eosTokenIds.union(configuration.eosTokenIds) |
There was a problem hiding this comment.
Set.union(_:) allocates a new Set. Since mutableConfiguration.eosTokenIds is mutable, consider using an in-place formUnion pattern to avoid the extra allocation (even if the sets are typically small).
| mutableConfiguration.eosTokenIds = eosTokenIds.union(configuration.eosTokenIds) | |
| mutableConfiguration.eosTokenIds = eosTokenIds | |
| mutableConfiguration.eosTokenIds.formUnion(configuration.eosTokenIds) |
|
|
||
| var mutableConfiguration = configuration | ||
| mutableConfiguration.eosTokenIds = eosTokenIds | ||
| mutableConfiguration.eosTokenIds = eosTokenIds.union(configuration.eosTokenIds) |
There was a problem hiding this comment.
Set.union(_:) allocates a new Set. Since mutableConfiguration.eosTokenIds is mutable, consider using an in-place formUnion pattern to avoid the extra allocation (even if the sets are typically small).
| mutableConfiguration.eosTokenIds = eosTokenIds.union(configuration.eosTokenIds) | |
| mutableConfiguration.eosTokenIds = eosTokenIds | |
| mutableConfiguration.eosTokenIds.formUnion(configuration.eosTokenIds) |
…padding loops
Gemma-4 generates (token ID=0) instead of a proper EOS when context exceeds the model's 1024-token sliding window attention limit. Without this fix, the model enters an infinite padding loop taking 120+ seconds to timeout.
Changes:
This terminates generation immediately when the model produces a padding token, preventing both infinite loops and false 'finish_reason: length' responses. The primary fix for prompt overflow is keeping tool schemas under the 1024-token sliding window; this is a safety net for edge cases.
Proposed changes
Please include a description of the problem or feature this PR is addressing. If there is a corresponding issue, include the issue #.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes