Repository navigation
Fix batched generation for prompts of different lengths - #2216
Merged
Merged
Conversation
Datta0
reviewed
Mar 28, 2025
Contributor
Author
|
@Datta0 What do you think? What else should we do before merging? |
Contributor
Author
|
@Datta0 do you know what the process is for getting approval of a maintainer? |
Contributor
|
Hey @RunFMe , I will text Daniel about this |
Contributor
Author
|
@Datta0 done) |
|
This helped me solve batch processing issue in LLaMA 3.2 1B Instruct model, please merge this to unsloth. |
Member
|
Whoops apologies totally missed this - great work! |
abiswas-realadvice
pushed a commit
to abiswas-realadvice/unsloth
that referenced
this pull request
May 14, 2026
* fix ignoring of attention mask after prefill stage in decoding * update naming to avoid confusion --------- Co-authored-by: Неизвестный Пользователь722497 <[email protected]>
danielhanchen
added a commit
that referenced
this pull request
Jun 10, 2026
…egress again (#1066, #3699) (#6145) * Add regression guard for batched left-padded generation (#1066, #3699) Three layers of tests plus a path-filtered CI workflow so the left-padding position_ids / attention-mask bug class cannot silently return: - tests/utils/test_prepare_inputs_ast_guard.py: import-free AST checks on _fast_prepare_inputs_for_generation (cumsum-from-mask branch present, cache_position only as fallback, no mask truncation, model families wired) - tests/utils/test_prepare_inputs_leftpad.py: CPU behavioral unit test with synthetic left-padded masks and fake caches; exact expected position_ids for prefill and cached decode - tests/utils/test_batched_leftpad_generation_gpu.py: optional GPU e2e, solo vs batched prefix match, skipped without CUDA - .github/workflows/batch-inference-guard.yml: ubuntu-latest CPU job running the two deterministic layers on PRs touching unsloth/models/** Validated: all pass on main; both CPU layers fail at 6d0f864~1 (pre #4100) and at 332eabf~1 (pre #2216), reproducing the historical bug signatures. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Cite staging proof in batch-inference-guard header (staging-2 PRs 170/171) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fold left-padding guard into consolidated Core CI; merge AST + behavioral tests No new workflow and no new CI job: the guard now runs as one HARD GATE step inside consolidated-tests-ci.yml, right after the callback signature drift detector, where the CPU torch stack is already installed. The AST structural checks and the behavioral unit tests live in a single file (tests/utils/test_prepare_inputs_leftpad.py); the AST layer stays stdlib-only with unsloth imported lazily inside the behavioral tests, so import breakage cannot mask the structural checks. Revalidated after the merge: 11 assertions pass on main, 8 fail at 6d0f864~1 (pre #4100). * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update staging proof reference for consolidated gate (PRs 170/172) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.
Previous code
_fast_prepare_inputs_for_generation is called to get forward method arguments for generating new token. Notice that it crops attention_mask and only takes into account the last value. Attention mask of length 1 triggers flag in the forward which makes attention mask None.
I fixed it by copying a piece of code from traditional prepare_inputs_for_generation which calls base_model._prepare_4d_causal_attention_mask_with_cache_position if it's present. This way we allow models which have this function (most popular) to create attention mask as they see fit.