[Feature] Support batch invariance on ROCm (#52231)
Signed-off-by: Matthew Wong <Matthew.Wong2@amd.com> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Andreas Karatzas <akaratza@amd.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
Andreas Karatzas
parent
50b21a2667
commit
e0317b28ce
@@ -10,7 +10,11 @@ import pytest
|
||||
import torch
|
||||
from utils import skip_unsupported
|
||||
|
||||
from vllm.model_executor.determinism.batch_invariant import matmul_batch_invariant
|
||||
import vllm.model_executor.determinism.batch_invariant as bi
|
||||
from vllm.model_executor.determinism.batch_invariant import (
|
||||
enable_batch_invariant_mode,
|
||||
matmul_batch_invariant,
|
||||
)
|
||||
from vllm.model_executor.determinism.batch_invariant_configs import (
|
||||
_BATCH_INVARIANT_MATMUL_TUNED_CONFIGS,
|
||||
_get_tuned_matmul_arch_family,
|
||||
@@ -129,3 +133,49 @@ def test_matmul_batch_invariance_across_tuned_m_buckets(m, transpose_b):
|
||||
batch_output = matmul_batch_invariant(a, b)
|
||||
|
||||
assert torch.equal(single_output[0], batch_output[0])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def batch_invariant_mode():
|
||||
# The overrides are process-global: remove them so later tests compare
|
||||
# against the stock aten kernels again.
|
||||
enable_batch_invariant_mode()
|
||||
yield
|
||||
assert bi._batch_invariant_LIB is not None
|
||||
bi._batch_invariant_LIB._destroy()
|
||||
bi._batch_invariant_MODE = False
|
||||
|
||||
|
||||
@skip_unsupported
|
||||
@pytest.mark.usefixtures("batch_invariant_mode")
|
||||
@pytest.mark.parametrize("op", ["mm", "addmm", "bmm"])
|
||||
def test_compiled_matmul_reaches_batch_invariant_out_overloads(op):
|
||||
"""Inductor lowers mm/addmm/bmm to ``extern_kernels.<op>(..., out=buf)``,
|
||||
i.e. the ``.out`` overload, which eager never dispatches."""
|
||||
if (
|
||||
op != "bmm"
|
||||
and current_platform.is_cuda()
|
||||
and not current_platform.is_device_capability_family(80)
|
||||
):
|
||||
pytest.skip("mm/addmm are not overridden on SM90+")
|
||||
torch.manual_seed(0)
|
||||
dtype, k, n, needle = torch.bfloat16, 1024, 768, 37
|
||||
w = torch.randn(4, k, n, device=DEVICE_TYPE, dtype=dtype)
|
||||
bias = torch.randn(n, device=DEVICE_TYPE, dtype=dtype)
|
||||
fn = {
|
||||
"mm": lambda x: torch.mm(x[0], w[0]),
|
||||
# Bare addmm: a following pointwise op would make Inductor unfuse the bias.
|
||||
"addmm": lambda x: torch.addmm(bias, x[0], w[0]),
|
||||
"bmm": lambda x: torch.bmm(x, w),
|
||||
}[op]
|
||||
compiled = torch.compile(fn, fullgraph=True)
|
||||
x = torch.randn(4, 256, k, device=DEVICE_TYPE, dtype=dtype)
|
||||
row = None
|
||||
for m in (64, 256, 128):
|
||||
xs = x[:, :m].clone()
|
||||
# With dynamic=True, a specialization on M would silently recompile.
|
||||
torch._dynamo.mark_dynamic(xs, 1)
|
||||
out = compiled(xs)
|
||||
assert torch.equal(out, fn(xs)), f"compiled {op} bypassed the BI kernel (M={m})"
|
||||
row = out[..., needle, :] if row is None else row
|
||||
assert torch.equal(out[..., needle, :], row)
|
||||
|
||||
Reference in New Issue
Block a user