{"record":{"id":"2b0f554ca5d2c321","repo":"rohitg00/ai-engineering-from-scratch","slug":"max-norm-must-be-positive","errorCode":null,"errorMessage":"max_norm must be positive","messagePattern":"max_norm must be positive","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/45-gradient-clipping-amp/code/main.py","lineNumber":112,"sourceCode":"            continue\n        grad = param.grad.detach()\n        squared_sum += float(grad.pow(2).sum().item())\n    return math.sqrt(squared_sum)\n\n\ndef clip_global_l2_norm(\n    parameters: list[torch.nn.Parameter],\n    max_norm: float,\n) -> tuple[float, float]:\n    \"\"\"Clip gradients in place to max_norm and return (pre_clip, post_clip).\n\n    Returns (pre_clip, post_clip). When pre_clip <= max_norm the gradients are\n    untouched and post_clip == pre_clip. When pre_clip > max_norm the gradients\n    are scaled by max_norm / pre_clip and post_clip == max_norm.\n    \"\"\"\n\n    if max_norm <= 0:\n        raise ValueError(\"max_norm must be positive\")\n    pre_clip = compute_global_l2_norm(parameters)\n    if not math.isfinite(pre_clip):\n        return pre_clip, pre_clip\n    if pre_clip <= max_norm:\n        return pre_clip, pre_clip\n    scale = max_norm / (pre_clip + 1e-12)\n    for param in parameters:\n        if param.grad is not None:\n            param.grad.detach().mul_(scale)\n    return pre_clip, max_norm\n\n\nclass AmpTrainState:\n    \"\"\"Training step with mixed precision and gradient clipping.\n\n    Wires together a model, an AdamW optimizer, a GradScaler, and an autocast\n    device. Exposes step(inputs, targets) which:\n","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/45-gradient-clipping-amp/code/main.py#L94-L130","documentation":"Error \"max_norm must be positive\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/45-gradient-clipping-amp/code/main.py:112 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}