Skip to content

vllm.model_executor.layers.fused_moe.runner.moe_runner

Classes:

  • MoERunner

    Standard MoE runner implementation for executing Mixture of Experts layers.

MoERunner

Bases: MoERunnerInterface

Standard MoE runner implementation for executing Mixture of Experts layers.

This is the primary concrete implementation of MoE execution logic, providing comprehensive support for standard MoE operations. It handles: - Expert routing and token dispatching using various routing strategies - Shared experts computation with optional parallel execution using CUDA streams - Tensor model parallel and expert parallel operations - Multiple quantization methods and optimized kernel selection - Both monolithic and decomposed expert execution paths - Integration with various parallel execution modes (TP, EP, DP)

The runner orchestrates the complete MoE forward pass including routing tokens to experts, executing expert computations in parallel, and combining results. It supports advanced features like overlapped execution of shared experts, optimized kernels for different parallel configurations, and seamless integration with vLLM's distributed execution framework.

Eventually, this class may be split into more specialized implementations for different configurations (e.g., with/without shared experts, gates, etc.).

Methods:

Attributes:

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
 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
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 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
 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
 649
 650
 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
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 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
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
class MoERunner(MoERunnerInterface):
    """
    Standard MoE runner implementation for executing Mixture of Experts layers.

    This is the primary concrete implementation of MoE execution logic, providing
    comprehensive support for standard MoE operations. It handles:
    - Expert routing and token dispatching using various routing strategies
    - Shared experts computation with optional parallel execution using CUDA streams
    - Tensor model parallel and expert parallel operations
    - Multiple quantization methods and optimized kernel selection
    - Both monolithic and decomposed expert execution paths
    - Integration with various parallel execution modes (TP, EP, DP)

    The runner orchestrates the complete MoE forward pass including routing tokens
    to experts, executing expert computations in parallel, and combining results.
    It supports advanced features like overlapped execution of shared experts,
    optimized kernels for different parallel configurations, and seamless
    integration with vLLM's distributed execution framework.

    Eventually, this class may be split into more specialized implementations
    for different configurations (e.g., with/without shared experts, gates, etc.).
    """

    def __init__(
        self,
        layer_name: str,
        moe_config: FusedMoEConfig,
        router: FusedMoERouter,
        routed_experts: RoutedExperts,
        enable_dbo: bool = False,
        gate: torch.nn.Module | None = None,
        shared_experts: torch.nn.Module | None = None,
        shared_expert_gate: torch.nn.Module | None = None,
        routed_input_transform: torch.nn.Module | None = None,
        routed_output_transform: torch.nn.Module | None = None,
        routed_scaling_factor: float = 1.0,
    ):
        super().__init__()
        self.moe_config = moe_config
        self.router = router
        self.routed_input_transform = routed_input_transform
        self.routed_output_transform = routed_output_transform
        self.routed_scaling_factor = routed_scaling_factor
        self.gate = gate
        self.shared_expert_gate = shared_expert_gate
        self.routed_experts = routed_experts
        self.enable_dbo = enable_dbo

        # When both gates are present and FSE is enabled, fuse their
        # weight matrices into [num_experts + num_shared, hidden] so one
        # F.linear produces combined logits. The topk kernel can then
        # apply routing softmax and shared expert activation (sigmoid)
        # in a single launch.
        self._fse_fuse_gate = gate is not None and shared_expert_gate is not None
        self._combined_gate_weight: torch.Tensor | None = None

        self._shared_experts: SharedExperts | None = None
        if shared_experts is not None:
            can_overlap = lambda: self._quant_method.mk_can_overlap_shared_experts
            self._shared_experts = SharedExperts(
                shared_experts,
                moe_config=moe_config,
                enable_dbo=enable_dbo,
                mk_can_overlap_shared_experts=can_overlap,
            )

        # Needed for string -> MoERunner layer lookup in custom ops.
        self.layer_name = layer_name

        self._forward_entry = self._select_forward()

        # For smuggling this layer into the fused moe custom op
        register_layer_for_moe_forward_op(get_current_vllm_config(), self)

    def load_weights(
        self, weights: Iterable[tuple[str, torch.Tensor]]
    ) -> Iterable[str]:
        return self.routed_experts.load_weights(weights)

    def _select_forward(self) -> Callable:
        if current_platform.is_tpu() or current_platform.is_cpu():
            # TODO: Once the OOM issue for the TPU backend is resolved, we
            # will switch to using the moe_forward custom op.
            # Note: CPU doesn't require wrapped _forward_impl.
            return _moe_forward if self._shared_experts is None else _moe_forward_shared

        return (
            torch.ops.vllm.moe_forward
            if self._shared_experts is None
            else torch.ops.vllm.moe_forward_shared
        )

    @property
    def shared_experts(self) -> SharedExperts | None:
        return self._shared_experts

    # TODO(bnell): Temporary hack. Get rid of this.
    def _replace_quant_method(self, quant_method: FusedMoEMethodBase):
        self.routed_experts._replace_quant_method(quant_method)

    # TODO(bnell): Hack for elastic_ep. Get rid of this
    def _set_moe_config(self, new_moe_config: FusedMoEConfig):
        self.moe_config = new_moe_config
        self.routed_experts._set_moe_config(new_moe_config)
        if self._shared_experts is not None:
            self._shared_experts._set_moe_config(new_moe_config)

    def _maybe_fuse_gate_weights(self):
        """Fuse router and shared expert gate weights on first call.

        Cannot be done at __init__ because gate weights are loaded after
        module construction (via weight_loader). Called once from
        _forward_impl before the first forward pass.
        """
        if self._combined_gate_weight is None:
            assert self.gate is not None and self.shared_expert_gate is not None
            self._combined_gate_weight = torch.cat(
                [self.gate.weight, self.shared_expert_gate.weight],
                dim=0,
            )

    @property
    def _quant_method(self) -> FusedMoEMethodBase:
        return self.routed_experts.quant_method

    def apply_routed_input_transform(
        self,
        hidden_states: torch.Tensor,
    ) -> tuple[torch.Tensor, torch.Tensor | None]:
        """Apply transform for routed experts (e.g., latent projection).

        This is called by MoERunner.forward_native. The original hidden_states
        is saved separately so shared experts get [S, hidden_size] while
        routed experts get the transformed [S, moe_latent_size].

        Returns (possibly transformed) hidden states and the input for shared
        experts (or None if there are no shared experts).
        """
        if self.routed_input_transform is not None:
            result = self.routed_input_transform(hidden_states)
            # ReplicatedLinear returns (output, extra_bias) tuple.
            # We only need the output tensor; extra_bias is not used here.
            if isinstance(result, tuple):
                return result[0], hidden_states
            return result, hidden_states

        return (
            hidden_states,
            hidden_states if self._shared_experts is not None else None,
        )

    def apply_routed_output_transform(
        self,
        fused_output: torch.Tensor,
    ) -> torch.Tensor:
        """Apply transform to routed expert output (e.g., latent to full dim).

        Used by latent MoE models (e.g., NemotronH) where routed experts
        operate in a compressed latent space and need projection back to
        the full hidden dimension before combining with shared expert output.
        """
        if self.routed_output_transform is not None:
            r = self.routed_output_transform(fused_output)
            fused_output = r[0] if isinstance(r, tuple) else r
        return fused_output

    def _maybe_apply_routed_scale_to_output(
        self,
        shared_output: torch.Tensor | None,
        fused_output: torch.Tensor,
    ) -> tuple[torch.Tensor | None, torch.Tensor]:
        """Apply routed_scaling_factor to the output with FP16 overflow
        protection.

        Scale the fused expert output by routed_scaling_factor. For FP16,
        avoid overflow by dividing shared_output by the scale instead
        (the decoder layer compensates with matching divisions).
        """
        if self.routed_scaling_factor != 1.0:
            if fused_output.dtype != torch.float16 or shared_output is None:
                fused_output *= self.routed_scaling_factor
            elif shared_output is not None:
                shared_output *= 1.0 / self.routed_scaling_factor
        return shared_output, fused_output

    @property
    def _fused_output_is_reduced(self) -> bool:
        return (
            self._quant_method.moe_kernel is not None
            and self._quant_method.moe_kernel.output_is_reduced()
        )

    def _maybe_reduce_shared_expert_output(
        self,
        shared_output: torch.Tensor | None,
        fused_output_is_reduced: bool | None = None,
    ) -> torch.Tensor | None:
        """All-reduce shared expert output when the combine kernel already
        reduced fused output.

        * If the combine kernel does the reduction for fused_output, reduce
          shared_output separately. O.w, reduce fused_output+shared_output later.
        * If we have SP (TP=N, DP=M, EP), there is a separate AG step handled
          in the model.
        """
        if fused_output_is_reduced is None:
            fused_output_is_reduced = self._fused_output_is_reduced

        if (
            shared_output is not None
            and not self.moe_config.is_sequence_parallel
            and fused_output_is_reduced
        ):
            shared_output = tensor_model_parallel_all_reduce(shared_output)
        return shared_output

    def _maybe_reduce_routed_output_before_transform(
        self,
        fused_output: torch.Tensor,
        fused_output_is_reduced: bool,
    ) -> tuple[torch.Tensor, bool]:
        """All-reduce latent routed output before its output transform.

        Latent MoE output transforms may contain non-linear ops, e.g. RMSNorm.
        TP partial routed outputs must be summed in latent space before such
        transforms are applied.
        """
        if (
            self.routed_output_transform is not None
            and not self.moe_config.is_sequence_parallel
            and (self.moe_config.tp_size > 1 or self.moe_config.ep_size > 1)
            and not fused_output_is_reduced
        ):
            fused_output = tensor_model_parallel_all_reduce(fused_output)
            fused_output_is_reduced = True
        return fused_output, fused_output_is_reduced

    def _maybe_reduce_final_output(
        self,
        states: torch.Tensor,
        trunc_size: int | None,
        output_is_reduced: bool | None = None,
    ) -> torch.Tensor:
        """All-reduce the combined output if needed.

        This is the "late" all-reduce path. When neither fused nor shared
        output was individually reduced, the combined sum is all-reduced
        here. Skipped when sequence-parallel is active (SP handles its
        own reduction) or when the early path already reduced both outputs.
        """
        # skip_final_all_reduce must not coexist with a pre-reduced fused
        # output. This should be enforced by MoE config initialization.
        if self.moe_config.skip_final_all_reduce:
            assert not self._fused_output_is_reduced, (
                "skip_final_all_reduce requires an un-reduced fused output"
            )

        # We don't need to reduce the final output if:
        # - We are not running with TP or DP
        # - The MK already reduced the fused output itself.
        if output_is_reduced is None:
            output_is_reduced = self._fused_output_is_reduced

        if (
            not self.moe_config.is_sequence_parallel
            and not self.moe_config.skip_final_all_reduce
            and (self.moe_config.tp_size > 1 or self.moe_config.ep_size > 1)
            and not output_is_reduced
        ):
            states = tensor_model_parallel_all_reduce(states)

        return states[..., :trunc_size] if trunc_size is not None else states

    def _encode_layer_name(self) -> str | LayerName:
        if _USE_LAYERNAME:
            return LayerName(self.layer_name)
        # Can be unavailable or None in unittests
        if (
            is_forward_context_available()
            and get_forward_context().all_moe_layers is not None
        ):
            return "from_forward_context"
        return self.layer_name

    def _maybe_pad_hidden_states(
        self,
        shared_experts_input: torch.Tensor | None,
        hidden_states: torch.Tensor,
    ) -> tuple[torch.Tensor, int | None, int | None]:
        """Pad hidden_states to moe_config.hidden_dim and compute the
        original dimension for later truncation.

        For latent MoE, the routed hidden_states may be smaller than
        hidden_dim. Padding ensures uniform tensor sizes through the
        fused MoE kernel. The returned trunc_size is used by
        _maybe_reduce_final_output to strip the padding from the result.
        """
        shared_experts_hidden_dim = (
            shared_experts_input.shape[-1] if shared_experts_input is not None else 0
        )
        transformed_hidden_dim: int | None = hidden_states.shape[-1]
        if (
            not self._quant_method.skip_forward_padding
            and self.moe_config.hidden_dim != transformed_hidden_dim
        ):
            assert transformed_hidden_dim is not None
            hidden_states = F.pad(
                hidden_states,
                (0, self.moe_config.hidden_dim - transformed_hidden_dim),
                mode="constant",
                value=0.0,
            )

        # Truncation sizes for stripping kernel padding from the output.
        # None means no truncation needed (no padding was applied).
        #
        # Two truncation points exist in forward():
        #   pre_xform:  applied to fused_output BEFORE routed_output_transform
        #   post_xform: applied to the final result AFTER all-reduce
        #
        # MoE with routed output transform or shared experts:
        #   - pre_xform applies if the transform needs unpadded routed output
        #     or shared+routed add needs matching hidden dims. For Nemotron-3
        #     Nano, TRTLLM NVFP4 pads routed MoE hidden dim 2688->2816, while
        #     shared output stays 2688.
        #   - post_xform uses shared_experts_hidden_dim when transform and shared
        #     experts make the final output full hidden dim.
        #
        # Standard MoE / MoE without transforms (GPT-OSS, Mixtral):
        #   - pre_xform is None (no early truncation)
        #   - post_xform strips padding after all-reduce (or None if unpadded)
        if transformed_hidden_dim == hidden_states.shape[-1]:
            transformed_hidden_dim = None

        pre_xform_trunc_size = None
        if self.routed_output_transform is not None or shared_experts_hidden_dim > 0:
            pre_xform_trunc_size = transformed_hidden_dim
        post_xform_trunc_size = transformed_hidden_dim
        if self.routed_output_transform is not None and shared_experts_hidden_dim > 0:
            post_xform_trunc_size = shared_experts_hidden_dim

        return hidden_states, pre_xform_trunc_size, post_xform_trunc_size

    def _maybe_apply_shared_experts(
        self,
        shared_experts_input: torch.Tensor | None,
        order: SharedExpertsOrder,
    ):
        if self._shared_experts is not None:
            assert shared_experts_input is not None
            self._shared_experts(shared_experts_input, order)

    def _apply_quant_method(
        self,
        hidden_states: torch.Tensor,
        router_logits: torch.Tensor,
        shared_experts_input: torch.Tensor | None,
        input_ids: torch.Tensor | None = None,
    ) -> tuple[torch.Tensor | None, torch.Tensor | UnfinalizedMoEOutput]:
        """Run expert routing and the fused MoE kernel via the quant method.

        Orchestrates shared expert execution (before/after), expert selection
        via the router, and the actual fused MoE computation. Returns
        (shared_expert_output, fused_expert_output).
        """
        self._maybe_apply_shared_experts(
            shared_experts_input, SharedExpertsOrder.NO_OVERLAP
        )

        if self.routed_experts.quant_method.is_monolithic:
            # Monolithic kernels: pass router_logits to routed_experts
            fused_out = self.routed_experts.forward_monolithic(
                x=hidden_states,
                router_logits=router_logits,
                input_ids=input_ids,
            )
        else:
            # Modular kernels: select experts first, then call routed_experts
            topk_weights, topk_ids = self.router.select_experts(
                hidden_states=hidden_states,
                router_logits=router_logits,
                topk_indices_dtype=self._quant_method.topk_indices_dtype,
                input_ids=input_ids,
            )

            fused_out = self.routed_experts.forward_modular(
                x=hidden_states,
                topk_weights=topk_weights,
                topk_ids=topk_ids,
                shared_experts=self._shared_experts,
                shared_experts_input=shared_experts_input,
            )

        self._maybe_apply_shared_experts(
            shared_experts_input,
            SharedExpertsOrder.MULTI_STREAM_OVERLAPPED,
        )

        return (
            self._shared_experts.output if self._shared_experts is not None else None,
            fused_out,
        )

    def _sequence_parallel_context(self):
        """Return a context manager for sequence-parallel token
        redistribution.

        When sequence parallelism is active, returns a context that handles
        local size tracking for proper token scatter/gather. Otherwise
        returns a no-op context.
        """
        ctx = get_forward_context()
        return (
            ctx.dp_metadata.sp_local_sizes(self.moe_config.sp_size)
            if ctx.dp_metadata
            else nullcontext()
        )

    def _maybe_sync_shared_experts_stream(
        self,
        shared_experts_input: torch.Tensor | None,
    ):
        # If router/gate provided, then apply it here.
        # (Note: This code runs only when "overlapped mode" is on to allow
        #        parallel execution of shared experts with the RoutedExperts via
        #        separate cuda stream)
        if self._shared_experts is not None:
            assert shared_experts_input is not None
            self._shared_experts.maybe_sync_shared_experts_stream(shared_experts_input)

    def _maybe_add_zero_expert_output(
        self,
        result: torch.Tensor,
    ) -> torch.Tensor:
        """Add the zero expert's contribution to the final result.

        When a ZeroExpertRouter is used, it computes a bias-like output
        from the "zero expert" that is added to the combined routed+shared
        expert output.
        """
        if isinstance(self.router, ZeroExpertRouter):
            zero_expert_output = self.router.zero_expert_output
            assert zero_expert_output is not None
            result = result + zero_expert_output
        return result

    def forward(
        self,
        hidden_states: torch.Tensor,
        router_logits: torch.Tensor,
        input_ids: torch.Tensor | None = None,
        shared_experts_input: torch.Tensor | None = None,
    ) -> torch.Tensor:
        """Invoke the fused moe layer.

        Input:
        - hidden_states
        - router_logits

        Output:
        - The new hidden_states.

        Calling sequence
        - forward
          - self._forward_entry (_moe_forward or _moe_forward_shared custom op)
            - _forward_impl

        Note: The existence of _moe_forward and _moe_forward_shared custom ops are due
        to the following reason:
        1. pytorch cannot handle union types in custom op signatures so
           _moe_forward and _moe_forward_shared must be split.
        """

        # Apply transform for routed experts (e.g., latent projection for
        # latent MoE). When the caller pre-applies the routed input transform
        # outside the runner (e.g. to overlap it on a separate stream), it
        # passes the already-transformed routed input as ``hidden_states`` and
        # the original hidden states as ``shared_experts_input``; skip the
        # transform in that case so shared experts still see the original input.
        if shared_experts_input is None:
            hidden_states, shared_experts_input = self.apply_routed_input_transform(
                hidden_states
            )

        # Record before `_maybe_pad_hidden_states` pads activations to match
        # `moe_config.hidden_dim`, e.g. after `align_trtllm_fp4_moe_hidden_dim_for_fi`
        # so routed output can be trimmed before
        # shared+routed add / latent up proj if needed.

        hidden_states, og_hidden_dim_pre_xform, og_hidden_dim_post_xform = (
            self._maybe_pad_hidden_states(
                shared_experts_input,
                hidden_states,
            )
        )

        result = self._forward_entry(
            hidden_states,
            router_logits,
            shared_experts_input,
            input_ids,
            self._encode_layer_name(),
            self.moe_config.hidden_dim_unpadded
            if self._quant_method.has_unpadded_output
            else 0,
        )

        #
        # Note: there are two all-reduce points below. They are mutually
        # exclusive, controlled by _fused_output_is_reduced
        #  - When True: the combine kernel already reduced fused_output,
        #    so we reduce shared_output here to match, then skip the
        #    all-reduce in _maybe_reduce_final_output.
        #  - When False: neither output is reduced yet, so we combine
        #    them first and all-reduce the sum in _maybe_reduce_final_output.

        # Extract outputs from result
        shared_output, fused_output = _unpack(result)
        fused_output = cast(torch.Tensor, fused_output)

        if og_hidden_dim_pre_xform is not None:
            fused_output = fused_output[..., :og_hidden_dim_pre_xform]

        fused_output_is_reduced = self._fused_output_is_reduced

        # Latent routed output has to be reduced before output transform,
        # because the transform may include non-linear normalization.
        fused_output, fused_output_is_reduced = (
            self._maybe_reduce_routed_output_before_transform(
                fused_output,
                fused_output_is_reduced,
            )
        )

        # If routed output is already reduced, reduce shared to match.
        # See note above re: the two all-reduce points.
        shared_output = self._maybe_reduce_shared_expert_output(
            shared_output, fused_output_is_reduced
        )

        shared_output, fused_output = self._maybe_apply_routed_scale_to_output(
            shared_output, fused_output
        )

        # Apply output transform (e.g. latent -> full dim)
        fused_output = self.apply_routed_output_transform(fused_output)

        if shared_output is not None:
            result = shared_output + fused_output
        else:
            result = fused_output

        result = self._maybe_reduce_final_output(
            result, og_hidden_dim_post_xform, fused_output_is_reduced
        )

        return self._maybe_add_zero_expert_output(result)

    @property
    def do_naive_dispatch_combine(self) -> bool:
        return (
            self.moe_config.dp_size > 1 or self.moe_config.is_sequence_parallel
        ) and not self._quant_method.supports_internal_mk

    def _maybe_dispatch(
        self,
        hidden_states: torch.Tensor,
        router_logits: torch.Tensor,
    ) -> tuple[torch.Tensor, torch.Tensor]:
        # For naive dispatch/combine Dp/Ep, dispatch the hidden states and
        # router logits to all experts.
        # NOTE: this will be removed once all kernels are migrated into the
        # MoEKernel framework.
        if self.do_naive_dispatch_combine:
            result = get_ep_group().dispatch_router_logits(
                hidden_states,
                router_logits,
                self.moe_config.is_sequence_parallel,
            )
            assert len(result) == 2
            hidden_states, router_logits = result

        if (
            self.moe_config.pcp_size > 1
            and not self.moe_config.moe_parallel_config.use_all2all_kernels
        ):
            hidden_states = get_pcp_group().all_gather(hidden_states, dim=0)
            router_logits = get_pcp_group().all_gather(router_logits, dim=0)

        return hidden_states, router_logits

    def _maybe_combine(
        self,
        shared_output: torch.Tensor | None,
        hidden_states: torch.Tensor | UnfinalizedMoEOutput,
    ) -> (
        torch.Tensor
        | UnfinalizedMoEOutput
        | tuple[torch.Tensor | None, torch.Tensor | UnfinalizedMoEOutput]
    ):
        if self.do_naive_dispatch_combine:
            hidden_states = get_ep_group().combine(
                cast(torch.Tensor, hidden_states),
                self.moe_config.is_sequence_parallel,
            )

        if (
            self.moe_config.pcp_size > 1
            and not self.moe_config.moe_parallel_config.use_all2all_kernels
        ):
            hidden_states = get_pcp_group().reduce_scatter(
                cast(torch.Tensor, hidden_states), dim=0
            )

        if self.shared_experts is not None:
            assert shared_output is not None
            return shared_output, hidden_states
        else:
            return hidden_states

    def _forward_impl(
        self,
        hidden_states: torch.Tensor,
        router_logits: torch.Tensor,
        shared_experts_input: torch.Tensor | None,
        input_ids: torch.Tensor | None = None,
    ) -> (
        torch.Tensor
        | UnfinalizedMoEOutput
        | tuple[torch.Tensor, torch.Tensor | UnfinalizedMoEOutput]
    ):
        """Entry point called by the custom op to run the MoE computation.

        Handles pre-dispatch setup (gate application, external shared expert
        triggering, quant config init) then performs the following steps
        within the sequence-parallel context.

        - Performs expert routing
        - fused MoE kernel execution
        - shared expert computation.

        Returns routed output, optionally paired with shared-expert output. A
        fused consumer may request the routed output in deferred-finalize form.
        """
        # TODO(bnell): this can be removed after MK migration is complete.
        self.routed_experts._ensure_moe_quant_config_init()

        # Sync aux and main stream for shared expert multi-stream overlap.
        self._maybe_sync_shared_experts_stream(shared_experts_input)

        # If the Runner holds the gate, apply it after the stream sync,
        # so it can run overlapped with the
        # NOTE: in future PR, MoE runner will always hold the gate.
        if self.gate is not None:
            if self._fse_fuse_gate:
                self._maybe_fuse_gate_weights()
                router_logits = F.linear(hidden_states, self._combined_gate_weight)
            else:
                router_logits, _ = self.gate(hidden_states)

        with self._sequence_parallel_context():
            # TODO(bnell): parts of the dispatch/combine steps will go away once
            # #32567 lands and the remaining kernels are made MKs.  The PCP
            # code will probably remain
            hidden_states, router_logits = self._maybe_dispatch(
                hidden_states,
                router_logits,
            )

            shared_output, hidden_states = self._apply_quant_method(
                hidden_states=hidden_states,
                router_logits=router_logits,
                shared_experts_input=shared_experts_input,
                input_ids=input_ids,
            )

            return self._maybe_combine(
                shared_output,
                hidden_states,
            )

    #########################################################
    #
    # Old methods from FusedMoE layer. Remove when possible.
    #
    #########################################################

    #
    # Properties
    #

    @property
    def layer_id(self):
        # Delayed import to avoid circular dependency
        from vllm.model_executor.models.utils import extract_layer_index

        return extract_layer_index(self.layer_name)

    #
    # Attributes still needed by models
    #

    @property
    def is_monolithic(self) -> bool:
        return self.routed_experts.quant_method.is_monolithic

    @property
    def activation(self) -> MoEActivation:
        return self.routed_experts.activation

    #
    # Expert maps
    #

    @property
    def expert_map_manager(self):
        """Forward to routed_experts.expert_map_manager for backward compatibility."""
        return self.routed_experts.expert_map_manager

    @property
    def expert_placement_strategy(self) -> ExpertPlacementStrategy:
        return self.expert_map_manager.placement_strategy

    @property
    def expert_global_to_physical(self) -> torch.Tensor | None:
        tables = self.expert_map_manager.routing_tables
        return tables[0] if tables else None

    @property
    def expert_physical_to_global(self) -> torch.Tensor | None:
        """Routing table: physical expert ID to global expert ID."""
        tables = self.expert_map_manager.routing_tables
        return tables[1] if tables else None

    @property
    def expert_local_to_global(self) -> torch.Tensor | None:
        """Routing table: local expert ID to global expert ID."""
        tables = self.expert_map_manager.routing_tables
        return tables[2] if tables else None

    @property
    def expert_map(self) -> torch.Tensor | None:
        return self.routed_experts.expert_map

    def _expert_routing_tables(
        self,
    ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None:
        return self.routed_experts._expert_routing_tables()

    def update_expert_map(self):
        self.routed_experts.update_expert_map()

    def _map_global_expert_id_to_local_expert_id(self, expert_id: int) -> int:
        """Map global expert ID to local expert ID."""
        return self.routed_experts._map_global_expert_id_to_local_expert_id(expert_id)

    def get_expert_weights(self) -> Iterable[torch.Tensor]:
        return self.routed_experts.get_expert_weights()

    #
    # EPLB
    #

    @property
    def eplb_state(self) -> EplbLayerState | None:
        return self.router.eplb_state

    def set_eplb_state(
        self,
        moe_layer_idx: int,
        expert_load_view: torch.Tensor,
        logical_to_physical_map: torch.Tensor,
        logical_replica_count: torch.Tensor,
    ) -> None:
        """
        Register the EPLB state in this layer.

        This is used later in forward pass, where we get the expert mapping
        and record the load metrics in `expert_load_view`.
        """
        if self.router.eplb_state is not None:
            self.router.eplb_state.set_layer_state(
                moe_layer_idx,
                expert_load_view,
                logical_to_physical_map,
                logical_replica_count,
            )

expert_local_to_global property

Routing table: local expert ID to global expert ID.

expert_map_manager property

Forward to routed_experts.expert_map_manager for backward compatibility.

expert_physical_to_global property

Routing table: physical expert ID to global expert ID.

_apply_quant_method(hidden_states, router_logits, shared_experts_input, input_ids=None)

Run expert routing and the fused MoE kernel via the quant method.

Orchestrates shared expert execution (before/after), expert selection via the router, and the actual fused MoE computation. Returns (shared_expert_output, fused_expert_output).

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _apply_quant_method(
    self,
    hidden_states: torch.Tensor,
    router_logits: torch.Tensor,
    shared_experts_input: torch.Tensor | None,
    input_ids: torch.Tensor | None = None,
) -> tuple[torch.Tensor | None, torch.Tensor | UnfinalizedMoEOutput]:
    """Run expert routing and the fused MoE kernel via the quant method.

    Orchestrates shared expert execution (before/after), expert selection
    via the router, and the actual fused MoE computation. Returns
    (shared_expert_output, fused_expert_output).
    """
    self._maybe_apply_shared_experts(
        shared_experts_input, SharedExpertsOrder.NO_OVERLAP
    )

    if self.routed_experts.quant_method.is_monolithic:
        # Monolithic kernels: pass router_logits to routed_experts
        fused_out = self.routed_experts.forward_monolithic(
            x=hidden_states,
            router_logits=router_logits,
            input_ids=input_ids,
        )
    else:
        # Modular kernels: select experts first, then call routed_experts
        topk_weights, topk_ids = self.router.select_experts(
            hidden_states=hidden_states,
            router_logits=router_logits,
            topk_indices_dtype=self._quant_method.topk_indices_dtype,
            input_ids=input_ids,
        )

        fused_out = self.routed_experts.forward_modular(
            x=hidden_states,
            topk_weights=topk_weights,
            topk_ids=topk_ids,
            shared_experts=self._shared_experts,
            shared_experts_input=shared_experts_input,
        )

    self._maybe_apply_shared_experts(
        shared_experts_input,
        SharedExpertsOrder.MULTI_STREAM_OVERLAPPED,
    )

    return (
        self._shared_experts.output if self._shared_experts is not None else None,
        fused_out,
    )

_forward_impl(hidden_states, router_logits, shared_experts_input, input_ids=None)

Entry point called by the custom op to run the MoE computation.

Handles pre-dispatch setup (gate application, external shared expert triggering, quant config init) then performs the following steps within the sequence-parallel context.

  • Performs expert routing
  • fused MoE kernel execution
  • shared expert computation.

Returns routed output, optionally paired with shared-expert output. A fused consumer may request the routed output in deferred-finalize form.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _forward_impl(
    self,
    hidden_states: torch.Tensor,
    router_logits: torch.Tensor,
    shared_experts_input: torch.Tensor | None,
    input_ids: torch.Tensor | None = None,
) -> (
    torch.Tensor
    | UnfinalizedMoEOutput
    | tuple[torch.Tensor, torch.Tensor | UnfinalizedMoEOutput]
):
    """Entry point called by the custom op to run the MoE computation.

    Handles pre-dispatch setup (gate application, external shared expert
    triggering, quant config init) then performs the following steps
    within the sequence-parallel context.

    - Performs expert routing
    - fused MoE kernel execution
    - shared expert computation.

    Returns routed output, optionally paired with shared-expert output. A
    fused consumer may request the routed output in deferred-finalize form.
    """
    # TODO(bnell): this can be removed after MK migration is complete.
    self.routed_experts._ensure_moe_quant_config_init()

    # Sync aux and main stream for shared expert multi-stream overlap.
    self._maybe_sync_shared_experts_stream(shared_experts_input)

    # If the Runner holds the gate, apply it after the stream sync,
    # so it can run overlapped with the
    # NOTE: in future PR, MoE runner will always hold the gate.
    if self.gate is not None:
        if self._fse_fuse_gate:
            self._maybe_fuse_gate_weights()
            router_logits = F.linear(hidden_states, self._combined_gate_weight)
        else:
            router_logits, _ = self.gate(hidden_states)

    with self._sequence_parallel_context():
        # TODO(bnell): parts of the dispatch/combine steps will go away once
        # #32567 lands and the remaining kernels are made MKs.  The PCP
        # code will probably remain
        hidden_states, router_logits = self._maybe_dispatch(
            hidden_states,
            router_logits,
        )

        shared_output, hidden_states = self._apply_quant_method(
            hidden_states=hidden_states,
            router_logits=router_logits,
            shared_experts_input=shared_experts_input,
            input_ids=input_ids,
        )

        return self._maybe_combine(
            shared_output,
            hidden_states,
        )

_map_global_expert_id_to_local_expert_id(expert_id)

Map global expert ID to local expert ID.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _map_global_expert_id_to_local_expert_id(self, expert_id: int) -> int:
    """Map global expert ID to local expert ID."""
    return self.routed_experts._map_global_expert_id_to_local_expert_id(expert_id)

_maybe_add_zero_expert_output(result)

Add the zero expert's contribution to the final result.

When a ZeroExpertRouter is used, it computes a bias-like output from the "zero expert" that is added to the combined routed+shared expert output.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _maybe_add_zero_expert_output(
    self,
    result: torch.Tensor,
) -> torch.Tensor:
    """Add the zero expert's contribution to the final result.

    When a ZeroExpertRouter is used, it computes a bias-like output
    from the "zero expert" that is added to the combined routed+shared
    expert output.
    """
    if isinstance(self.router, ZeroExpertRouter):
        zero_expert_output = self.router.zero_expert_output
        assert zero_expert_output is not None
        result = result + zero_expert_output
    return result

_maybe_apply_routed_scale_to_output(shared_output, fused_output)

Apply routed_scaling_factor to the output with FP16 overflow protection.

Scale the fused expert output by routed_scaling_factor. For FP16, avoid overflow by dividing shared_output by the scale instead (the decoder layer compensates with matching divisions).

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _maybe_apply_routed_scale_to_output(
    self,
    shared_output: torch.Tensor | None,
    fused_output: torch.Tensor,
) -> tuple[torch.Tensor | None, torch.Tensor]:
    """Apply routed_scaling_factor to the output with FP16 overflow
    protection.

    Scale the fused expert output by routed_scaling_factor. For FP16,
    avoid overflow by dividing shared_output by the scale instead
    (the decoder layer compensates with matching divisions).
    """
    if self.routed_scaling_factor != 1.0:
        if fused_output.dtype != torch.float16 or shared_output is None:
            fused_output *= self.routed_scaling_factor
        elif shared_output is not None:
            shared_output *= 1.0 / self.routed_scaling_factor
    return shared_output, fused_output

_maybe_fuse_gate_weights()

Fuse router and shared expert gate weights on first call.

Cannot be done at init because gate weights are loaded after module construction (via weight_loader). Called once from _forward_impl before the first forward pass.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _maybe_fuse_gate_weights(self):
    """Fuse router and shared expert gate weights on first call.

    Cannot be done at __init__ because gate weights are loaded after
    module construction (via weight_loader). Called once from
    _forward_impl before the first forward pass.
    """
    if self._combined_gate_weight is None:
        assert self.gate is not None and self.shared_expert_gate is not None
        self._combined_gate_weight = torch.cat(
            [self.gate.weight, self.shared_expert_gate.weight],
            dim=0,
        )

_maybe_pad_hidden_states(shared_experts_input, hidden_states)

Pad hidden_states to moe_config.hidden_dim and compute the original dimension for later truncation.

For latent MoE, the routed hidden_states may be smaller than hidden_dim. Padding ensures uniform tensor sizes through the fused MoE kernel. The returned trunc_size is used by _maybe_reduce_final_output to strip the padding from the result.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _maybe_pad_hidden_states(
    self,
    shared_experts_input: torch.Tensor | None,
    hidden_states: torch.Tensor,
) -> tuple[torch.Tensor, int | None, int | None]:
    """Pad hidden_states to moe_config.hidden_dim and compute the
    original dimension for later truncation.

    For latent MoE, the routed hidden_states may be smaller than
    hidden_dim. Padding ensures uniform tensor sizes through the
    fused MoE kernel. The returned trunc_size is used by
    _maybe_reduce_final_output to strip the padding from the result.
    """
    shared_experts_hidden_dim = (
        shared_experts_input.shape[-1] if shared_experts_input is not None else 0
    )
    transformed_hidden_dim: int | None = hidden_states.shape[-1]
    if (
        not self._quant_method.skip_forward_padding
        and self.moe_config.hidden_dim != transformed_hidden_dim
    ):
        assert transformed_hidden_dim is not None
        hidden_states = F.pad(
            hidden_states,
            (0, self.moe_config.hidden_dim - transformed_hidden_dim),
            mode="constant",
            value=0.0,
        )

    # Truncation sizes for stripping kernel padding from the output.
    # None means no truncation needed (no padding was applied).
    #
    # Two truncation points exist in forward():
    #   pre_xform:  applied to fused_output BEFORE routed_output_transform
    #   post_xform: applied to the final result AFTER all-reduce
    #
    # MoE with routed output transform or shared experts:
    #   - pre_xform applies if the transform needs unpadded routed output
    #     or shared+routed add needs matching hidden dims. For Nemotron-3
    #     Nano, TRTLLM NVFP4 pads routed MoE hidden dim 2688->2816, while
    #     shared output stays 2688.
    #   - post_xform uses shared_experts_hidden_dim when transform and shared
    #     experts make the final output full hidden dim.
    #
    # Standard MoE / MoE without transforms (GPT-OSS, Mixtral):
    #   - pre_xform is None (no early truncation)
    #   - post_xform strips padding after all-reduce (or None if unpadded)
    if transformed_hidden_dim == hidden_states.shape[-1]:
        transformed_hidden_dim = None

    pre_xform_trunc_size = None
    if self.routed_output_transform is not None or shared_experts_hidden_dim > 0:
        pre_xform_trunc_size = transformed_hidden_dim
    post_xform_trunc_size = transformed_hidden_dim
    if self.routed_output_transform is not None and shared_experts_hidden_dim > 0:
        post_xform_trunc_size = shared_experts_hidden_dim

    return hidden_states, pre_xform_trunc_size, post_xform_trunc_size

_maybe_reduce_final_output(states, trunc_size, output_is_reduced=None)

All-reduce the combined output if needed.

This is the "late" all-reduce path. When neither fused nor shared output was individually reduced, the combined sum is all-reduced here. Skipped when sequence-parallel is active (SP handles its own reduction) or when the early path already reduced both outputs.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _maybe_reduce_final_output(
    self,
    states: torch.Tensor,
    trunc_size: int | None,
    output_is_reduced: bool | None = None,
) -> torch.Tensor:
    """All-reduce the combined output if needed.

    This is the "late" all-reduce path. When neither fused nor shared
    output was individually reduced, the combined sum is all-reduced
    here. Skipped when sequence-parallel is active (SP handles its
    own reduction) or when the early path already reduced both outputs.
    """
    # skip_final_all_reduce must not coexist with a pre-reduced fused
    # output. This should be enforced by MoE config initialization.
    if self.moe_config.skip_final_all_reduce:
        assert not self._fused_output_is_reduced, (
            "skip_final_all_reduce requires an un-reduced fused output"
        )

    # We don't need to reduce the final output if:
    # - We are not running with TP or DP
    # - The MK already reduced the fused output itself.
    if output_is_reduced is None:
        output_is_reduced = self._fused_output_is_reduced

    if (
        not self.moe_config.is_sequence_parallel
        and not self.moe_config.skip_final_all_reduce
        and (self.moe_config.tp_size > 1 or self.moe_config.ep_size > 1)
        and not output_is_reduced
    ):
        states = tensor_model_parallel_all_reduce(states)

    return states[..., :trunc_size] if trunc_size is not None else states

_maybe_reduce_routed_output_before_transform(fused_output, fused_output_is_reduced)

All-reduce latent routed output before its output transform.

Latent MoE output transforms may contain non-linear ops, e.g. RMSNorm. TP partial routed outputs must be summed in latent space before such transforms are applied.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _maybe_reduce_routed_output_before_transform(
    self,
    fused_output: torch.Tensor,
    fused_output_is_reduced: bool,
) -> tuple[torch.Tensor, bool]:
    """All-reduce latent routed output before its output transform.

    Latent MoE output transforms may contain non-linear ops, e.g. RMSNorm.
    TP partial routed outputs must be summed in latent space before such
    transforms are applied.
    """
    if (
        self.routed_output_transform is not None
        and not self.moe_config.is_sequence_parallel
        and (self.moe_config.tp_size > 1 or self.moe_config.ep_size > 1)
        and not fused_output_is_reduced
    ):
        fused_output = tensor_model_parallel_all_reduce(fused_output)
        fused_output_is_reduced = True
    return fused_output, fused_output_is_reduced

_maybe_reduce_shared_expert_output(shared_output, fused_output_is_reduced=None)

All-reduce shared expert output when the combine kernel already reduced fused output.

  • If the combine kernel does the reduction for fused_output, reduce shared_output separately. O.w, reduce fused_output+shared_output later.
  • If we have SP (TP=N, DP=M, EP), there is a separate AG step handled in the model.
Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _maybe_reduce_shared_expert_output(
    self,
    shared_output: torch.Tensor | None,
    fused_output_is_reduced: bool | None = None,
) -> torch.Tensor | None:
    """All-reduce shared expert output when the combine kernel already
    reduced fused output.

    * If the combine kernel does the reduction for fused_output, reduce
      shared_output separately. O.w, reduce fused_output+shared_output later.
    * If we have SP (TP=N, DP=M, EP), there is a separate AG step handled
      in the model.
    """
    if fused_output_is_reduced is None:
        fused_output_is_reduced = self._fused_output_is_reduced

    if (
        shared_output is not None
        and not self.moe_config.is_sequence_parallel
        and fused_output_is_reduced
    ):
        shared_output = tensor_model_parallel_all_reduce(shared_output)
    return shared_output

_sequence_parallel_context()

Return a context manager for sequence-parallel token redistribution.

When sequence parallelism is active, returns a context that handles local size tracking for proper token scatter/gather. Otherwise returns a no-op context.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def _sequence_parallel_context(self):
    """Return a context manager for sequence-parallel token
    redistribution.

    When sequence parallelism is active, returns a context that handles
    local size tracking for proper token scatter/gather. Otherwise
    returns a no-op context.
    """
    ctx = get_forward_context()
    return (
        ctx.dp_metadata.sp_local_sizes(self.moe_config.sp_size)
        if ctx.dp_metadata
        else nullcontext()
    )

apply_routed_input_transform(hidden_states)

Apply transform for routed experts (e.g., latent projection).

This is called by MoERunner.forward_native. The original hidden_states is saved separately so shared experts get [S, hidden_size] while routed experts get the transformed [S, moe_latent_size].

Returns (possibly transformed) hidden states and the input for shared experts (or None if there are no shared experts).

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def apply_routed_input_transform(
    self,
    hidden_states: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor | None]:
    """Apply transform for routed experts (e.g., latent projection).

    This is called by MoERunner.forward_native. The original hidden_states
    is saved separately so shared experts get [S, hidden_size] while
    routed experts get the transformed [S, moe_latent_size].

    Returns (possibly transformed) hidden states and the input for shared
    experts (or None if there are no shared experts).
    """
    if self.routed_input_transform is not None:
        result = self.routed_input_transform(hidden_states)
        # ReplicatedLinear returns (output, extra_bias) tuple.
        # We only need the output tensor; extra_bias is not used here.
        if isinstance(result, tuple):
            return result[0], hidden_states
        return result, hidden_states

    return (
        hidden_states,
        hidden_states if self._shared_experts is not None else None,
    )

apply_routed_output_transform(fused_output)

Apply transform to routed expert output (e.g., latent to full dim).

Used by latent MoE models (e.g., NemotronH) where routed experts operate in a compressed latent space and need projection back to the full hidden dimension before combining with shared expert output.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def apply_routed_output_transform(
    self,
    fused_output: torch.Tensor,
) -> torch.Tensor:
    """Apply transform to routed expert output (e.g., latent to full dim).

    Used by latent MoE models (e.g., NemotronH) where routed experts
    operate in a compressed latent space and need projection back to
    the full hidden dimension before combining with shared expert output.
    """
    if self.routed_output_transform is not None:
        r = self.routed_output_transform(fused_output)
        fused_output = r[0] if isinstance(r, tuple) else r
    return fused_output

forward(hidden_states, router_logits, input_ids=None, shared_experts_input=None)

Invoke the fused moe layer.

Input: - hidden_states - router_logits

Output: - The new hidden_states.

Calling sequence - forward - self._forward_entry (_moe_forward or _moe_forward_shared custom op) - _forward_impl

Note: The existence of _moe_forward and _moe_forward_shared custom ops are due to the following reason: 1. pytorch cannot handle union types in custom op signatures so _moe_forward and _moe_forward_shared must be split.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def forward(
    self,
    hidden_states: torch.Tensor,
    router_logits: torch.Tensor,
    input_ids: torch.Tensor | None = None,
    shared_experts_input: torch.Tensor | None = None,
) -> torch.Tensor:
    """Invoke the fused moe layer.

    Input:
    - hidden_states
    - router_logits

    Output:
    - The new hidden_states.

    Calling sequence
    - forward
      - self._forward_entry (_moe_forward or _moe_forward_shared custom op)
        - _forward_impl

    Note: The existence of _moe_forward and _moe_forward_shared custom ops are due
    to the following reason:
    1. pytorch cannot handle union types in custom op signatures so
       _moe_forward and _moe_forward_shared must be split.
    """

    # Apply transform for routed experts (e.g., latent projection for
    # latent MoE). When the caller pre-applies the routed input transform
    # outside the runner (e.g. to overlap it on a separate stream), it
    # passes the already-transformed routed input as ``hidden_states`` and
    # the original hidden states as ``shared_experts_input``; skip the
    # transform in that case so shared experts still see the original input.
    if shared_experts_input is None:
        hidden_states, shared_experts_input = self.apply_routed_input_transform(
            hidden_states
        )

    # Record before `_maybe_pad_hidden_states` pads activations to match
    # `moe_config.hidden_dim`, e.g. after `align_trtllm_fp4_moe_hidden_dim_for_fi`
    # so routed output can be trimmed before
    # shared+routed add / latent up proj if needed.

    hidden_states, og_hidden_dim_pre_xform, og_hidden_dim_post_xform = (
        self._maybe_pad_hidden_states(
            shared_experts_input,
            hidden_states,
        )
    )

    result = self._forward_entry(
        hidden_states,
        router_logits,
        shared_experts_input,
        input_ids,
        self._encode_layer_name(),
        self.moe_config.hidden_dim_unpadded
        if self._quant_method.has_unpadded_output
        else 0,
    )

    #
    # Note: there are two all-reduce points below. They are mutually
    # exclusive, controlled by _fused_output_is_reduced
    #  - When True: the combine kernel already reduced fused_output,
    #    so we reduce shared_output here to match, then skip the
    #    all-reduce in _maybe_reduce_final_output.
    #  - When False: neither output is reduced yet, so we combine
    #    them first and all-reduce the sum in _maybe_reduce_final_output.

    # Extract outputs from result
    shared_output, fused_output = _unpack(result)
    fused_output = cast(torch.Tensor, fused_output)

    if og_hidden_dim_pre_xform is not None:
        fused_output = fused_output[..., :og_hidden_dim_pre_xform]

    fused_output_is_reduced = self._fused_output_is_reduced

    # Latent routed output has to be reduced before output transform,
    # because the transform may include non-linear normalization.
    fused_output, fused_output_is_reduced = (
        self._maybe_reduce_routed_output_before_transform(
            fused_output,
            fused_output_is_reduced,
        )
    )

    # If routed output is already reduced, reduce shared to match.
    # See note above re: the two all-reduce points.
    shared_output = self._maybe_reduce_shared_expert_output(
        shared_output, fused_output_is_reduced
    )

    shared_output, fused_output = self._maybe_apply_routed_scale_to_output(
        shared_output, fused_output
    )

    # Apply output transform (e.g. latent -> full dim)
    fused_output = self.apply_routed_output_transform(fused_output)

    if shared_output is not None:
        result = shared_output + fused_output
    else:
        result = fused_output

    result = self._maybe_reduce_final_output(
        result, og_hidden_dim_post_xform, fused_output_is_reduced
    )

    return self._maybe_add_zero_expert_output(result)

set_eplb_state(moe_layer_idx, expert_load_view, logical_to_physical_map, logical_replica_count)

Register the EPLB state in this layer.

This is used later in forward pass, where we get the expert mapping and record the load metrics in expert_load_view.

Source code in vllm/model_executor/layers/fused_moe/runner/moe_runner.py
def set_eplb_state(
    self,
    moe_layer_idx: int,
    expert_load_view: torch.Tensor,
    logical_to_physical_map: torch.Tensor,
    logical_replica_count: torch.Tensor,
) -> None:
    """
    Register the EPLB state in this layer.

    This is used later in forward pass, where we get the expert mapping
    and record the load metrics in `expert_load_view`.
    """
    if self.router.eplb_state is not None:
        self.router.eplb_state.set_layer_state(
            moe_layer_idx,
            expert_load_view,
            logical_to_physical_map,
            logical_replica_count,
        )