{"record":{"id":"6aa153bec80af53b","repo":"sgl-project/sglang","slug":"forward-batch-cannot-be-none","errorCode":null,"errorMessage":"forward_batch cannot be None","messagePattern":"forward_batch cannot be None","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/layers/attention/backends/sliding_tile_attn.py","lineNumber":216,"sourceCode":"        return self.untile(output)\n\n    def forward(\n        self,\n        q: torch.Tensor,\n        k: torch.Tensor,\n        v: torch.Tensor,\n        attn_metadata: SlidingTileAttentionMetadata,\n    ) -> torch.Tensor:\n        if self.mask_strategy is None:\n            raise ValueError(\"mask_strategy cannot be None for SlidingTileAttention\")\n        if self.mask_strategy[0] is None:\n            raise ValueError(\"mask_strategy[0] cannot be None for SlidingTileAttention\")\n\n        timestep = attn_metadata.current_timestep\n        forward_context: ForwardContext = get_forward_context()\n        forward_batch = forward_context.forward_batch\n        if forward_batch is None:\n            raise ValueError(\"forward_batch cannot be None\")\n        # pattern:'.double_blocks.0.attn.impl' or '.single_blocks.0.attn.impl'\n        layer_idx = int(self.prefix.split(\".\")[-3])\n        if attn_metadata.STA_param is None or len(attn_metadata.STA_param) <= layer_idx:\n            raise ValueError(\"Invalid STA_param\")\n        STA_param = attn_metadata.STA_param[layer_idx]\n\n        text_length = q.shape[1] - self.img_seq_length\n        has_text = text_length > 0\n\n        query = q.transpose(1, 2).contiguous()\n        key = k.transpose(1, 2).contiguous()\n        value = v.transpose(1, 2).contiguous()\n\n        head_num = query.size(1)\n        sp_group = get_sp_group()\n        current_rank = sp_group.rank_in_group\n        start_head = current_rank * head_num\n","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/layers/attention/backends/sliding_tile_attn.py#L198-L234","documentation":"STA forward reads the live ForwardContext.forward_batch (the current request batch) for batch/timestep inputs; if the context's forward_batch is None it raises ValueError.","triggerScenarios":"Calling sta_backend.forward(...) outside a real model forward pass — e.g. unit tests, benchmarks, or warmup — where get_forward_context() returns a context without a bound forward_batch.","commonSituations":"Standalone kernel microbenchmarks constructing metadata manually; running layers before the scheduler attached a batch; context cleared between engine steps.","solutions":["Run the layer inside a normal model forward where ForwardContext has a real forward_batch","In tests, install a dummy ForwardContext with a ForwardBatch before calling forward","Refactor tests to go through the model runner rather than the raw backend"],"exampleFix":"# before\nout = sta_impl.forward(q, k, v, meta)  # forward_batch is None\n# after\nctx = ForwardContext(forward_batch=make_dummy_forward_batch(q.shape[0]))\nset_forward_context(ctx)\nout = sta_impl.forward(q, k, v, meta)","handlingStrategy":"validation","validationCode":"ctx = get_forward_context()\nassert ctx is not None and ctx.forward_batch is not None, \"STA forward must run inside a real forward pass\"","typeGuard":"def in_forward_pass() -> bool:\n    ctx = get_forward_context()\n    return ctx is not None and ctx.forward_batch is not None","tryCatchPattern":null,"preventionTips":["Don't call STA forward outside the model runner","In tests, install a ForwardContext with a dummy ForwardBatch first"],"tags":["sliding-tile-attention","forward-context","null-check","runtime-state"],"backgroundTag":"required-state-missing","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}