huggingface
config_error
ai_generated
true
RuntimeError: The number of batches in the dataloader (127) is not divisible by gradient_accumulation_steps (8). This may cause uneven gradient updates.
ID: huggingface/gradient-accumulation-steps-mismatch
85%Fix Rate
84%Confidence
1Evidence
2024-02-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| huggingface/transformers 4.37.0 | active | — | — | — |
| huggingface/accelerate 0.27.0 | active | — | — | — |
| huggingface/transformers 4.38.0 | active | — | — | — |
Root Cause
The total number of batches per epoch is not a multiple of gradient_accumulation_steps, leading to a partial update at the end of each epoch.
generic中文
每个 epoch 的总批次数不是 gradient_accumulation_steps 的倍数,导致每个 epoch 结束时出现部分更新。
Official Documentation
https://huggingface.co/docs/transformers/en/main_classes/trainer#gradient-accumulationWorkarounds
-
90% success Adjust the batch size or dataset size so that total batches per epoch is a multiple of gradient_accumulation_steps. For example, set `per_device_train_batch_size=8` and `gradient_accumulation_steps=4` if dataset has 256 samples: `TrainingArguments(per_device_train_batch_size=8, gradient_accumulation_steps=4)`
Adjust the batch size or dataset size so that total batches per epoch is a multiple of gradient_accumulation_steps. For example, set `per_device_train_batch_size=8` and `gradient_accumulation_steps=4` if dataset has 256 samples: `TrainingArguments(per_device_train_batch_size=8, gradient_accumulation_steps=4)`
-
80% success Use `DataLoader` with `drop_last=True` and ensure dataset length is a multiple of batch_size * gradient_accumulation_steps: `train_dataset = train_dataset.select(range(len(train_dataset) - len(train_dataset) % (batch_size * grad_acc_steps)))`
Use `DataLoader` with `drop_last=True` and ensure dataset length is a multiple of batch_size * gradient_accumulation_steps: `train_dataset = train_dataset.select(range(len(train_dataset) - len(train_dataset) % (batch_size * grad_acc_steps)))`
中文步骤
Adjust the batch size or dataset size so that total batches per epoch is a multiple of gradient_accumulation_steps. For example, set `per_device_train_batch_size=8` and `gradient_accumulation_steps=4` if dataset has 256 samples: `TrainingArguments(per_device_train_batch_size=8, gradient_accumulation_steps=4)`
Use `DataLoader` with `drop_last=True` and ensure dataset length is a multiple of batch_size * gradient_accumulation_steps: `train_dataset = train_dataset.select(range(len(train_dataset) - len(train_dataset) % (batch_size * grad_acc_steps)))`
Dead Ends
Common approaches that don't work:
-
Set `dataloader_drop_last=True` in TrainingArguments to drop the last incomplete batch
70% fail
This only drops the last batch, but the total batch count may still not be divisible by gradient_accumulation_steps; it only works if the dataset size is a multiple of batch_size.
-
Increase gradient_accumulation_steps to a larger number
80% fail
Making it larger usually worsens the divisibility problem and increases memory usage; it doesn't address the root cause.