{"record":{"id":"7e1d8e9cf8a6a97b","repo":"sgl-project/sglang","slug":"invalid-threshold-type-for-topk-threshold-type","errorCode":null,"errorMessage":"Invalid threshold_type for topk: {threshold_type}. Choose 'query_head', 'block', or 'overall'.","messagePattern":"Invalid threshold_type for topk: (.+?)\\. Choose 'query_head', 'block', or 'overall'\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/csrc/attn/vmoba_attn/vmoba/vmoba.py","lineNumber":744,"sourceCode":"            gate_mask = (valid_gate_mask.flatten() & others_mask_flat).view(gate.shape)\n        elif threshold_type == \"head_global\":\n            # per-head top-k across all chunks and sequence positions\n            C, H, S = gate.shape\n            CS = C * S\n            flat_gate = gate.permute(1, 0, 2).reshape(H, CS)\n            flat_valid = valid_gate_mask.permute(1, 0, 2).reshape(H, CS)\n            flat_gate_masked = torch.where(\n                flat_valid, flat_gate, torch.full_like(flat_gate, -float(\"inf\"))\n            )\n            # pick top-k indices per head\n            _, topk_idx = torch.topk(\n                flat_gate_masked, k=moba_topk * S, dim=1, largest=True, sorted=False\n            )\n            gate_idx_flat = torch.zeros_like(flat_valid, dtype=torch.bool)\n            gate_idx_flat.scatter_(1, topk_idx, True)\n            gate_mask = gate_idx_flat.reshape(H, C, S).permute(1, 0, 2)\n        else:\n            raise ValueError(\n                f\"Invalid threshold_type for topk: {threshold_type}. \"\n                \"Choose 'query_head', 'block', or 'overall'.\"\n            )\n    elif select_mode == \"threshold\":\n        # Delegate to the specific thresholding function\n        valid_gate_mask = gate != -float(\"inf\")  # (num_chunk, num_head, seqlen)\n        if threshold_type == \"query_head\":\n            gate_mask = _select_threshold_query_head(\n                gate, valid_gate_mask, gate_self_chunk_mask, simsum_threshold\n            )\n        elif threshold_type == \"block\":\n            gate_mask = _select_threshold_block(\n                gate, valid_gate_mask, gate_self_chunk_mask, simsum_threshold\n            )\n        elif threshold_type == \"overall\":\n            gate_mask = _select_threshold_overall(\n                gate, valid_gate_mask, gate_self_chunk_mask, simsum_threshold\n            )","sourceCodeStart":726,"sourceCodeEnd":762,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/csrc/attn/vmoba_attn/vmoba/vmoba.py#L726-L762","documentation":"moba_attn_varlen validates threshold_type when select_mode == 'topk'. Only 'query_head', 'block', and 'overall' map to a top-k gate-selection routine; any other string reaches the else branch and raises ValueError.","triggerScenarios":"Calling moba_attn_varlen(..., select_mode='topk', threshold_type=<anything other than 'query_head'|'block'|'overall'>), e.g. threshold_type='head_global' or a typo like 'queryhead'.","commonSituations":"Passing a threshold-mode-only value like 'head_global' while select_mode='topk'; renaming/typo in config strings; copy-pasting configs from the threshold code path into topk runs.","solutions":["Set threshold_type to one of 'query_head', 'block', or 'overall' when select_mode='topk'","If you meant per-head global thresholding, switch to select_mode='threshold' with threshold_type='head_global'","Check for typos/whitespace in the config string before calling moba_attn_varlen"],"exampleFix":"# before\nmoba_attn_varlen(q, k, v, ..., select_mode=\"topk\", threshold_type=\"head_global\")\n# after\nmoba_attn_varlen(q, k, v, ..., select_mode=\"topk\", threshold_type=\"block\")","handlingStrategy":"validation","validationCode":"VALID_TOPK_THRESHOLD_TYPES = {\"query_head\", \"block\", \"overall\"}\nassert select_mode != \"topk\" or threshold_type in VALID_TOPK_THRESHOLD_TYPES, (\n    f\"threshold_type must be one of {VALID_TOPK_THRESHOLD_TYPES} for topk mode\"\n)","typeGuard":"def is_valid_topk_threshold_type(t: str) -> bool:\n    return t in {\"query_head\", \"block\", \"overall\"}","tryCatchPattern":"try:\n    moba_attn_varlen(..., select_mode=\"topk\", threshold_type=threshold_type)\nexcept ValueError as e:\n    if \"Invalid threshold_type\" in str(e):\n        threshold_type = \"block\"\n        moba_attn_varlen(..., select_mode=\"topk\", threshold_type=threshold_type)\n    else:\n        raise","preventionTips":["Centralize valid threshold_type strings as module constants","Validate config strings at config-load time, not in the kernel call","Add unit tests covering each valid threshold_type"],"tags":["validation","value-error","moba","attention","config"],"backgroundTag":"invalid-enum-argument","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}