vllm.model_executor.layers.fused_moe.routed_experts_capturer ¶
_RoutedExpertsCapturerReal ¶
Bases: RoutedExpertsCapturer
Capturer with GPU device cache and CPU host cache.
Performance strategy -- async D2H with optimized host-cache scatter:
Every decode step we issue a non-blocking D2H copy on a dedicated CUDA stream. The scatter into per-request host-cache buffers is deferred to the start of the NEXT step (by which time the copy has finished). The scatter loop is optimized with direct scalar access to avoid numpy slice views, int() conversions, and .max() calls.
At extraction time (when a request finishes), data is already in a contiguous host buffer -- just a numpy slice, no concatenation.
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | |
_scatter_to_host ¶
Scatter D2H data into per-request host cache buffers.
Staging layout is (L, N, K). Host cache layout is (seq_len, L, K). We transpose the staging slice to (N, L, K) before scattering so that indexing by token position naturally yields (L, K) rows.
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
finalize_pending_copy ¶
Ensure the most recent async D2H copy has been scattered into host cache buffers. Call before get_routed_experts.
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
_RoutedExpertsDeviceCache ¶
Per-device (GPU) cache for capturing routed expert IDs during forward pass. Always writes at row 0 so that CUDA graph replay sees the same addresses that were recorded at capture time.
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
_RoutedExpertsHostCache ¶
Host (CPU) cache using numpy arrays for per-request routing data.
Numpy arrays avoid torch dispatcher overhead for scatter operations. Lazy per-request allocation avoids a massive up-front buffer.
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
bind_routing_capture_to_model ¶
Bind routing capture buffers to all FusedMoE layers in the model.
Must be called AFTER init_routed_experts_capturer_with_shared_cache() and BEFORE CUDA graph capture. All TP ranks get a real buffer so that the custom op call produces identical graph structure.
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
extract_routed_experts_for_current_batch ¶
extract_routed_experts_for_current_batch(
req_ids: list[str],
requests: dict,
req_id_to_index: dict[str, int],
num_tokens_no_spec: ndarray,
max_model_len: int,
) -> dict[str, tuple] | None
Extract routed experts for requests predicted to finish this step.
Checks all stop conditions the scheduler will check (max_tokens, EOS token, stop tokens, max_model_len) so that every finished request gets its routing data attached to the ModelRunnerOutput.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
req_ids | list[str] | Ordered request IDs for the current batch. | required |
requests | dict | Map of req_id to CachedRequestState (read-only). | required |
req_id_to_index | dict[str, int] | Map of req_id to input batch index. | required |
num_tokens_no_spec | ndarray | Array of total token counts per request index. | required |
max_model_len | int | Maximum model sequence length. | required |
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | |
free_routing_buffers ¶
free_routing_buffers(
finished_req_ids: set[str],
preempted_req_ids: set[str] | None = None,
) -> None
Free host cache buffers for finished and preempted requests.
Finished requests had their routing data extracted in the previous step; preempted requests will be re-prefilled from scratch.
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
init_routed_experts_capturer_with_shared_cache ¶
init_routed_experts_capturer_with_shared_cache(
enable: bool,
model_config: ModelConfig,
num_fused_shared_experts: int,
num_batched_tokens: int,
max_model_len: int,
device: str,
rank: int = 0,
world_size: int = 1,
) -> RoutedExpertsCapturer
Initialize capturer with rank-aware handling (only rank 0 captures).
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
issue_routing_d2h_copy ¶
issue_routing_d2h_copy(
input_batch_req_ids: list[str],
num_scheduled_tokens: dict[str, int],
positions: Tensor,
positions_cpu: Tensor,
) -> None
Issue async D2H copy of routed experts after the forward pass.
Called EARLY in the execute_model epilogue so the copy overlaps with eplb, kv_connector finalization, and draft work. finalize_pending_copy() + get_routed_experts() happen later in extract_routed_experts_for_current_batch().
Source code in vllm/model_executor/layers/fused_moe/routed_experts_capturer.py
split_routed_experts ¶
split_routed_experts(
routed_experts: ndarray,
prompt_len: int,
num_output_tokens: int | None = None,
) -> tuple[ndarray | None, ndarray | None]
Split routing data into prompt and generation portions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
routed_experts | ndarray | Full routing array of shape (seq_len, L, K). | required |
prompt_len | int | Number of prompt tokens for the request. | required |
num_output_tokens | int | None | Actual number of generated tokens (from detokenizer). When provided, the generation portion is clipped to this length — necessary with MTP where the model runner may capture routing for more tokens than the final output contains. | None |
Returns:
| Type | Description |
|---|---|
ndarray | None | (prompt_routed_experts, gen_routed_experts) numpy arrays, either |
ndarray | None | of which may be None if the corresponding portion is empty. |