[Refactor] refactor humming linear and moe backends to use explicit layer configs (#49610)
Signed-off-by: Jinzhen Lin <jinzhen.ljz@antgroup.com> Co-authored-by: OpenAI Codex <noreply@openai.com>
This commit is contained in:
co-authored by
OpenAI Codex
parent
e229fdbc87
commit
fcde8e1460
@@ -4,7 +4,7 @@
|
||||
|
||||
import json
|
||||
import math
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
import torch
|
||||
|
||||
@@ -62,8 +62,15 @@ from vllm.utils.import_utils import has_humming
|
||||
from vllm.v1.worker.workspace import current_workspace_manager
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.model_executor.layers.fused_moe import RoutedExperts
|
||||
from vllm.utils.humming import GemmType as HummingGemmType
|
||||
from vllm.model_executor.layers.quantization.utils.humming_utils import (
|
||||
HummingMoEQuantConfig,
|
||||
)
|
||||
from vllm.utils.humming import (
|
||||
GemmType as HummingGemmType,
|
||||
)
|
||||
from vllm.utils.humming import (
|
||||
LayerConfig as HummingLayerConfig,
|
||||
)
|
||||
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -88,15 +95,20 @@ def get_humming_moe_gemm_type() -> str:
|
||||
class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
def __init__(
|
||||
self,
|
||||
layer: "RoutedExperts",
|
||||
moe_config: FusedMoEConfig,
|
||||
quant_config: FusedMoEQuantConfig,
|
||||
max_num_tokens: int | None = None,
|
||||
num_dispatchers: int | None = None,
|
||||
):
|
||||
self.layer = layer
|
||||
self.num_experts = self.layer.local_num_experts
|
||||
self.global_num_experts = self.layer.global_num_experts
|
||||
humming_quant_config = cast("HummingMoEQuantConfig", quant_config)
|
||||
self.humming_configs: dict[str, HummingLayerConfig] = {
|
||||
"w13": humming_quant_config.w1_humming_config,
|
||||
"w2": humming_quant_config.w2_humming_config,
|
||||
}
|
||||
self.locks = torch.zeros(1024, dtype=torch.int32, device=moe_config.device)
|
||||
self.num_experts = moe_config.num_local_experts
|
||||
self.global_num_experts = moe_config.num_experts
|
||||
self.quant_config = quant_config
|
||||
self.init_humming_moe()
|
||||
|
||||
if self.is_batched():
|
||||
@@ -111,31 +123,73 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
self._permute_scratch: MoEPermuteScratch | None = None
|
||||
|
||||
def init_humming_moe(self):
|
||||
from vllm.utils.humming import HummingMethod
|
||||
from vllm.utils.humming import get_heuristics_config
|
||||
|
||||
self.compute_config = {
|
||||
"use_batch_invariant": envs.VLLM_BATCH_INVARIANT,
|
||||
"use_f16_accum": envs.VLLM_HUMMING_USE_F16_ACCUM,
|
||||
"gemm_type": self.humming_gemm_type().value,
|
||||
}
|
||||
self.w13_tuning_config = HummingMethod.get_default_tuning_configs(
|
||||
layer=self.layer,
|
||||
self.w13_tuning_config = get_heuristics_config(
|
||||
layer_config=self.humming_configs["w13"],
|
||||
use_f16_accum=envs.VLLM_HUMMING_USE_F16_ACCUM,
|
||||
use_batch_invariant=envs.VLLM_BATCH_INVARIANT,
|
||||
gemm_type=self.humming_gemm_type(),
|
||||
sublayer_name="w13",
|
||||
)
|
||||
self.w2_tuning_config = HummingMethod.get_default_tuning_configs(
|
||||
layer=self.layer,
|
||||
self.w2_tuning_config = get_heuristics_config(
|
||||
layer_config=self.humming_configs["w2"],
|
||||
use_f16_accum=envs.VLLM_HUMMING_USE_F16_ACCUM,
|
||||
use_batch_invariant=envs.VLLM_BATCH_INVARIANT,
|
||||
gemm_type=self.humming_gemm_type(),
|
||||
sublayer_name="w2",
|
||||
)
|
||||
self.compute_config_str = json.dumps(self.compute_config)
|
||||
self.w13_tuning_config_str = json.dumps(self.w13_tuning_config)
|
||||
self.w2_tuning_config_str = json.dumps(self.w2_tuning_config)
|
||||
|
||||
def quantize_input(
|
||||
self,
|
||||
sublayer_name: str,
|
||||
inputs: torch.Tensor,
|
||||
quanted_input: torch.Tensor | None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor | None]:
|
||||
from vllm.utils.humming import may_quant_input
|
||||
|
||||
return may_quant_input(
|
||||
self.humming_configs[sublayer_name],
|
||||
inputs=inputs,
|
||||
quanted_input=quanted_input,
|
||||
)
|
||||
|
||||
def humming_forward(
|
||||
self,
|
||||
sublayer_name: str,
|
||||
inputs: torch.Tensor,
|
||||
weight: torch.Tensor,
|
||||
input_scale: torch.Tensor | None,
|
||||
outputs: torch.Tensor,
|
||||
**kwargs: Any,
|
||||
) -> torch.Tensor:
|
||||
from vllm.utils.humming import humming_forward
|
||||
|
||||
is_w13 = sublayer_name == "w13"
|
||||
return humming_forward(
|
||||
self.humming_configs[sublayer_name],
|
||||
inputs=inputs,
|
||||
weight=weight,
|
||||
weight_scale=(
|
||||
self.quant_config.w1_scale if is_w13 else self.quant_config.w2_scale
|
||||
),
|
||||
zero_point=(self.quant_config.w1_zp if is_w13 else self.quant_config.w2_zp),
|
||||
bias=(self.quant_config.w1_bias if is_w13 else self.quant_config.w2_bias),
|
||||
weight_scale_2=(
|
||||
self.quant_config.g1_alphas if is_w13 else self.quant_config.g2_alphas
|
||||
),
|
||||
input_scale=input_scale,
|
||||
outputs=outputs,
|
||||
locks=self.locks,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def _get_permute_scratch(self) -> MoEPermuteScratch | None:
|
||||
if self._permute_scratch is None and moe_permute_unpermute_supported():
|
||||
self._permute_scratch = MoEPermuteScratch(
|
||||
@@ -218,8 +272,7 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
@property
|
||||
def expects_unquantized_inputs(self) -> bool:
|
||||
"""
|
||||
Humming kernels handle input quantization internally via
|
||||
HummingMethod.may_quant_input() in the apply() method.
|
||||
Humming kernels handle input quantization internally in apply().
|
||||
|
||||
This property tells the prepare/finalize step to skip input
|
||||
quantization (by setting defer_input_quant=True) and pass
|
||||
@@ -255,6 +308,10 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
def _supports_parallel_config(moe_parallel_config: FusedMoEParallelConfig) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def _supports_batch_invariance() -> bool:
|
||||
return True
|
||||
|
||||
def moe_problem_size(
|
||||
self,
|
||||
a1: torch.Tensor,
|
||||
@@ -262,10 +319,8 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
w2: torch.Tensor,
|
||||
topk_ids: torch.Tensor,
|
||||
) -> tuple[int, int, int, int, int]:
|
||||
from vllm.utils.humming import HummingLayerMeta
|
||||
|
||||
meta1: HummingLayerMeta = self.layer.humming_metas["w13"]
|
||||
meta2: HummingLayerMeta = self.layer.humming_metas["w2"]
|
||||
meta1 = self.humming_configs["w13"]
|
||||
meta2 = self.humming_configs["w2"]
|
||||
|
||||
assert meta1.num_experts == meta2.num_experts
|
||||
|
||||
@@ -282,25 +337,25 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
assert a1.size(0) == num_experts
|
||||
num_tokens = a1.size(1)
|
||||
|
||||
return (
|
||||
meta1.num_experts,
|
||||
num_tokens,
|
||||
# Logical intermediate width for both gated and non-gated activations
|
||||
self.layer.intermediate_size_per_partition,
|
||||
meta1.shape_k,
|
||||
top_k,
|
||||
)
|
||||
intermediate_dim = meta2.shape_k - meta2.pad_shape_k
|
||||
hidden_dim = meta1.shape_k - meta1.pad_shape_k
|
||||
return meta1.num_experts, num_tokens, intermediate_dim, hidden_dim, top_k
|
||||
|
||||
def get_buffer_metas(self, M: int, topk: int, activation: MoEActivation):
|
||||
from vllm.utils.humming import GemmType as HummingGemmType
|
||||
from vllm.utils.humming import dtypes
|
||||
|
||||
num_experts = self.num_experts
|
||||
N = self.layer.intermediate_size_per_partition
|
||||
K = self.layer.hidden_size
|
||||
w13_config = self.humming_configs["w13"]
|
||||
w2_config = self.humming_configs["w2"]
|
||||
gate_up_dim = w13_config.shape_n - w13_config.pad_shape_n
|
||||
intermediate_dim = w2_config.shape_k - w2_config.pad_shape_k
|
||||
K = w13_config.shape_k - w13_config.pad_shape_k
|
||||
assert isinstance(num_experts, int)
|
||||
assert isinstance(N, int)
|
||||
assert isinstance(gate_up_dim, int)
|
||||
assert isinstance(intermediate_dim, int)
|
||||
assert isinstance(K, int)
|
||||
assert intermediate_dim == self.adjust_N_for_activation(gate_up_dim, activation)
|
||||
|
||||
# hidden_states
|
||||
# (-> quanted_gate_up_input) (if not BF16/FP16 activation)
|
||||
@@ -310,7 +365,8 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
# -> down_output
|
||||
# (-> output) (if not is_batched)
|
||||
# Neighboring nodes are required to utilize distinct workspaces.
|
||||
# The output must be derived from workspace1.
|
||||
# The final output buffer is supplied by the modular kernel and may
|
||||
# alias workspace1.
|
||||
|
||||
output_shape: tuple[int, ...]
|
||||
if self.is_batched():
|
||||
@@ -327,10 +383,8 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
real_shape_m = M * topk
|
||||
output_shape = (M, K)
|
||||
|
||||
gate_up_size = N * (2 if activation.is_gated else 1)
|
||||
down_input_size = N
|
||||
a_dtype = self.layer.humming_metas["w13"].a_dtype
|
||||
c_dtype = self.layer.humming_metas["w13"].c_dtype
|
||||
a_dtype = self.humming_configs["w13"].a_dtype
|
||||
c_dtype = self.humming_configs["w13"].c_dtype
|
||||
num_bits = a_dtype.num_bits
|
||||
torch_dtype_map = {
|
||||
dtypes.float16: torch.float16,
|
||||
@@ -348,15 +402,15 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
"dtype": torch_dtype_map[a_dtype],
|
||||
},
|
||||
"gate_up_output": {
|
||||
"shape": (real_shape_m, gate_up_size),
|
||||
"shape": (real_shape_m, gate_up_dim),
|
||||
"dtype": torch_dtype_map[c_dtype],
|
||||
},
|
||||
"activation_output": {
|
||||
"shape": (real_shape_m, down_input_size),
|
||||
"shape": (real_shape_m, intermediate_dim),
|
||||
"dtype": torch_dtype_map[c_dtype],
|
||||
},
|
||||
"quanted_down_input": {
|
||||
"shape": (real_shape_m, down_input_size),
|
||||
"shape": (real_shape_m, intermediate_dim),
|
||||
"dtype": torch_dtype_map[a_dtype],
|
||||
},
|
||||
"down_output": {
|
||||
@@ -414,7 +468,7 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
|
||||
output_key = "down_output" if self.is_batched() else "output"
|
||||
output_shape = buffer_metas[output_key]["shape"]
|
||||
elem_size = self.layer.params_dtype.itemsize
|
||||
elem_size = self.moe_config.in_dtype.itemsize
|
||||
|
||||
return (
|
||||
(workspace1_nbytes // elem_size,),
|
||||
@@ -438,7 +492,7 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
def make_workspaces(self, M: int, topk: int, activation: MoEActivation):
|
||||
shapes = self._workspace_shapes(M, topk, activation)
|
||||
workspace1_shape, workspace2_shape, output_shape = shapes
|
||||
torch_dtype = self.layer.params_dtype
|
||||
torch_dtype = self.moe_config.in_dtype
|
||||
workspace1, workspace2 = current_workspace_manager().get_simultaneous(
|
||||
(workspace1_shape, torch_dtype),
|
||||
(workspace2_shape, torch_dtype),
|
||||
@@ -600,12 +654,11 @@ class HummingIndexedExperts(HummingExpertsBase):
|
||||
"""
|
||||
Standard apply implementation for Humming indexed experts.
|
||||
|
||||
Note: Humming kernels handle weights and quantization internally through
|
||||
the layer object, so w1, w2, a1q_scale, a2_scale parameters are not used.
|
||||
The output is written into workspace13 via the buffer management.
|
||||
Humming performs activation quantization internally and consumes the
|
||||
weights supplied by the modular kernel interface.
|
||||
Results are written to the output buffer supplied by the modular
|
||||
kernel, which may alias workspace13.
|
||||
"""
|
||||
from vllm.utils.humming import HummingMethod
|
||||
|
||||
assert not apply_router_weight_on_input
|
||||
|
||||
hidden_states = hidden_states.view(-1, hidden_states.size(-1))
|
||||
@@ -616,6 +669,7 @@ class HummingIndexedExperts(HummingExpertsBase):
|
||||
topk_ids.size(1),
|
||||
activation,
|
||||
)
|
||||
buffers["output"] = output
|
||||
|
||||
moe_kwargs1, moe_kwargs2 = self.prepare_humming_moe_kwargs(
|
||||
topk_ids=topk_ids,
|
||||
@@ -623,19 +677,18 @@ class HummingIndexedExperts(HummingExpertsBase):
|
||||
expert_tokens_meta=expert_tokens_meta,
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w13",
|
||||
inputs=hidden_states,
|
||||
quanted_input=buffers.get("quanted_gate_up_input", None),
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w13",
|
||||
inputs=inputs,
|
||||
weight=w1,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["gate_up_output"],
|
||||
sublayer_name="w13",
|
||||
**moe_kwargs1,
|
||||
)
|
||||
|
||||
@@ -645,19 +698,18 @@ class HummingIndexedExperts(HummingExpertsBase):
|
||||
output=buffers["activation_output"],
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w2",
|
||||
inputs=buffers["activation_output"],
|
||||
quanted_input=buffers.get("quanted_down_input", None),
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w2",
|
||||
inputs=inputs,
|
||||
weight=w2,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["down_output"].view(-1, hidden_states.size(-1)),
|
||||
sublayer_name="w2",
|
||||
**moe_kwargs2,
|
||||
)
|
||||
|
||||
@@ -705,12 +757,11 @@ class HummingGroupedExperts(HummingExpertsBase):
|
||||
"""
|
||||
Standard apply implementation for Humming grouped experts.
|
||||
|
||||
Note: Humming kernels handle weights and quantization internally through
|
||||
the layer object, so w1, w2, a1q_scale, a2_scale parameters are not used.
|
||||
The output is written into workspace13 via the buffer management.
|
||||
Humming performs activation quantization internally and consumes the
|
||||
weights supplied by the modular kernel interface.
|
||||
Results are written to the output buffer supplied by the modular
|
||||
kernel, which may alias workspace13.
|
||||
"""
|
||||
from vllm.utils.humming import HummingMethod
|
||||
|
||||
assert not apply_router_weight_on_input
|
||||
|
||||
valid_shape_m = self.estimate_local_valid_shape_m(topk_ids)
|
||||
@@ -722,6 +773,7 @@ class HummingGroupedExperts(HummingExpertsBase):
|
||||
topk_ids.size(1),
|
||||
activation,
|
||||
)
|
||||
buffers["output"] = output
|
||||
|
||||
hidden_states, _, expert_first_token_offset, inv_perm, _ = moe_permute(
|
||||
hidden_states=hidden_states,
|
||||
@@ -733,23 +785,22 @@ class HummingGroupedExperts(HummingExpertsBase):
|
||||
scratch=self._get_permute_scratch(),
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w13",
|
||||
inputs=hidden_states,
|
||||
quanted_input=buffers.get("quanted_gate_up_input", None),
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w13",
|
||||
inputs=inputs,
|
||||
weight=w1,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["gate_up_output"],
|
||||
valid_shape_m=valid_shape_m,
|
||||
expert_layout=expert_first_token_offset,
|
||||
compute_config=self.compute_config_str,
|
||||
tuning_config=self.w13_tuning_config_str,
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
self.apply_activation(
|
||||
@@ -758,23 +809,22 @@ class HummingGroupedExperts(HummingExpertsBase):
|
||||
output=buffers["activation_output"],
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w2",
|
||||
inputs=buffers["activation_output"],
|
||||
quanted_input=buffers.get("quanted_down_input", None),
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w2",
|
||||
inputs=inputs,
|
||||
weight=w2,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["down_output"],
|
||||
valid_shape_m=valid_shape_m,
|
||||
expert_layout=expert_first_token_offset,
|
||||
compute_config=self.compute_config_str,
|
||||
tuning_config=self.w2_tuning_config_str,
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
moe_unpermute(
|
||||
@@ -821,12 +871,11 @@ class BatchedHummingGroupedExperts(HummingExpertsBase):
|
||||
"""
|
||||
Standard apply implementation for Humming batched grouped experts.
|
||||
|
||||
Note: Humming kernels handle weights and quantization internally through
|
||||
the layer object, so w1, w2, a1q_scale, a2_scale parameters are not used.
|
||||
The output is written into workspace13 via the buffer management.
|
||||
Humming performs activation quantization internally and consumes the
|
||||
weights supplied by the modular kernel interface.
|
||||
Results are written to the output buffer supplied by the modular
|
||||
kernel, which may alias workspace13.
|
||||
"""
|
||||
from vllm.utils.humming import HummingMethod
|
||||
|
||||
assert not apply_router_weight_on_input
|
||||
assert expert_tokens_meta is not None
|
||||
|
||||
@@ -841,24 +890,24 @@ class BatchedHummingGroupedExperts(HummingExpertsBase):
|
||||
topk_ids.size(1),
|
||||
activation,
|
||||
)
|
||||
buffers["down_output"] = output
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w13",
|
||||
inputs=hidden_states,
|
||||
quanted_input=buffers.get("quanted_gate_up_input", None),
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w13",
|
||||
inputs=inputs,
|
||||
weight=w1,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["gate_up_output"],
|
||||
valid_shape_m=valid_shape_m,
|
||||
expert_layout=expert_num_tokens,
|
||||
compute_config=self.compute_config_str,
|
||||
tuning_config=self.w13_tuning_config_str,
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
self.apply_activation(
|
||||
@@ -867,21 +916,20 @@ class BatchedHummingGroupedExperts(HummingExpertsBase):
|
||||
output=buffers["activation_output"],
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w2",
|
||||
inputs=buffers["activation_output"],
|
||||
quanted_input=buffers.get("quanted_down_input", None),
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w2",
|
||||
inputs=inputs,
|
||||
weight=w2,
|
||||
input_scale=input_scale,
|
||||
outputs=output.view(-1, hidden_states.size(-1)),
|
||||
valid_shape_m=valid_shape_m,
|
||||
expert_layout=expert_num_tokens,
|
||||
compute_config=self.compute_config_str,
|
||||
tuning_config=self.w2_tuning_config_str,
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user