SuperAPI AI 工具集

trt-strong-typing-migration

编程与开发

查看项目

更新于 10-16 02:34

技能介绍

用途:把弱类型 TensorRT 构建流程迁移为强类型等价流程。强类型是 TensorRT 11.x 推荐模式;弱类型在 10.12 弃用并计划移除。11.x 强制强类型,kSTRONGLY_TYPED 标志被接受但忽略,trtexec --stronglyTyped 为默认开启的空操作;10.12–10.x 需显式启用。设置该标志总是安全的。

主要能力:

  1. ONNX 源预处理:FP32 模型需用 ModelOpt AutoCast 在图内声明精度,例如 python -m modelopt.onnx.autocast --onnx_path model.fp32.onnx --output_path model.mixed.onnx --low_precision_type fp16 --calibration_data calib.npz;INT8/FP8 用 modelopt.onnx.quantization 生成带显式 Q/DQ 的 ONNX。转换后先在 ONNX Runtime 验证,与 FP32 模型按 rtol/atol=5e-3 比对,不一致则停止。
  2. Python 构建器:create_network 改传 STRONGLY_TYPED;删除 set_flag(FP16/BF16/INT8) 与 layer.precision、set_output_type,改用 network.add_cast 或 Q/DQ。kTF32 保留。可用 scripts/migrate.py 改写 AST,默认 dry-run,--write 才落盘。
  3. trtexec:删除 --fp16/--bf16/--int8/--best/--precisionConstraints/--layerPrecisions/--layerOutputTypes;--stronglyTyped 在 11.x 可省,10.12–10.x 必需;--noTF32 保留。
  4. C++ 构建器:createNetworkV2 用 kSTRONGLY_TYPED;删除 kFP16/kBF16/kINT8 与精度约束标志、setPrecision/setOutputType,改用 addCast 或 Q/DQ;kTF32 保留。setToType() 仅适用于 cast/quantize/dequantize/fill 层。

使用方式:先确认 TensorRT ≥10.12 并捕获弱类型基线输出,再按入口点迁移、重建引擎、与基线比对。

限制:10.11 及更早不支持强类型;迁移后输出偏差超容差应改用 trt-quantization-accuracy;全新 ONNX 无现成构建流程应先走 trt-onnx-quickstart;不要同时传 --int8/--fp16 与 --stronglyTyped。