类型转换 (Cast)

将给定的输入张量输出到输出张量。输入从一种精度转换为另一种精度。

允许支持的数据类型之间的所有转换。

属性 (Attributes)

toType 输出张量的数据类型 (DataType)。

输入 (Inputs)

input: T1 类型的张量。

输出 (Outputs)

output: T2 类型的张量。

数据类型 (Data Types)

T1: bool, uint8, int8, int32, int64, float16, bfloat16, float32

T2: bool, uint8, int8, int32, int64, float16, bfloat16, float32

形状信息 (Shape Information)

inputoutput 是形状为 \([a_0,...,a_n]\) 的张量

示例 (Examples)

类型转换 (Cast)
in1 = network.add_input("input1", dtype=trt.float32, shape=(1, 1, 3, 3))
layer = network.add_cast(in1, trt.int32)
network.mark_output(layer.get_output(0))

inputs[in1.name] = np.array(
    [
        [
            [
                [1.2, 2.3, 3.4],
                [4.5, 5.6, 6.7],
                [7.8, 8.9, 9.1],
            ]
        ]
    ]
)

outputs[layer.get_output(0).name] = layer.get_output(0).shape

expected[layer.get_output(0).name] = np.array(
    [
        [
            [
                [1.0, 2.0, 3.0],
                [4.0, 5.0, 6.0],
                [7.0, 8.0, 9.0],
            ]
        ]
    ]
)

C++ API

有关 C++ ICastLayer 算子的更多信息,请参阅 C++ ICastLayer 文档

Python API

有关 Python ICastLayer 算子的更多信息,请参阅 Python ICastLayer 文档