feat(causal_llm): add LogitsProcessor hook for guidance / constrained decoding#2381
Open
JulienBalianSonos wants to merge 1 commit into
Open
feat(causal_llm): add LogitsProcessor hook for guidance / constrained decoding#2381JulienBalianSonos wants to merge 1 commit into
JulienBalianSonos wants to merge 1 commit into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Adds a small, generic hook to
CausalLlmStateso callers can adjust thenext-token logits before selection — the building block for constrained
decoding / guidance (grammar or JSON-Schema masking, logit biasing, custom
sampling), without baking any such logic into
causal_llm.LogitsProcessor::processis called once per token, after the repeatpenalty and just before argmax, with the full token sequence so far.
FnMut(&mut [f32], &[u32])aLogitsProcessor, andNoLogitsProcessoris a no-op default.generate_next_token()now simply delegates togenerate_next_token_with(&mut NoLogitsProcessor), so existing behavior and the public API are unchanged.Why
The sampler does argmax internally with no way to influence it, so downstream
projects can't do grammar/JSON-constrained decoding. This is the minimal seam
to enable it.
We've used it downstream to drive llguidance
for guaranteed JSON / JSON-Schema / Lark-grammar / regex output. None of that
lands here —
causal_llmgains no dependency; the hook is engine-agnostic.Design notes
Debug/freeze/SendofCausalLlmStateare untouched. The processor is passed per call.f32::NEG_INFINITY, thenthe existing argmax picks the best allowed token.
Tests
Added unit tests (no model needed): a closure masks a token;
NoLogitsProcessorleaves logits unchanged.
cargo check -p causal_llmpasses.