Skip to content

[Perf] Add a switch to enable NZ layout in weights #1410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions vllm_ascend/ascend_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __init__(self, vllm_config):
self.expert_map_path = additional_config.get("expert_map_path", None)
self.chunked_prefill_for_mla = additional_config.get(
"chunked_prefill_for_mla", False)
self.enable_weight_nz_layout = additional_config.get(
"enable_weight_nz_layout", False)


class TorchairGraphConfig:
Expand Down
13 changes: 8 additions & 5 deletions vllm_ascend/attention/mla_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from vllm_ascend.multistream.context import get_multistream_comm_context
from vllm_ascend.multistream.ms_split import model_input_split_v1_mla_attn
from vllm_ascend.ops.attention import vanilla_chunked_prefill_mla
from vllm_ascend.utils import npu_stream_switch, npu_wait_tensor
from vllm_ascend.utils import (ACL_FORMAT_FRACTAL_NZ, npu_stream_switch,
npu_wait_tensor)

if TYPE_CHECKING:
from vllm.v1.core.sched.output import SchedulerOutput
Expand Down Expand Up @@ -651,10 +652,12 @@ def get_and_maybe_dequant_weights(layer: LinearBase):
self.W_UV = W_UV.transpose(0, 1).contiguous()
# Convert from (L, N, P) to (N, P, L)
self.W_UK_T = W_UK.permute(1, 2, 0).contiguous()

# Waiting for BMM NZ support
# self.W_UV.data = torch_npu.npu_format_cast(self.W_UV.data, 29)
# self.W_UK_T.data = torch_npu.npu_format_cast(self.W_UK_T.data, 29)
if get_ascend_config().enable_weight_nz_layout:
# cast quantized weight tensors in NZ layout for higher inference speed
self.W_UV.data = torch_npu.npu_format_cast(self.W_UV.data,
ACL_FORMAT_FRACTAL_NZ)
self.W_UK_T.data = torch_npu.npu_format_cast(
self.W_UK_T.data, ACL_FORMAT_FRACTAL_NZ)

def _compute_prefill_context(
self,
Expand Down
10 changes: 9 additions & 1 deletion vllm_ascend/quantization/w8a8.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import torch
import torch_npu

from vllm_ascend.ascend_config import get_ascend_config
from vllm_ascend.utils import ACL_FORMAT_FRACTAL_NZ


def quant_per_tensor(in_tensor: torch.Tensor, input_scale: torch.Tensor,
input_offset: torch.Tensor):
Expand All @@ -37,6 +40,8 @@ class AscendW8A8LinearMethod:
def __init__(self) -> None:
# aclnn quant matmul requires to transpose matrix B, set to true by default.
self.transpose_weight = True
ascend_config = get_ascend_config()
self.enable_weight_nz_layout = ascend_config.enable_weight_nz_layout

@staticmethod
def get_weight(
Expand Down Expand Up @@ -110,6 +115,9 @@ def process_weights_after_loading(self, layer):
requires_grad=False).to(layer.aclnn_input_scale.dtype)
if self.transpose_weight:
layer.weight.data = layer.weight.data.transpose(0, 1).contiguous()
layer.weight.data = torch_npu.npu_format_cast(layer.weight.data, 29)
if self.enable_weight_nz_layout:
# cast quantized weight tensors in NZ layout for higher inference speed
layer.weight.data = torch_npu.npu_format_cast(
layer.weight.data, ACL_FORMAT_FRACTAL_NZ)
layer.weight_scale.data = torch.flatten(layer.weight_scale.data)
layer.weight_offset.data = torch.flatten(layer.weight_offset.data)
15 changes: 13 additions & 2 deletions vllm_ascend/quantization/w8a8_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ class AscendW8A8DynamicLinearMethod:

def __init__(self):
self.transpose_weight = True
ascend_config = get_ascend_config()
self.enable_weight_nz_layout = ascend_config.enable_weight_nz_layout

@staticmethod
def get_weight(input_size: int, output_size: int,
Expand Down Expand Up @@ -617,8 +619,10 @@ def apply(
def process_weights_after_loading(self, layer):
if self.transpose_weight:
layer.weight.data = layer.weight.data.transpose(0, 1).contiguous()
# cast quantized weight tensors in NZ format (29) for higher inference speed
layer.weight.data = torch_npu.npu_format_cast(layer.weight.data, 29)
if self.enable_weight_nz_layout:
# cast quantized weight tensors in NZ layout for higher inference speed
layer.weight.data = torch_npu.npu_format_cast(
layer.weight.data, ACL_FORMAT_FRACTAL_NZ)
layer.weight_scale.data = layer.weight_scale.data.flatten()
layer.weight_scale_fp32 = layer.weight_scale.data.to(torch.float32)
layer.weight_offset.data = layer.weight_offset.data.flatten()
Expand All @@ -635,6 +639,7 @@ def __init__(self):

ascend_config = get_ascend_config()
self.torchair_graph_enabled = ascend_config.torchair_graph_config.enabled
self.enable_weight_nz_layout = ascend_config.enable_weight_nz_layout

try:
device_group = self.ep_group.device_group
Expand Down Expand Up @@ -816,6 +821,12 @@ def process_weights_after_loading(self, layer):
1, 2).contiguous()
layer.w2_weight.data = layer.w2_weight.data.transpose(
1, 2).contiguous()
if self.enable_weight_nz_layout:
# cast quantized weight tensors in NZ layout for higher inference speed
layer.w13_weight.data = torch_npu.npu_format_cast(
layer.w13_weight.data, ACL_FORMAT_FRACTAL_NZ)
layer.w2_weight.data = torch_npu.npu_format_cast(
layer.w2_weight.data, ACL_FORMAT_FRACTAL_NZ)
if envs.VLLM_ENABLE_FUSED_EXPERTS_ALLGATHER_EP:
torch_npu.npu_format_cast_(layer.w2_weight, ACL_FORMAT_FRACTAL_NZ)
layer.w13_weight_scale.data = layer.w13_weight_scale.data.view(
Expand Down
Loading