{
  "id": "huggingface/gradient-accumulation-steps-mismatch",
  "signature": "RuntimeError: The number of batches in the dataloader (127) is not divisible by gradient_accumulation_steps (8). This may cause uneven gradient updates.",
  "signature_zh": "RuntimeError: 数据加载器中的批次数 (127) 不能被 gradient_accumulation_steps (8) 整除。这可能导致梯度更新不均匀。",
  "regex": "RuntimeError: The number of batches in the dataloader \\(\\d+\\) is not divisible by gradient_accumulation_steps \\(\\d+\\).*",
  "domain": "huggingface",
  "category": "config_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "每个 epoch 的总批次数不是 gradient_accumulation_steps 的倍数，导致每个 epoch 结束时出现部分更新。",
  "versions": [
    {
      "version": "huggingface/transformers 4.37.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "huggingface/accelerate 0.27.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "huggingface/transformers 4.38.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Set `dataloader_drop_last=True` in TrainingArguments to drop the last incomplete batch",
      "why_fails": "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.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Increase gradient_accumulation_steps to a larger number",
      "why_fails": "Making it larger usually worsens the divisibility problem and increases memory usage; it doesn't address the root cause.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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)`",
      "success_rate": 0.9,
      "how": "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)`",
      "condition": "",
      "sources": []
    },
    {
      "action": "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)))`",
      "success_rate": 0.8,
      "how": "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)))`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "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)))`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://huggingface.co/docs/transformers/en/main_classes/trainer#gradient-accumulation",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2024-02-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}