Skip to content

vllm.models.deepseek_v41.amd.mhc

The DSV4.1 delayed mHC seams on ROCm.

On gfx950, mhc_pre_delayed_overlap moves the gate projection onto a side stream, overlapped with the next sublayer. On gfx942, mhc_pre_delayed_seam runs AITER's mhc_fused_post_pre_delayed, which applies the previous sublayer's post, collapses the residual with the carried pre-mix and computes the next gates in one call, from MHC_SEAM_MIN_TOKENS tokens on seams that have a post block and no fused norm.

Classes:

  • MHCOverlapStream –

    The global aux stream with the fork and join events of the overlap.

Functions:

MHCOverlapStream

The global aux stream with the fork and join events of the overlap.

Every fork and join waits on its event before the next records it, so all layers share the two events.

Methods:

  • prepare_mega_mhc –

    Allocate AITER Mega-MHC's per-stream scratch on the side stream.

Source code in vllm/models/deepseek_v41/amd/mhc.py
class MHCOverlapStream:
    """The global aux stream with the fork and join events of the overlap.

    Every fork and join waits on its event before the next records it, so all
    layers share the two events.
    """

    def __init__(self) -> None:
        stream = aux_stream()
        assert stream is not None
        self.stream = stream
        self._fork_event = torch.cuda.Event()
        self._join_event = torch.cuda.Event()
        self._mega_mhc_ready = False

    def prepare_mega_mhc(self, hidden_size: int, device: torch.device) -> None:
        """Allocate AITER Mega-MHC's per-stream scratch on the side stream.

        The gates run there only inside FULL graph capture, which cannot
        allocate it; sizing it for `MHC_OVERLAP_MAX_TOKENS` covers every
        batch the overlap captures.
        """
        if self._mega_mhc_ready:
            return
        with torch.cuda.stream(self.stream):
            rocm_aiter_ops.mega_mhc_prepare_stream(
                MHC_OVERLAP_MAX_TOKENS, hidden_size, device
            )
        self._mega_mhc_ready = True

    def fork(self) -> None:
        self._fork_event.record()
        self.stream.wait_event(self._fork_event)

    def join(self) -> None:
        self._join_event.record(self.stream)
        torch.cuda.current_stream().wait_event(self._join_event)

prepare_mega_mhc(hidden_size, device)

Allocate AITER Mega-MHC's per-stream scratch on the side stream.

The gates run there only inside FULL graph capture, which cannot allocate it; sizing it for MHC_OVERLAP_MAX_TOKENS covers every batch the overlap captures.

Source code in vllm/models/deepseek_v41/amd/mhc.py
def prepare_mega_mhc(self, hidden_size: int, device: torch.device) -> None:
    """Allocate AITER Mega-MHC's per-stream scratch on the side stream.

    The gates run there only inside FULL graph capture, which cannot
    allocate it; sizing it for `MHC_OVERLAP_MAX_TOKENS` covers every
    batch the overlap captures.
    """
    if self._mega_mhc_ready:
        return
    with torch.cuda.stream(self.stream):
        rocm_aiter_ops.mega_mhc_prepare_stream(
            MHC_OVERLAP_MAX_TOKENS, hidden_size, device
        )
    self._mega_mhc_ready = True

mhc_pre_delayed_mxfp8(pre_delayed, residual, fn, hc_scale, hc_base, rms_eps, hc_pre_eps, hc_sinkhorn_eps, hc_post_mult_value, sinkhorn_repeat, pre_mix=None, x=None, norm_weight=None, norm_eps=1e-06, sublayer_out=None, post_layer_mix=None, comb_res_mix=None, fn_packed=None)

pre_delayed whose Mega-MHC seam also writes the layer input as MXFP8.

The sublayer's first GEMM then reads that copy (see attach_mxfp8_input) instead of quantizing the bf16 layer input in a separate launch. Seams pre_delayed would not run on Mega-MHC are left to it.

Source code in vllm/models/deepseek_v41/amd/mhc.py
def mhc_pre_delayed_mxfp8(
    pre_delayed: MHCPreDelayedOp,
    residual: torch.Tensor,
    fn: torch.Tensor,
    hc_scale: torch.Tensor,
    hc_base: torch.Tensor,
    rms_eps: float,
    hc_pre_eps: float,
    hc_sinkhorn_eps: float,
    hc_post_mult_value: float,
    sinkhorn_repeat: int,
    pre_mix: torch.Tensor | None = None,
    x: torch.Tensor | None = None,
    norm_weight: torch.Tensor | None = None,
    norm_eps: float = 1e-6,
    sublayer_out: torch.Tensor | None = None,
    post_layer_mix: torch.Tensor | None = None,
    comb_res_mix: torch.Tensor | None = None,
    fn_packed: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
    """`pre_delayed` whose Mega-MHC seam also writes the layer input as MXFP8.

    The sublayer's first GEMM then reads that copy (see `attach_mxfp8_input`)
    instead of quantizing the bf16 layer input in a separate launch. Seams
    `pre_delayed` would not run on Mega-MHC are left to it.
    """
    if (
        not HAS_AITER_MHC_FUSED_POST_PRE_DELAYED_RMS_NORM
        or x is not None
        or norm_weight is None
        or fn_packed is None
        or norm_weight.dtype != torch.bfloat16
        or not _aiter_mhc_supported(residual, norm_weight, supports_norm=True)
    ):
        return pre_delayed(
            residual,
            fn,
            hc_scale,
            hc_base,
            rms_eps,
            hc_pre_eps,
            hc_sinkhorn_eps,
            hc_post_mult_value,
            sinkhorn_repeat,
            pre_mix=pre_mix,
            x=x,
            norm_weight=norm_weight,
            norm_eps=norm_eps,
            sublayer_out=sublayer_out,
            post_layer_mix=post_layer_mix,
            comb_res_mix=comb_res_mix,
            fn_packed=fn_packed,
        )
    residual_out = torch.empty_like(residual) if sublayer_out is not None else None
    post_mix, comb_mix, layer_input, next_pre_mix, values, scales = (
        rocm_aiter_ops.mhc_mega_post_pre_delayed_rms_norm_mxfp8(
            residual,
            fn_packed,
            hc_scale,
            hc_base,
            rms_eps,
            hc_pre_eps,
            hc_sinkhorn_eps,
            hc_post_mult_value,
            sinkhorn_repeat,
            pre_mix,
            sublayer_out,
            post_layer_mix,
            comb_res_mix,
            norm_weight,
            norm_eps,
            residual_out,
        )
    )
    attach_mxfp8_input(
        layer_input,
        QuantizedActivation(
            values, scales, layer_input.dtype, layer_input.shape, kMxfp8Dynamic
        ),
    )
    return (
        residual if residual_out is None else residual_out,
        post_mix,
        comb_mix,
        layer_input,
        next_pre_mix,
    )

mhc_pre_delayed_overlap(pre_delayed, residual, fn, hc_scale, hc_base, rms_eps, hc_pre_eps, hc_sinkhorn_eps, hc_post_mult_value, sinkhorn_repeat, pre_mix=None, x=None, norm_weight=None, norm_eps=1e-06, sublayer_out=None, post_layer_mix=None, comb_res_mix=None, fn_packed=None, *, overlap)

pre_delayed with the gate projection moved onto overlap.stream.

Returns residual, post_mix, comb_mix, layer_input, next_pre_mix. Only the residual and layer input are ready on the caller stream; call overlap.join() before using the gates. Seams without a post block, a carried pre-mix or a fused norm run pre_delayed on the caller stream.

Source code in vllm/models/deepseek_v41/amd/mhc.py
def mhc_pre_delayed_overlap(
    pre_delayed: MHCPreDelayedOp,
    residual: torch.Tensor,
    fn: torch.Tensor,
    hc_scale: torch.Tensor,
    hc_base: torch.Tensor,
    rms_eps: float,
    hc_pre_eps: float,
    hc_sinkhorn_eps: float,
    hc_post_mult_value: float,
    sinkhorn_repeat: int,
    pre_mix: torch.Tensor | None = None,
    x: torch.Tensor | None = None,
    norm_weight: torch.Tensor | None = None,
    norm_eps: float = 1e-6,
    sublayer_out: torch.Tensor | None = None,
    post_layer_mix: torch.Tensor | None = None,
    comb_res_mix: torch.Tensor | None = None,
    fn_packed: torch.Tensor | None = None,
    *,
    overlap: MHCOverlapStream,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
    """`pre_delayed` with the gate projection moved onto `overlap.stream`.

    Returns residual, post_mix, comb_mix, layer_input, next_pre_mix. Only the
    residual and layer input are ready on the caller stream; call
    `overlap.join()` before using the gates. Seams without a post block, a
    carried pre-mix or a fused norm run `pre_delayed` on the caller stream.
    """
    main = torch.cuda.current_stream()
    if (
        x is not None
        or pre_mix is None
        or norm_weight is None
        or sublayer_out is None
        or post_layer_mix is None
        or comb_res_mix is None
    ):
        # Fork anyway: the caller joins after every seam, and a join needs the
        # stream to be part of the capture.
        overlap.fork()
        return pre_delayed(
            residual,
            fn,
            hc_scale,
            hc_base,
            rms_eps,
            hc_pre_eps,
            hc_sinkhorn_eps,
            hc_post_mult_value,
            sinkhorn_repeat,
            pre_mix=pre_mix,
            x=x,
            norm_weight=norm_weight,
            norm_eps=norm_eps,
            sublayer_out=sublayer_out,
            post_layer_mix=post_layer_mix,
            comb_res_mix=comb_res_mix,
            fn_packed=fn_packed,
        )
    residual, layer_input = mhc_post_collapse_rms_norm_triton(
        residual,
        sublayer_out,
        post_layer_mix,
        comb_res_mix,
        pre_mix,
        norm_weight,
        norm_eps,
    )
    stream = overlap.stream
    overlap.fork()
    with torch.cuda.stream(stream):
        _, post_mix, comb_mix, _, next_pre_mix = pre_delayed(
            residual,
            fn,
            hc_scale,
            hc_base,
            rms_eps,
            hc_pre_eps,
            hc_sinkhorn_eps,
            hc_post_mult_value,
            sinkhorn_repeat,
            pre_mix=pre_mix,
            norm_weight=norm_weight,
            norm_eps=norm_eps,
            fn_packed=fn_packed,
        )
    for tensor in (residual, pre_mix):
        tensor.record_stream(stream)
    for tensor in (post_mix, comb_mix, next_pre_mix):
        tensor.record_stream(main)
    return residual, post_mix, comb_mix, layer_input, next_pre_mix

mhc_pre_delayed_seam(pre_delayed, residual, fn, hc_scale, hc_base, rms_eps, hc_pre_eps, hc_sinkhorn_eps, hc_post_mult_value, sinkhorn_repeat, pre_mix=None, x=None, norm_weight=None, norm_eps=1e-06, sublayer_out=None, post_layer_mix=None, comb_res_mix=None, fn_packed=None)

pre_delayed with the seam run as one AITER call where it applies.

Same arguments and returns as MHCPreDelayedOp. Layer 0 (x), seams without a post block (Engram), and seams with a fused norm or a packed fn (Mega-MHC) keep pre_delayed. The caller checks supports_mhc_seam once.

Source code in vllm/models/deepseek_v41/amd/mhc.py
def mhc_pre_delayed_seam(
    pre_delayed: MHCPreDelayedOp,
    residual: torch.Tensor,
    fn: torch.Tensor,
    hc_scale: torch.Tensor,
    hc_base: torch.Tensor,
    rms_eps: float,
    hc_pre_eps: float,
    hc_sinkhorn_eps: float,
    hc_post_mult_value: float,
    sinkhorn_repeat: int,
    pre_mix: torch.Tensor | None = None,
    x: torch.Tensor | None = None,
    norm_weight: torch.Tensor | None = None,
    norm_eps: float = 1e-6,
    sublayer_out: torch.Tensor | None = None,
    post_layer_mix: torch.Tensor | None = None,
    comb_res_mix: torch.Tensor | None = None,
    fn_packed: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
    """`pre_delayed` with the seam run as one AITER call where it applies.

    Same arguments and returns as `MHCPreDelayedOp`. Layer 0 (`x`), seams
    without a post block (Engram), and seams with a fused norm or a packed
    `fn` (Mega-MHC) keep `pre_delayed`. The caller checks `supports_mhc_seam`
    once.
    """
    if (
        sublayer_out is None
        or post_layer_mix is None
        or comb_res_mix is None
        or x is not None
        or norm_weight is not None
        or fn_packed is not None
        or residual.shape[0] < MHC_SEAM_MIN_TOKENS
    ):
        return pre_delayed(
            residual,
            fn,
            hc_scale,
            hc_base,
            rms_eps,
            hc_pre_eps,
            hc_sinkhorn_eps,
            hc_post_mult_value,
            sinkhorn_repeat,
            pre_mix=pre_mix,
            x=x,
            norm_weight=norm_weight,
            norm_eps=norm_eps,
            sublayer_out=sublayer_out,
            post_layer_mix=post_layer_mix,
            comb_res_mix=comb_res_mix,
            fn_packed=fn_packed,
        )
    from aiter.ops.mhc import mhc_fused_post_pre_delayed

    return mhc_fused_post_pre_delayed(
        residual,
        fn,
        hc_scale,
        hc_base,
        rms_eps,
        hc_pre_eps,
        hc_sinkhorn_eps,
        hc_post_mult_value,
        sinkhorn_repeat,
        pre_mix,
        sublayer_out,
        post_layer_mix,
        comb_res_mix,
    )

supports_mhc_seam(hidden_size, hc_mult) cached

True on gfx942 when AITER's probe accepts this shape.

Source code in vllm/models/deepseek_v41/amd/mhc.py
@functools.cache
def supports_mhc_seam(hidden_size: int, hc_mult: int) -> bool:
    """True on gfx942 when AITER's probe accepts this shape."""
    if not HAS_AITER_MHC or get_cdna_version() != 3:
        return False
    from aiter.ops.mhc import mhc_fused_post_pre_delayed_asm_supported

    return bool(mhc_fused_post_pre_delayed_asm_supported(hidden_size, hc_mult))