{"record":{"id":"7e09eca68500e01d","repo":"sgl-project/sglang","slug":"image-rotary-emb-must-be-cos-sin-cache-tensors","errorCode":null,"errorMessage":"image_rotary_emb must be cos_sin_cache tensors","messagePattern":"image_rotary_emb must be cos_sin_cache tensors","errorType":"validation","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/models/dits/qwen_image.py","lineNumber":715,"sourceCode":"            txt_value,\n        ) = _get_qkv_projections(self, hidden_states, encoder_hidden_states)\n\n        # Reshape for multi-head attention\n        img_query = img_query.unflatten(-1, (self.local_num_heads, self.head_dim))\n        img_key = img_key.unflatten(-1, (self.local_num_heads, self.head_dim))\n        img_value = img_value.unflatten(-1, (self.local_num_heads, self.head_dim))\n\n        txt_query = txt_query.unflatten(-1, (self.local_num_heads, self.head_dim))\n        txt_key = txt_key.unflatten(-1, (self.local_num_heads, self.head_dim))\n        txt_value = txt_value.unflatten(-1, (self.local_num_heads, self.head_dim))\n\n        img_cache = txt_cache = None\n        if image_rotary_emb is not None:\n            if not (\n                isinstance(image_rotary_emb[0], torch.Tensor)\n                and image_rotary_emb[0].dim() == 2\n            ):\n                raise RuntimeError(\"image_rotary_emb must be cos_sin_cache tensors\")\n\n            img_cache, txt_cache = image_rotary_emb\n\n        if self.qk_norm:\n            img_query, img_key = apply_qk_norm_with_optional_rope(\n                q=img_query,\n                k=img_key,\n                q_norm=self.norm_q,\n                k_norm=self.norm_k,\n                head_dim=self.head_dim,\n                cos_sin_cache=img_cache,\n                is_neox=False,\n                allow_inplace=True,\n            )\n            txt_query, txt_key = apply_qk_norm_with_optional_rope(\n                q=txt_query,\n                k=txt_key,\n                q_norm=self.norm_added_q,","sourceCodeStart":697,"sourceCodeEnd":733,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/models/dits/qwen_image.py#L697-L733","documentation":"The attention forward expects image_rotary_emb to be a (cos_sin_cache_img, cos_sin_cache_txt) pair of 2-D tensors (the precomputed cache format used with fused RoPE kernels). Passing tuple/list of per-position cos/sin tensors (diffusers-style 4-tuples) raises RuntimeError.","triggerScenarios":"Passing diffusers-style rotary outputs (cos, sin lists of [B,S,D] tensors) instead of the 2-D cos_sin_cache tensors this implementation consumes.","commonSituations":"Porting pipelines from diffusers Qwen-Image; rope utility returning a different format after a refactor.","solutions":["Convert to cos_sin_cache format via the model's rope cache helper (e.g. precompute/stack cos,sin into a 2-D cache per stream)","Pass the caches produced by the model's own rotary utility rather than external cos/sin lists"],"exampleFix":"# before\nimg_cache, txt_cache = image_rotary_emb  # diffusers 4-tuple -> RuntimeError\n# after\nrope_caches = build_cos_sin_cache(head_dim, max_seq, device, dtype)\nimg_cache, txt_cache = rope_caches.img, rope_caches.txt","handlingStrategy":"type-guard","validationCode":"img_c, txt_c = image_rotary_emb\nassert torch.is_tensor(img_c) and img_c.dim() == 2 and torch.is_tensor(txt_c)","typeGuard":"def is_cos_sin_cache(re) -> bool:\n    return (\n        isinstance(re, (tuple, list)) and len(re) == 2\n        and all(torch.is_tensor(t) and t.dim() == 2 for t in re)\n    )","tryCatchPattern":null,"preventionTips":["Use the model's rope cache builder, not diffusers rope output","Unit-test the rotary format at integration boundaries"],"tags":["qwen-image","rope","cos-sin-cache","format-validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}