{"record":{"id":"85df0324dbe753e5","repo":"rohitg00/ai-engineering-from-scratch","slug":"ids-must-be-b-t-got-shape-tuple-ids-shape","errorCode":null,"errorMessage":"ids must be (B, T), got shape {tuple(ids.shape)}","messagePattern":"ids must be \\(B, T\\), got shape (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/32-token-positional-embeddings/code/main.py","lineNumber":50,"sourceCode":"class 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:\n            raise ValueError(f\"d_model must be >= 1, got {d_model}\")\n        self.max_context_length = max_context_length","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/32-token-positional-embeddings/code/main.py#L32-L68","documentation":"TokenEmbedding.forward enforces the (batch, time) rank contract; any other rank (an unbatched 1D sequence, a 3D tensor, or a scalar) is rejected because the positional embedding and attention layers downstream assume a leading batch dimension.","triggerScenarios":"Thrown at phases/19-capstone-projects/32-token-positional-embeddings/code/main.py:50 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Add a batch dimension with ids.unsqueeze(0) for single sequences","Squeeze extra dims before passing ids through the model","Validate tensor rank in the dataset/collate function so batches are always (B, T)"],"exampleFix":null,"handlingStrategy":"validation","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"}