{"record":{"id":"24c90e3d6767f007","repo":"sgl-project/sglang","slug":"f-invalid-prefix-for-sparsevideogen2attentionimpl","errorCode":null,"errorMessage":"f\"Invalid prefix for SparseVideoGen2AttentionImpl: {prefix}\"","messagePattern":"f\"Invalid prefix for SparseVideoGen2AttentionImpl: (.+?)\"","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/layers/attention/backends/sparse_video_gen_2_attn.py","lineNumber":210,"sourceCode":"        **extra_impl_args,\n    ) -> None:\n        if causal:\n            raise ValueError(\n                \"Sparse Video Gen 2 attention does not support causal attention\"\n            )\n        if not svg2_available:\n            raise ImportError(\n                \"Sparse Video Gen 2 attention backend requires svg package to be installed\"\n                \"Please install it by following the instructions at \"\n                \"https://github.com/svg-project/Sparse-VideoGen\"\n            )\n        self.prefix = prefix\n        self.layer_idx = self._get_layer_idx(prefix)\n\n    def _get_layer_idx(self, prefix: str) -> int:\n        parts = prefix.split(\".\")\n        if len(parts) < 3:\n            raise ValueError(\n                f\"Invalid prefix for SparseVideoGen2AttentionImpl: {prefix}\"\n            )\n        return int(parts[-3])\n\n    def kmeans_init(\n        self,\n        query: torch.Tensor,\n        key: torch.Tensor,\n        attn_metadata: SparseVideoGen2AttentionMetadata,\n    ):\n        cfg, num_heads, seq_len, dim = query.size()\n        qlabels, qcentroids, qcluster_sizes, qiter = batch_kmeans_Euclid(\n            query.reshape(cfg * num_heads, seq_len, dim),\n            n_clusters=attn_metadata.num_q_centroids,\n            max_iters=attn_metadata.kmeans_iter_init,\n        )\n        klabels, kcentroids, kcluster_sizes, kiter = batch_kmeans_Euclid(\n            key.reshape(cfg * num_heads, seq_len, dim),","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/layers/attention/backends/sparse_video_gen_2_attn.py#L192-L228","documentation":"SparseVideoGen2AttentionImpl derives its layer index from the prefix string by taking the third-from-last dot-separated component (parts[-3]) and parsing it as an int. If the prefix has fewer than 3 parts (e.g. 'layers.5' or 'blocks'), it raises this ValueError.","triggerScenarios":"Constructing the impl with a shallow prefix like 'blocks.3.q_proj', 'model.layers', or a custom name without the expected '...{layer_idx}.{module}.{child}' depth, so len(prefix.split('.')) < 3.","commonSituations":"Loading per-layer weights under renamed/remapped module paths (e.g. after checkpoint conversion or prefix stripping), or passing a user-defined prefix when manually instantiating the impl instead of letting the model builder generate hierarchical prefixes like 'transformer.blocks.12.attn'.","solutions":["Pass the full hierarchical prefix containing at least 3 dot-separated components where parts[-3] is the numeric layer index, e.g. 'backbone.layers.12.attn'.","If you renamed modules, keep a numeric layer component third from the end, or subclass and override _get_layer_idx for custom mapping.","Regenerate prefixes from the model's named_modules() rather than hand-writing them."],"exampleFix":"# before\nimpl = SparseVideoGen2AttentionImpl(prefix=\"attn\", ...)  # too shallow -> ValueError\n\n# after\nimpl = SparseVideoGen2AttentionImpl(prefix=\"model.layers.12.attn\", ...)  # parts[-3] == '12'","handlingStrategy":"validation","validationCode":"def is_valid_svg2_prefix(prefix: str) -> bool:\n    parts = prefix.split(\".\")\n    return len(parts) >= 3 and parts[-3].isdigit()\n\nassert is_valid_svg2_prefix(prefix), f\"bad prefix: {prefix}\"","typeGuard":"def is_valid_svg2_prefix(prefix: str) -> bool:\n    parts = prefix.split(\".\")\n    return len(parts) >= 3 and parts[-3].isdigit()","tryCatchPattern":null,"preventionTips":["Generate prefixes from model.named_modules() rather than hand-writing strings.","Keep a numeric layer-index component third-from-last in module paths (e.g. model.layers.12.attn).","Add a startup sanity check over all prefixes when wiring custom backends."],"tags":["prefix","layer-index","weight-loading","validation"],"backgroundTag":"invalid-module-prefix","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}