Skip to content

Commit 9cbbe1b

Browse files
authored
fix: fix the numpy related CI failure issue (#4535)
1 parent ef57f1b commit 9cbbe1b

4 files changed

Lines changed: 103 additions & 9 deletions

File tree

‎py/torch_tensorrt/dynamo/conversion/plugins/_generate_plugin.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Any, Callable, Tuple
77

88
import numpy as np
9-
import numpy.typing as npt
109
import sympy
1110
import torch
1211
from sympy import lambdify
@@ -55,10 +54,24 @@ def _safe_issubclass(cls: Any, classinfo: Any) -> bool:
5554
_patch_trt_validator_issubclass()
5655

5756

57+
def np_scalar_attr_annotation(dtype: Any) -> Any:
58+
# The annotation TRT expects for a scalar plugin attribute backed by a
59+
# 1-element numpy array. Deliberately *not* ``npt.NDArray[dtype]``: numpy
60+
# 2.5 redefined ``NDArray`` as a PEP 695 alias
61+
# (``type NDArray[ScalarT] = np.ndarray[_AnyShape, np.dtype[ScalarT]]``),
62+
# so ``typing.get_origin(npt.NDArray[np.float64])`` is the alias itself
63+
# rather than ``np.ndarray``. TRT's ``_is_npt_ndarray`` checks exactly that
64+
# origin, so on numpy >= 2.5 every scalar attribute is rejected with
65+
# "Attribute '<name>' of type NDArray[numpy.float64] is not a supported
66+
# serializable type." Spelling the alias' own expansion keeps the origin
67+
# ``np.ndarray`` on every numpy version.
68+
return np.ndarray[Any, np.dtype[dtype]]
69+
70+
5871
_TORCH_SCHEMA_TYPE_TO_PLUGIN_ATTR_TYPE = {
59-
"float": npt.NDArray[np.float64],
60-
"int": npt.NDArray[np.int64],
61-
"bool": npt.NDArray[np.bool_],
72+
"float": np_scalar_attr_annotation(np.float64),
73+
"int": np_scalar_attr_annotation(np.int64),
74+
"bool": np_scalar_attr_annotation(np.bool_),
6275
}
6376

6477

‎py/torch_tensorrt/kernels/_register.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@ def _register_aot_impl(op_name: str, ptx: bytes, spec: CudaPythonSpec) -> None:
147147
from typing import Tuple, Union # noqa: F401 – used in annotations dict
148148

149149
import numpy as np
150-
import numpy.typing as npt
151150
import tensorrt.plugin as trtp
152151

152+
from torch_tensorrt.dynamo.conversion.plugins._generate_plugin import (
153+
np_scalar_attr_annotation,
154+
)
155+
153156
ns, name = op_name.split("::")
154157
torch_op = getattr(getattr(torch.ops, ns), name)
155158
schema = torch_op._schemas[""]
@@ -163,11 +166,11 @@ def _register_aot_impl(op_name: str, ptx: bytes, spec: CudaPythonSpec) -> None:
163166
else:
164167
attr_arg_names.append(arg.name)
165168
if arg.type.isSubtypeOf(torch._C.FloatType.get()):
166-
attr_annotations[arg.name] = npt.NDArray[np.float64]
169+
attr_annotations[arg.name] = np_scalar_attr_annotation(np.float64)
167170
elif arg.type.isSubtypeOf(torch._C.IntType.get()):
168-
attr_annotations[arg.name] = npt.NDArray[np.int64]
171+
attr_annotations[arg.name] = np_scalar_attr_annotation(np.int64)
169172
elif arg.type.isSubtypeOf(torch._C.BoolType.get()):
170-
attr_annotations[arg.name] = npt.NDArray[np.bool_]
173+
attr_annotations[arg.name] = np_scalar_attr_annotation(np.bool_)
171174
elif arg.type.isSubtypeOf(torch._C.StringType.get()):
172175
attr_annotations[arg.name] = str
173176
else:

‎tests/ci/suites.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def for_variant(self, variant: Variant) -> dict[str, Any]:
266266
platforms=("linux-x86_64",),
267267
),
268268
Suite(
269-
# Standard: the automatic-plugin pair. RTX: the whole automatic_plugin
269+
# Standard: the automatic-plugin trio. RTX: the whole automatic_plugin
270270
# directory, except the Linux-only FlashInfer test below.
271271
# (The redundant conversion/ re-run from the old l2_plugin is dropped.)
272272
"plugins-automatic",
@@ -276,6 +276,7 @@ def for_variant(self, variant: Variant) -> dict[str, Any]:
276276
paths=(
277277
"automatic_plugin/test_automatic_plugin.py",
278278
"automatic_plugin/test_automatic_plugin_with_attrs.py",
279+
"automatic_plugin/test_plugin_attr_annotations.py",
279280
),
280281
overrides={
281282
"rtx": {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Regression tests for the annotations we hand TRT for scalar plugin attributes.
2+
3+
numpy 2.5 redefined ``npt.NDArray`` as a PEP 695 alias, which made
4+
``typing.get_origin(npt.NDArray[np.float64])`` return the alias instead of
5+
``np.ndarray``. TRT's plugin validator keys off that origin, so every plugin
6+
with a scalar attribute started failing with
7+
8+
ValueError: Attribute 'alpha' of type NDArray[numpy.float64] is not a
9+
supported serializable type.
10+
11+
These tests are CPU-only and version-agnostic: they pin the property TRT
12+
actually relies on rather than the spelling of the annotation.
13+
"""
14+
15+
import typing
16+
import unittest
17+
18+
import numpy as np
19+
import pytest
20+
from torch.testing._internal.common_utils import run_tests
21+
22+
from torch_tensorrt.dynamo.conversion.plugins._generate_plugin import (
23+
_TORCH_SCHEMA_TYPE_TO_PLUGIN_ATTR_TYPE,
24+
np_scalar_attr_annotation,
25+
)
26+
27+
_DTYPES = (np.float64, np.int64, np.bool_)
28+
29+
30+
class TestScalarAttrAnnotation(unittest.TestCase):
31+
def test_origin_is_ndarray(self):
32+
"""TRT's ``_is_npt_ndarray`` accepts an annotation only if its origin is
33+
``np.ndarray`` — via ``typing.get_origin`` or ``__origin__``."""
34+
for dtype in _DTYPES:
35+
with self.subTest(dtype=dtype):
36+
ann = np_scalar_attr_annotation(dtype)
37+
self.assertIs(typing.get_origin(ann), np.ndarray)
38+
self.assertIs(getattr(ann, "__origin__", None), np.ndarray)
39+
40+
def test_dtype_is_recoverable(self):
41+
"""TRT's ``_infer_numpy_type`` digs the dtype out of the second arg."""
42+
for dtype in _DTYPES:
43+
with self.subTest(dtype=dtype):
44+
args = typing.get_args(np_scalar_attr_annotation(dtype))
45+
self.assertEqual(len(args), 2)
46+
self.assertEqual(typing.get_args(args[1])[0], dtype)
47+
48+
def test_schema_type_map_uses_it(self):
49+
"""The map the plugin generator reads must go through the same helper."""
50+
self.assertEqual(
51+
set(_TORCH_SCHEMA_TYPE_TO_PLUGIN_ATTR_TYPE), {"float", "int", "bool"}
52+
)
53+
for annotation in _TORCH_SCHEMA_TYPE_TO_PLUGIN_ATTR_TYPE.values():
54+
self.assertIs(typing.get_origin(annotation), np.ndarray)
55+
56+
def test_trt_validator_accepts_it(self):
57+
"""End of the chain: TRT's own helpers must accept what we emit."""
58+
utils = pytest.importorskip(
59+
"tensorrt_bindings.plugin._utils",
60+
reason="TensorRT plugin bindings not available",
61+
)
62+
validate = pytest.importorskip(
63+
"tensorrt_bindings.plugin._validate",
64+
reason="TensorRT plugin bindings not available",
65+
)
66+
for dtype in _DTYPES:
67+
with self.subTest(dtype=dtype):
68+
ann = np_scalar_attr_annotation(dtype)
69+
self.assertTrue(utils._is_numpy_array(ann))
70+
self.assertTrue(utils._is_npt_ndarray(ann))
71+
self.assertIn(
72+
utils._infer_numpy_type(ann), validate.SERIALIZABLE_NP_DTYPES
73+
)
74+
75+
76+
if __name__ == "__main__":
77+
run_tests()

0 commit comments

Comments
 (0)