Repository navigation
fix(provider): stabilize Gemma batching memory and decode masks #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6dc0fff
Add post-load measured-headroom guard (close the estimate-vs-measured…
anupsv 55296b1
fix(provider): bound Gemma batching memory envelope
anupsv 279897b
fix(provider): avoid Gemma batch decode mask divergence
anupsv 5f1c334
Merge remote-tracking branch 'origin/feat/unified-memory-cap' into fi…
anupsv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -999,15 +999,22 @@ func bodyForProvider(rawBody []byte, requiresVision bool, provider *registry.Pro | |
| return rawBody | ||
| } | ||
|
|
||
| // defaultMaxOutputTokens is the ceiling injected into requests that don't set | ||
| // max_tokens. It bounds the worst-case cost of a single inference so the | ||
| // defaultMaxOutputTokens is the generation bound injected into requests that | ||
| // don't set max_tokens. It bounds the worst-case cost of a single inference so the | ||
| // pre-flight balance reservation covers the entire generation; without this | ||
| // cap a consumer could stream output exceeding their reservation and the | ||
| // post-inference charge would fail silently (see GitHub issue #33). Consumers | ||
| // who need longer generations must set max_tokens explicitly and carry the | ||
| // balance to cover it. | ||
| const defaultMaxOutputTokens = 8192 | ||
|
|
||
| func implicitMaxTokensBound(modelMaxOutputLength int) int { | ||
| if modelMaxOutputLength > 0 && modelMaxOutputLength < defaultMaxOutputTokens { | ||
| return modelMaxOutputLength | ||
| } | ||
| return defaultMaxOutputTokens | ||
|
anupsv marked this conversation as resolved.
Resolved
|
||
| } | ||
|
anupsv marked this conversation as resolved.
Resolved
anupsv marked this conversation as resolved.
Resolved
|
||
|
|
||
|
anupsv marked this conversation as resolved.
Resolved
|
||
| // explicitMaxTokens returns the consumer-specified max output tokens from any | ||
| // of the recognized field names, or 0 if none were set. | ||
| func explicitMaxTokens(parsed map[string]any) int { | ||
|
|
@@ -1176,11 +1183,10 @@ func (s *Server) reserveAdditionalForProvider(pr *registry.PendingRequest, provi | |
| // ensureMaxTokensBound injects a max-tokens bound into parsed when the | ||
| // consumer didn't specify any max-tokens field, so the outgoing request to | ||
| // the provider is bounded by the amount we reserve upfront. The bound is | ||
| // the model's max_output_length from the registry (or defaultMaxOutputTokens | ||
| // as fallback). The injected field name depends on the API flavor: Responses | ||
| // API uses max_output_tokens, everything else uses max_tokens. Returns true | ||
| // when an injection occurred, so the caller can re-marshal the outgoing body | ||
| // if needed. | ||
| // the platform default clamped by the model's max_output_length when lower. | ||
| // The injected field name depends on the API flavor: Responses API uses | ||
| // max_output_tokens, everything else uses max_tokens. Returns true when an | ||
|
anupsv marked this conversation as resolved.
Resolved
anupsv marked this conversation as resolved.
Resolved
|
||
| // injection occurred, so the caller can re-marshal the outgoing body if needed. | ||
| func ensureMaxTokensBound(parsed map[string]any, isResponsesAPI bool, bound int) bool { | ||
| if n := explicitMaxTokens(parsed); n > 0 { | ||
| // Normalize alias fields the provider engine doesn't read: a chat | ||
|
|
@@ -1532,7 +1538,7 @@ func (s *Server) handleChatCompletions(w http.ResponseWriter, r *http.Request) { | |
|
|
||
| // Inject model-specific defaults from the registry: reasoning_parser | ||
| // and max_tokens bound. Single DB lookup (cached for platform prices). | ||
| maxOutputBound := defaultMaxOutputTokens | ||
| maxOutputBound := implicitMaxTokensBound(0) | ||
| if rec, err := s.store.GetModelRegistryRecord(model); err == nil { | ||
|
anupsv marked this conversation as resolved.
Resolved
|
||
| // Reasoning parser from runtime_parameters. | ||
| if _, hasRP := parsed["reasoning_parser"]; !hasRP && rec.RuntimeParameters != nil { | ||
|
|
@@ -1541,18 +1547,15 @@ func (s *Server) handleChatCompletions(w http.ResponseWriter, r *http.Request) { | |
| rawBody, _ = marshalForwardBody(parsed) | ||
| } | ||
| } | ||
| // Use the registry's max_output_length as the default max_tokens | ||
| // bound instead of the hardcoded 8192. This lets models like | ||
| // GPT-OSS 20B (32K output) generate longer responses when the | ||
| // consumer omits max_tokens. | ||
| if rec.MaxOutputLength > 0 { | ||
| maxOutputBound = rec.MaxOutputLength | ||
| } | ||
| // max_output_length is an advertised maximum, not the implicit default. | ||
| // Omitted max_tokens stays on the safer platform default; models with a | ||
| // lower published max still clamp the injected bound to their maximum. | ||
| maxOutputBound = implicitMaxTokensBound(rec.MaxOutputLength) | ||
| } | ||
|
|
||
| // Bound the generation so the pre-flight reservation covers it. If the | ||
| // consumer didn't set max_tokens, inject the model's max_output_length | ||
| // (or defaultMaxOutputTokens as fallback). Without this bound the | ||
| // consumer didn't set max_tokens, inject the platform default clamped by | ||
| // the model's max_output_length when lower. Without this bound the | ||
| // provider could return more tokens than we reserved for, and the | ||
| // silent post-inference charge failure would hand the consumer free | ||
| // inference (GitHub issue #33). | ||
|
|
@@ -4100,9 +4103,9 @@ func (s *Server) handleGenericInference(w http.ResponseWriter, r *http.Request, | |
| // Completions and Anthropic messages both use the max_tokens field (never | ||
| // max_output_tokens, which is Responses API only). Inject a default if | ||
| // unset so the pre-flight reservation bounds the generation. | ||
| genericMaxOutput := defaultMaxOutputTokens | ||
| genericMaxOutput := implicitMaxTokensBound(0) | ||
| if rec, err := s.store.GetModelRegistryRecord(model); err == nil && rec.MaxOutputLength > 0 { | ||
| genericMaxOutput = rec.MaxOutputLength | ||
| genericMaxOutput = implicitMaxTokensBound(rec.MaxOutputLength) | ||
|
anupsv marked this conversation as resolved.
Resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 [INFO] 🏷️ handleGenericInference uses map[string]any for JSON parsing 💡 Suggestion: Consider defining a typed struct for generic inference requests with proper field tags 📊 Score: 2×2 = 4 · Category: map[string]any usage |
||
| } | ||
| ensureMaxTokensBound(parsed, false, genericMaxOutput) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule mlx-swift-lm
updated
2 files
| +15 −0 | Libraries/MLXLMCommon/BatchKVCache.swift | |
| +55 −0 | Tests/MLXLMTests/ContinuousBatchingTests.swift |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.