常量¶
基于输入值生成输出张量。
属性¶
shape
:输出张量的形状。
weights
:类型为 T
的权重。必须与输出张量中的元素数量匹配。
输出¶
output:类型为 T
的张量。
数据类型¶
T:bool
、int4
、int8
、int32
、int64
、float8
、float16
、float32
、bfloat16
形状信息¶
output 是一个形状为 shape
的张量。
DLA 支持¶
仅当此算子作为第二个输入连接到 PReLU 算子时,DLA 才支持它。
示例¶
常量
input_shape = [1, 3, 3, 3]
w = np.arange(0.0, 27.0, dtype=np.dtype("f4"))
layer = network.add_constant(shape=input_shape, weights=trt.Weights(w))
network.mark_output(layer.get_output(0))
outputs[layer.get_output(0).name] = layer.get_output(0).shape
expected[layer.get_output(0).name] = w.reshape(input_shape)
常量 Int4 权重
def pack_int4(array: np.ndarray):
result = []
array = array.flatten()
for low, high in zip(array[::2], array[1::2]):
low = np.rint(np.clip(low, -8, 7)).astype(np.int8)
high = np.rint(np.clip(high, -8, 7)).astype(np.int8)
result.append(high << 4 | low & 0x0F)
return np.asarray(result, dtype=np.int8)
w = np.array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[-1, -2, -3, -4, -5, -6, -7, -8],
[ 7, 6, 5, 4, 3, 2, 1, 0],
[-7, -6, -5, -4, -3, -2, -1, 0]], dtype=np.int8)
w_packed = pack_int4(w)
weights = network.add_constant(shape=w.shape, weights=trt.Weights(trt.int4, w_packed.ctypes.data, w.size))
# Quantized weights must be followed by a DQ node
scale = network.add_constant(shape=(), weights=np.ones(shape=(1), dtype=np.float32))
dequantize = network.add_dequantize(weights.get_output(0), scale.get_output(0), trt.float32)
dequantize.precision = trt.int4
network.mark_output(dequantize.get_output(0))
outputs[dequantize.get_output(0).name] = dequantize.get_output(0).shape
expected[dequantize.get_output(0).name] = w
C++ API¶
有关 C++ IConstantLayer 算子的更多信息,请参阅 C++ IConstantLayer 文档。
Python API¶
有关 Python IConstantLayer 算子的更多信息,请参阅 Python IConstantLayer 文档。