{
  "id": "pytorch/grad-scale-unscale-error",
  "signature": "RuntimeError: GradScaler unscale_() encountered None gradient. Ensure loss.backward() and optimizer.step() are called correctly.",
  "signature_zh": "运行时错误：GradScaler 的 unscale_() 遇到了 None 梯度。请确保 loss.backward() 和 optimizer.step() 被正确调用。",
  "regex": "RuntimeError: GradScaler unscale_\\(\\) encountered None gradient",
  "domain": "pytorch",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "GradScaler's unscale_() method was called on a parameter whose gradient is None, typically because the parameter was not used in the forward pass or was excluded from gradient computation.",
  "root_cause_type": "generic",
  "root_cause_zh": "GradScaler 的 unscale_() 方法被调用时某个参数的梯度为 None，通常是因为该参数在前向传播中未被使用或被排除在梯度计算之外。",
  "versions": [
    {
      "version": "torch>=1.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "torch<=2.5.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Setting all parameters to require grad=True",
      "why_fails": "If a parameter is truly unused in the forward pass, setting requires_grad=True will still result in a None gradient after backward, because no gradient flows through it.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Calling unscale_() multiple times to clear None gradients",
      "why_fails": "The GradScaler's internal state does not allow multiple unscale_() calls per optimizer step; it raises a different error about double unscale.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Exclude parameters that are not used in the forward pass from the optimizer. For example, if a submodule is conditionally used, filter its parameters: optimizer = torch.optim.SGD([p for p in model.parameters() if p.requires_grad and p.grad is not None], lr=0.01)",
      "success_rate": 0.9,
      "how": "Exclude parameters that are not used in the forward pass from the optimizer. For example, if a submodule is conditionally used, filter its parameters: optimizer = torch.optim.SGD([p for p in model.parameters() if p.requires_grad and p.grad is not None], lr=0.01)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use torch.no_grad() context or set requires_grad=False on parameters that are not part of the computation graph to avoid gradient computation.",
      "success_rate": 0.85,
      "how": "Use torch.no_grad() context or set requires_grad=False on parameters that are not part of the computation graph to avoid gradient computation.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Exclude parameters that are not used in the forward pass from the optimizer. For example, if a submodule is conditionally used, filter its parameters: optimizer = torch.optim.SGD([p for p in model.parameters() if p.requires_grad and p.grad is not None], lr=0.01)",
    "Use torch.no_grad() context or set requires_grad=False on parameters that are not part of the computation graph to avoid gradient computation."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/amp.html#torch.cuda.amp.GradScaler.unscale_",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2023-07-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}