Skip to content

Conversation

@galopyz
Copy link
Contributor

@galopyz galopyz commented Aug 25, 2025

This PR adds sequence unpadding and repadding with flash attention 2 varlen for the attention and flash attention rope. Inside of the model, attention_mask is created and sequences are unpadded before passing to decoder layers. After all the decoder layers and final norm, the unpadded sequences are padded again before passing to lm_head. We repad for the loss function calculation. In the future, we can get rid of repadding and do loss calculation ourselves as _unpad_modernbert_input also returns unpadded labels.

Results for sdpa (green) vs. FA2 (red) on wandb: https://wandb.ai/local-research-group/smollm2-135m-training-avelina/workspace?nw=nwusergalopyz. Grey one uses FA2 with batch size of 8 instead of 4. Because there are no padding tokens, I could fit 8bs inside of RTX 3090. (24GB RAM)

@galopyz galopyz marked this pull request as draft August 25, 2025 22:41
@galopyz galopyz marked this pull request as ready for review August 26, 2025 00:21
@galopyz galopyz changed the title Sequence packing Unpadding Aug 26, 2025
@galopyz galopyz changed the title Unpadding Unpadding sequences Aug 26, 2025
@galopyz
Copy link
Contributor Author

galopyz commented Aug 26, 2025

To switch between flash attention and sdpa, we need to change

_attn_implementation = "flash_attention_2",

inside of LlamaConfig to either "flash_attention_2" or "sdpa".

@galopyz galopyz closed this Oct 13, 2025
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.

1 participant