IInt8Calibrator

tensorrt.CalibrationAlgoType

要使用的校准算法版本。

成员

LEGACY_CALIBRATION

ENTROPY_CALIBRATION

ENTROPY_CALIBRATION_2

MINMAX_CALIBRATION

class tensorrt.IInt8Calibrator(self: tensorrt.tensorrt.IInt8Calibrator)

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

应用程序实现的校准接口。校准是构建器在决定 8 位推理的合适比例因子时执行的步骤。它还必须提供一种方法来检索代表性图像,校准过程可以使用这些图像来检查激活分布。它可以选择性地实现一种方法,用于缓存校准结果,以便在后续运行中重用。

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

class MyCalibrator(trt.IInt8Calibrator):
    def __init__(self):
        trt.IInt8Calibrator.__init__(self)
变量:
  • batch_sizeint 用于校准批次的批次大小。

  • algorithmCalibrationAlgoType 此校准器使用的算法。

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

获取此校准器使用的算法。

返回值:

此校准器使用的算法。

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

保存校准缓存。

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

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

cache – 要写入的校准缓存。