Expert Parallelism (EP) Support for DeepSeek V3/R1 (#3602)
Co-authored-by: laixin <xielx@shanghaitech.edu.cn> Co-authored-by: HandH1998 <1335248067@qq.com> Co-authored-by: laixin <q865809639@gmail.com>
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import logging
|
||||
from typing import Optional
|
||||
from typing import List, Optional
|
||||
|
||||
import torch
|
||||
import triton
|
||||
import triton.language as tl
|
||||
|
||||
from sglang.srt.layers.quantization.fp8_kernel import per_token_group_quant_fp8
|
||||
|
||||
_is_cuda = torch.cuda.is_available() and torch.version.cuda
|
||||
if _is_cuda:
|
||||
from sglang.srt.layers.quantization.fp8_kernel import (
|
||||
sglang_per_token_group_quant_fp8,
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -218,12 +225,19 @@ def grouped_gemm_triton_kernel(
|
||||
seg_indptr,
|
||||
weight_indices,
|
||||
m_num_tiles_indptr,
|
||||
use_fp8_w8a8,
|
||||
scale_a,
|
||||
scale_b,
|
||||
use_fp8_w8a8: tl.constexpr,
|
||||
group_n: tl.constexpr,
|
||||
group_k: tl.constexpr,
|
||||
a_stride_0: tl.constexpr,
|
||||
b_stride_0: tl.constexpr,
|
||||
b_stride_1: tl.constexpr,
|
||||
as_stride_0: tl.constexpr,
|
||||
as_stride_1: tl.constexpr,
|
||||
bs_stride_0: tl.constexpr,
|
||||
bs_stride_2: tl.constexpr,
|
||||
bs_stride_1: tl.constexpr,
|
||||
BLOCK_SIZE_M: tl.constexpr,
|
||||
BLOCK_SIZE_N: tl.constexpr,
|
||||
BLOCK_SIZE_K: tl.constexpr,
|
||||
@@ -260,6 +274,12 @@ def grouped_gemm_triton_kernel(
|
||||
+ (n_range_start + offs_bn[:, None]) * b_stride_1
|
||||
+ offs_k[None, :]
|
||||
)
|
||||
|
||||
if group_k > 0 and group_n > 0:
|
||||
a_scale_ptrs = scale_a + (m_range_start + offs_am[:, None]) * as_stride_0
|
||||
offs_bsn = (n_range_start + offs_bn) // group_n
|
||||
b_scale_ptrs = scale_b + (expert_id * bs_stride_0) + offs_bsn * bs_stride_1
|
||||
|
||||
accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
|
||||
for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)):
|
||||
a_tile = tl.load(
|
||||
@@ -268,14 +288,23 @@ def grouped_gemm_triton_kernel(
|
||||
b_tile = tl.load(
|
||||
b_ptr, mask=offs_k[None, :] < (K - k * BLOCK_SIZE_K), other=0.0
|
||||
)
|
||||
accumulator = tl.dot(a_tile, b_tile.T, accumulator)
|
||||
|
||||
if group_k > 0 and group_n > 0:
|
||||
k_start = k * BLOCK_SIZE_K
|
||||
offs_ks = k_start // group_k
|
||||
a_scale = tl.load(a_scale_ptrs + offs_ks * as_stride_1)
|
||||
b_scale = tl.load(b_scale_ptrs + offs_ks * bs_stride_2)
|
||||
accumulator += tl.dot(a_tile, b_tile.T) * a_scale * b_scale[None, :]
|
||||
else:
|
||||
accumulator = tl.dot(a_tile, b_tile.T, accumulator)
|
||||
a_ptr += BLOCK_SIZE_K
|
||||
b_ptr += BLOCK_SIZE_K
|
||||
|
||||
if use_fp8_w8a8:
|
||||
if use_fp8_w8a8 and not (group_k > 0 and group_n > 0):
|
||||
scale_a_value = tl.load(scale_a + expert_id)
|
||||
scale_b_value = tl.load(scale_b + expert_id)
|
||||
accumulator *= scale_a_value * scale_b_value
|
||||
|
||||
c_tile = accumulator.to(c_dtype)
|
||||
|
||||
offs_cm = m_range_start + tl.arange(0, BLOCK_SIZE_M)
|
||||
@@ -307,14 +336,29 @@ def grouped_gemm_triton(
|
||||
use_fp8_w8a8: bool = False,
|
||||
scale_a: torch.Tensor = None,
|
||||
scale_b: torch.Tensor = None,
|
||||
block_shape: Optional[List[int]] = None,
|
||||
):
|
||||
assert weight_column_major == True # TODO: more
|
||||
if use_fp8_w8a8:
|
||||
if use_fp8_w8a8 and block_shape is None:
|
||||
assert scale_a is not None and scale_b is not None
|
||||
|
||||
if block_shape is not None:
|
||||
assert len(block_shape) == 2
|
||||
block_n, block_k = block_shape[0], block_shape[1]
|
||||
if _is_cuda:
|
||||
a, scale_a = sglang_per_token_group_quant_fp8(a, block_k)
|
||||
else:
|
||||
a, scale_a = per_token_group_quant_fp8(a, block_k)
|
||||
|
||||
assert triton.cdiv(a.shape[-1], block_k) == scale_a.shape[-1]
|
||||
assert triton.cdiv(b.shape[-2], block_n) == scale_b.shape[-2]
|
||||
assert triton.cdiv(b.shape[-1], block_k) == scale_b.shape[-1]
|
||||
|
||||
# TODO: adjust config or tune kernel
|
||||
# Reduce block size to prevent L40 shared memory overflow.
|
||||
config = {
|
||||
"BLOCK_SIZE_M": 128,
|
||||
"BLOCK_SIZE_N": 128,
|
||||
"BLOCK_SIZE_M": 64,
|
||||
"BLOCK_SIZE_N": 32,
|
||||
"BLOCK_SIZE_K": 128,
|
||||
}
|
||||
|
||||
@@ -338,12 +382,19 @@ def grouped_gemm_triton(
|
||||
seg_indptr,
|
||||
weight_indices,
|
||||
m_num_tiles_indptr,
|
||||
use_fp8_w8a8,
|
||||
scale_a,
|
||||
scale_b,
|
||||
use_fp8_w8a8,
|
||||
0 if block_shape is None else block_shape[0],
|
||||
0 if block_shape is None else block_shape[1],
|
||||
a.stride(0),
|
||||
b.stride(0),
|
||||
b.stride(1),
|
||||
scale_a.stride(0) if scale_a is not None and scale_a.ndim == 2 else 0,
|
||||
scale_a.stride(1) if scale_a is not None and scale_a.ndim == 2 else 0,
|
||||
scale_b.stride(0) if scale_b is not None and scale_b.ndim >= 2 else 0,
|
||||
scale_b.stride(2) if scale_b is not None and scale_b.ndim == 3 else 0,
|
||||
scale_b.stride(1) if scale_b is not None and scale_b.ndim >= 2 else 0,
|
||||
**config,
|
||||
)
|
||||
return c
|
||||
|
||||
@@ -17,6 +17,7 @@ from sglang.srt.layers.moe.ep_moe.kernels import (
|
||||
run_moe_ep_preproess,
|
||||
silu_and_mul_triton_kernel,
|
||||
)
|
||||
from sglang.srt.layers.moe.fused_moe_triton import FusedMoeWeightScaleSupported
|
||||
from sglang.srt.layers.moe.fused_moe_triton.layer import FusedMoEMethodBase
|
||||
from sglang.srt.layers.moe.topk import select_experts
|
||||
from sglang.srt.layers.quantization.base_config import (
|
||||
@@ -61,6 +62,7 @@ class GroupedGemmRunner(torch.nn.Module):
|
||||
use_fp8_w8a8: bool = False,
|
||||
scale_a: torch.Tensor = None,
|
||||
scale_b: torch.Tensor = None,
|
||||
block_shape: Optional[List[int]] = None,
|
||||
):
|
||||
if self.use_flashinfer:
|
||||
# TODO: flashinfer
|
||||
@@ -87,6 +89,7 @@ class GroupedGemmRunner(torch.nn.Module):
|
||||
use_fp8_w8a8,
|
||||
scale_a,
|
||||
scale_b,
|
||||
block_shape=block_shape,
|
||||
)
|
||||
return c
|
||||
|
||||
@@ -147,12 +150,20 @@ class EPMoE(torch.nn.Module):
|
||||
if quant_config is None:
|
||||
self.quant_method: Optional[QuantizeMethodBase] = UnquantizedEPMoEMethod()
|
||||
self.use_fp8_w8a8 = False
|
||||
self.use_block_quant = False
|
||||
self.block_shape = None
|
||||
self.activation_scheme = None
|
||||
else:
|
||||
self.quant_method: Optional[QuantizeMethodBase] = Fp8EPMoEMethod(
|
||||
quant_config
|
||||
)
|
||||
self.use_fp8_w8a8 = True
|
||||
self.use_block_quant = getattr(self.quant_method, "block_quant", False)
|
||||
self.block_shape = (
|
||||
self.quant_method.quant_config.weight_block_size
|
||||
if self.use_block_quant
|
||||
else None
|
||||
)
|
||||
self.fp8_dtype = torch.float8_e4m3fn
|
||||
self.activation_scheme = quant_config.activation_scheme
|
||||
|
||||
@@ -173,7 +184,8 @@ class EPMoE(torch.nn.Module):
|
||||
|
||||
if self.grouped_gemm_runner is None:
|
||||
self.grouped_gemm_runner = GroupedGemmRunner(
|
||||
hidden_states.device, use_flashinfer=False # TODO: use flashinfer
|
||||
hidden_states.device,
|
||||
use_flashinfer=False, # TODO: use flashinfer
|
||||
)
|
||||
|
||||
topk_weights, topk_ids = select_experts(
|
||||
@@ -195,9 +207,13 @@ class EPMoE(torch.nn.Module):
|
||||
gateup_input = torch.empty(
|
||||
(int(hidden_states.shape[0] * self.top_k), hidden_states.shape[1]),
|
||||
device=hidden_states.device,
|
||||
dtype=self.fp8_dtype if self.use_fp8_w8a8 else hidden_states.dtype,
|
||||
dtype=(
|
||||
self.fp8_dtype
|
||||
if (self.use_fp8_w8a8 and not self.use_block_quant)
|
||||
else hidden_states.dtype
|
||||
),
|
||||
)
|
||||
if self.activation_scheme == "dynamic":
|
||||
if self.activation_scheme == "dynamic" and not self.use_block_quant:
|
||||
max_value = (
|
||||
torch.max(hidden_states)
|
||||
.repeat(self.num_experts_per_partition)
|
||||
@@ -243,7 +259,12 @@ class EPMoE(torch.nn.Module):
|
||||
weight_indices=weight_indices_cur_rank,
|
||||
use_fp8_w8a8=self.use_fp8_w8a8,
|
||||
scale_a=self.w13_input_scale,
|
||||
scale_b=self.w13_weight_scale,
|
||||
scale_b=(
|
||||
self.w13_weight_scale_inv
|
||||
if self.use_block_quant
|
||||
else self.w13_weight_scale
|
||||
),
|
||||
block_shape=self.block_shape,
|
||||
)
|
||||
|
||||
# Act
|
||||
@@ -251,9 +272,13 @@ class EPMoE(torch.nn.Module):
|
||||
gateup_output.shape[0],
|
||||
gateup_output.shape[1] // 2,
|
||||
device=gateup_output.device,
|
||||
dtype=self.fp8_dtype if self.use_fp8_w8a8 else hidden_states.dtype,
|
||||
dtype=(
|
||||
self.fp8_dtype
|
||||
if (self.use_fp8_w8a8 and not self.use_block_quant)
|
||||
else hidden_states.dtype
|
||||
),
|
||||
)
|
||||
if self.w2_input_scale is None:
|
||||
if self.w2_input_scale is None and not self.use_block_quant:
|
||||
self.w2_input_scale = torch.ones(
|
||||
self.num_experts_per_partition,
|
||||
dtype=torch.float32,
|
||||
@@ -291,7 +316,12 @@ class EPMoE(torch.nn.Module):
|
||||
weight_indices=weight_indices_cur_rank,
|
||||
use_fp8_w8a8=self.use_fp8_w8a8,
|
||||
scale_a=self.w2_input_scale,
|
||||
scale_b=self.w2_weight_scale,
|
||||
scale_b=(
|
||||
self.w2_weight_scale_inv
|
||||
if self.use_block_quant
|
||||
else self.w2_weight_scale
|
||||
),
|
||||
block_shape=self.block_shape,
|
||||
)
|
||||
|
||||
# PostReorder
|
||||
@@ -358,7 +388,11 @@ class EPMoE(torch.nn.Module):
|
||||
# Special case for fp8 scales.
|
||||
if "scale" in weight_name:
|
||||
self._load_fp8_scale(
|
||||
param.data, loaded_weight, weight_name, shard_id, expert_id
|
||||
param.data,
|
||||
loaded_weight,
|
||||
weight_name,
|
||||
shard_id,
|
||||
expert_id,
|
||||
)
|
||||
return
|
||||
|
||||
@@ -395,18 +429,33 @@ class EPMoE(torch.nn.Module):
|
||||
param_data[expert_id] = loaded_weight
|
||||
# Weight scales
|
||||
elif "weight_scale" in weight_name:
|
||||
if self.use_block_quant:
|
||||
block_n, block_k = self.block_shape[0], self.block_shape[1]
|
||||
if shard_id == "w1":
|
||||
param_data[expert_id][
|
||||
: (self.intermediate_size + block_n - 1) // block_n, :
|
||||
] = loaded_weight
|
||||
elif shard_id == "w3":
|
||||
param_data[expert_id][
|
||||
(self.intermediate_size + block_n - 1) // block_n :, :
|
||||
] = loaded_weight
|
||||
else: # w2
|
||||
param_data[expert_id] = loaded_weight
|
||||
# If we are in merged column case (gate_up_proj)
|
||||
if shard_id in ("w1", "w3"):
|
||||
# We have to keep the weight scales of w1 and w3 because
|
||||
# we need to re-quantize w1/w3 weights after weight loading.
|
||||
idx = 0 if shard_id == "w1" else 1
|
||||
param_data[expert_id][idx] = loaded_weight
|
||||
# If we are in the row parallel case (down_proj)
|
||||
else:
|
||||
param_data[expert_id] = loaded_weight
|
||||
if shard_id in ("w1", "w3"):
|
||||
# We have to keep the weight scales of w1 and w3 because
|
||||
# we need to re-quantize w1/w3 weights after weight loading.
|
||||
idx = 0 if shard_id == "w1" else 1
|
||||
param_data[expert_id][idx] = loaded_weight
|
||||
|
||||
# If we are in the row parallel case (down_proj)
|
||||
else:
|
||||
param_data[expert_id] = loaded_weight
|
||||
|
||||
|
||||
class UnquantizedEPMoEMethod(FusedMoEMethodBase, CustomOp):
|
||||
|
||||
def create_weights(
|
||||
self,
|
||||
layer: torch.nn.Module,
|
||||
@@ -498,6 +547,7 @@ class Fp8EPMoEMethod(Fp8MoEMethod):
|
||||
|
||||
def __init__(self, quant_config: Fp8Config):
|
||||
self.quant_config = quant_config
|
||||
self.block_quant = self.quant_config.weight_block_size is not None
|
||||
|
||||
def create_weights(
|
||||
self,
|
||||
@@ -512,6 +562,29 @@ class Fp8EPMoEMethod(Fp8MoEMethod):
|
||||
if self.quant_config.is_checkpoint_fp8_serialized:
|
||||
params_dtype = torch.float8_e4m3fn
|
||||
|
||||
tp_size = get_tensor_model_parallel_world_size()
|
||||
if self.block_quant:
|
||||
block_n, block_k = (
|
||||
self.quant_config.weight_block_size[0],
|
||||
self.quant_config.weight_block_size[1],
|
||||
)
|
||||
# NOTE(HandH1998): To ensure proper alignment of the block-wise quantization scales, the output_size of the weights for both the gate and up layers must be divisible by block_n.
|
||||
# Required by collum parallel or enabling merged weights
|
||||
if intermediate_size % block_n != 0:
|
||||
raise ValueError(
|
||||
f"The output_size of gate's and up's weight = "
|
||||
f"{intermediate_size} is not divisible by "
|
||||
f"weight quantization block_n = {block_n}."
|
||||
)
|
||||
if tp_size > 1:
|
||||
# Required by row parallel
|
||||
if intermediate_size % block_k != 0:
|
||||
raise ValueError(
|
||||
f"The input_size of down's weight = "
|
||||
f"{intermediate_size} is not divisible by "
|
||||
f"weight quantization block_k = {block_k}."
|
||||
)
|
||||
|
||||
# WEIGHTS
|
||||
w13_weight = torch.nn.Parameter(
|
||||
torch.empty(
|
||||
@@ -538,21 +611,49 @@ class Fp8EPMoEMethod(Fp8MoEMethod):
|
||||
set_weight_attrs(w2_weight, extra_weight_attrs)
|
||||
|
||||
# WEIGHT_SCALES
|
||||
# Allocate 2 scales for w1 and w3 respectively.
|
||||
w13_weight_scale = torch.nn.Parameter(
|
||||
torch.ones(num_experts_per_partition, 2, dtype=torch.float32),
|
||||
requires_grad=False,
|
||||
)
|
||||
layer.register_parameter("w13_weight_scale", w13_weight_scale)
|
||||
if self.block_quant:
|
||||
w13_weight_scale = torch.nn.Parameter(
|
||||
torch.ones(
|
||||
num_experts_per_partition,
|
||||
2 * ((intermediate_size + block_n - 1) // block_n),
|
||||
(hidden_size + block_k - 1) // block_k,
|
||||
dtype=torch.float32,
|
||||
),
|
||||
requires_grad=False,
|
||||
)
|
||||
w2_weight_scale = torch.nn.Parameter(
|
||||
torch.ones(
|
||||
num_experts_per_partition,
|
||||
(hidden_size + block_n - 1) // block_n,
|
||||
(intermediate_size + block_k - 1) // block_k,
|
||||
dtype=torch.float32,
|
||||
),
|
||||
requires_grad=False,
|
||||
)
|
||||
layer.register_parameter("w13_weight_scale_inv", w13_weight_scale)
|
||||
layer.register_parameter("w2_weight_scale_inv", w2_weight_scale)
|
||||
assert self.quant_config.activation_scheme == "dynamic"
|
||||
else:
|
||||
# WEIGHT_SCALES
|
||||
# Allocate 2 scales for w1 and w3 respectively.
|
||||
w13_weight_scale = torch.nn.Parameter(
|
||||
torch.ones(num_experts_per_partition, 2, dtype=torch.float32),
|
||||
requires_grad=False,
|
||||
)
|
||||
layer.register_parameter("w13_weight_scale", w13_weight_scale)
|
||||
|
||||
w2_weight_scale = torch.nn.Parameter(
|
||||
torch.ones(num_experts_per_partition, dtype=torch.float32),
|
||||
requires_grad=False,
|
||||
)
|
||||
layer.register_parameter("w2_weight_scale", w2_weight_scale)
|
||||
w2_weight_scale = torch.nn.Parameter(
|
||||
torch.ones(num_experts_per_partition, dtype=torch.float32),
|
||||
requires_grad=False,
|
||||
)
|
||||
layer.register_parameter("w2_weight_scale", w2_weight_scale)
|
||||
# Add the quantization method used (per tensor/grouped/channel)
|
||||
# to ensure the weight scales are loaded in properly
|
||||
extra_weight_attrs.update({"quant_method": "tensor"})
|
||||
extra_weight_attrs.update(
|
||||
{"quant_method": FusedMoeWeightScaleSupported.BLOCK.value}
|
||||
if self.block_quant
|
||||
else {"quant_method": FusedMoeWeightScaleSupported.TENSOR.value}
|
||||
)
|
||||
# If loading fp8 checkpoint, pass the weight loaders.
|
||||
# If loading an fp16 checkpoint, do not (we will quantize in
|
||||
# process_weights_after_loading()
|
||||
|
||||
Reference in New Issue
Block a user