{"record":{"id":"2ff98c66d0430601","repo":"rohitg00/ai-engineering-from-scratch","slug":"torch-is-required-for-this-lesson-install-with-p-2ff98c","errorCode":null,"errorMessage":"torch is required for this lesson. Install with: pip install torch","messagePattern":"torch is required for this lesson\\. Install with: pip install torch","errorType":"exception","errorClass":"SystemExit","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/45-gradient-clipping-amp/code/main.py","lineNumber":29,"sourceCode":"The demo at the bottom trains a small torch.nn.Linear model for 20 steps and\ninjects a non-finite gradient on a specific step to exercise the skip path.\nRun: python3 code/main.py\n\"\"\"\n\nfrom __future__ import annotations\n\nimport csv\nimport math\nimport sys\nfrom dataclasses import asdict, dataclass, field\nfrom pathlib import Path\nfrom typing import Callable, Iterable\n\ntry:\n    import torch\n    from torch import nn\nexcept ImportError as exc:\n    raise SystemExit(\n        \"torch is required for this lesson. Install with: pip install torch\"\n    ) from exc\n\n\nDEFAULT_MAX_NORM = 1.0\nDEFAULT_DEVICE = \"cpu\"\nNORM_TYPE = 2.0\n\n\n@dataclass\nclass StepLog:\n    \"\"\"One row of the per-step training log.\"\"\"\n\n    step: int\n    lr: float\n    grad_l2_pre_clip: float\n    grad_l2_post_clip: float\n    loss: float","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/45-gradient-clipping-amp/code/main.py#L11-L47","documentation":"Error \"torch is required for this lesson. Install with: pip install torch\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/45-gradient-clipping-amp/code/main.py:29 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"}