{"record":{"id":"a53b7c2823406b1a","repo":"rohitg00/ai-engineering-from-scratch","slug":"ids-must-be-long-tensor-got-ids-dtype","errorCode":null,"errorMessage":"ids must be long tensor, got {ids.dtype}","messagePattern":"ids must be long tensor, got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/32-token-positional-embeddings/code/main.py","lineNumber":48,"sourceCode":"\n\nclass TokenEmbedding(nn.Module):\n    \"\"\"Vocabulary-id to vector lookup.\"\"\"\n\n    def __init__(self, vocab_size: int, d_model: int, init_std: float = DEFAULT_INIT_STD) -> None:\n        super().__init__()\n        if vocab_size < 1:\n            raise ValueError(f\"vocab_size must be >= 1, got {vocab_size}\")\n        if d_model < 1:\n            raise ValueError(f\"d_model must be >= 1, got {d_model}\")\n        self.vocab_size = vocab_size\n        self.d_model = d_model\n        self.embedding = nn.Embedding(vocab_size, d_model)\n        _init_normal(self.embedding.weight, std=init_std)\n\n    def forward(self, ids: torch.Tensor) -> torch.Tensor:\n        if ids.dtype != torch.long:\n            raise TypeError(f\"ids must be long tensor, got {ids.dtype}\")\n        if ids.dim() != 2:\n            raise ValueError(f\"ids must be (B, T), got shape {tuple(ids.shape)}\")\n        return self.embedding(ids)\n\n\nclass LearnedPositionalEmbedding(nn.Module):\n    \"\"\"Position-id to vector lookup with learned parameters.\"\"\"\n\n    def __init__(\n        self,\n        max_context_length: int,\n        d_model: int,\n        init_std: float = DEFAULT_INIT_STD,\n    ) -> None:\n        super().__init__()\n        if max_context_length < 1:\n            raise ValueError(f\"max_context_length must be >= 1, got {max_context_length}\")\n        if d_model < 1:","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/32-token-positional-embeddings/code/main.py#L30-L66","documentation":"TokenEmbedding.forward is a dtype guard: nn.Embedding requires integer indices, and torch.long is the contract the module declares, so a float or int32 tensor is rejected before the lookup kernel emits a cryptic device/dtype error.","triggerScenarios":"Thrown at phases/19-capstone-projects/32-token-positional-embeddings/code/main.py:48 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Convert ids with .to(torch.long) before calling forward","Build id tensors with dtype=torch.long at creation (e.g. torch.tensor(ids, dtype=torch.long))","Check ids.dtype and raise a descriptive error at the data-loading boundary"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}