MCPcopy Create free account
hub / github.com/Ropedia/SpatialBench / forward

Method forward

benchmark/models/lingbot_map/layers/block.py:229–304  ·  view source on GitHub ↗
(
        self,
        x: Tensor,
        pos=None,
        enable_ulysses_cp=False,
        num_patches=None,
        num_special=None,
        num_frames=None,
        enable_3d_rope=False,
        kv_cache=None,
        global_idx=0,
        num_frame_per_block=1,
        num_frame_for_scale=-1,
        num_register_tokens=4,
    )

Source from the content-addressed store, hash-verified

227 return self.attn.prepare_qkv(self.norm1(x), pos=pos, enable_3d_rope=enable_3d_rope)
228
229 def forward(
230 self,
231 x: Tensor,
232 pos=None,
233 enable_ulysses_cp=False,
234 num_patches=None,
235 num_special=None,
236 num_frames=None,
237 enable_3d_rope=False,
238 kv_cache=None,
239 global_idx=0,
240 num_frame_per_block=1,
241 num_frame_for_scale=-1,
242 num_register_tokens=4,
243 ) -> Tensor:
244 # Phase 2 (streaming): single-frame FlashInfer paged attention.
245 # Handle inline so attn_pre (norm1+prepare_qkv) can be compiled as one CUDA graph.
246 is_streaming = (kv_cache is not None and (num_frames is None or num_frames <= 1))
247 if is_streaming:
248 manager = kv_cache
249 # Compiled: norm1 + qkv linear + reshape + q_norm + k_norm + RoPE + format
250 q_nhd, k_nhd, v_nhd = self.attn_pre(x, pos=pos, enable_3d_rope=enable_3d_rope)
251
252 # Non-keyframe path: attend to cache+current but don't persist the
253 # current frame. FlashInfer paged attention can only read from the
254 # paged cache, so we temporarily append (with eviction deferred so
255 # it stays clean), attend, and then roll back the append. Mirrors
256 # the ``skip_append`` behavior of the SDPA dict path.
257 skip_append = getattr(manager, '_skip_append', False)
258 if skip_append:
259 prev_defer = manager._defer_eviction
260 manager._defer_eviction = True
261 manager.append_frame(global_idx, k_nhd, v_nhd)
262 attn_x = manager.compute_attention(global_idx, q_nhd)
263 manager.rollback_last_frame(global_idx)
264 manager._defer_eviction = prev_defer
265 else:
266 # Eager: write frame K/V to paged cache
267 manager.append_frame(global_idx, k_nhd, v_nhd)
268 # CPU-only: update eviction state (deque ops, no GPU kernel)
269 manager.evict_frames(
270 block_idx=global_idx,
271 scale_frames=self.attn.kv_cache_scale_frames,
272 sliding_window=self.attn.kv_cache_sliding_window,
273 cross_frame_special=self.attn.kv_cache_cross_frame_special,
274 include_scale_frames=self.attn.kv_cache_include_scale_frames,
275 camera_only=self.attn.kv_cache_camera_only,
276 num_register_tokens=num_register_tokens,
277 )
278 # Eager: FlashInfer BatchPrefillWithPagedKVCacheWrapper
279 attn_x = manager.compute_attention(global_idx, q_nhd)
280
281 # [tpf, H, D] -> [B, tpf, C] (B=1 in streaming, contiguous from FlashInfer output)
282 attn_x = attn_x.reshape(x.shape[0], q_nhd.shape[0],
283 self.attn.num_heads * self.attn.head_dim)
284 # Compiled: output projection
285 attn_x = self.attn.proj(attn_x)
286 x = x + self.ls1(attn_x)

Callers

nothing calls this directly

Calls 6

attn_preMethod · 0.95
ffn_residualMethod · 0.95
append_frameMethod · 0.80
rollback_last_frameMethod · 0.80
evict_framesMethod · 0.80
compute_attentionMethod · 0.45

Tested by

no test coverage detected