Skip to content

Auto-enable padding-free SFT - #3672

Merged
danielhanchen merged 68 commits into
unslothai:mainfrom
djsaunde:padding-free-seqlen-metadata-v2
Dec 10, 2025
Merged

danielhanchen merged 68 commits into
unslothai:mainfrom
djsaunde:padding-free-seqlen-metadata-v2

Conversation

@djsaunde

@djsaunde djsaunde commented Dec 2, 2025

Copy link
Copy Markdown
Contributor

This PR is built on top of #3566, and should be merged after it.

This PR auto-sets padding_free=True when applicable (text-only SFT training) and does some work behind the scenes to compute sequence length metadata, so we can use the varlen flash attention kernels / block-diagonal SDPA / xformers kernels.

This gives us throughput gains with sufficiently large models / batch sizes; e.g., a super small model like unsloth/qwen2.5-0.5b requires per_device_train_batch_size = 16 or higher in order to see (significant) throughput gains, while unsloth/llama-3-8b requires only per_device_train_batch_size = 4 or higher. For example, with unsloth/llama-3-8b with per_device_train_batch_size = 8, we observe much faster training (52s padding-free vs 96s not; about 1.85x speedup).

image

There are very slight loss and gradient norm differences in the padding-free vs. not settings; I think these can be chalked up to the different kernels being used (FA2 varlen vs. dense, block-diagonal vs. causal kernels for xformers / SDPA).

djsaunde and others added 30 commits November 23, 2025 11:20
@djsaunde
djsaunde force-pushed the padding-free-seqlen-metadata-v2 branch from d5b342f to a69f35b Compare December 9, 2025 22:03
@djsaunde

Copy link
Copy Markdown
Contributor Author

Closing in favor of #3702.

@djsaunde djsaunde closed this Dec 10, 2025
@djsaunde djsaunde reopened this Dec 10, 2025
Comment thread unsloth/trainer.py Resolved
@danielhanchen
danielhanchen merged commit 35606da into unslothai:main Dec 10, 2025
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
* implement (sdpa, xformers, fa2) sample packing

* attention dispatching

* ddp working OOTB with CLI

* packed SWA and softcap support

* enable batch flattening

* LGPL license headers

* mask packed sequence boundaries

* auto-enable sample packing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add explicit toggle for sample packing

* Add explicit toggle for sample packing

* Update __init__.py

* Update unsloth/kernels/rope_embedding.py

* Update unsloth/kernels/rope_embedding.py

* remove grad output clones; restore deleted FastLanguageModel arg

* fix

* restore rope embedding clones

* xformers mask cache

* implement (sdpa, xformers, fa2) sample packing

* attention dispatching

* ddp working OOTB with CLI

* packed SWA and softcap support

* enable batch flattening

* LGPL license headers

* mask packed sequence boundaries

* auto-enable sample packing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add explicit toggle for sample packing

* Add explicit toggle for sample packing

* Update __init__.py

* Update unsloth/kernels/rope_embedding.py

* Update unsloth/kernels/rope_embedding.py

* remove grad output clones; restore deleted FastLanguageModel arg

* fix

* restore rope embedding clones

* xformers mask cache

* add back accidental deletion

* Update unsloth/kernels/rope_embedding.py

Co-authored-by: Daniel Han <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix merge conflicts

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add **kwargs

* add back clobbered

* Update rope_embedding.py

* Update rope_embedding.py

* simplify trl warnings filter

* docstring

* nit

* bugfix

* add padding-free seqlen metadata

* auto-enable padding free

* gemma2 disable

* Apply suggestion from @danielhanchen

* Update trainer.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update trainer.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han <[email protected]>
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