{"record":{"id":"ef3988b392873099","repo":"rohitg00/ai-engineering-from-scratch","slug":"temperature-must-be-positive","errorCode":null,"errorMessage":"temperature must be positive","messagePattern":"temperature must be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/35-gpt-model-assembly/code/main.py","lineNumber":206,"sourceCode":"    threshold = values[..., -1:]\n    return torch.where(logits < threshold, torch.full_like(logits, float(\"-inf\")), logits)\n\n\ndef generate(\n    model: GPTModel,\n    prompt: torch.Tensor,\n    max_new_tokens: int,\n    temperature: float = 1.0,\n    top_k: int | None = None,\n    seed: int | None = None,\n) -> torch.Tensor:\n    \"\"\"Autoregressive generation with multinomial sampling, temperature, top-k.\n\n    Holds the active window to model.cfg.context_length by sliding the oldest\n    tokens out when the running sequence overflows.\n    \"\"\"\n    if temperature <= 0:\n        raise ValueError(\"temperature must be positive\")\n    if seed is not None:\n        torch.manual_seed(seed)\n\n    was_training = model.training\n    model.eval()\n    tokens = prompt.clone()\n    try:\n        with torch.no_grad():\n            for _ in range(max_new_tokens):\n                window = tokens[:, -model.cfg.context_length:]\n                logits = model(window)\n                next_logits = logits[:, -1, :] / temperature\n                next_logits = top_k_filter(next_logits, top_k)\n                probs = F.softmax(next_logits, dim=-1)\n                next_token = torch.multinomial(probs, num_samples=1)\n                tokens = torch.cat([tokens, next_token], dim=1)\n        return tokens\n    finally:","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/35-gpt-model-assembly/code/main.py#L188-L224","documentation":"Error \"temperature must be positive\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/35-gpt-model-assembly/code/main.py:206 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"}