vllm.models.flint.ops.moe
¶
Fused NVFP4 W4A16 routed-expert MoE for Flint (plus decode kernels for the BF16 checkpoint's TRTLLM-layout experts).
Numerics: activations stay BF16; weights are dequantized exactly (E2M1 * E4M3) and products are accumulated in fp32 with the per-expert fp32 global scale applied to the fp32 accumulator. Router logits are BF16, top-k selection and sigmoid/renormalization are fp32 (exact exp/division) and the routing weights are rounded to BF16. Rounding points follow the reference (TRTLLM MoE rule): gate/up stay fp32 and act = bf16(silu(g) * u), the sigmoid computed as rcp.approx(1 + ex2.approx(-g * log2(e))) as in FlashInfer's kernel; each expert output is y_e = bf16(acc_e * global_scale_e); the routed sum is bf16(sum_e tw_e * y_e) accumulated in fp32; the output is bf16(routed + shared). The decode, decode-block and grouped paths implement this rule.
Decode (few tokens): three Triton kernels, (1) router GEMV + top-k, (2) per
(token, expert) gate/up GEMV + SiLU * up, (3) down GEMV over the top-k experts
+ weighted combine + shared-expert add. Larger batches: cuBLAS router GEMM +
Triton top-k, FlashInfer's SM100 W4A16 grouped GEMMs (see moe_cute) and a
Triton combine. Both paths read the same weights in FlashInfer's layout (see
FlintMoEWeights.from_modelopt).
Classes:
-
FlintBf16DecodeMoE–Decode path (few tokens) for Flint's BF16 routed experts.
-
FlintMoEWeights–Routed-expert NVFP4 weights in FlashInfer's W4A16 layout.
-
FlintNvfp4Experts–Routed experts of one Flint MoE layer (NVFP4 GGUF checkpoints).
-
LLMoEWeights–Weights of one NVFP4 MoE layer for
flint_moe_ll(views, no copies).
Functions:
-
flint_moe–Routed-expert MoE output + shared_output for BF16 x [M, H].
-
flint_moe_bf16_decode–Decode-path MoE on TRTLLM-layout BF16 experts (w13 [E, 2I, H], w2
-
flint_moe_ll–Decode MoE block for M <= LL_MOE_MAX_TOKENS tokens.
-
ll_weights_from_layers–LLMoEWeightsviewing the loaded layers' tensors: the shared-expert -
make_route_counter–Zeroed counter buffer for the router GEMV kernel; allocate it outside
-
route–BF16 router logits -> (top-k ids int32, renormalized fp32 weights).
-
trtllm_bf16_row_maps–Logical -> physical row maps of the TRTLLM BF16 expert weights as
FlintBf16DecodeMoE
¶
Bases: Module
Decode path (few tokens) for Flint's BF16 routed experts.
Reads the expert weights of a vLLM FusedMoE layer that uses the FlashInfer TRTLLM BF16 backend (BlockMajorK, rows shuffled), so no extra weight copy is needed; larger batches keep using that layer.
Source code in vllm/models/flint/ops/moe.py
FlintMoEWeights
dataclass
¶
Routed-expert NVFP4 weights in FlashInfer's W4A16 layout.
Methods:
-
from_modelopt–From ModelOpt tensors: w13 [E, 2I, H/2] ([gate; up]), w13_scale
Source code in vllm/models/flint/ops/moe.py
from_modelopt(w13, w13_scale, w13_scale_2, w2, w2_scale, w2_scale_2)
classmethod
¶
From ModelOpt tensors: w13 [E, 2I, H/2] ([gate; up]), w13_scale [E, 2I, H/16], w2 [E, H, I/2], w2_scale [E, H, I/16], fp32 [E].
Source code in vllm/models/flint/ops/moe.py
FlintNvfp4Experts
¶
Bases: Module
Routed experts of one Flint MoE layer (NVFP4 GGUF checkpoints).
Loads per-expert ModelOpt NVFP4 tensors ({e}.{gate,up,down}_proj.
{weight, weight_scale, weight_scale_2}) and, once every expert is loaded,
repacks them into FlintMoEWeights and frees the checkpoint layout.
Methods:
-
__init__–max_num_tokens: largest batch; the grouped-GEMM kernels for every
-
forward–shared: shared-expert output, or the shared-expert module, which
Source code in vllm/models/flint/ops/moe.py
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 | |
__init__(num_experts, hidden_size, intermediate_size, top_k, max_num_tokens=0)
¶
max_num_tokens: largest batch; the grouped-GEMM kernels for every tactic up to it are compiled when the weights are loaded.
Source code in vllm/models/flint/ops/moe.py
forward(hidden_states, router_weight, shared)
¶
shared: shared-expert output, or the shared-expert module, which then runs on the auxiliary stream concurrently with the routed path.
Source code in vllm/models/flint/ops/moe.py
LLMoEWeights
dataclass
¶
Weights of one NVFP4 MoE layer for flint_moe_ll (views, no copies).
Shared expert: ModelOpt-packed weights (int32 views) with flat 128x4-swizzled scale bytes and fp32 global scales.
Source code in vllm/models/flint/ops/moe.py
_add_rms_norm(a_ptr, r_ptr, w_ptr, t, eps, H)
¶
norm.fused_add_rms_norm for row t: (bf16 residual, bf16 out), both
from the unrounded fp32 sum s = a + r.
Source code in vllm/models/flint/ops/moe.py
_block_major_offsets(rows, k, ROWS)
¶
Element offsets of (rows, k) in FlashInfer's BF16 BlockMajorK expert layout: [K / 64, ROWS, 64].
Source code in vllm/models/flint/ops/moe.py
_combine_kernel(y_ptr, perm_ptr, tw_ptr, shared_ptr, out_ptr, H, TOPK, BH)
¶
out[t] = bf16(bf16(sum_j tw[t, j] * y[perm[t * TOPK + j]]) + shared[t]), weighted sum in fp32.
Grid (M, H // BH).
Source code in vllm/models/flint/ops/moe.py
_dequant8(q, s)
¶
int32 words of E2M1 codes + per-word E4M3 scales (uint8), same shape -> eight bf16 or fp16 tensors (code j of each word, value * scale, exact).
Source code in vllm/models/flint/ops/moe.py
_dequant_rows(q, s, BN, BK)
¶
[BN, BK / 8] int32 code words + per-word E4M3 scales -> [BN, BK] bf16 or fp16 (value * scale, exact), K in natural order.
Source code in vllm/models/flint/ops/moe.py
_down_gemv_bf16_kernel(act_ptr, ids_ptr, tw_ptr, w_ptr, rowmap_ptr, shared_ptr, out_ptr, H, INTER, TOPK, BN)
¶
BF16 experts: out[t] = bf16(bf16(sum_j tw[t, j] * y_j) + shared[t]), y_j = bf16(act[t * TOPK + j] @ W2[e_j]^T).
w_ptr: TRTLLM BlockMajorK down weight [E, I / 64, H, 64]; rowmap: output row -> physical row. Grid (M, H // BN).
Source code in vllm/models/flint/ops/moe.py
_down_gemv_kernel(act_ptr, ids_ptr, tw_ptr, w_ptr, s_ptr, gs_ptr, shared_ptr, out_ptr, H, INTER, TOPK, BN)
¶
out[t] = bf16(bf16(sum_j tw[t, j] * y_j) + shared[t]) with y_j = bf16(act[t * TOPK + j] @ W2[e_j]^T), weighted sum in fp32.
w_ptr: int32 view of the down weight. Grid (M, H // BN).
Source code in vllm/models/flint/ops/moe.py
_gate_up_gemv_bf16_kernel(x_ptr, ids_ptr, w_ptr, rowmap_ptr, act_ptr, H, INTER, TOPK, BN, BK)
¶
BF16 experts: act[p] = silu(x[t] @ Wg[e]^T) * (x[t] @ Wu[e]^T).
w_ptr: TRTLLM BlockMajorK gate/up weight [E, H / 64, 2I, 64]; rowmap: logical row (gate rows, then up rows) -> physical row. Grid (M * TOPK, INTER // BN).
Source code in vllm/models/flint/ops/moe.py
_gate_up_gemv_kernel(x_ptr, ids_ptr, w_ptr, s_ptr, gs_ptr, act_ptr, H, INTER, TOPK, BN, BK)
¶
act[p] = silu(x[t] @ Wg[e]^T) * (x[t] @ Wu[e]^T), pair p = t * TOPK + j.
w_ptr: int32 view of the FlashInfer GEMM1 weight ([up; gate] interleaved in 64-row groups). Grid (M * TOPK, INTER // BN).
Source code in vllm/models/flint/ops/moe.py
_router_kernel(x_ptr, res_ptr, ln_w_ptr, eps, res_out_ptr, xn_ptr, wr_ptr, logits_ptr, ids_ptr, tw_ptr, counter_ptr, H, E, TOPK, EB, FUSE_NORM)
¶
Router GEMV (bf16 logits) of experts [pe * EB, + EB) for token t, then
top-k by the token's last program. Grid (M, E // EB), or (M * E // EB,)
with FUSE_NORM, where the GEMV input is norm.fused_add_rms_norm of x and
res (program pe == 0 writes the residual and normed rows). Router weights
are fetched before the PDL dependency wait.
The last program is found with a per-token atomic counter that is never reset (the low bits of the signed int32 count; wraparound-safe since the program count is a power of two), so the buffer must be zeroed once at allocation and only used by launches serialized on one stream.
Source code in vllm/models/flint/ops/moe.py
_scale_offsets(e, rows, words, ROWS, K)
¶
Byte offsets of the E4M3 block scales of (rows, int32 words) in FlashInfer's MMA scale layout: physical (E, ROWS / 128, K / 64, 32, 4, 4) for logical (row % 32, row % 128 // 32, row // 128, group % 4, group // 4).
Source code in vllm/models/flint/ops/moe.py
_start_shared(shared, x, fork, join)
¶
Run a shared-expert module on the auxiliary stream (fork/join events, CUDA-graph safe). Returns (output, event to wait on before reading it).
Source code in vllm/models/flint/ops/moe.py
_topk_kernel(logits_ptr, ids_ptr, tw_ptr, E, TOPK)
¶
Top-k of bf16 router logits. Grid (M,).
Source code in vllm/models/flint/ops/moe.py
_topk_row(logits, offs, ids_ptr, tw_ptr, E, TOPK, TOPK_P2)
¶
Top-k of one row of bf16 logits (ties -> lowest expert index), fp32
sigmoid scores renormalized over the selected experts, s / (sum +
1e-20), rounded to bf16 (the reference packs bf16 routing weights).
Each logit and its index are packed into one order-preserving int32 key so every selection step is a single max-reduction.
Source code in vllm/models/flint/ops/moe.py
flint_moe(x, router_weight, weights, top_k, counter, shared_output, shared_ready=None)
¶
Routed-expert MoE output + shared_output for BF16 x [M, H].
counter: buffer from make_route_counter. shared_ready: event the
current stream waits on before the first read of shared_output (which
may be produced on another stream).
Source code in vllm/models/flint/ops/moe.py
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 770 771 772 773 774 775 776 777 778 779 780 | |
flint_moe_bf16_decode(x, router_weight, w13, w2, row_maps, top_k, counter, shared_output, shared_ready=None)
¶
Decode-path MoE on TRTLLM-layout BF16 experts (w13 [E, 2I, H], w2 [E, H, I] storage in BlockMajorK order) with the TRTLLM BF16 MoE rounding points: act = bf16(silu(g) * u) from fp32 gate/up, y_e = bf16(acc), routed = bf16(sum tw * y_e) in fp32, out = bf16(routed + shared).
Source code in vllm/models/flint/ops/moe.py
flint_moe_ll(attn_out, residual, ln_weight, eps, w, top_k, counter)
¶
Decode MoE block for M <= LL_MOE_MAX_TOKENS tokens.
Computes residual' = attn_out + residual and x = rms_norm(residual')
as norm.fused_add_rms_norm, then the routed + shared expert MoE of
x (NVFP4 rule). Returns (moe_out, residual'); inputs are not
modified. A Triton router kernel and two CuTe split-K expert kernels
(moe_gemv) chained with programmatic dependent launch. counter: buffer
from make_route_counter.
Source code in vllm/models/flint/ops/moe.py
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 | |
ll_weights_from_layers(router_weight, shared_gate_up, shared_down, experts)
¶
LLMoEWeights viewing the loaded layers' tensors: the shared-expert
linears are vLLM linear layers holding the FlashInfer-prepared NVFP4
tensors (ModelOpt-packed weight, 128x4-swizzled scales, fp32 global
scale).
Source code in vllm/models/flint/ops/moe.py
make_route_counter(device=None)
¶
Zeroed counter buffer for the router GEMV kernel; allocate it outside CUDA graph capture and use it from one stream only.
Source code in vllm/models/flint/ops/moe.py
route(x, router_weight, top_k, counter)
¶
BF16 router logits -> (top-k ids int32, renormalized fp32 weights).
Source code in vllm/models/flint/ops/moe.py
trtllm_bf16_row_maps(hidden_size, intermediate_size, device)
¶
Logical -> physical row maps of the TRTLLM BF16 expert weights as
produced by convert_moe_weights_to_flashinfer_trtllm_block_layout
(gated gate/up interleave + epilogue-tile shuffle). Returns (w13 map over
[gate rows; up rows], w2 map over output rows), int32.