-
Notifications
You must be signed in to change notification settings - Fork 120
Add state to logits processing #425
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
Merged
jonatanklosko
merged 36 commits into
elixir-nx:main
from
bitcrowd:task/sample-6-add-state-to-logits-processing
Nov 18, 2025
Merged
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
fc0825a
[#SAMPLE-6] Add state to logits processing
joelpaulkoch 01ab3af
stateful logits processors
joelpaulkoch 5413662
adding another test
joelpaulkoch 9d4ef39
fix test so compilation works
joelpaulkoch 4ce01cc
demonstrate stateful logits processor through test assertions
joelpaulkoch 2161b77
independent state for batch entries
joelpaulkoch fefc9fd
renamed initial_suppressed_token_index for clarity
xhr15 6e8612a
renamend next_suppressed_index -> :next_suppressed_token_index
xhr15 e43254a
logits_processor_states -> logits_processor_state in batch tests
xhr15 a2f0015
added a test with batch size 1 for clarity
xhr15 0cdc0ad
renaming suppressed_id -> suppressed_token_id
xhr15 cc6d6e3
more comments
xhr15 3816e7c
changed to to demonstrate stack functionality
xhr15 fe58712
merged tests
xhr15 c97890a
removed test for processor only used in test
xhr15 fbf5ef3
introduces LogitsProcessor module
xhr15 dfa223c
clean up
joelpaulkoch 9098bda
mix format
joelpaulkoch 544d80f
vectorized sequences are called sequence in context
joelpaulkoch 2ba5e0a
don't vectorize all the logits processor state
joelpaulkoch 196c8f0
swap {logits, state} to {state, logits}
joelpaulkoch ee2a01e
rename logits_processor_state to logits_processor_states
joelpaulkoch 3563ff0
states as tuples
joelpaulkoch 6db771e
update test
joelpaulkoch c8442e0
single initial state for all batch entries
joelpaulkoch 41dd2ad
vectorize sequence for init, derive vectorized state
joelpaulkoch 311f77c
switch to EXLA as the evaluator is lacking vectorisation support:
xhr15 ec92264
Apply suggestion from @jonatanklosko
xhr15 201e103
removed comments
xhr15 70d7f65
slimmed down comments more
xhr15 ce92584
introduced types for init_context and process_context
xhr15 6d8f494
don't vectorize initial_enforced_token_id in test as it's the same ov…
xhr15 578ce11
bonus track: two more livebooks concerning logits processing. Not str…
xhr15 1f7798f
Update test/bumblebee/text/generation/logits_processing_test.exs
xhr15 e9d0c78
moving livebooks to separate PR
xhr15 1da730e
logits_processor.ex aktualisieren
xhr15 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| defmodule Bumblebee.LogitsProcessor do | ||
| @moduledoc """ | ||
| An interface for configuring and using logits processors. | ||
| Logits processors are used during autoregressive generation to modify | ||
| predicted scores at each generation step. This allows for applying | ||
| certain rules to the model output to control which tokens are picked | ||
| at each generation step, and which are not. | ||
| Every module implementing this behaviour is expected to also define | ||
| a configuration struct. | ||
| """ | ||
|
|
||
| @type t :: Bumblebee.Configurable.t() | ||
|
|
||
| @type state :: Nx.Container.t() | ||
|
|
||
| @type process_context :: %{ | ||
| sequence: Nx.Tensor.t(), | ||
| length: Nx.Tensor.t(), | ||
| input_length: Nx.Tensor.t() | ||
| } | ||
|
|
||
| @type init_context :: %{} | ||
|
|
||
| @doc """ | ||
| Initializes state for a new logits processor. | ||
| Returns `state`, which is an opaque `Nx.Container`, and it is then | ||
| passed to and returned from `process/2`. | ||
| Oftentimes logits processors are stateless, in which case this | ||
| function can return an empty container, such as `{}`. | ||
| """ | ||
| @callback init(t(), init_context()) :: state() | ||
|
|
||
| @doc """ | ||
| Processes logits, applying specific rules. | ||
| """ | ||
| @callback process( | ||
| t(), | ||
| state(), | ||
| logits :: Nx.Tensor.t(), | ||
| context :: process_context() | ||
| ) :: {state :: map(), logits :: Nx.Tensor.t()} | ||
| end | ||
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.