IInt8MinMaxCalibrator

class tensorrt.IInt8MinMaxCalibrator(self: tensorrt.tensorrt.IInt8MinMaxCalibrator)

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

扩展了 IInt8Calibrator 类。

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

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

这是所有后端 NLP 任务的首选校准器。它支持每个激活张量的缩放。

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

表示这是 minmax 校准器。

返回:

CalibrationAlgoType.MINMAX_CALIBRATION

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

保存校准缓存。

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

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

cache – 要写入的校准缓存。