Skip to content
Closed
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
33 changes: 24 additions & 9 deletions tests/ut/ops/test_mla.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,19 +403,34 @@ def test_initialization(self, mock_tp_size, mock_get_vllm_config, mock_indexer_c
@patch("vllm_ascend.ops.mla.IndexerWrapper")
@patch("vllm_ascend.ops.mla.get_current_vllm_config")
@patch("vllm_ascend.ops.mla.get_tensor_model_parallel_world_size")
def test_marks_layers_only_when_fused_preprocess_type_resolved(
self, mock_tp_size, mock_get_vllm_config, mock_indexer_cls
):
for resolved_type, should_mark in ((PreprocessType.MLAPO, True), (None, False)):
with self.subTest(resolved_type=resolved_type):
def test_marks_layers_for_sfa_or_mla_fused_preprocess(self, mock_tp_size, mock_get_vllm_config, mock_indexer_cls):
cases = (
(True, PreprocessType.MLAPO, False, False, True),
(True, None, True, True, False),
(False, None, True, False, True),
(False, None, False, True, True),
(False, None, False, False, False),
)
for has_type_resolver, resolved_type, enable_mlapo, fa_quant_layer, should_mark in cases:
with self.subTest(
has_type_resolver=has_type_resolver,
resolved_type=resolved_type,
enable_mlapo=enable_mlapo,
fa_quant_layer=fa_quant_layer,
):
fused_qkv_a_proj = SimpleNamespace()
q_proj = SimpleNamespace()
mock_mla_attn = MagicMock()
mock_mla_attn.process_weights_after_loading = MagicMock()
mock_mla_attn.impl = MagicMock()
mock_mla_attn.impl._fused_preprocess_type.return_value = resolved_type
mock_mla_attn.impl.fused_qkv_a_proj = fused_qkv_a_proj
mock_mla_attn.impl.q_proj = q_proj
mock_mla_attn.impl = SimpleNamespace(
process_weights_after_loading=MagicMock(),
fused_qkv_a_proj=fused_qkv_a_proj,
q_proj=q_proj,
enable_mlapo=enable_mlapo,
fa_quant_layer=fa_quant_layer,
)
if has_type_resolver:
mock_mla_attn.impl._fused_preprocess_type = MagicMock(return_value=resolved_type)

with patch("vllm_ascend.ops.mla.MLAAttention", return_value=mock_mla_attn):
mock_tp_size.return_value = 2
Expand Down
9 changes: 8 additions & 1 deletion vllm_ascend/ops/mla.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,14 @@ def __init__(
# layers, so quant methods must skip their own NZ conversion.
# Mark before VLLM calls process_weights_after_loading on submodules.
impl = self.mla_attn.impl
if getattr(impl, "_fused_preprocess_type", None) and impl._fused_preprocess_type() is not None:
fused_preprocess_type = getattr(impl, "_fused_preprocess_type", None)
# SFA exposes an explicit resolver; dense MLA enters fused preprocess
# directly when MLAPO or FA quant is enabled.
if fused_preprocess_type is not None:
fused_preprocess_managed = fused_preprocess_type() is not None
else:
fused_preprocess_managed = impl.enable_mlapo or impl.fa_quant_layer
if fused_preprocess_managed:
for _layer in (impl.fused_qkv_a_proj, impl.q_proj):
Comment on lines +228 to 231

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Accessing attributes like 'enable_mlapo', 'fa_quant_layer', 'fused_qkv_a_proj', and 'q_proj' directly on 'impl' can raise an AttributeError if a different attention backend implementation is used. Using 'getattr' with a default value is safer and more robust.

Suggested change
else:
fused_preprocess_managed = impl.enable_mlapo or impl.fa_quant_layer
if fused_preprocess_managed:
for _layer in (impl.fused_qkv_a_proj, impl.q_proj):
else:
fused_preprocess_managed = getattr(impl, "enable_mlapo", False) or getattr(impl, "fa_quant_layer", False)
if fused_preprocess_managed:
for _layer in (getattr(impl, "fused_qkv_a_proj", None), getattr(impl, "q_proj", None)):

if _layer is not None:
_layer._fused_preprocess_managed = True
Expand Down
Loading