[Bugfix][NPU] Materialize causal mask in FlashAttention dense path (#7324)
Signed-off-by: Oliver7th <fangzhengyi@huawei.com>
This commit is contained in:
1 parent
8e738ce21b
commit
767cc7977e
3 files changed
+291
-6
No files matched your search
@@ -503,11 +503,12 @@ def test_packed_varlen_metadata_must_be_complete(monkeypatch):
|
||||
# - boundary resolution in ``_resolve_packed_seq_npu`` (pure Python, no device
|
||||
# sync, no MindIE-SD import);
|
||||
# - env dispatch in ``forward_fa_npu`` (which op a packed forward takes);
|
||||
# - native torch_npu dispatch for right-down causal attention;
|
||||
# - the laser input pre-scaling math in ``_forward_prefix_kv_slice_npu``.
|
||||
#
|
||||
# They run on CPU without a real NPU by injecting a fake ``mindiesd`` module
|
||||
# into ``sys.modules`` (same pattern as ``test_paged_attention`` /
|
||||
# ``benchmarks/conftest``). No production code is exercised for real kernels.
|
||||
# They run on CPU without a real NPU by injecting fake ``mindiesd`` and
|
||||
# ``torch_npu`` modules into ``sys.modules`` (same pattern as
|
||||
# ``test_paged_attention`` / ``benchmarks/conftest``). No real kernel runs.
|
||||
|
||||
|
||||
def _npu_impl(causal: bool = False) -> FlashAttentionImpl:
|
||||
@@ -529,6 +530,15 @@ def _fake_mindiesd(monkeypatch, *, attention_forward=None, attention_forward_var
|
||||
return fake
|
||||
|
||||
|
||||
def _fake_torch_npu(monkeypatch, *, npu_fusion_attention=None):
|
||||
"""Install the minimal torch_npu surface used by causal NPU attention."""
|
||||
fake = SimpleNamespace(
|
||||
npu_fusion_attention=npu_fusion_attention or Mock(return_value=(torch.zeros(1), None, None, None, 0, 0, 0)),
|
||||
)
|
||||
monkeypatch.setitem(sys.modules, "torch_npu", fake)
|
||||
return fake
|
||||
|
||||
|
||||
# --- Test group A: boundary resolution (_resolve_packed_seq_npu) -------------
|
||||
|
||||
|
||||
@@ -685,7 +695,109 @@ def test_npu_varlen_opt_in_unset_takes_mask_path(monkeypatch):
|
||||
assert out is fake_forward.return_value
|
||||
|
||||
|
||||
# --- Test group C: laser input pre-scaling in _forward_prefix_kv_slice_npu --
|
||||
# --- Test group C: native NPU causal attention dispatch ----------------------
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("query_length", "key_length", "expected_inner_precise"),
|
||||
[(4, 4, 0), (2, 4, 0), (4, 2, 2)],
|
||||
)
|
||||
def test_npu_causal_uses_native_right_down_mode(
|
||||
monkeypatch,
|
||||
query_length,
|
||||
key_length,
|
||||
expected_inner_precise,
|
||||
):
|
||||
expected_out = torch.randn(1, query_length, 2, 4)
|
||||
fusion_attention = Mock(return_value=(expected_out, None, None, None, 0, 0, 0))
|
||||
fake_torch_npu = _fake_torch_npu(monkeypatch, npu_fusion_attention=fusion_attention)
|
||||
impl = FlashAttentionImpl(num_heads=8, head_size=4, softmax_scale=0.5, causal=True)
|
||||
query = torch.randn(1, query_length, 2, 4)
|
||||
key = torch.randn(1, key_length, 2, 4)
|
||||
|
||||
out = impl.forward_fa_npu(query, key, key)
|
||||
|
||||
fake_torch_npu.npu_fusion_attention.assert_called_once()
|
||||
args, kwargs = fake_torch_npu.npu_fusion_attention.call_args
|
||||
assert torch.equal(args[0], query)
|
||||
assert torch.equal(args[1], key)
|
||||
assert torch.equal(args[2], key)
|
||||
assert all(tensor.is_contiguous() for tensor in args[:3])
|
||||
assert kwargs["head_num"] == query.shape[2]
|
||||
assert kwargs["input_layout"] == "BSND"
|
||||
assert kwargs["scale"] == 0.5
|
||||
assert kwargs["keep_prob"] == 1.0
|
||||
assert kwargs["sparse_mode"] == 3
|
||||
assert kwargs["inner_precise"] == expected_inner_precise
|
||||
|
||||
block_mask = kwargs["atten_mask"]
|
||||
assert block_mask.shape == (2048, 2048)
|
||||
assert block_mask.dtype == torch.bool
|
||||
assert block_mask.is_contiguous()
|
||||
expected_corner = torch.tensor(
|
||||
[
|
||||
[False, True, True, True],
|
||||
[False, False, True, True],
|
||||
[False, False, False, True],
|
||||
[False, False, False, False],
|
||||
]
|
||||
)
|
||||
assert torch.equal(block_mask[:4, :4], expected_corner)
|
||||
assert out is expected_out
|
||||
|
||||
|
||||
def test_npu_causal_composes_explicit_keep_mask(monkeypatch):
|
||||
expected_out = torch.randn(1, 4, 2, 4)
|
||||
fusion_attention = Mock(return_value=(expected_out, None, None, None, 0, 0, 0))
|
||||
fake_torch_npu = _fake_torch_npu(monkeypatch, npu_fusion_attention=fusion_attention)
|
||||
impl = FlashAttentionImpl(num_heads=2, head_size=4, softmax_scale=0.5, causal=True)
|
||||
query = torch.randn(1, 4, 2, 4)
|
||||
key_keep_mask = torch.tensor([[True, False, True, True]])
|
||||
|
||||
out = impl.forward_fa_npu(
|
||||
query,
|
||||
query,
|
||||
query,
|
||||
AttentionMetadata(attn_mask=key_keep_mask),
|
||||
)
|
||||
|
||||
fake_torch_npu.npu_fusion_attention.assert_called_once()
|
||||
kwargs = fake_torch_npu.npu_fusion_attention.call_args.kwargs
|
||||
assert kwargs["sparse_mode"] == 1
|
||||
assert kwargs["inner_precise"] == 2
|
||||
expected_block_mask = torch.tensor(
|
||||
[
|
||||
[
|
||||
[
|
||||
[False, True, True, True],
|
||||
[False, True, True, True],
|
||||
[False, True, False, True],
|
||||
[False, True, False, False],
|
||||
]
|
||||
]
|
||||
]
|
||||
)
|
||||
assert torch.equal(kwargs["atten_mask"], expected_block_mask)
|
||||
assert kwargs["atten_mask"].is_contiguous()
|
||||
assert out is expected_out
|
||||
|
||||
|
||||
def test_npu_noncausal_without_explicit_mask_stays_unmasked(monkeypatch):
|
||||
captured: dict = {}
|
||||
|
||||
def fake_attention_forward(query, key, value, **kwargs):
|
||||
captured.update(kwargs)
|
||||
return torch.zeros_like(query)
|
||||
|
||||
_fake_mindiesd(monkeypatch, attention_forward=fake_attention_forward)
|
||||
query = torch.randn(1, 4, 2, 4)
|
||||
|
||||
_npu_impl(causal=False).forward_fa_npu(query, query, query)
|
||||
|
||||
assert captured["attn_mask"] is None
|
||||
|
||||
|
||||
# --- Test group D: laser input pre-scaling in _forward_prefix_kv_slice_npu --
|
||||
|
||||
|
||||
def test_prefix_kv_slice_applies_laser_input_scaling(monkeypatch):
|
||||
|
||||
Reference in new issue
Block a user