Skip to content

Commit a3026c9

Browse files
[None][test] AutoDeploy: Fix standalone linear simple on B200 (#15675)
Signed-off-by: gramnarayan <105831528+govind-ramnarayan@users.noreply.github.com>
1 parent c4b9fcb commit a3026c9

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

tensorrt_llm/_torch/auto_deploy/custom_ops/linear/linear.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515

1616
"""Custom ops for linear layers."""
1717

18-
from typing import List, Optional
18+
from typing import Callable, List, Optional
1919

2020
import torch
2121

2222
from ..._compat import get_sm_version
2323

2424

25+
def _get_trtllm_cublas_mm() -> Optional[Callable[..., torch.Tensor]]:
26+
"""Return TRT-LLM's cublas_mm op when it is registered in this process."""
27+
try:
28+
return torch.ops.trtllm.cublas_mm
29+
except (AttributeError, RuntimeError):
30+
return None
31+
32+
2533
@torch.library.custom_op("auto_deploy::torch_linear_simple", mutates_args=())
2634
def simple(
2735
input: torch.Tensor,
@@ -72,11 +80,22 @@ def simple(
7280
# split-K + reduce + zero-fill for small-M (decode) projection GEMMs.
7381
# (Same trick PT introduced for GPT-OSS via use_custom_cublas_mm in
7482
# modeling_gpt_oss.py; we apply it model-agnostically based on dtype + SM.)
75-
if get_sm_version() >= 100 and input.dtype == torch.bfloat16 and weight.dtype == torch.bfloat16:
83+
if (
84+
input.is_cuda
85+
and weight.is_cuda
86+
and (bias is None or bias.is_cuda)
87+
and input.dtype == torch.bfloat16
88+
and weight.dtype == torch.bfloat16
89+
and get_sm_version() >= 100
90+
):
91+
trtllm_cublas_mm = _get_trtllm_cublas_mm()
92+
if trtllm_cublas_mm is None:
93+
return torch.ops.aten.linear(input, weight, bias)
94+
7695
# cublas_mm requires 2D mat_a/mat_b. Flatten leading dims and unflatten on exit.
7796
in_shape = input.shape
7897
input_2d = input.reshape(-1, in_shape[-1])
79-
out_2d = torch.ops.trtllm.cublas_mm(
98+
out_2d = trtllm_cublas_mm(
8099
input_2d,
81100
weight.t(),
82101
bias,

tests/unittest/auto_deploy/standalone/test_standalone_package.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,40 @@ def test_ops_registered(self, standalone_package):
141141
)
142142
assert result.returncode == 0, f"stdout: {result.stdout}\nstderr: {result.stderr}"
143143

144+
def test_linear_simple_works_in_standalone_on_b200(self, standalone_package):
145+
"""The standalone linear op must work on the B200 bf16 path."""
146+
result = _run_isolated(
147+
standalone_package,
148+
"""
149+
from unittest import mock
150+
151+
import torch
152+
import llmc
153+
from llmc.custom_ops.linear import linear
154+
155+
if torch.cuda.is_available():
156+
device = "cuda"
157+
dtype = torch.bfloat16
158+
else:
159+
device = "cpu"
160+
dtype = torch.float32
161+
162+
x = torch.randn(2, 3, 5, device=device, dtype=dtype)
163+
weight = torch.randn(7, 5, device=device, dtype=dtype)
164+
bias = torch.randn(7, device=device, dtype=dtype)
165+
166+
# Force the Blackwell bf16 branch even when this standalone test
167+
# runs on H100 CI instead of B200 CI.
168+
with mock.patch.object(linear, "get_sm_version", return_value=100):
169+
out = torch.ops.auto_deploy.torch_linear_simple(x, weight, bias)
170+
171+
ref = torch.ops.aten.linear(x, weight, bias)
172+
torch.testing.assert_close(out, ref)
173+
print("OK")
174+
""",
175+
)
176+
assert result.returncode == 0, f"stdout: {result.stdout}\nstderr: {result.stderr}"
177+
144178
def test_compat_types(self, standalone_package):
145179
"""Standalone _compat types should work."""
146180
result = _run_isolated(

0 commit comments

Comments
 (0)