{"record":{"id":"34b003a7a32a33fb","repo":"sgl-project/sglang","slug":"invalid-attention-metadata-values-sparsity-should","errorCode":null,"errorMessage":"Invalid attention metadata values.Sparsity should be in [0, 1), skip_first_steps should be non-negative.Got sparsity={sparsity}, skip_first_steps={skip_first_steps}","messagePattern":"Invalid attention metadata values\\.Sparsity should be in \\[0, 1\\), skip_first_steps should be non-negative\\.Got sparsity=(.+?), skip_first_steps=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/layers/attention/backends/block_sparse_attn.py","lineNumber":93,"sourceCode":"        Args:\n            current_timestep: The current diffusion timestep.\n            skip_first_steps: Number of initial timesteps to skip before applying\n                sparsity. Must be non‑negative.\n            sparsity: Fraction of tokens to drop (block‑wise) in the block sparse\n                attention mechanism. Must be in the range [0.0, 1.0).\n            raw_latent_shape: Shape of the latent tensor before patching.\n            patch_size: Patch size as (T, height, width). Only the height\n                and width components are used to divide the latent dimensions.\n            **kwargs: Additional keyword arguments (ignored, but accepted for\n                compatibility with base class or calling conventions).\n\n        Returns:\n            BlockSparseAttentionMetadata\n        Note:\n            The `block_frame_stride` is needed to set the first blocks to be non‑sparse.\n        \"\"\"\n        if not (skip_first_steps >= 0 and 0.0 <= sparsity < 1.0):\n            raise ValueError(\n                (\n                    \"Invalid attention metadata values.\"\n                    f\"Sparsity should be in [0, 1), skip_first_steps should be non-negative.\"\n                    f\"Got sparsity={sparsity}, skip_first_steps={skip_first_steps}\"\n                )\n            )\n\n        if sparsity == 0.0:\n            logger.warning(\n                (\n                    \"Sparsity is set to 0.0, which means no tokens will be dropped.\"\n                    \"For better performance use Laser Attention or increase sparsity.\"\n                )\n            )\n\n        if len(raw_latent_shape) >= 5:\n            latent_height, latent_width = raw_latent_shape[3:5]\n        else:","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/layers/attention/backends/block_sparse_attn.py#L75-L111","documentation":"BlockSparseAttentionMetadata.build validates its sparsity arguments: sparsity must be in [0.0, 1.0) and skip_first_steps >= 0. Violations mean the block-sparse mask would be nonsensical, so it fails fast at metadata construction time.","triggerScenarios":"Calling BlockSparseAttentionMetadata.build with sparsity >= 1.0, negative sparsity, or negative skip_first_steps.","commonSituations":"Config files that specify density (fraction kept) where the API expects sparsity; typos like sparsity: -0.1 or 1.2; negative skip_first_steps intended to mean 'attend from step 0'.","solutions":["Fix the values so 0.0 <= sparsity < 1.0 and skip_first_steps >= 0","If your config holds density, convert with sparsity = 1 - density","Clamp/validate the two values where the config is parsed, before build() is called"],"exampleFix":"# before\nmeta = BlockSparseAttentionMetadata.build(sparsity=0.75 /* actually density */, skip_first_steps=-1, ...)\n# after\nsparsity = 1.0 - density\nassert 0.0 <= sparsity < 1.0 and skip_first_steps >= 0\nmeta = BlockSparseAttentionMetadata.build(sparsity=sparsity, skip_first_steps=max(0, skip_first_steps), ...)","handlingStrategy":"validation","validationCode":"assert 0.0 <= sparsity < 1.0, f\"sparsity {sparsity} out of [0,1)\"\nassert skip_first_steps >= 0, \"skip_first_steps must be non-negative\"","typeGuard":null,"tryCatchPattern":"try:\n    meta = BlockSparseAttentionMetadata.build(sparsity=s, skip_first_steps=n, ...)\nexcept ValueError as e:\n    if \"Invalid attention metadata values\" in str(e):\n        s, n = sanitize_sparsity_args(s, n)\n        meta = BlockSparseAttentionMetadata.build(sparsity=s, skip_first_steps=n, ...)","preventionTips":["If config holds density, convert with sparsity = 1 - density","Validate numeric ranges at config load, not at kernel time"],"tags":["block-sparse","attention","metadata","validation","value-out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}