tensorrt 分割_TensorRT实战(三)——如何处理Dynamic Shapes

本文归纳自英伟达TensorRT-7.2.1开发指南,感兴趣的朋友可以看下 TensorRT Developer Guide 的 Working With Dynamic Shapes 相关内容.

steps for building an engine with dynamic shapes

网络定义 (network definition) 必须是显式 Batch 定义

IBuilder::createNetworkV2(1U << static_cast(NetworkDefinitionCreationFlag::kEXPLICIT_BATCH))

使用 -1 来表示实际运行时确定的输入 tensor 维度。Specify each runtime dimension of an input tensor by using -1 as a placeholder for the dimension.

在构建时 (build time) 需要指定一个或多个优化配置 (optimization profiles) 来指定运行时输入的 batch 范围以及自动校准时的 batch

To use engine (如果想要改变运行的维度,只需要更改下面的(b)和(c)即可)

(a) 创建 (Create) 不含动态 batch 的 execution context

(b) 具化 (Specify) 使用步骤3 中的某一个 optimization profile

(c) 具化 (Specify) execution context 的输入维度

(d) 入队工作 (Enqueue work)

Specifying Runtime Dimensions

构建网络时,使用 -1 来表示实际运行时确定的维度:

networkDefinition.addInput("foo", DataType::kFLOAT, Dims3(3, -1, -1))

实际运行时,在选择特定 optimization profile 后必须指定输入的维度

context.setBindingDimensions(0, Dims3(3, 150, 250))

在运行时,通过 engine 获取的 binding dimensions 与 build 网络时相同,即为含有 -1 的

engine.getBindingDimensions(0)

returns a Dims with dimensions {3, -1, -1}

想要获取实际维度,则查询实际运行的上下文 query the execution context:

context.getBindingDimensions(0)

returns a Dims with dimensions {3, 150, 250}

Optimization Profiles

优化配置用于指定网络输入维度的范围以及默认最优维度。首先,创建一个 IOptimizationProfile 类的对象,接着设置最小、最优、最大维度,最后将其添加到网络配置中。

IOptimizationProfile* profile = builder.createOptimizationProfile();

profile->setDimensions("foo", OptProfileSelector::kMIN, Dims3(3,100,200);

profile->setDimensions("foo", OptProfileSelector::kOPT, Dims3(3,150,250);

profile->setDimensions("foo", OptProfileSelector::kMAX, Dims3(3,200,300);

config->addOptimizationProfile(profile)

运行时在设置输入维度之前,需要选择一个 optimization profile。这些 profile 的编号根据我们添加他们的顺序从 0 开始编号。

context.setOptimizationProfile(0)

setOptimizationProfile 可以在不同 profile 之间进行切换 (It must be called after any enqueue() or enqueueV2() operations finish in the current context.)

INT8 Calibration With Dynamic Shapes

Calibration 校准是使用 optimization profile 中的 KOPT 值进行的,校准数据集的 size 也必须和 profile 中的相匹配。

config->setCalibrationProfile(profile)

而 kMIN、kMIN 的值都会被 看OPT覆盖。

你可能感兴趣的:(tensorrt,分割)