{"record":{"id":"01fa1a3711522d3d","repo":"invoke-ai/InvokeAI","slug":"text-embedding-y-must-be-b-l-d-01fa1a","errorCode":null,"errorMessage":"Text embedding y must be [B, L, D]","messagePattern":"Text embedding y must be \\[B, L, D\\]","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/pid/_src/networks/pixeldit_official.py","lineNumber":1428,"sourceCode":"        nn.init.zeros_(self.final_layer.linear.weight)\n        nn.init.zeros_(self.final_layer.linear.bias)\n\n    def forward(self, x, t, y, s=None, mask=None):\n        B, _, H, W = x.shape\n        # Derive grid token count deterministically from spatial size\n        Hs = H // self.patch_size\n        Ws = W // self.patch_size\n        L = Hs * Ws\n\n        # Patch tokens for condition pathway\n        pos = self.fetch_pos(Hs, Ws, x.device)\n        x_patches = torch.nn.functional.unfold(x, kernel_size=self.patch_size, stride=self.patch_size).transpose(1, 2)\n\n        t_emb = self.t_embedder(t.view(-1)).view(B, -1, self.hidden_size)\n\n        # Text tokens -> project to hidden_size and add learned pos\n        if y.dim() != 3:\n            raise ValueError(\"Text embedding y must be [B, L, D]\")\n        Ltxt = min(y.shape[1], self.txt_max_length)\n        y = y[:, :Ltxt, :]\n        y_emb = self.y_embedder(y).view(B, Ltxt, self.hidden_size)\n        y_emb = y_emb + self.y_pos_embedding[:, :Ltxt, :].to(y_emb.dtype)\n\n        # PixDiT design: no AdaLN modulation applied on text stream\n        condition = torch.nn.functional.silu(t_emb)\n\n        # Condition blocks on patch tokens with MM-DiT joint attention to text tokens\n        pad = None\n        pos_txt = self.fetch_pos_text(Ltxt, x.device) if self.use_text_rope else None\n        if mask is not None and isinstance(mask, torch.Tensor):\n            m = mask\n            while m.dim() > 2 and m.size(1) == 1:\n                m = m.squeeze(1)\n            if m.dim() == 3 and m.size(1) == 1:\n                m = m.squeeze(1)\n            if m.dim() == 2:","sourceCodeStart":1410,"sourceCodeEnd":1446,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/pid/_src/networks/pixeldit_official.py#L1410-L1446","documentation":"forward() expects the text conditioning tensor y to be a 3-D tensor of shape [batch, sequence, dim]. Any other rank (e.g. a flattened [B*L, D] tensor or a 2-D [B, D] pooled embedding) cannot be indexed as [B, L, D], so the model raises ValueError immediately. This protects the downstream y_embedder and positional-embedding add, which assume exactly three dimensions.","triggerScenarios":"Passing y with y.dim() != 3 to PixDiT_T2I.forward, e.g. after an accidental squeeze(), from an encoder that returns pooled 2-D embeddings, or after batching/reshaping mistakes.","commonSituations":"Swapping text encoders (one returns [B, D], another [B, L, D]), calling forward with pre-pooled CLS embeddings, or denoising-loop wrappers that reshape the conditioning tensor.","solutions":["Unsqueeze a missing sequence dimension: y = y.unsqueeze(1) for [B, D] -> [B, 1, D]","If y is [B*L, D], reshape to [B, L, D] using the known sequence length","Verify the text encoder output shape before passing it as conditioning"],"exampleFix":"// before\nmodel(x, t, text_emb.squeeze(0))\n// after\nif text_emb.dim() == 2:\n    text_emb = text_emb.unsqueeze(1)\nmodel(x, t, text_emb)","handlingStrategy":"type-guard","validationCode":"assert isinstance(y, torch.Tensor) and y.dim() == 3, f\"y must be [B, L, D], got {tuple(y.shape)}\"","typeGuard":"def is_text_emb(y) -> bool:\n    return isinstance(y, torch.Tensor) and y.dim() == 3","tryCatchPattern":"try:\n    out = model(x, t, y)\nexcept ValueError as e:\n    if \"must be [B, L, D]\" in str(e):\n        y = y.unsqueeze(1) if y.dim() == 2 else y.view(B, -1, y.shape[-1])\n        out = model(x, t, y)\n    else:\n        raise","preventionTips":["Normalize text encoder outputs in one wrapper","Log conditioning shapes at pipeline start","Add shape asserts in custom loops"],"tags":["shape","value-error","text-embedding","tensor-rank"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}