chore: import upstream snapshot with attribution
Docker Image CI / build-ubuntu2004 (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:36:55 +08:00
commit c8a779b1bb
1887 changed files with 3245738 additions and 0 deletions
@@ -0,0 +1,183 @@
#
# SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import numpy as np
import onnx
import pytest
import tensorrt as trt
import torch
from polygraphy import mod, util
from polygraphy.datatype import DataType, DataTypeEntry
DATATYPES = DataType.__members__.values()
class TestDataType:
def compare_names(self, name, expected_name, replace_map):
# Names may not match up exactly, so use the replace_map to make adjustments to the
# foreign type before comparing against the Polygraphy type
for old, new in replace_map.items():
if name == old:
name = new
assert name == expected_name
@pytest.mark.parametrize("dtype", DATATYPES, ids=str)
def test_numpy(self, dtype):
if dtype in [
DataType.BFLOAT16,
DataType.FLOAT8E4M3FN,
DataType.FLOAT8E4M3FNUZ,
DataType.FLOAT8E5M2,
DataType.FLOAT8E5M2FNUZ,
DataType.INT4,
DataType.FLOAT4,
]:
pytest.xfail("Type not supported by NumPy")
np_type = dtype.numpy()
assert DataType.to_dtype(dtype, "numpy") == np_type
assert np_type.itemsize == dtype.itemsize
self.compare_names(np_type.name, dtype.name, {"str": "string"})
assert isinstance(np_type, np.dtype)
assert DataType.from_dtype(np_type) == dtype
@pytest.mark.parametrize("dtype", DATATYPES, ids=str)
def test_onnxrt(self, dtype):
if dtype in [
DataType.INT4,
DataType.FLOAT4,
]:
pytest.skip("Type not supported by ONNX-RT")
onnxrt_type = DataType.to_dtype(dtype, "onnxruntime")
assert dtype.onnxruntime() == onnxrt_type
assert isinstance(onnxrt_type, str)
self.compare_names(
onnxrt_type.replace("tensor(", "").replace(")", ""),
dtype.name,
{
"double": "float64",
"float": "float32",
},
)
assert DataType.from_dtype(onnxrt_type) == dtype
@pytest.mark.parametrize("dtype", DATATYPES, ids=str)
def test_onnx(self, dtype):
if dtype in [
DataType.INT4,
DataType.FLOAT4,
]:
pytest.skip("Type not supported by ONNX")
onnx_type = dtype.onnx()
assert DataType.to_dtype(dtype, "onnx") == onnx_type
assert isinstance(onnx_type, int)
onnx_type_map = util.invert_dict(dict(onnx.TensorProto.DataType.items()))
self.compare_names(
onnx_type_map[onnx_type].lower(),
dtype.name,
{
"double": "float64",
"float": "float32",
},
)
assert DataType.from_dtype(onnx_type) == dtype
@pytest.mark.skipif(
mod.version(trt.__version__) < mod.version("8.7"),
reason="Unsupported before TRT 8.7",
)
@pytest.mark.parametrize("dtype", DATATYPES, ids=str)
def test_tensorrt(self, dtype):
unsupported_types = [
DataType.FLOAT64,
DataType.INT16,
DataType.UINT16,
DataType.UINT32,
DataType.UINT64,
DataType.STRING,
DataType.INT64,
DataType.FLOAT8E4M3FNUZ,
DataType.FLOAT8E5M2,
DataType.FLOAT8E5M2FNUZ,
]
if mod.version(trt.__version__) < mod.version("10.8"):
unsupported_types.append(DataType.FLOAT4)
if dtype in unsupported_types:
pytest.xfail("Type not supported by TensorRT")
tensorrt_dtype = dtype.tensorrt()
assert DataType.to_dtype(dtype, "tensorrt") == tensorrt_dtype
assert isinstance(tensorrt_dtype, trt.DataType)
self.compare_names(
tensorrt_dtype.name.lower(),
dtype.name,
{
"double": "float64",
"float": "float32",
"half": "float16",
"fp8": "float8e4m3fn",
"fp4": "float4",
"bf16": "bfloat16",
},
)
assert DataType.from_dtype(tensorrt_dtype) == dtype
@pytest.mark.parametrize("trt_dtype", trt.DataType.__members__.values())
def test_all_tensorrt_types_supported(self, trt_dtype):
dtype = DataType.from_dtype(trt_dtype, "tensorrt")
assert isinstance(dtype, DataTypeEntry)
assert dtype.tensorrt() == trt_dtype
@pytest.mark.parametrize("dtype", DATATYPES, ids=str)
def test_torch(self, dtype):
if dtype in [
DataType.FLOAT8E4M3FN,
DataType.FLOAT8E4M3FNUZ,
DataType.FLOAT8E5M2,
DataType.FLOAT8E5M2FNUZ,
DataType.UINT16,
DataType.UINT32,
DataType.UINT64,
DataType.STRING,
DataType.INT4,
DataType.FLOAT4,
]:
pytest.xfail("Type not supported by Torch")
torch_type = dtype.torch()
assert DataType.to_dtype(dtype, "torch") == torch_type
assert isinstance(torch_type, torch.dtype)
self.compare_names(str(torch_type).replace("torch.", ""), dtype.name, {})
assert DataType.from_dtype(torch_type) == dtype
@@ -0,0 +1,70 @@
#
# SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
from polygraphy.common.interface import TypedDict, TypedList
from polygraphy.exception import PolygraphyException
@pytest.fixture()
def int_to_float():
class IntToFloat(TypedDict(lambda: int, lambda: float)):
pass
return IntToFloat()
class TestTypedDict:
def test_wrong_type_set_item_value(self, int_to_float):
with pytest.raises(PolygraphyException, match="Unsupported value type"):
int_to_float[0] = "hi"
def test_wrong_type_set_item_key(self, int_to_float):
with pytest.raises(PolygraphyException, match="Unsupported key type"):
int_to_float["hi"] = 1.0
def test_wrong_type_update(self, int_to_float):
with pytest.raises(PolygraphyException, match="Unsupported key type"):
int_to_float.update({"hi": 1.0})
@pytest.fixture()
def ints():
class Ints(TypedList(lambda: int)):
pass
return Ints()
class TestTypedList:
def test_wrong_type_append(self, ints):
with pytest.raises(PolygraphyException, match="Unsupported element type"):
ints.append(1.0)
def test_wrong_type_extend(self, ints):
with pytest.raises(PolygraphyException, match="Unsupported element type"):
ints.extend([0, 1, 2, 3, "surprise"])
def test_wrong_type_iadd(self, ints):
with pytest.raises(PolygraphyException, match="Unsupported element type"):
ints += [0, 1.0]
def test_wrong_type_setitem(self, ints):
ints.append(0)
with pytest.raises(PolygraphyException, match="Unsupported element type"):
ints[0] = 1.0
@@ -0,0 +1,36 @@
#
# SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from polygraphy.common import TensorMetadata
from polygraphy.datatype import DataType
class TestTensorMetadata:
def test_str(self):
meta = TensorMetadata().add("X", dtype=DataType.FLOAT32, shape=(64, 64))
assert str(meta) == "{X [dtype=float32, shape=(64, 64)]}"
def test_str_no_dtype(self):
meta = TensorMetadata().add("X", dtype=None, shape=(64, 64))
assert str(meta) == "{X [shape=(64, 64)]}"
def test_str_no_shape(self):
meta = TensorMetadata().add("X", dtype=DataType.FLOAT32, shape=None)
assert str(meta) == "{X [dtype=float32]}"
def test_str_no_meta(self):
meta = TensorMetadata().add("X", dtype=None, shape=None)
assert str(meta) == "{X}"