|
| 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