IInt8EntropyCalibrator2

class tensorrt.IInt8EntropyCalibrator2(self: tensorrt.tensorrt.IInt8EntropyCalibrator2)

[已弃用] 在 TensorRT 10.1 中已弃用。已被显式量化取代。

扩展了 IInt8Calibrator 类。

要实现自定义校准器,请确保在 __init__() 中显式实例化基类。

class MyCalibrator(trt.IInt8EntropyCalibrator2):
    def __init__(self):
        trt.IInt8EntropyCalibrator2.__init__(self)

这是首选的校准器。这是 DLA 必需的校准器,因为它支持每个激活张量的缩放。

get_algorithm(self: tensorrt.tensorrt.IInt8EntropyCalibrator2) tensorrt.tensorrt.CalibrationAlgoType

表示这是熵校准器 2。

返回值:

CalibrationAlgoType.ENTROPY_CALIBRATION_2

get_batch(self: tensorrt.tensorrt.IInt8EntropyCalibrator2, names: List[str]) List[int]

获取用于校准的输入批次。输入批次的大小必须与 get_batch_size() 返回的批次大小相匹配。

一个可能的实现可能如下所示

def get_batch(names):
    try:
        # Assume self.batches is a generator that provides batch data.
        data = next(self.batches)
        # Assume that self.device_input is a device buffer allocated by the constructor.
        cuda.memcpy_htod(self.device_input, data)
        return [int(self.device_input)]
    except StopIteration:
        # When we're out of batches, we return either [] or None.
        # This signals to TensorRT that there is no calibration data remaining.
        return None
参数:

names – 绑定数组中每个对象的网络输入名称。

返回值:

一个 list 设备内存指针,设置为包含每个网络输入数据的内存,或者如果不再有用于校准的批次,则为空 list。 你可以使用 pycuda 分配这些设备缓冲区,例如,然后将它们强制转换为 int 以检索指针。

get_batch_size(self: tensorrt.tensorrt.IInt8Calibrator) int

获取用于校准批次的批次大小。

返回值:

批次大小。

read_calibration_cache(self: tensorrt.tensorrt.IInt8EntropyCalibrator2) buffer

加载校准缓存。

校准可能很耗时,因此生成一次校准数据,然后在后续网络构建中使用它可能很有用。缓存包括用于生成它的回归截止值和分位数,如果这些值与当前校准器的设置不匹配,则不会使用该缓存。但是,如果网络结构发生变化或输入数据集发生变化,也应重新校准网络,应用程序有责任确保这一点。

读取缓存就像在 Python 中读取任何其他文件一样。例如,一种可能的实现是

def read_calibration_cache(self):
    # If there is a cache, use it instead of calibrating again. Otherwise, implicitly return None.
    if os.path.exists(self.cache_file):
        with open(self.cache_file, "rb") as f:
            return f.read()
返回值:

缓存对象;如果没有数据,则为 None。

write_calibration_cache(self: tensorrt.tensorrt.IInt8EntropyCalibrator2, cache: buffer) None

保存校准缓存。

写入缓存就像在 Python 中写入任何其他缓冲区一样。例如,一种可能的实现是

def write_calibration_cache(self, cache):
    with open(self.cache_file, "wb") as f:
        f.write(cache)
参数:

cache – 要写入的校准缓存。