Skip to content

Conversation

@galopyz
Copy link
Contributor

@galopyz galopyz commented Oct 13, 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)

This PR also adds ModernBert sequence packing. With sequence packing, we can pack sequences together into a packed sequence of size [int(batch_size / micro_batch_size), int(micro_batch_size * max_seq_len)]. This ensures we are feeding fixed number of tokens every microbatch, unlike just unpadding approach where microbatches can have varying number of tokens depending on the length of the sequences.

There are also some minor changes:

  • Also used variables for micro batch size and batch size in yaml.

  • To use sequence packing, numba is added.

  • Disabled aim logger.

  • Added full fine tuning option to custom_model_training.py.

Copy link
Contributor

@vishalbakshi vishalbakshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@galopyz and I have both run multiple successful training runs (locally and on Modal) using this branch (modernbert_sequence_packing).

@vishalbakshi vishalbakshi merged commit 8d45a5c into main Oct 14, 2025
1 check passed
@vishalbakshi vishalbakshi deleted the modernbert_sequence_packing branch October 14, 2025 02:08
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