Skip to content

fix(Gemma4): add pad token (ID=0) to eosTokenIds to prevent infinite … - #28

Merged
solderzzc merged 2 commits into
mainfrom
fix/gemma4-pad-eos-token
Apr 21, 2026
Merged

solderzzc merged 2 commits into
mainfrom
fix/gemma4-pad-eos-token

Conversation

@solderzzc

Copy link
Copy Markdown
Member

…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:

  • 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.

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 x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

…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.
Copilot AI review requested due to automatic review settings April 21, 2026 19:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 eosTokenIds as an initializer parameter on ModelConfiguration.
  • Add <pad> / token ID 0 as 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.

Comment on lines 113 to 128
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

Copilot AI Apr 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread Libraries/MLXLLM/LLMModelFactory.swift Outdated
// Build a ModelConfiguration with loaded EOS token IDs and tool call format
var mutableConfiguration = configuration
mutableConfiguration.eosTokenIds = eosTokenIds
mutableConfiguration.eosTokenIds = eosTokenIds.union(configuration.eosTokenIds)

Copilot AI Apr 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
mutableConfiguration.eosTokenIds = eosTokenIds.union(configuration.eosTokenIds)
mutableConfiguration.eosTokenIds = eosTokenIds
mutableConfiguration.eosTokenIds.formUnion(configuration.eosTokenIds)

Copilot uses AI. Check for mistakes.
Comment thread Libraries/MLXVLM/VLMModelFactory.swift Outdated

var mutableConfiguration = configuration
mutableConfiguration.eosTokenIds = eosTokenIds
mutableConfiguration.eosTokenIds = eosTokenIds.union(configuration.eosTokenIds)

Copilot AI Apr 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
mutableConfiguration.eosTokenIds = eosTokenIds.union(configuration.eosTokenIds)
mutableConfiguration.eosTokenIds = eosTokenIds
mutableConfiguration.eosTokenIds.formUnion(configuration.eosTokenIds)

Copilot uses AI. Check for mistakes.
@solderzzc
solderzzc merged commit d1ff6d4 into main Apr 21, 2026
6 checks passed
@solderzzc
solderzzc deleted the fix/gemma4-pad-eos-token branch April 21, 2026 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants