|
| 1 | +# Internal one-shot helper (not imported at runtime): regenerate fused_moe.py from vLLM. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def main() -> None: |
| 10 | + repo = Path(__file__).resolve().parents[5] |
| 11 | + src = ( |
| 12 | + repo |
| 13 | + / ".venv-vllm/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/fused_moe.py" |
| 14 | + ) |
| 15 | + dst = Path(__file__).resolve().parent / "fused_moe.py" |
| 16 | + lines = src.read_text(encoding="utf-8").splitlines(keepends=True) |
| 17 | + out = "".join(lines[:1896]) |
| 18 | + |
| 19 | + reps: list[tuple[str, str]] = [ |
| 20 | + ("import vllm.envs as envs", "from . import envs"), |
| 21 | + ( |
| 22 | + "import vllm.model_executor.layers.fused_moe.modular_kernel as mk\n", |
| 23 | + "", |
| 24 | + ), |
| 25 | + ("from vllm import _custom_ops as ops\n", "from . import ops_shim as ops\n"), |
| 26 | + ("from vllm.logger import init_logger", "from .logging_utils import init_logger"), |
| 27 | + ( |
| 28 | + "from vllm.model_executor.layers.fused_moe.activation import", |
| 29 | + "from .activation import", |
| 30 | + ), |
| 31 | + ( |
| 32 | + "from vllm.model_executor.layers.fused_moe.config import (\n" |
| 33 | + " FUSED_MOE_UNQUANTIZED_CONFIG,\n" |
| 34 | + " FusedMoEConfig,\n" |
| 35 | + " FusedMoEParallelConfig,\n" |
| 36 | + " FusedMoEQuantConfig,\n" |
| 37 | + " _get_config_dtype_str,\n)", |
| 38 | + "from .config_light import (\n" |
| 39 | + " FUSED_MOE_UNQUANTIZED_CONFIG,\n" |
| 40 | + " FusedMoEQuantConfig,\n" |
| 41 | + " _get_config_dtype_str,\n)", |
| 42 | + ), |
| 43 | + ( |
| 44 | + "from vllm.model_executor.layers.fused_moe.moe_align_block_size import", |
| 45 | + "from .moe_align_block_size import", |
| 46 | + ), |
| 47 | + ( |
| 48 | + "from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import (\n" |
| 49 | + " TopKWeightAndReduceNoOP,\n)\n", |
| 50 | + "", |
| 51 | + ), |
| 52 | + ( |
| 53 | + "from vllm.model_executor.layers.fused_moe.utils import", |
| 54 | + "from .utils_moe import", |
| 55 | + ), |
| 56 | + ( |
| 57 | + "from vllm.model_executor.layers.quantization.utils.mxfp4_utils import dequant_mxfp4\n", |
| 58 | + "", |
| 59 | + ), |
| 60 | + ( |
| 61 | + "from vllm.model_executor.layers.quantization.utils.mxfp6_utils import dequant_mxfp6\n", |
| 62 | + "", |
| 63 | + ), |
| 64 | + ( |
| 65 | + "from vllm.model_executor.layers.quantization.utils.quant_utils import (\n" |
| 66 | + " QuantKey,\n" |
| 67 | + " kFp8Dynamic128Sym,\n" |
| 68 | + " kFp8DynamicTensorSym,\n" |
| 69 | + " kFp8DynamicTokenSym,\n" |
| 70 | + " kFp8Static128BlockSym,\n" |
| 71 | + " kFp8StaticChannelSym,\n" |
| 72 | + " kFp8StaticTensorSym,\n)\n", |
| 73 | + "", |
| 74 | + ), |
| 75 | + ("from vllm.platforms import current_platform", "from .platform_ctx import current_platform"), |
| 76 | + ("from vllm.triton_utils import tl, triton", "import triton\nimport triton.language as tl"), |
| 77 | + ( |
| 78 | + "from vllm.utils.torch_utils import direct_register_custom_op", |
| 79 | + "from .torch_register import direct_register_custom_op, infinilm_fused_lib", |
| 80 | + ), |
| 81 | + ] |
| 82 | + for a, b in reps: |
| 83 | + if a not in out: |
| 84 | + raise SystemExit(f"missing pattern fragment:\n{a[:200]}") |
| 85 | + out = out.replace(a, b) |
| 86 | + |
| 87 | + out = out.replace( |
| 88 | + "activation_out_dim = mk.FusedMoEExpertsModular.adjust_N_for_activation(\n" |
| 89 | + " N, activation_enum\n" |
| 90 | + " )", |
| 91 | + "activation_out_dim = N if not activation_enum.is_gated else N // 2", |
| 92 | + ) |
| 93 | + |
| 94 | + out = out.replace( |
| 95 | + " from vllm.model_executor.layers.fused_moe import get_config\n\n" |
| 96 | + " override_config = get_config()", |
| 97 | + " override_config = None", |
| 98 | + ) |
| 99 | + |
| 100 | + out = out.replace("torch.ops.vllm.", "torch.ops.infinilm.") |
| 101 | + |
| 102 | + out = out.replace( |
| 103 | + "direct_register_custom_op(\n op_name=\"inplace_fused_experts\",", |
| 104 | + "direct_register_custom_op(\n op_name=\"inplace_fused_experts\",\n" |
| 105 | + " target_lib=infinilm_fused_lib,", |
| 106 | + ) |
| 107 | + out = out.replace( |
| 108 | + "direct_register_custom_op(\n op_name=\"outplace_fused_experts\",", |
| 109 | + "direct_register_custom_op(\n op_name=\"outplace_fused_experts\",\n" |
| 110 | + " target_lib=infinilm_fused_lib,", |
| 111 | + ) |
| 112 | + |
| 113 | + needle = "def dispatch_fused_moe_kernel(\n" |
| 114 | + idx = out.find(needle) |
| 115 | + if idx == -1: |
| 116 | + raise SystemExit("dispatch_fused_moe_kernel not found") |
| 117 | + insert_at = idx + len(needle) |
| 118 | + # After opening def line + newline, insert guard at body start |
| 119 | + guard = ( |
| 120 | + " if (use_int8_w8a16 or use_int4_w4a16) and (\n" |
| 121 | + " block_shape is not None and block_shape[1] > 0\n" |
| 122 | + " ):\n" |
| 123 | + " raise NotImplementedError(\n" |
| 124 | + " \"InfiniLM vendor fused_moe: INT4/INT8 WNA16 CUDA path requires vLLM native ops.\"\n" |
| 125 | + " )\n\n" |
| 126 | + ) |
| 127 | + # Find first line after def that's already indented (skip docstring? none) |
| 128 | + # Insert right after `) -> None:` line end - actually after `):` |
| 129 | + line_end = out.find(") -> None:", idx) |
| 130 | + if line_end == -1: |
| 131 | + line_end = out.find("):", idx) |
| 132 | + body_start = out.find("\n", line_end) + 1 |
| 133 | + out = out[:body_start] + guard + out[body_start:] |
| 134 | + |
| 135 | + ocp_start = out.find(" if ocp_mx_scheme is not None:\n # TODO: On platforms") |
| 136 | + if ocp_start == -1: |
| 137 | + raise SystemExit("ocp_mx block not found") |
| 138 | + ocp_end = out.find("\n qhidden_states, a1q_scale = moe_kernel_quantize_input(", ocp_start) |
| 139 | + if ocp_end == -1: |
| 140 | + raise SystemExit("ocp_mx block end not found") |
| 141 | + out = ( |
| 142 | + out[:ocp_start] |
| 143 | + + " if ocp_mx_scheme is not None:\n" |
| 144 | + " raise NotImplementedError(\n" |
| 145 | + " \"InfiniLM vendor fused_moe: ocp_mx_scheme is unsupported.\"\n" |
| 146 | + " )\n\n" |
| 147 | + + out[ocp_end + 1 :] |
| 148 | + ) |
| 149 | + |
| 150 | + hdr = ( |
| 151 | + '# SPDX-License-Identifier: Apache-2.0\n# SPDX-FileCopyrightText: Copyright contributors to the vLLM project\n"""Fused MoE Triton kernels (InfiniLM vendor; see NOTICE)."""\n\n' |
| 152 | + ) |
| 153 | + # Docstring already in file - replace opening docstring line |
| 154 | + out = out.replace( |
| 155 | + '"""Fused MoE Triton kernels."""\n\n', |
| 156 | + '"""Fused MoE Triton kernels (InfiniLM vendor; see NOTICE)."""\n\n', |
| 157 | + 1, |
| 158 | + ) |
| 159 | + |
| 160 | + dst.write_text(out, encoding="utf-8") |
| 161 | + print("wrote", dst) |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + main() |
0 commit comments