vllm.model_executor.layers.fused_moe.prepare_megamoe
¶
Triton input-staging kernels for DeepGEMM MegaMoE.
Quantize hidden states to fp8 with E8M0 group scales, to NVFP4 with per-token global scales, or copy them as BF16, and repack the routing top-k tensors into the int64/float32 layout that the DeepGEMM MegaMoE kernels consume. Given an EPLB layer state, routing ids are mapped to physical replicas on the way.
Functions:
-
prepare_megamoe_inputs–Quantize hidden states and repack top-k routing for DeepGEMM MegaMoE.
-
prepare_megamoe_nvfp4_inputs–Stage DeepGEMM NVFP4 MegaMoE inputs (
kNvfp4DynamicToken).
_eplb_kernel_args(eplb_state)
¶
Replica-map and load-recording kernel args; inert without EPLB.
Source code in vllm/model_executor/layers/fused_moe/prepare_megamoe.py
_pack_bytes(values, NUM_BYTES)
¶
Pack the last dim of [..., NUM_BYTES] byte values into int32 words.
Source code in vllm/model_executor/layers/fused_moe/prepare_megamoe.py
_quantize_fp8_groups(hidden, BLOCK_M, BLOCK_K, GROUP_K, USE_UE8M0)
¶
Return fp8 values, fp32 group scales and, for UE8M0, the scale exponents.
Source code in vllm/model_executor/layers/fused_moe/prepare_megamoe.py
_resolve_hidden_quant(hidden_quant, block_k)
¶
Map a hidden-state QuantKey to the kernel's (GROUP_K, USE_UE8M0).
Source code in vllm/model_executor/layers/fused_moe/prepare_megamoe.py
_shared_sf_row(token_id, SHARED_BLOCK_M)
¶
Row of token_id in the shared-expert MN-major UTCCP scale pages.
Source code in vllm/model_executor/layers/fused_moe/prepare_megamoe.py
_store_topk(token_id, token_mask, topk_ids, topk_weights, is_padding, topk_idx_out, topk_weights_out, logical_to_physical, replica_count, expert_load, record_enabled, num_unpadded_tokens, num_logical, map_stride, num_physical, topk_ids_stride_m, topk_ids_stride_k, topk_weights_stride_m, topk_weights_stride_k, is_padding_stride_m, topk_idx_stride_m, topk_idx_stride_k, topk_weights_out_stride_m, topk_weights_out_stride_k, top_k, BLOCK_M, BLOCK_TOPK)
¶
Repack top-k routing as int64 ids and fp32 weights; padding is unrouted.
Source code in vllm/model_executor/layers/fused_moe/prepare_megamoe.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 | |
prepare_megamoe_inputs(hidden_states, topk_weights, topk_ids, x, x_sf, topk_idx_out, topk_weights_out, is_padding=None, shared_x_sf=None, shared_block_m=None, hidden_quant=kMxfp8Dynamic, eplb_state=None)
¶
Quantize hidden states and repack top-k routing for DeepGEMM MegaMoE.
Parameters:
-
(hidden_states¶Tensor) –Input activations of shape
[num_tokens, hidden_dim]. -
(topk_weights¶Tensor) –Router top-k weights of shape
[num_tokens, top_k]. -
(topk_ids¶Tensor) –Router top-k expert ids of shape
[num_tokens, top_k]. -
(x¶Tensor) –Output buffer for the dispatched hidden states.
-
(x_sf¶Tensor | None) –Output buffer for the hidden-state scale factors;
Nonewhenhidden_quantisNone. -
(topk_idx_out¶Tensor) –Output buffer for the repacked top-k expert ids.
-
(topk_weights_out¶Tensor) –Output buffer for the repacked top-k weights.
-
(is_padding¶Tensor | None, default:None) –Optional per-token mask; padded tokens are not routed.
-
(shared_x_sf¶Tensor | None, default:None) –Optional output buffer for shared-expert scale factors. Must be given together with
shared_block_m. -
(shared_block_m¶int | None, default:None) –Block M used to lay out
shared_x_sf. -
(hidden_quant¶QuantKey | None, default:kMxfp8Dynamic) –Hidden-state quantization scheme. E8M0 scales are packed four per int32 into
x_sf; fp32 scales are stored directly.Nonecopies the hidden states intoxunquantized. -
(eplb_state¶EplbLayerState | None, default:None) –Optional EPLB layer state; maps ids to replicas and records load.
Source code in vllm/model_executor/layers/fused_moe/prepare_megamoe.py
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 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | |
prepare_megamoe_nvfp4_inputs(hidden_states, topk_weights, topk_ids, x, x_sf, x_scales, topk_idx_out, topk_weights_out, is_padding=None, shared_x=None, shared_x_sf=None, shared_block_m=None, w2_global_scale=None, l2_alpha=None, l2_activation_scale=1.0, eplb_state=None)
¶
Stage DeepGEMM NVFP4 MegaMoE inputs (kNvfp4DynamicToken).
Each token is scaled by its own fp32 x_scales (row amax / 2688) and
quantized to E2M1 with one E4M3 scale per 16 values.
Parameters:
-
(hidden_states¶Tensor) –BF16 activations of shape
[num_tokens, hidden_dim]. -
(topk_weights¶Tensor) –Router top-k weights of shape
[num_tokens, top_k]. -
(topk_ids¶Tensor) –Router top-k expert ids of shape
[num_tokens, top_k]. -
(x¶Tensor) –Packed E2M1 output,
[num_tokens, hidden_dim // 2]. -
(x_sf¶Tensor) –E4M3 scales packed four per int32,
[num_tokens, hidden_dim // 64]. -
(x_scales¶Tensor) –Per-token fp32 dequantization scales,
[num_tokens]. -
(topk_idx_out¶Tensor) –Output buffer for the repacked top-k expert ids.
-
(topk_weights_out¶Tensor) –Output buffer for the repacked top-k weights.
-
(is_padding¶Tensor | None, default:None) –Optional per-token mask; padded tokens are not routed.
-
(shared_x¶Tensor | None, default:None) –Optional shared-expert input, fp8 (MXFP8 with
shared_x_sf) or a BF16 copy of the hidden states. -
(shared_x_sf¶Tensor | None, default:None) –UE8M0 scales of an fp8
shared_xin the shared-expert TMA layout forshared_block_m. -
(shared_block_m¶int | None, default:None) –Block M used to lay out
shared_x_sf. -
(w2_global_scale¶Tensor | None, default:None) –Per-local-expert L2 weight global scales.
-
(l2_alpha¶Tensor | None, default:None) –Output
w2_global_scale * l2_activation_scale, the L2 dequantization scale of each local expert. Written even without local tokens, as other ranks' tokens still reach these experts. -
(l2_activation_scale¶float, default:1.0) –Global scale the kernel quantizes L2 inputs with.
-
(eplb_state¶EplbLayerState | None, default:None) –Optional EPLB layer state; maps ids to replicas and records load.
Source code in vllm/model_executor/layers/fused_moe/prepare_megamoe.py
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 | |