{"record":{"id":"8e38f81e40c0ddee","repo":"open-mmlab/mmdetection","slug":"unknown-pos-tensor-shape-1","errorCode":null,"errorMessage":"Unknown pos_tensor shape(-1):{}","messagePattern":"Unknown pos_tensor shape\\(-1\\):(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mmdet/models/layers/transformer/utils.py","lineNumber":95,"sourceCode":"                        dim=-1).flatten(2)\n    pos_y = torch.stack((pos_y[..., 0::2].sin(), pos_y[..., 1::2].cos()),\n                        dim=-1).flatten(2)\n    if coord_tensor.size(-1) == 2:\n        pos = torch.cat((pos_y, pos_x), dim=-1)\n    elif coord_tensor.size(-1) == 4:\n        w_embed = coord_tensor[..., 2] * scale\n        pos_w = w_embed[..., None] / dim_t\n        pos_w = torch.stack((pos_w[..., 0::2].sin(), pos_w[..., 1::2].cos()),\n                            dim=-1).flatten(2)\n\n        h_embed = coord_tensor[..., 3] * scale\n        pos_h = h_embed[..., None] / dim_t\n        pos_h = torch.stack((pos_h[..., 0::2].sin(), pos_h[..., 1::2].cos()),\n                            dim=-1).flatten(2)\n\n        pos = torch.cat((pos_y, pos_x, pos_w, pos_h), dim=-1)\n    else:\n        raise ValueError('Unknown pos_tensor shape(-1):{}'.format(\n            coord_tensor.size(-1)))\n    return pos\n\n\ndef inverse_sigmoid(x: Tensor, eps: float = 1e-5) -> Tensor:\n    \"\"\"Inverse function of sigmoid.\n\n    Args:\n        x (Tensor): The tensor to do the inverse.\n        eps (float): EPS avoid numerical overflow. Defaults 1e-5.\n    Returns:\n        Tensor: The x has passed the inverse function of sigmoid, has the same\n        shape with input.\n    \"\"\"\n    x = x.clamp(min=0, max=1)\n    x1 = x.clamp(min=eps)\n    x2 = (1 - x).clamp(min=eps)\n    return torch.log(x1 / x2)","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/layers/transformer/utils.py#L77-L113","documentation":"coordinate_to_encoding only encodes 2D points (last dim == 2, sine embedding) or 4D boxes (last dim == 4, CSLS embedding). A coord_tensor whose last dimension is anything else (e.g. 3, 5, 6) raises ValueError.","triggerScenarios":"Calling coordinate_to_encoding(pos_tensor) with shape [...,3] or [...,5+], typically from a transformer forward that passes reference points or RoI coords of unexpected dimensionality.","commonSituations":"Custom detection heads that feed 3D keypoints or 6D poses into a positional encoding meant for 2D detection; mismatched box encoding (xyxy vs cxcywh vs extra dims) between components.","solutions":["Inspect coord_tensor.shape[-1] and reduce/fix it to 2 (point) or 4 (box)","If passing boxes, ensure they are exactly 4 values (cxcywh or xyxy), not concatenated with extra features","For genuinely higher-dim coords, write a custom encoding instead of this utility"],"exampleFix":"# before\nenc = coordinate_to_encoding(pts)  # pts.shape[-1] == 3\n# after\nenc = coordinate_to_encoding(pts[..., :2])","handlingStrategy":"validation","validationCode":"assert coord_tensor.size(-1) in (2, 4), coord_tensor.shape","typeGuard":"def is_encodable_coord(t): return t.dim() >= 2 and t.size(-1) in (2, 4)","tryCatchPattern":null,"preventionTips":["Assert last-dim size before encoding","Keep reference-point tensors strictly 2D or 4D in custom heads"],"tags":["mmdet","transformer","positional-encoding","shape-mismatch"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}