ADAPT-Chase commited on
Commit
bc940b7
·
verified ·
1 Parent(s): 5aed0ba

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/__init__.cpython-312.pyc +0 -0
  2. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/custom_op.cpython-312.pyc +0 -0
  3. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/parameter.cpython-312.pyc +0 -0
  4. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/pooling_metadata.cpython-312.pyc +0 -0
  5. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/sampling_metadata.cpython-312.pyc +0 -0
  6. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/utils.cpython-312.pyc +0 -0
  7. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/__init__.cpython-312.pyc +0 -0
  8. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/activation.cpython-312.pyc +0 -0
  9. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/layernorm.cpython-312.pyc +0 -0
  10. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/lightning_attn.cpython-312.pyc +0 -0
  11. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/linear.cpython-312.pyc +0 -0
  12. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/logits_processor.cpython-312.pyc +0 -0
  13. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/pooler.cpython-312.pyc +0 -0
  14. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/resampler.cpython-312.pyc +0 -0
  15. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/sampler.cpython-312.pyc +0 -0
  16. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/utils.cpython-312.pyc +0 -0
  17. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/vocab_parallel_embedding.cpython-312.pyc +0 -0
  18. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__init__.py +80 -0
  19. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/__init__.cpython-312.pyc +0 -0
  20. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/batched_deep_gemm_moe.cpython-312.pyc +0 -0
  21. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/batched_triton_or_deep_gemm_moe.cpython-312.pyc +0 -0
  22. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/config.cpython-312.pyc +0 -0
  23. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/cpu_fused_moe.cpython-312.pyc +0 -0
  24. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/cutlass_moe.cpython-312.pyc +0 -0
  25. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/deep_gemm_moe.cpython-312.pyc +0 -0
  26. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/deep_gemm_utils.cpython-312.pyc +0 -0
  27. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/deepep_ht_prepare_finalize.cpython-312.pyc +0 -0
  28. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/deepep_ll_prepare_finalize.cpython-312.pyc +0 -0
  29. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/flashinfer_cutlass_moe.cpython-312.pyc +0 -0
  30. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/flashinfer_cutlass_prepare_finalize.cpython-312.pyc +0 -0
  31. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/fused_batched_moe.cpython-312.pyc +0 -0
  32. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/fused_marlin_moe.cpython-312.pyc +0 -0
  33. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/fused_moe.cpython-312.pyc +0 -0
  34. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/gpt_oss_triton_kernels_moe.cpython-312.pyc +0 -0
  35. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/layer.cpython-312.pyc +0 -0
  36. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/modular_kernel.cpython-312.pyc +0 -0
  37. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/moe_align_block_size.cpython-312.pyc +0 -0
  38. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/moe_pallas.cpython-312.pyc +0 -0
  39. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/moe_permute_unpermute.cpython-312.pyc +0 -0
  40. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/moe_torch_iterative.cpython-312.pyc +0 -0
  41. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/pplx_prepare_finalize.cpython-312.pyc +0 -0
  42. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/prepare_finalize.cpython-312.pyc +0 -0
  43. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/rocm_aiter_fused_moe.cpython-312.pyc +0 -0
  44. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/routing_simulator.cpython-312.pyc +0 -0
  45. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/topk_weight_and_reduce.cpython-312.pyc +0 -0
  46. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/triton_deep_gemm_moe.cpython-312.pyc +0 -0
  47. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/utils.cpython-312.pyc +0 -0
  48. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/batched_deep_gemm_moe.py +306 -0
  49. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/batched_triton_or_deep_gemm_moe.py +164 -0
  50. tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/config.py +490 -0
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (527 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/custom_op.cpython-312.pyc ADDED
Binary file (8.32 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/parameter.cpython-312.pyc ADDED
Binary file (20.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/pooling_metadata.cpython-312.pyc ADDED
Binary file (3.4 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/sampling_metadata.cpython-312.pyc ADDED
Binary file (19.4 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/__pycache__/utils.cpython-312.pyc ADDED
Binary file (4.02 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (196 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/activation.cpython-312.pyc ADDED
Binary file (27.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/layernorm.cpython-312.pyc ADDED
Binary file (11.9 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/lightning_attn.cpython-312.pyc ADDED
Binary file (22.8 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/linear.cpython-312.pyc ADDED
Binary file (65.4 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/logits_processor.cpython-312.pyc ADDED
Binary file (7.31 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/pooler.cpython-312.pyc ADDED
Binary file (36.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/resampler.cpython-312.pyc ADDED
Binary file (11.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/sampler.cpython-312.pyc ADDED
Binary file (42.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/utils.cpython-312.pyc ADDED
Binary file (8.1 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/__pycache__/vocab_parallel_embedding.cpython-312.pyc ADDED
Binary file (23.9 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__init__.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
+
4
+ from contextlib import contextmanager
5
+ from typing import Any, Optional
6
+
7
+ from vllm.model_executor.layers.fused_moe.config import FusedMoEConfig
8
+ from vllm.model_executor.layers.fused_moe.layer import (
9
+ FusedMoE, FusedMoEMethodBase, FusedMoeWeightScaleSupported)
10
+ from vllm.model_executor.layers.fused_moe.modular_kernel import (
11
+ FusedMoEActivationFormat, FusedMoEPermuteExpertsUnpermute,
12
+ FusedMoEPrepareAndFinalize)
13
+ from vllm.triton_utils import HAS_TRITON
14
+
15
+ _config: Optional[dict[str, Any]] = None
16
+
17
+
18
+ @contextmanager
19
+ def override_config(config):
20
+ global _config
21
+ old_config = _config
22
+ _config = config
23
+ yield
24
+ _config = old_config
25
+
26
+
27
+ def get_config() -> Optional[dict[str, Any]]:
28
+ return _config
29
+
30
+
31
+ __all__ = [
32
+ "FusedMoE",
33
+ "FusedMoEConfig",
34
+ "FusedMoEMethodBase",
35
+ "FusedMoeWeightScaleSupported",
36
+ "FusedMoEPermuteExpertsUnpermute",
37
+ "FusedMoEActivationFormat",
38
+ "FusedMoEPrepareAndFinalize",
39
+ "override_config",
40
+ "get_config",
41
+ ]
42
+
43
+ if HAS_TRITON:
44
+ # import to register the custom ops
45
+ import vllm.model_executor.layers.fused_moe.fused_marlin_moe # noqa
46
+ import vllm.model_executor.layers.fused_moe.fused_moe # noqa
47
+ from vllm.model_executor.layers.fused_moe.batched_deep_gemm_moe import (
48
+ BatchedDeepGemmExperts)
49
+ from vllm.model_executor.layers.fused_moe.batched_triton_or_deep_gemm_moe import ( # noqa: E501
50
+ BatchedTritonOrDeepGemmExperts)
51
+ from vllm.model_executor.layers.fused_moe.cutlass_moe import (
52
+ CutlassBatchedExpertsFp8, CutlassExpertsFp8, cutlass_moe_fp4,
53
+ cutlass_moe_fp8)
54
+ from vllm.model_executor.layers.fused_moe.deep_gemm_moe import (
55
+ DeepGemmExperts)
56
+ from vllm.model_executor.layers.fused_moe.fused_batched_moe import (
57
+ BatchedTritonExperts)
58
+ from vllm.model_executor.layers.fused_moe.fused_moe import (
59
+ TritonExperts, fused_experts, fused_moe, fused_topk,
60
+ get_config_file_name, grouped_topk)
61
+ from vllm.model_executor.layers.fused_moe.triton_deep_gemm_moe import (
62
+ TritonOrDeepGemmExperts)
63
+
64
+ __all__ += [
65
+ "fused_moe",
66
+ "fused_topk",
67
+ "fused_experts",
68
+ "get_config_file_name",
69
+ "grouped_topk",
70
+ "cutlass_moe_fp8",
71
+ "cutlass_moe_fp4",
72
+ "CutlassExpertsFp8",
73
+ "CutlassBatchedExpertsFp8",
74
+ "TritonExperts",
75
+ "BatchedTritonExperts",
76
+ "DeepGemmExperts",
77
+ "BatchedDeepGemmExperts",
78
+ "TritonOrDeepGemmExperts",
79
+ "BatchedTritonOrDeepGemmExperts",
80
+ ]
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (2.49 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/batched_deep_gemm_moe.cpython-312.pyc ADDED
Binary file (12.3 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/batched_triton_or_deep_gemm_moe.cpython-312.pyc ADDED
Binary file (7.38 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/config.cpython-312.pyc ADDED
Binary file (18.9 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/cpu_fused_moe.cpython-312.pyc ADDED
Binary file (8.26 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/cutlass_moe.cpython-312.pyc ADDED
Binary file (40.8 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/deep_gemm_moe.cpython-312.pyc ADDED
Binary file (15.9 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/deep_gemm_utils.cpython-312.pyc ADDED
Binary file (15.2 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/deepep_ht_prepare_finalize.cpython-312.pyc ADDED
Binary file (8.61 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/deepep_ll_prepare_finalize.cpython-312.pyc ADDED
Binary file (8.86 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/flashinfer_cutlass_moe.cpython-312.pyc ADDED
Binary file (8.59 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/flashinfer_cutlass_prepare_finalize.cpython-312.pyc ADDED
Binary file (5.4 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/fused_batched_moe.cpython-312.pyc ADDED
Binary file (37.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/fused_marlin_moe.cpython-312.pyc ADDED
Binary file (10.3 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/fused_moe.cpython-312.pyc ADDED
Binary file (70.2 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/gpt_oss_triton_kernels_moe.cpython-312.pyc ADDED
Binary file (9.56 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/layer.cpython-312.pyc ADDED
Binary file (65.7 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/modular_kernel.cpython-312.pyc ADDED
Binary file (31.7 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/moe_align_block_size.cpython-312.pyc ADDED
Binary file (4.3 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/moe_pallas.cpython-312.pyc ADDED
Binary file (4.95 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/moe_permute_unpermute.cpython-312.pyc ADDED
Binary file (9 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/moe_torch_iterative.cpython-312.pyc ADDED
Binary file (2.76 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/pplx_prepare_finalize.cpython-312.pyc ADDED
Binary file (9.9 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/prepare_finalize.cpython-312.pyc ADDED
Binary file (4.03 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/rocm_aiter_fused_moe.cpython-312.pyc ADDED
Binary file (14.9 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/routing_simulator.cpython-312.pyc ADDED
Binary file (12.1 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/topk_weight_and_reduce.cpython-312.pyc ADDED
Binary file (8.04 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/triton_deep_gemm_moe.cpython-312.pyc ADDED
Binary file (7.06 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/__pycache__/utils.cpython-312.pyc ADDED
Binary file (11.4 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/batched_deep_gemm_moe.py ADDED
@@ -0,0 +1,306 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
+ from typing import Optional
4
+
5
+ import torch
6
+
7
+ import vllm.model_executor.layers.fused_moe.modular_kernel as mk
8
+ from vllm.logger import init_logger
9
+ from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig
10
+ from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import (
11
+ TopKWeightAndReduceDelegate)
12
+ from vllm.model_executor.layers.fused_moe.utils import _resize_cache
13
+ from vllm.triton_utils import tl, triton
14
+ from vllm.utils.deep_gemm import (fp8_m_grouped_gemm_nt_masked,
15
+ is_blackwell_deep_gemm_e8m0_used)
16
+
17
+ logger = init_logger(__name__)
18
+
19
+
20
+ @triton.jit
21
+ def _silu_mul_fp8_quant_deep_gemm(
22
+ # Pointers ------------------------------------------------------------
23
+ input_ptr, # 16-bit activations (E, T, 2*H)
24
+ y_q_ptr, # fp8 quantized activations (E, T, H)
25
+ y_s_ptr, # 16-bit scales (E, T, G)
26
+ counts_ptr, # int32 num tokens per expert (E)
27
+
28
+ # Sizes ---------------------------------------------------------------
29
+ H: tl.constexpr, # hidden dimension (per output)
30
+ GROUP_SIZE: tl.constexpr, # elements per group (usually 128)
31
+
32
+ # Strides for input (elements) ---------------------------------------
33
+ stride_i_e,
34
+ stride_i_t,
35
+ stride_i_h,
36
+
37
+ # Strides for y_q (elements) -----------------------------------------
38
+ stride_yq_e,
39
+ stride_yq_t,
40
+ stride_yq_h,
41
+
42
+ # Strides for y_s (elements) -----------------------------------------
43
+ stride_ys_e,
44
+ stride_ys_t,
45
+ stride_ys_g,
46
+
47
+ # Stride for counts (elements)
48
+ stride_counts_e,
49
+
50
+ # Numeric params ------------------------------------------------------
51
+ eps: tl.constexpr,
52
+ fp8_min: tl.constexpr,
53
+ fp8_max: tl.constexpr,
54
+ use_ue8m0: tl.constexpr,
55
+
56
+ # Meta ---------------------------------------------------------------
57
+ BLOCK: tl.constexpr,
58
+ NUM_STAGES: tl.constexpr,
59
+ ):
60
+ G = H // GROUP_SIZE
61
+
62
+ # map program id -> (e, g)
63
+ pid = tl.program_id(0)
64
+ e = pid // G
65
+ g = pid % G
66
+
67
+ e = e.to(tl.int64)
68
+ g = g.to(tl.int64)
69
+
70
+ # number of valid tokens for this expert
71
+ n_tokens = tl.load(counts_ptr + e * stride_counts_e).to(tl.int64)
72
+
73
+ cols = tl.arange(0, BLOCK)
74
+ cols = cols.to(tl.int64)
75
+ mask_h = cols < BLOCK
76
+
77
+ for t in tl.range(0, n_tokens, num_stages=NUM_STAGES):
78
+ base_i_offset = (e * stride_i_e + t * stride_i_t +
79
+ g * GROUP_SIZE * stride_i_h)
80
+ base_yq_offset = (e * stride_yq_e + t * stride_yq_t +
81
+ g * GROUP_SIZE * stride_yq_h)
82
+ base_ys_offset = e * stride_ys_e + t * stride_ys_t + g * stride_ys_g
83
+
84
+ mask = mask_h
85
+ x = tl.load(input_ptr + base_i_offset + cols * stride_i_h,
86
+ mask=mask,
87
+ other=0.0).to(tl.float32)
88
+ y2 = tl.load(input_ptr + base_i_offset + H * stride_i_h +
89
+ cols * stride_i_h,
90
+ mask=mask,
91
+ other=0.0).to(tl.float32)
92
+
93
+ x = x * (1.0 / (1.0 + tl.exp(-x)))
94
+ y = x * y2
95
+
96
+ _absmax = tl.maximum(tl.max(tl.abs(y)), eps)
97
+ scale_raw = _absmax / fp8_max
98
+ y_s = tl.math.exp2(tl.ceil(
99
+ tl.log2(scale_raw))) if use_ue8m0 else scale_raw
100
+ y_q = tl.clamp(y / y_s, fp8_min, fp8_max).to(y_q_ptr.dtype.element_ty)
101
+
102
+ tl.store(y_q_ptr + base_yq_offset + cols * stride_yq_h, y_q, mask=mask)
103
+ tl.store(y_s_ptr + base_ys_offset, y_s)
104
+
105
+
106
+ def silu_mul_fp8_quant_deep_gemm(
107
+ y: torch.Tensor, # (E, T, 2*H) float32
108
+ tokens_per_expert: torch.Tensor, # (E,) number of valid tokens per expert
109
+ group_size: int = 128,
110
+ eps: float = 1e-10,
111
+ ):
112
+ """Quantize silu(y[..., :H]) * y[..., H:] to FP8 with group per-token scales
113
+
114
+ y has shape (E, T, 2*H). The first half of the last dimension is
115
+ silu-activated, multiplied by the second half, then quantized into FP8.
116
+
117
+ Returns `(y_q, y_s)` where
118
+ * `y_q` is the FP8 tensor of shape `(E, T, H)`, same layout as `y[..., :H]`.
119
+ * `y_s` has shape `(E, T, H // group_size)` and strides `(T*G, 1, T)`
120
+ """
121
+ assert y.ndim == 3, "y must be (E, T, 2*H)"
122
+ E, T, H2 = y.shape
123
+ assert H2 % 2 == 0, "last dim of y must be even (2*H)"
124
+ H = H2 // 2
125
+ G = H // group_size
126
+ assert H % group_size == 0, "H must be divisible by group_size"
127
+ assert tokens_per_expert.ndim == 1 and tokens_per_expert.shape[0] == E, \
128
+ "tokens_per_expert must be shape (E,)"
129
+ tokens_per_expert = tokens_per_expert.to(device=y.device,
130
+ dtype=torch.int32)
131
+
132
+ # allocate outputs
133
+ fp8_dtype = torch.float8_e4m3fn
134
+ y_q = torch.empty((E, T, H), dtype=fp8_dtype, device=y.device)
135
+
136
+ # strides (elements)
137
+ stride_i_e, stride_i_t, stride_i_h = y.stride()
138
+ stride_yq_e, stride_yq_t, stride_yq_h = y_q.stride()
139
+
140
+ # desired scale strides (elements): (T*G, 1, T)
141
+ stride_ys_e = T * G
142
+ stride_ys_t = 1
143
+ stride_ys_g = T
144
+ y_s = torch.empty_strided((E, T, G),
145
+ (stride_ys_e, stride_ys_t, stride_ys_g),
146
+ dtype=torch.float32,
147
+ device=y.device)
148
+
149
+ stride_cnt_e = tokens_per_expert.stride()[0]
150
+
151
+ # static grid over experts and H-groups.
152
+ # A loop inside the kernel handles the token dim
153
+ grid = (E * G, )
154
+
155
+ f_info = torch.finfo(fp8_dtype)
156
+ fp8_max = f_info.max
157
+ fp8_min = f_info.min
158
+
159
+ _silu_mul_fp8_quant_deep_gemm[grid](
160
+ y,
161
+ y_q,
162
+ y_s,
163
+ tokens_per_expert,
164
+ H,
165
+ group_size,
166
+ stride_i_e,
167
+ stride_i_t,
168
+ stride_i_h,
169
+ stride_yq_e,
170
+ stride_yq_t,
171
+ stride_yq_h,
172
+ stride_ys_e,
173
+ stride_ys_t,
174
+ stride_ys_g,
175
+ stride_cnt_e,
176
+ eps,
177
+ fp8_min,
178
+ fp8_max,
179
+ is_blackwell_deep_gemm_e8m0_used(),
180
+ BLOCK=group_size,
181
+ NUM_STAGES=8,
182
+ num_warps=1,
183
+ )
184
+
185
+ return y_q, y_s
186
+
187
+
188
+ class BatchedDeepGemmExperts(mk.FusedMoEPermuteExpertsUnpermute):
189
+
190
+ # The Deep Gemm kernels only support block size of 128
191
+ DEEPGEMM_BLOCK_SHAPE: list[int] = [128, 128]
192
+
193
+ def __init__(self,
194
+ max_num_tokens: int,
195
+ num_dispatchers: int,
196
+ block_shape: list[int],
197
+ per_act_token_quant=False):
198
+ """
199
+ max_num_tokens: Maximum number of tokens from a DP Rank
200
+ num_dispatchers: The number of DP dispatchers.
201
+ block_shape: Block quantization block shape.
202
+ per_act_token_quant: Per activation token quantization flag.
203
+ """
204
+ super().__init__(
205
+ FusedMoEQuantConfig(
206
+ quant_dtype=torch.float8_e4m3fn,
207
+ per_act_token_quant=per_act_token_quant,
208
+ block_shape=block_shape,
209
+ ))
210
+ assert self.block_shape == self.DEEPGEMM_BLOCK_SHAPE
211
+ self.max_num_tokens = max_num_tokens
212
+ self.num_dispatchers = num_dispatchers
213
+
214
+ @property
215
+ def activation_formats(
216
+ self
217
+ ) -> tuple[mk.FusedMoEActivationFormat, mk.FusedMoEActivationFormat]:
218
+ return (mk.FusedMoEActivationFormat.BatchedExperts,
219
+ mk.FusedMoEActivationFormat.BatchedExperts)
220
+
221
+ def supports_chunking(self) -> bool:
222
+ return False
223
+
224
+ def supports_expert_map(self) -> bool:
225
+ return False
226
+
227
+ def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce:
228
+ # Let PrepareAndFinalize::finalize() decide the impl.
229
+ return TopKWeightAndReduceDelegate()
230
+
231
+ def workspace_shapes(
232
+ self,
233
+ a: torch.Tensor,
234
+ aq: torch.Tensor,
235
+ M: int,
236
+ N: int,
237
+ K: int,
238
+ topk: int,
239
+ global_num_experts: int,
240
+ local_num_experts: int,
241
+ expert_tokens_metadata: Optional[mk.ExpertTokensMetadata],
242
+ ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...], torch.dtype]:
243
+ assert a.dim() == 2
244
+ # FIXME (varun): We should be able to dispatch only from the leader
245
+ # DP ranks in the case of TP > 1. At the moment, all the Ranks
246
+ # end up sending their tokens. This needs to be fixed.
247
+ num_dispatchers = self.num_dispatchers
248
+ num_experts = local_num_experts
249
+ max_num_tokens = a.size(
250
+ 0) if self.max_num_tokens is None else self.max_num_tokens
251
+ workspace13 = (num_experts, max_num_tokens * num_dispatchers,
252
+ max(K, N))
253
+ workspace2 = (num_experts, max_num_tokens * num_dispatchers, (N // 2))
254
+ output = (num_experts, max_num_tokens * num_dispatchers, K)
255
+ return (workspace13, workspace2, output, a.dtype)
256
+
257
+ def apply(
258
+ self,
259
+ output: torch.Tensor,
260
+ hidden_states: torch.Tensor,
261
+ w1: torch.Tensor,
262
+ w2: torch.Tensor,
263
+ topk_weights: torch.Tensor,
264
+ topk_ids: torch.Tensor,
265
+ activation: str,
266
+ global_num_experts: int,
267
+ expert_map: Optional[torch.Tensor],
268
+ w1_scale: Optional[torch.Tensor],
269
+ w2_scale: Optional[torch.Tensor],
270
+ w1_zp: Optional[torch.Tensor],
271
+ w2_zp: Optional[torch.Tensor],
272
+ a1q_scale: Optional[torch.Tensor],
273
+ a2_scale: Optional[torch.Tensor],
274
+ workspace13: torch.Tensor,
275
+ workspace2: torch.Tensor,
276
+ expert_tokens_meta: Optional[mk.ExpertTokensMetadata],
277
+ apply_router_weight_on_input: bool,
278
+ ):
279
+ assert expert_tokens_meta is not None
280
+ expert_num_tokens = expert_tokens_meta.expert_num_tokens
281
+
282
+ assert hidden_states.ndim == 3
283
+ assert self.block_shape is not None
284
+
285
+ a1q = hidden_states
286
+ _, N, K = w1.size()
287
+
288
+ assert w2.size(1) == K
289
+
290
+ E, max_num_tokens, N, K, top_k_num = mk._moe_problem_size(
291
+ hidden_states, w1, w2, topk_ids)
292
+
293
+ workspace1 = _resize_cache(workspace13, (E, max_num_tokens, N))
294
+
295
+ # (from deepgemm docs) : A value hint (which is a value on CPU)
296
+ # for the M expectation of each batch, correctly setting this value
297
+ # may lead to better performance.
298
+ expected_m = max_num_tokens
299
+ fp8_m_grouped_gemm_nt_masked((a1q, a1q_scale), (w1, w1_scale),
300
+ workspace1, expert_num_tokens, expected_m)
301
+
302
+ a2q, a2q_scale = silu_mul_fp8_quant_deep_gemm(workspace1,
303
+ expert_num_tokens)
304
+
305
+ fp8_m_grouped_gemm_nt_masked((a2q, a2q_scale), (w2, w2_scale), output,
306
+ expert_num_tokens, expected_m)
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/batched_triton_or_deep_gemm_moe.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
+ from typing import Optional
4
+
5
+ import torch
6
+
7
+ import vllm.model_executor.layers.fused_moe.modular_kernel as mk
8
+ from vllm.model_executor.layers.fused_moe.batched_deep_gemm_moe import (
9
+ BatchedDeepGemmExperts)
10
+ from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig
11
+ from vllm.model_executor.layers.fused_moe.fused_batched_moe import (
12
+ BatchedTritonExperts)
13
+
14
+
15
+ class BatchedTritonOrDeepGemmExperts(mk.FusedMoEPermuteExpertsUnpermute):
16
+
17
+ def __init__(self,
18
+ max_num_tokens: int,
19
+ num_dispatchers: int,
20
+ use_fp8_w8a8: bool = False,
21
+ use_int8_w8a8: bool = False,
22
+ use_int8_w8a16: bool = False,
23
+ use_int4_w4a16: bool = False,
24
+ block_shape: Optional[list[int]] = None,
25
+ per_act_token_quant: bool = False,
26
+ allow_deep_gemm: bool = False):
27
+ assert not use_int8_w8a8, "NYI"
28
+ assert not use_int8_w8a16, "NYI"
29
+ assert not use_int4_w4a16, "NYI"
30
+
31
+ super().__init__(
32
+ FusedMoEQuantConfig.make(
33
+ use_fp8_w8a8=use_fp8_w8a8,
34
+ use_int8_w8a8=use_int8_w8a8,
35
+ use_int8_w8a16=use_int8_w8a16,
36
+ use_int4_w4a16=use_int4_w4a16,
37
+ block_shape=block_shape,
38
+ per_act_token_quant=per_act_token_quant,
39
+ ))
40
+
41
+ self.batched_triton_experts = BatchedTritonExperts(
42
+ max_num_tokens=max_num_tokens,
43
+ num_dispatchers=num_dispatchers,
44
+ use_fp8_w8a8=use_fp8_w8a8,
45
+ use_int8_w8a8=use_int8_w8a8,
46
+ use_int8_w8a16=use_int8_w8a16,
47
+ use_int4_w4a16=use_int4_w4a16,
48
+ per_act_token_quant=self.per_act_token_quant,
49
+ block_shape=self.block_shape,
50
+ )
51
+
52
+ self.allow_deep_gemm = (allow_deep_gemm and use_fp8_w8a8
53
+ and self.block_shape
54
+ == BatchedDeepGemmExperts.DEEPGEMM_BLOCK_SHAPE)
55
+
56
+ self.batched_deep_gemm_experts = BatchedDeepGemmExperts(
57
+ max_num_tokens=max_num_tokens,
58
+ num_dispatchers=num_dispatchers,
59
+ block_shape=self.block_shape, # type: ignore[arg-type]
60
+ ) if self.allow_deep_gemm else None
61
+
62
+ assert (self.batched_deep_gemm_experts is not None
63
+ or self.batched_triton_experts is not None)
64
+
65
+ @property
66
+ def activation_formats(
67
+ self
68
+ ) -> tuple[mk.FusedMoEActivationFormat, mk.FusedMoEActivationFormat]:
69
+ if self.batched_triton_experts is not None:
70
+ assert (self.batched_deep_gemm_experts is None
71
+ or self.batched_deep_gemm_experts.activation_formats
72
+ == self.batched_triton_experts.activation_formats)
73
+ return self.batched_triton_experts.activation_formats
74
+ else:
75
+ assert self.batched_deep_gemm_experts is not None
76
+ return self.batched_deep_gemm_experts.activation_formats
77
+
78
+ def supports_chunking(self) -> bool:
79
+ bdge = self.batched_deep_gemm_experts
80
+ bte = self.batched_triton_experts
81
+ return ((bdge is None or bdge.supports_chunking())
82
+ and (bte is None or bte.supports_chunking()))
83
+
84
+ def supports_expert_map(self) -> bool:
85
+ bdge = self.batched_deep_gemm_experts
86
+ bte = self.batched_triton_experts
87
+ return ((bdge is None or bdge.supports_expert_map())
88
+ and (bte is None or bte.supports_expert_map()))
89
+
90
+ def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce:
91
+ bdge = self.batched_deep_gemm_experts
92
+ bte = self.batched_triton_experts
93
+ bdge_war = bdge.finalize_weight_and_reduce_impl() if bdge else None
94
+ bte_war = bte.finalize_weight_and_reduce_impl() if bte else None
95
+ is_bdge_war = bdge_war is not None
96
+ is_bte_war = bte_war is not None
97
+
98
+ if is_bdge_war and is_bte_war:
99
+ assert bdge_war == bte_war, (
100
+ "Both implementations should agree on WeightAndReduce impls. "
101
+ f"Got bdge_war: {bdge_war}, and bte_war: {bte_war}")
102
+
103
+ if bdge_war is not None:
104
+ return bdge_war
105
+
106
+ assert bte_war is not None
107
+ return bte_war
108
+
109
+ def workspace_shapes(
110
+ self,
111
+ a: torch.Tensor,
112
+ aq: torch.Tensor,
113
+ M: int,
114
+ N: int,
115
+ K: int,
116
+ topk: int,
117
+ global_num_experts: int,
118
+ local_num_experts: int,
119
+ expert_tokens_metadata: Optional[mk.ExpertTokensMetadata],
120
+ ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...], torch.dtype]:
121
+ # Note: the deep gemm workspaces are strictly larger than the triton
122
+ # workspaces so we can be pessimistic here and allocate for DeepGemm
123
+ # even if we fall back to triton later, e.g. if expert maps are set.
124
+ if self.allow_deep_gemm:
125
+ assert self.batched_deep_gemm_experts is not None
126
+ return self.batched_deep_gemm_experts.workspace_shapes(
127
+ a, aq, M, N, K, topk, global_num_experts, local_num_experts,
128
+ expert_tokens_metadata)
129
+ else:
130
+ assert self.batched_triton_experts is not None
131
+ return self.batched_triton_experts.workspace_shapes(
132
+ a, aq, M, N, K, topk, global_num_experts, local_num_experts,
133
+ expert_tokens_metadata)
134
+
135
+ def apply(
136
+ self,
137
+ output: torch.Tensor,
138
+ hidden_states: torch.Tensor,
139
+ w1: torch.Tensor,
140
+ w2: torch.Tensor,
141
+ topk_weights: torch.Tensor,
142
+ topk_ids: torch.Tensor,
143
+ activation: str,
144
+ global_num_experts: int,
145
+ expert_map: Optional[torch.Tensor],
146
+ w1_scale: Optional[torch.Tensor],
147
+ w2_scale: Optional[torch.Tensor],
148
+ w1_zp: Optional[torch.Tensor],
149
+ w2_zp: Optional[torch.Tensor],
150
+ a1q_scale: Optional[torch.Tensor],
151
+ a2_scale: Optional[torch.Tensor],
152
+ workspace13: torch.Tensor,
153
+ workspace2: torch.Tensor,
154
+ expert_tokens_meta: Optional[mk.ExpertTokensMetadata],
155
+ apply_router_weight_on_input: bool,
156
+ ):
157
+ experts = (self.batched_deep_gemm_experts
158
+ if self.allow_deep_gemm else self.batched_triton_experts)
159
+ assert experts is not None
160
+ experts.apply(output, hidden_states, w1, w2, topk_weights, topk_ids,
161
+ activation, global_num_experts, expert_map, w1_scale,
162
+ w2_scale, w1_zp, w2_zp, a1q_scale, a2_scale, workspace13,
163
+ workspace2, expert_tokens_meta,
164
+ apply_router_weight_on_input)
tool_server/.venv/lib/python3.12/site-packages/vllm/model_executor/layers/fused_moe/config.py ADDED
@@ -0,0 +1,490 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
+ from dataclasses import dataclass
4
+ from typing import Optional, Union
5
+
6
+ import torch
7
+ from compressed_tensors.quantization import (QuantizationArgs,
8
+ QuantizationStrategy,
9
+ QuantizationType)
10
+
11
+ import vllm.envs as envs
12
+ from vllm.config import ParallelConfig
13
+ from vllm.distributed import get_dp_group, get_tensor_model_parallel_rank
14
+ from vllm.logger import init_logger
15
+ from vllm.model_executor.layers.quantization.base_config import (
16
+ QuantizationConfig)
17
+ from vllm.utils import cdiv
18
+ from vllm.utils.flashinfer import has_flashinfer_cutlass_fused_moe
19
+
20
+ logger = init_logger(__name__)
21
+
22
+
23
+ def _get_quant_config_quantization_args(
24
+ quant_config: Optional[QuantizationConfig],
25
+ prop_name: str,
26
+ ) -> Optional[QuantizationArgs]:
27
+ if (quant_config is not None and hasattr(quant_config, 'target_scheme_map')
28
+ and "Linear" in quant_config.target_scheme_map and
29
+ "input_activations" in quant_config.target_scheme_map["Linear"]):
30
+ return quant_config.target_scheme_map["Linear"].get(prop_name)
31
+ else:
32
+ return None
33
+
34
+
35
+ def get_quant_config_input_quant(
36
+ quant_config: Optional[QuantizationConfig]
37
+ ) -> Optional[QuantizationArgs]:
38
+ return _get_quant_config_quantization_args(quant_config,
39
+ "input_activations")
40
+
41
+
42
+ def get_quant_config_weight_quant(
43
+ quant_config: Optional[QuantizationConfig]
44
+ ) -> Optional[QuantizationArgs]:
45
+ return _get_quant_config_quantization_args(quant_config, "weights")
46
+
47
+
48
+ def get_config_quant_dtype(
49
+ use_fp8_w8a8: bool,
50
+ use_int8_w8a8: bool,
51
+ use_int8_w8a16: bool,
52
+ use_int4_w4a16: bool,
53
+ use_mxfp4_w4a4: bool,
54
+ ) -> Union[None, torch.dtype, str]:
55
+ if use_fp8_w8a8:
56
+ return torch.float8_e4m3fn
57
+ elif use_int8_w8a8:
58
+ return torch.int8
59
+ elif use_mxfp4_w4a4:
60
+ return "mxfp4"
61
+ return None
62
+
63
+
64
+ @dataclass
65
+ class FusedMoEQuantConfig:
66
+ # The post quantization activation type.
67
+ # TODO (bnell): use scalar_type instead of Union.
68
+ quant_dtype: Union[torch.dtype, str, None] = None
69
+ per_act_token_quant: bool = False
70
+ per_out_ch_quant: bool = False
71
+ block_shape: Optional[list[int]] = None
72
+
73
+ # TODO: add col major flag?
74
+ # add detailed quant info for input, intermediates, weights, etc?
75
+
76
+ def __post_init__(self):
77
+ assert (not self.per_act_token_quant
78
+ or self.block_shape is None), "illegal quantization"
79
+
80
+ @property
81
+ def is_quantized(self) -> bool:
82
+ return self.quant_dtype is not None
83
+
84
+ @property
85
+ def is_per_act_token(self) -> bool:
86
+ return self.per_act_token_quant
87
+
88
+ @property
89
+ def is_block_quantized(self) -> bool:
90
+ return self.block_shape is not None
91
+
92
+ @property
93
+ def is_per_tensor(self) -> bool:
94
+ return not self.per_act_token_quant and self.block_shape is None
95
+
96
+ def scale_shape(
97
+ self,
98
+ max_tokens: int,
99
+ hidden_dim: int,
100
+ ) -> Optional[tuple[int, int]]:
101
+ if self.is_quantized:
102
+ if self.is_block_quantized:
103
+ assert self.block_shape is not None
104
+ _, block_k = self.block_shape
105
+ k_tiles = cdiv(hidden_dim, block_k)
106
+ return (max_tokens, k_tiles)
107
+ elif self.is_per_act_token:
108
+ return (max_tokens, 1)
109
+ else:
110
+ return (1, 1)
111
+ else:
112
+ return None
113
+
114
+ def batched_scale_shape(
115
+ self,
116
+ num_experts: int,
117
+ max_tokens: int,
118
+ hidden_dim: int,
119
+ ) -> Optional[tuple[int, int, int]]:
120
+ if self.is_quantized:
121
+ scale_shape = self.scale_shape(max_tokens, hidden_dim)
122
+ assert scale_shape is not None
123
+ return (num_experts, *scale_shape)
124
+ else:
125
+ return None
126
+
127
+ @staticmethod
128
+ def make(
129
+ use_fp8_w8a8: bool = False,
130
+ use_int8_w8a8: bool = False,
131
+ use_int8_w8a16: bool = False,
132
+ use_int4_w4a16: bool = False,
133
+ use_mxfp4_w4a4: bool = False,
134
+ per_act_token_quant: bool = False,
135
+ per_out_ch_quant: bool = False,
136
+ block_shape: Optional[list[int]] = None,
137
+ ) -> "FusedMoEQuantConfig":
138
+ assert sum([
139
+ int(flag) for flag in [
140
+ use_fp8_w8a8,
141
+ use_int8_w8a8,
142
+ use_int8_w8a16,
143
+ use_int4_w4a16,
144
+ use_mxfp4_w4a4,
145
+ ]
146
+ ]) <= 1, "Quantization flags are mutually exclusive."
147
+
148
+ quant_dtype = get_config_quant_dtype(
149
+ use_fp8_w8a8=use_fp8_w8a8,
150
+ use_int8_w8a8=use_int8_w8a8,
151
+ use_int8_w8a16=use_int8_w8a16,
152
+ use_int4_w4a16=use_int4_w4a16,
153
+ use_mxfp4_w4a4=use_mxfp4_w4a4,
154
+ )
155
+ return FusedMoEQuantConfig(
156
+ quant_dtype,
157
+ per_act_token_quant,
158
+ per_out_ch_quant,
159
+ block_shape,
160
+ )
161
+
162
+
163
+ @dataclass
164
+ class FusedMoEParallelConfig:
165
+ tp_size: int
166
+ dp_size: int
167
+ ep_size: int
168
+ tp_rank: int
169
+ dp_rank: int
170
+ ep_rank: int
171
+
172
+ use_ep: bool # whether to use EP or not
173
+
174
+ @property
175
+ def use_all2all_kernels(self):
176
+ return self.dp_size > 1 and self.use_ep
177
+
178
+ @property
179
+ def use_pplx_kernels(self):
180
+ return (self.use_all2all_kernels
181
+ and envs.VLLM_ALL2ALL_BACKEND == "pplx")
182
+
183
+ @property
184
+ def use_deepep_ht_kernels(self):
185
+ return (self.use_all2all_kernels
186
+ and envs.VLLM_ALL2ALL_BACKEND == "deepep_high_throughput")
187
+
188
+ @property
189
+ def use_deepep_ll_kernels(self):
190
+ return (self.use_all2all_kernels
191
+ and envs.VLLM_ALL2ALL_BACKEND == "deepep_low_latency")
192
+
193
+ @property
194
+ def use_flashinfer_cutlass_kernels(self):
195
+ return (envs.VLLM_USE_FLASHINFER_MOE_FP4
196
+ and has_flashinfer_cutlass_fused_moe()
197
+ and envs.VLLM_FLASHINFER_MOE_BACKEND == "throughput")
198
+
199
+ @staticmethod
200
+ def make(tp_size_: int, dp_size_: int,
201
+ vllm_parallel_config: ParallelConfig) -> "FusedMoEParallelConfig":
202
+ """
203
+ Determine MoE parallel configuration. Based on the input `tp_size_`,
204
+ `dp_size_` and vllm's parallel config, determine what
205
+ level's of parallelism to use in the fused moe layer.
206
+
207
+ Args:
208
+ tp_size_ (int): `tp_size` passed into the FusedMoE constructor.
209
+ dp_size_ (int): `dp_size` passed into the FusedMoE constructor.
210
+ vllm_parallel_config (ParallelConfig): vLLM's parallel config
211
+ object which contains the `enable_expert_parallel` flag.
212
+
213
+ Examples:
214
+ When there is no parallelism requested,
215
+ i.e. `tp_size_` = `dp_size_` = 1, we simply return the sizes
216
+ unaltered and the ranks set to 0.
217
+
218
+ Expert Parallelism is considered only when either `dp_size_` or
219
+ `tp_size_` is non trivial.
220
+
221
+ When TP = 2, DP = 1 and EP = False, the configuration on different
222
+ devices:
223
+
224
+ - device 0 : TP = {2, 0} DP = {1, 0} EP = {1, 0} //
225
+ legend : {size, rank}
226
+ - device 1 : TP = {2, 1} DP = {1, 0} EP = {1, 0}
227
+ - Comment : Tensors are sharded across 2 devices.
228
+
229
+ When TP = 1, DP = 2 and EP = False, the configuration on different
230
+ devices:
231
+
232
+ - device 0 : TP = {2, 0} DP = {2, 0} EP = {1, 0}
233
+ - device 1 : TP = {2, 1} DP = {2, 1} EP = {1, 0}
234
+ - Comment: There are 2 engine instances and the tensors are sharded
235
+ across 2 decvices.
236
+
237
+ When TP = 2, DP = 2 and EP = False, the configuration on different
238
+ devices:
239
+
240
+ - device 0: TP = {4, 0} DP = {2, 0} EP = {1, 0}
241
+ - device 1: TP = {4, 1} DP = {2, 0} EP = {1, 0}
242
+ - device 2: TP = {4, 2} DP = {2, 1} EP = {1, 0}
243
+ - device 3: TP = {4, 3} DP = {2, 1} EP = {1, 0}
244
+ - Comment: There are 2 engine instances and the tensors are sharded
245
+ across 4 devices.
246
+
247
+ When, TP = 2, DP = 1 and EP = True, the configuration on different
248
+ devices:
249
+
250
+ - device 0: TP = {1, 0} DP = {1, 0} EP = {2, 0}
251
+ - device 1: TP = {1, 0} DP = {1, 0} EP = {2, 1}
252
+ - Comment: The experts are split between the 2 devices.
253
+
254
+ When, TP = 1, DP = 2 and EP = True, the configuration on different
255
+ devices:
256
+
257
+ - device 0: TP = {1, 0} DP = {2, 0} EP = {2, 0}
258
+ - device 1: TP = {1, 0} DP = {2, 1} EP = {2, 1}
259
+ - Comment: There are 2 engine instances and the experts are split
260
+ between the 2 devices.
261
+
262
+ When TP = 2, DP = 2 and EP = True, the configuration on different
263
+ devices:
264
+
265
+ - device 0: TP = {1, 0} DP = {2, 0} EP = {4, 0}
266
+ - device 1: TP = {1, 0} DP = {2, 0} EP = {4, 1}
267
+ - device 2: TP = {1, 0} DP = {2, 1} EP = {4, 2}
268
+ - device 3: TP = {1, 0} DP = {2, 1} EP = {4, 3}
269
+ - Comment: There are 2 engine instances and the experts are split
270
+ between the 4 devices.
271
+ """
272
+
273
+ def flatten_tp_across_dp(dp_rank: int):
274
+ tp_rank = 0 if tp_size_ == 1 else get_tensor_model_parallel_rank()
275
+ # There are actually dp_size_ * tp_size_ devices. Update tp_size
276
+ # and tp_rank so we shard across all devices.
277
+ tp_size = dp_size_ * tp_size_
278
+ tp_rank = dp_rank * tp_size_ + tp_rank
279
+ return tp_size, tp_rank
280
+
281
+ use_ep = (dp_size_ * tp_size_ > 1
282
+ and vllm_parallel_config.enable_expert_parallel)
283
+
284
+ dp_size = dp_size_
285
+ dp_rank = get_dp_group().rank_in_group if dp_size > 1 else 0
286
+ tp_size, tp_rank = flatten_tp_across_dp(dp_rank)
287
+
288
+ if not use_ep:
289
+ return FusedMoEParallelConfig(tp_size=tp_size,
290
+ tp_rank=tp_rank,
291
+ dp_size=dp_size,
292
+ dp_rank=dp_rank,
293
+ ep_size=1,
294
+ ep_rank=0,
295
+ use_ep=False)
296
+ # DP + EP / TP + EP / DP + TP + EP
297
+ assert use_ep
298
+ # In EP, each device owns a set of experts fully. There is no tensor
299
+ # parallel update tp_size, tp_rank, ep_size and ep_rank to reflect that.
300
+ ep_size = tp_size
301
+ ep_rank = tp_rank
302
+ return FusedMoEParallelConfig(tp_size=1,
303
+ tp_rank=0,
304
+ dp_size=dp_size,
305
+ dp_rank=dp_rank,
306
+ ep_size=ep_size,
307
+ ep_rank=ep_rank,
308
+ use_ep=True)
309
+
310
+
311
+ # Adapted from pplx-kernels tests/all_to_all_utils.py
312
+ @dataclass
313
+ class FusedMoEConfig:
314
+ num_experts: int
315
+ experts_per_token: int
316
+ hidden_dim: int
317
+
318
+ num_local_experts: int
319
+ moe_parallel_config: FusedMoEParallelConfig
320
+
321
+ # The activation type.
322
+ in_dtype: torch.dtype
323
+
324
+ quant_config: Optional[FusedMoEQuantConfig] = None
325
+
326
+ max_num_tokens: int = envs.VLLM_MOE_DP_CHUNK_SIZE
327
+
328
+ has_bias: bool = False
329
+
330
+ def __post_init__(self):
331
+ if self.dp_size > 1:
332
+ logger.debug_once("Using FusedMoEConfig::max_num_tokens=%d",
333
+ self.max_num_tokens)
334
+
335
+ assert self.max_num_tokens > 0
336
+
337
+ @property
338
+ def quant_dtype(self) -> Union[torch.dtype, str, None]:
339
+ if self.quant_config is not None:
340
+ return self.quant_config.quant_dtype
341
+ else:
342
+ return None
343
+
344
+ @property
345
+ def block_shape(self) -> Optional[list[int]]:
346
+ if self.quant_config is not None:
347
+ return self.quant_config.block_shape
348
+ else:
349
+ return None
350
+
351
+ @property
352
+ def per_act_token_quant(self) -> bool:
353
+ if self.quant_config is not None:
354
+ return self.quant_config.per_act_token_quant
355
+ else:
356
+ return False
357
+
358
+ @property
359
+ def per_out_ch_quant(self) -> bool:
360
+ if self.quant_config is not None:
361
+ return self.quant_config.per_out_ch_quant
362
+ else:
363
+ return False
364
+
365
+ @property
366
+ def tp_size(self):
367
+ return self.moe_parallel_config.tp_size
368
+
369
+ @property
370
+ def dp_size(self):
371
+ return self.moe_parallel_config.dp_size
372
+
373
+ @property
374
+ def ep_size(self):
375
+ return self.moe_parallel_config.ep_size
376
+
377
+ @property
378
+ def tp_rank(self):
379
+ return self.moe_parallel_config.tp_rank
380
+
381
+ @property
382
+ def dp_rank(self):
383
+ return self.moe_parallel_config.dp_rank
384
+
385
+ @property
386
+ def ep_rank(self):
387
+ return self.moe_parallel_config.ep_rank
388
+
389
+ @property
390
+ def use_ep(self):
391
+ return self.moe_parallel_config.use_ep
392
+
393
+ @property
394
+ def use_pplx_kernels(self):
395
+ return self.moe_parallel_config.use_pplx_kernels
396
+
397
+ @property
398
+ def use_deepep_ht_kernels(self):
399
+ return self.moe_parallel_config.use_deepep_ht_kernels
400
+
401
+ @property
402
+ def use_deepep_ll_kernels(self):
403
+ return self.moe_parallel_config.use_deepep_ll_kernels
404
+
405
+ @property
406
+ def use_flashinfer_cutlass_kernels(self):
407
+ return self.moe_parallel_config.use_flashinfer_cutlass_kernels
408
+
409
+ @staticmethod
410
+ def make(
411
+ num_experts: int,
412
+ experts_per_token: int,
413
+ hidden_dim: int,
414
+ num_local_experts: int,
415
+ moe_parallel_config: FusedMoEParallelConfig,
416
+ in_dtype: torch.dtype,
417
+ max_num_tokens: int = envs.VLLM_MOE_DP_CHUNK_SIZE,
418
+ quant_config: Optional[Union[FusedMoEQuantConfig,
419
+ QuantizationConfig]] = None,
420
+ has_bias: bool = False,
421
+ ) -> "FusedMoEConfig":
422
+
423
+ _quant_config: Optional[FusedMoEQuantConfig] = None
424
+
425
+ if quant_config is not None and isinstance(quant_config,
426
+ QuantizationConfig):
427
+ if hasattr(quant_config, 'weight_block_size'):
428
+ block_shape = quant_config.weight_block_size
429
+ else:
430
+ block_shape = None
431
+ per_act_token_quant = False
432
+ per_out_ch_quant = False
433
+ quant_dtype: Union[torch.dtype, str, None] = None
434
+
435
+ input_quant = get_quant_config_input_quant(quant_config)
436
+ weight_quant = get_quant_config_weight_quant(quant_config)
437
+
438
+ if input_quant is not None:
439
+ per_act_token_quant = (input_quant.strategy
440
+ == QuantizationStrategy.TOKEN
441
+ if input_quant is not None else False)
442
+
443
+ if input_quant.num_bits == 8:
444
+ if input_quant.type == QuantizationType.FLOAT:
445
+ quant_dtype = torch.float8_e4m3fn
446
+ elif input_quant.type == QuantizationType.INT:
447
+ quant_dtype = torch.int8
448
+
449
+ from vllm.model_executor.layers.quantization.fp8 import Fp8Config
450
+ if quant_dtype is None and isinstance(quant_config, Fp8Config):
451
+ quant_dtype = torch.float8_e4m3fn
452
+
453
+ from vllm.model_executor.layers.quantization.modelopt import (
454
+ ModelOptNvFp4Config)
455
+ if quant_dtype is None and isinstance(quant_config,
456
+ ModelOptNvFp4Config):
457
+ quant_dtype = "nvfp4"
458
+
459
+ if weight_quant is not None:
460
+ per_out_ch_quant = (
461
+ weight_quant.strategy == QuantizationStrategy.CHANNEL)
462
+
463
+ if quant_dtype is not None:
464
+ _quant_config = FusedMoEQuantConfig(
465
+ quant_dtype=quant_dtype,
466
+ per_act_token_quant=per_act_token_quant,
467
+ per_out_ch_quant=per_out_ch_quant,
468
+ block_shape=block_shape,
469
+ )
470
+ else:
471
+ _quant_config = FusedMoEQuantConfig()
472
+ if moe_parallel_config.dp_size > 1:
473
+ logger.warning_once("MoE DP setup unable to determine "
474
+ "quantization scheme or unsupported "
475
+ "quantization type. This model will "
476
+ "not run with DP enabled.")
477
+ else:
478
+ _quant_config = quant_config
479
+
480
+ return FusedMoEConfig(
481
+ num_experts=num_experts,
482
+ experts_per_token=experts_per_token,
483
+ hidden_dim=hidden_dim,
484
+ num_local_experts=num_local_experts,
485
+ moe_parallel_config=moe_parallel_config,
486
+ in_dtype=in_dtype,
487
+ quant_config=_quant_config,
488
+ max_num_tokens=max_num_tokens,
489
+ has_bias=has_bias,
490
+ )