Repository navigation
SFT sample packing - #3566
SFT sample packing#3566
Conversation
072de80 to
efe4424
Compare
f352814 to
9cd0b26
Compare
8f06935 to
50e5104
Compare
|
I tested all the SFT notebooks on the main page README. I'd like to test a few more notebooks, including ones that we don't expect to use packing (multimodal training, RL training, ...). |
|
Okay, I ran through all the notebooks displayed in the README. As expected, non of the non-text-only SFT notebooks utilized sample packing, and none of them hit any errors when switching to this branch. |
585e26f to
e498912
Compare
e498912 to
6c90169
Compare
| window_size = (-1, -1) if (kv_seq_len <= sw) else (sw, sw) | ||
|
|
||
| use_varlen = ( | ||
| seq_info is not None and past_key_value is None and window_size == (-1, -1) |
There was a problem hiding this comment.
Q: So when past_key_value is not None aka decoding phase of generation, do we always use flash_attn_func and not flash_attn_varlen? Is that what this is trying to do? Does that place an implicit assumption that post prefill, inputs are padded to neat shapes?
There was a problem hiding this comment.
correct! the flash attention varlen API doesn't support key/value caching, and we don't pack inputs during inference anyways, so we use the dense API. no such assumption AFAIK, not sure I follow you on that.
| K: Tensor, | ||
| V: Tensor, | ||
| ) -> Tensor: | ||
| """Run attention using config / context info.""" |
There was a problem hiding this comment.
Can we please add a couple of lines of info on which backend is used when and why?
| ) | ||
|
|
||
| if requires_grad: | ||
| K_mod = K_mod.reshape(bsz, kv_seq_len, n_heads, head_dim) |
There was a problem hiding this comment.
Where is the expansion happening? Otherwise reshaping to n_heads would not be possible right?
There was a problem hiding this comment.
just before this, K_mod / V_mod are viewed as (bsz, kv_seq_len, n_kv_heads, 1, head_dim) and then expanded:
K_mod = K_t.view(bsz, kv_seq_len, config.n_kv_heads, 1, head_dim)
V_mod = V_t.view(bsz, kv_seq_len, config.n_kv_heads, 1, head_dim)
K_mod = K_mod.expand(
bsz, kv_seq_len, config.n_kv_heads, config.n_groups, head_dim
)
V_mod = V_mod.expand(
bsz, kv_seq_len, config.n_kv_heads, config.n_groups, head_dim
)
if requires_grad:
K_mod = K_mod.reshape(bsz, kv_seq_len, n_heads, head_dim)
V_mod = V_mod.reshape(bsz, kv_seq_len, n_heads, head_dim)
else:
Q_mod = Q_t.view(
bsz, q_len, config.n_kv_heads, config.n_groups, head_dim
)| attn_bias, XFORMERS_BLOCK_DIAG_CLS | ||
| ) | ||
|
|
||
| if config.n_groups != 1 and not requires_grad and has_block: |
There was a problem hiding this comment.
NIT: Feel like we should condense these into the same check at the cost of repeating the code of single out = xformers_attn()...
There was a problem hiding this comment.
I combined them but kept the single xformers_attn call, hopefully I understood you correctly.
* 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 * Apply suggestion from @danielhanchen * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update unsloth/trainer.py * Update unsloth/trainer.py * Update unsloth/trainer.py * Update unsloth/trainer.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <[email protected]>
This PR patches the SFTTrainer class' constructor to auto-enable sample packing when applicable (i.e., when the user is not training a multi-modal model or passing in a custom data collator). Sample packing is a good default in SFT since we reduce the amount of zero-padding we see and increase the training token/s throughput as a result.
Followup to #3525; can merge after that PR is.I closed that PR, I think we should just merge this one.Needs to be extensively tested so we don't break any existing notebooks / scripts.
Edit: This PR no longer auto-enables sample packing as it could slightly change the SFT training dynamics. Users must now pass
packing=Trueinto theirSFTConfigobjects.