GitHub Repo
Transformers Report Flags Memory Bottleneck in Whole-Word Masking, with Quadratic Costs in Long-Sequence Data Collation
A November 20 issue report says Transformers’ whole-word masking creates an intermediate array that grows quadratically with sequence length; an 8,192-token test used about 1 GiB of memory. The reporter proposed a linear-complexity alternative, but no formal fix had been submitted as of the review.
Hugging Face Transformers received a long-sequence training performance report on November 20: when DataCollatorForLanguageModeling is configured with whole_word_mask=True, the data-collation stage may incur substantial CPU memory and time costs. The reporter reproduced the issue on stable versions 5.23.0 and 5.24.0.dev0. As of the review, the issue remained open, with no linked pull request proposing a fix. Issue report
Whole-word masking masks the subword tokens belonging to the same word together. The official documentation shows that this option is disabled by default. The data collator groups examples into batches and can add random masking, so the report mainly concerns training workflows that explicitly enable whole-word masking. Data collator documentation
The report says the internal _whole_word_mask function creates an array with shape [batch, length, length]. In a CPU test using a ModernBERT tokenizer and a batch size of 8, increasing the sequence length from 512 to 8,192 tokens raised the tracked peak allocation from 4 MiB to about 1,025 MiB, while collation time increased from 0.09 seconds to 3.16 seconds. These figures measure the data-collation stage and should not be taken as complete training throughput or GPU memory requirements. Reproduction code and results
This is especially relevant to long-context encoders. ModernBERT natively supports 8,192 tokens and uses designs such as alternating local and global attention and unpadding. From an engineering perspective, this suggests that even when model computation is optimized, preprocessing may still slow data delivery. The model card confirms its long-context capability but does not validate this performance report. ModernBERT model card
The reporter proposed replacing pairwise comparisons with word ID lookups, making the amount of processing grow linearly with the number of tokens in the batch, and said the local output was consistent. The proposal still requires maintainer review and independent reproduction. Teams using this workflow can measure data-collation time separately from model execution time, then check whether a fix preserves special-token exclusions and masking semantics. Disabling whole-word masking directly would change the training configuration. Proposed alternative
Sources
- Concurrent chat templating silently loses assistant generation masks
- fix(chat templating): isolate concurrent assistant tracking
- SFT Trainer:Train on assistant messages only
- Whole word masking in DataCollatorForLanguageModeling uses O(batch × length²) memory and time
- Transformers:Data collator
- ModernBERT-base 模型卡