{"record":{"id":"3432a8911114fa87","repo":"rohitg00/ai-engineering-from-scratch","slug":"device-type-must-be-cpu-or-cuda-got-device-t","errorCode":null,"errorMessage":"device_type must be 'cpu' or 'cuda', got {device_type}","messagePattern":"device_type must be 'cpu' or 'cuda', got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/45-gradient-clipping-amp/code/main.py","lineNumber":152,"sourceCode":"      4. scaler.unscale_(optimizer).\n      5. Gradient finiteness check; non-finite grad skips optimizer step.\n      6. Clip to max_norm.\n      7. scaler.step(optimizer); scaler.update().\n    \"\"\"\n\n    def __init__(\n        self,\n        model: nn.Module,\n        lr: float = 1e-2,\n        max_norm: float = DEFAULT_MAX_NORM,\n        device_type: str = DEFAULT_DEVICE,\n        weight_decay: float = 0.01,\n        amp_dtype: torch.dtype | None = None,\n    ) -> None:\n        if max_norm <= 0:\n            raise ValueError(\"max_norm must be positive\")\n        if device_type not in (\"cpu\", \"cuda\"):\n            raise ValueError(f\"device_type must be 'cpu' or 'cuda', got {device_type}\")\n        self.model = model\n        self.max_norm = max_norm\n        self.device_type = device_type\n        self.optimizer = torch.optim.AdamW(\n            model.parameters(),\n            lr=lr,\n            weight_decay=weight_decay,\n        )\n        scaler_enabled = device_type == \"cuda\"\n        self.scaler = torch.amp.GradScaler(device_type, enabled=scaler_enabled)\n        if amp_dtype is None:\n            amp_dtype = torch.bfloat16 if device_type == \"cpu\" else torch.float16\n        self.amp_dtype = amp_dtype\n        self.global_step = 0\n        self._log: list[StepLog] = []\n        self._skip_log: list[SkipLog] = []\n        self._loss_fn: Callable[[torch.Tensor, torch.Tensor], torch.Tensor] = nn.functional.mse_loss\n","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/45-gradient-clipping-amp/code/main.py#L134-L170","documentation":"Error \"device_type must be 'cpu' or 'cuda', got {device_type}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/45-gradient-clipping-amp/code/main.py:152 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"}