IInt8EntropyCalibrator

class tensorrt.IInt8EntropyCalibrator(self: tensorrt.tensorrt.IInt8EntropyCalibrator)

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

扩展了 IInt8Calibrator 类。

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

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

这是旧版熵校准器。它比旧版校准器更简单,并且产生更好的结果。

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

表示这是熵校准器。

返回值:

CalibrationAlgoType.ENTROPY_CALIBRATION

get_batch(self: tensorrt.tensorrt.IInt8EntropyCalibrator, 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 – 绑定数组中每个对象的网络输入名称。

返回值:

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

get_batch_size(self: tensorrt.tensorrt.IInt8Calibrator) int

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

返回值:

批次大小。

read_calibration_cache(self: tensorrt.tensorrt.IInt8EntropyCalibrator) 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.IInt8EntropyCalibrator, cache: buffer) None

保存校准缓存。

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

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

cache – 要写入的校准缓存。