{"record":{"id":"91371f6e717ae141","repo":"sgl-project/sglang","slug":"cosmos3crossattention-requires-num-key-value-heads","errorCode":null,"errorMessage":"Cosmos3CrossAttention requires num_key_value_heads divisible by tp_size, got {num_key_value_heads=} {self.tp_size=}.","messagePattern":"Cosmos3CrossAttention requires num_key_value_heads divisible by tp_size, got (.+?) (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/models/dits/cosmos3video.py","lineNumber":726,"sourceCode":"        num_key_value_heads: int,\n        head_dim: int,\n        prefix: str = \"\",\n        quant_config: QuantizationConfig | None = None,\n        supported_attention_backends: set | None = None,\n    ):\n        super().__init__()\n        self.hidden_size = hidden_size\n        self.num_attention_heads = num_attention_heads\n        self.num_key_value_heads = num_key_value_heads\n        self.head_dim = head_dim\n        self.tp_size = get_tp_world_size()\n        if num_attention_heads % self.tp_size != 0:\n            raise ValueError(\n                \"Cosmos3CrossAttention requires num_attention_heads divisible \"\n                f\"by tp_size, got {num_attention_heads=} {self.tp_size=}.\"\n            )\n        if num_key_value_heads % self.tp_size != 0:\n            raise ValueError(\n                \"Cosmos3CrossAttention requires num_key_value_heads divisible \"\n                f\"by tp_size, got {num_key_value_heads=} {self.tp_size=}.\"\n            )\n        self.local_num_attention_heads = num_attention_heads // self.tp_size\n        self.local_num_key_value_heads = num_key_value_heads // self.tp_size\n\n        self.q_size = num_attention_heads * head_dim\n        self.kv_size = num_key_value_heads * head_dim\n        self.to_qkv = MergedColumnParallelLinear(\n            hidden_size,\n            [self.q_size, self.kv_size, self.kv_size],\n            bias=False,\n            gather_output=False,\n            quant_config=quant_config,\n            prefix=add_prefix(\"to_qkv\", prefix),\n        )\n        self.to_out = RowParallelLinear(\n            num_attention_heads * head_dim,","sourceCodeStart":708,"sourceCodeEnd":744,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/models/dits/cosmos3video.py#L708-L744","documentation":"Cosmos3CrossAttention shards key/value heads for the text-encoding side across TP ranks. num_key_value_heads of the cross-attention block must be divisible by tp_size; otherwise per-rank KV heads cannot be computed and __init__ fails.","triggerScenarios":"TP degree larger than, or not a divisor of, the cross-attention layer's num_key_value_heads — e.g. tp=8 with 4 cross-attn KV heads.","commonSituations":"Same class of mistake as the other head-divisibility errors but on cross-attention KV heads, which are often the smallest head count in the model and therefore the binding TP constraint.","solutions":["Lower --tp to a divisor of the cross-attention num_key_value_heads","Inspect the config for all of num_attention_heads / num_key_value_heads in both causal and cross attention; choose TP dividing every one","When max TP is too small, shard other dims or use more replicas instead"],"exampleFix":"# before\n--tp 8   # cross-attn kv heads = 4\n# after\n--tp 4","handlingStrategy":"validation","validationCode":"assert cross_attn_num_kv_heads % tp == 0","typeGuard":"def tp_divides(n: int, tp: int) -> bool:\n    return n % tp == 0","tryCatchPattern":null,"preventionTips":["Treat cross-attention KV heads as the TP bound in DiT models","Pre-flight check configs before multi-GPU launches"],"tags":["sglang","cosmos3","tensor-parallel","cross-attention","kv-heads"],"backgroundTag":"attention-heads-not-divisible-by-tp","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}