Skip to content

Commit c605cd0

Browse files
committed
add fuse rms quant
1 parent 6fbae83 commit c605cd0

4 files changed

Lines changed: 50 additions & 10 deletions

File tree

vllm/model_executor/layers/linear.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,17 @@ def weight_loader(self, param: Parameter, loaded_weight: torch.Tensor):
387387
param.data.copy_(loaded_weight)
388388

389389
def forward(
390-
self, x: torch.Tensor
390+
self, x: torch.Tensor, x_quant_scales: torch.Tensor = None,
391391
) -> Union[torch.Tensor, tuple[torch.Tensor, Optional[Parameter]]]:
392392
bias = self.bias if not self.skip_bias_add else None
393393
assert self.quant_method is not None
394-
output = self.quant_method.apply(self, x, bias)
394+
# output = self.quant_method.apply(self, x, bias)
395+
if isinstance(self.quant_method, UnquantizedLinearMethod):
396+
assert x_quant_scales is None, "UnquantizedLinearMethod should not have quantized input"
397+
output = self.quant_method.apply(self, x, bias)
398+
else:
399+
output = self.quant_method.apply(self, x, bias, x_quant_scales=x_quant_scales)
400+
395401
output_bias = self.bias if self.skip_bias_add else None
396402
if not self.return_bias:
397403
return output

vllm/model_executor/layers/quantization/fp8.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,8 @@ def process_weights_after_loading(self, layer: Module) -> None:
404404
def apply(self,
405405
layer: torch.nn.Module,
406406
x: torch.Tensor,
407-
bias: Optional[torch.Tensor] = None) -> torch.Tensor:
407+
bias: Optional[torch.Tensor] = None,
408+
x_quant_scales: torch.Tensor = None) -> torch.Tensor:
408409

409410
if self.use_marlin:
410411
return apply_fp8_marlin_linear(
@@ -427,6 +428,7 @@ def apply(self,
427428
bias=bias,
428429
cutlass_block_fp8_supported=self.cutlass_block_fp8_supported,
429430
use_aiter_and_is_supported=self.use_aiter_and_is_supported,
431+
input_quant_scale=x_quant_scales,
430432
)
431433

432434
return self.fp8_linear.apply(input=x,

vllm/model_executor/layers/quantization/utils/fp8_utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def apply_w8a8_block_fp8_linear(
115115
bias: Optional[torch.Tensor] = None,
116116
cutlass_block_fp8_supported: bool = CUTLASS_BLOCK_FP8_SUPPORTED,
117117
use_aiter_and_is_supported: bool = False,
118+
input_quant_scale: torch.Tensor = None,
118119
) -> torch.Tensor:
119120
assert input_scale is None
120121
# View input as 2D matrix for fp8 methods
@@ -135,7 +136,12 @@ def apply_w8a8_block_fp8_linear(
135136
scale_a=x_scale,
136137
scale_b=weight_scale.T)
137138
else:
138-
if use_aiter_and_is_supported:
139+
output_dtype = input.dtype
140+
if input_quant_scale is not None:
141+
q_input = input
142+
x_scale = input_quant_scale
143+
output_dtype = torch.bfloat16
144+
elif use_aiter_and_is_supported:
139145
# print(f"input_2d.shape: {input_2d.shape}, input_2d.stride: {input_2d.stride()}")
140146
q_input, x_scale = aiter_per1x128_quant(input_2d.contiguous(), quant_dtype=rocm_aiter.dtypes.fp8)
141147
else:
@@ -150,11 +156,11 @@ def apply_w8a8_block_fp8_linear(
150156
x_scale,
151157
weight_scale,
152158
block_size,
153-
output_dtype=input.dtype)
159+
output_dtype=output_dtype)
154160

155161
if bias is not None:
156162
output = output + bias
157-
return output.to(dtype=input.dtype).view(*output_shape)
163+
return output.to(dtype=output_dtype).view(*output_shape)
158164

159165

160166
def apply_w8a8_block_fp8_linear_fake(
@@ -166,6 +172,7 @@ def apply_w8a8_block_fp8_linear_fake(
166172
bias: Optional[torch.Tensor] = None,
167173
cutlass_block_fp8_supported: bool = CUTLASS_BLOCK_FP8_SUPPORTED,
168174
use_aiter_and_is_supported: bool = False,
175+
input_quant_scale: torch.Tensor = None,
169176
) -> torch.Tensor:
170177
output_shape = [*input.shape[:-1], weight.shape[0]]
171178
return torch.empty(output_shape, dtype=input.dtype, device=input.device)

vllm/model_executor/models/deepseek_v2.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
is_rocm_aiter_fuse_routed_scaling_factor,
6464
)
6565

66+
from aiter.ops.triton.fused_fp8_quant import fused_rms_fp8_group_quant
67+
import aiter as rocm_aiter
68+
rocm_aiter_fp8_dtype = rocm_aiter.dtypes.fp8
69+
rocm_aiter_fp8_quant_group_size = 128
6670

6771
class DeepseekV2MLP(nn.Module):
6872

@@ -484,10 +488,15 @@ def forward(
484488
positions: torch.Tensor,
485489
hidden_states: torch.Tensor,
486490
) -> torch.Tensor:
491+
hidden_states_quant = None
492+
if isinstance(hidden_states, tuple):
493+
hidden_states, hidden_states_quant = hidden_states
494+
487495
if self.q_lora_rank is not None:
488496
# q_c = self.q_a_proj(hidden_states)[0]
489497
# kv_lora = self.kv_a_proj_with_mqa(hidden_states)[0]
490-
qkv_lora = self.fused_qkv_a_proj(hidden_states)[0]
498+
qkv_lora = self.fused_qkv_a_proj(hidden_states, x_quant_scales = hidden_states_quant)[0]
499+
# qkv_lora = self.fused_qkv_a_proj(hidden_states)[0]
491500
q_c, kv_lora = qkv_lora.split(
492501
[self.q_lora_rank, self.kv_lora_rank + self.qk_rope_head_dim],
493502
dim=-1,
@@ -576,12 +585,28 @@ def forward(
576585
residual: Optional[torch.Tensor],
577586
) -> torch.Tensor:
578587
# Self Attention
588+
weight = self.input_layernorm.weight
589+
eps = self.input_layernorm.variance_epsilon
579590
if residual is None:
580591
residual = hidden_states
581-
hidden_states = self.input_layernorm(hidden_states)
592+
hidden_states, hidden_states_quant = fused_rms_fp8_group_quant(hidden_states, weight, eps,
593+
None, None, eps,
594+
group_size = rocm_aiter_fp8_quant_group_size,
595+
dtype_quant=rocm_aiter_fp8_dtype,
596+
res1=None)
582597
else:
583-
hidden_states, residual = self.input_layernorm(
584-
hidden_states, residual)
598+
(hidden_states, hidden_states_quant), residual = fused_rms_fp8_group_quant(hidden_states, weight, eps,
599+
None, None, eps,
600+
group_size = rocm_aiter_fp8_quant_group_size,
601+
dtype_quant=rocm_aiter_fp8_dtype,
602+
res1=residual)
603+
hidden_states = (hidden_states, hidden_states_quant)
604+
# if residual is None:
605+
# residual = hidden_states
606+
# hidden_states = self.input_layernorm(hidden_states)
607+
# else:
608+
# hidden_states, residual = self.input_layernorm(
609+
# hidden_states, residual)
585610
hidden_states = self.self_attn(
586611
positions=positions,
587612
hidden_states=hidden_states,

0 commit comments

Comments
 (0)