TensorRT(1)--8-bit Inference

本文介绍了使用TensorRT将FP32精度的CNN网络转换为INT8精度的过程,包括通过矫正数据集确定阈值和scale系数的方法,以及如何在不显著损失准确性的情况下提高运算效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TensorRT Build Phase

TensorRT需要三个文件去部署神经网络,其分别为:

  1. 网络结构文件(deploy.prototxt)
  2. 训练好的网络权值(net.caffemodel)
  3. 每一个输出类别对应的标签文件

8-bit Inference with TensorRT

基本介绍

在其他条件相同的情况下,使用8-bit的数据格式来进行网络权值以及激活值的表示,随着batch_size的不同,其运算速度可以快两到三倍,同时其精度也没有明显的损失。因此而言,这种数据模型之后是很有前景的。

  • 目标: 在没有明显准确度丢失的情况下将FP32的CNNs网络转换为INT8.(Goal: Convert FP32 CNNs into INT8 without significant accuracy loss.).
  • 理由: INT8类型的存储方式有很高的throughout和较低的内存需求.(Why: INT8 math has higher throughout, and lower memory requirement.).
  • 挑战: 相对于FP32, INT8有明显较低的精度和动态范围.(Challenge: INT8 has significantly lower precision and dynamic range that FP32.).
  • 解决方式: 在将权值以及计算时最小化有效信息损失.(Solution: Minimize loss of information when quantizing model weights to INT8 and during INT8 computation of activations.).
  • 结果: 上述转换可以通过TensorRT来进行实现,同时该方法不需要额外的fine tuning和retraining.(Result: Mathod was implemented in TensorRT. It does not require any additional fine tuning or retraining.).

上述转换方法实现的基本思路是首先构建一种FP32数据向INT8数据的映射关系,该映射中的边界并不是两种数据类型的最大值,而是将FP32设置成一个Threshold,将这个阈值与INT8的最大值(127)构建映射关系,具体实现形式是通过一个scale来进行对应。

首先的问题是,这种映射关系的阈值如何寻找呢?不同的网路显然这一阈值是不同的,因此我们需要一个矫正数据集(calibration dataset)来进行scale的选取,其选择的标准为最小化KL_divergence(: KL_divergence(P,Q):= SUM(P[i] * log(P[i] / Q[i] ), i))

其次的问题是scale的表示与获取。scale的表示相对而言较为简单,如下形式:A = scale_A * QA, 。然后在TensorRT工作流运行时运行矫正算法,来优化scale系数。

矫正(Calibration)

首先,我们在矫正数据集上运行FP32精度的inference, 对于网络的每一层来说,收集激活时的直方图,从而产生不同saturation thresholds对应的量化分布图,然后选取能够最小化KL_divergence(ref_distr, quant_distr)的阈值作为选择的阈值。

TensorRT典型的工作流

在进行INT8的TensorRT inference之前,我们需要训练好了的FP32精度的模型以及矫正数据集(主要是选择阈值以及scale)。

实施的基本步骤是:

  1. 在矫正数据集上运行FP32 Inference
  2. 收集需要的数据(不同阈值下的KL量化分布图)
  3. 运行矫正算法–> 优化scale系数
  4. 量化FP32权值到INT8
  5. 产生CalibrationTable和INT8 execution engine

关于INT8 Inference的源代码在后续进行分析。

参考资料 : 8-bit Inference With TensorRT

6. How to Run Locally DeepSeek-V3 can be deployed locally using the following hardware and open-source community software: DeepSeek-Infer Demo: We provide a simple and lightweight demo for FP8 and BF16 inference. SGLang: Fully support the DeepSeek-V3 model in both BF16 and FP8 inference modes, with Multi-Token Prediction coming soon. LMDeploy: Enables efficient FP8 and BF16 inference for local and cloud deployment. TensorRT-LLM: Currently supports BF16 inference and INT4/8 quantization, with FP8 support coming soon. vLLM: Support DeepSeek-V3 model with FP8 and BF16 modes for tensor parallelism and pipeline parallelism. AMD GPU: Enables running the DeepSeek-V3 model on AMD GPUs via SGLang in both BF16 and FP8 modes. Huawei Ascend NPU: Supports running DeepSeek-V3 on Huawei Ascend devices. Since FP8 training is natively adopted in our framework, we only provide FP8 weights. If you require BF16 weights for experimentation, you can use the provided conversion script to perform the transformation. Here is an example of converting FP8 weights to BF16: cd inference python fp8_cast_bf16.py --input-fp8-hf-path /path/to/fp8_weights --output-bf16-hf-path /path/to/bf16_weights 回答二、软件环境搭建 推理框架选择(需通过pip安装): 原生FP8推理:使用SGLang框架 Bash pip install sglang BF16/FP8混合支持:使用LMDeploy Bash pip install lmdeploy TensorRT加速:安装TensorRT-LLM Bash git clone https://github.com/NVIDIA/TensorRT-LLM.git && cd TensorRT-LLM && pip install -e . 模型权重获取: Bash huggingface-cli download DeepSeek/DeepSeek-V3-671B-FP8 --include "*.bin" --local-dir ./deepseek-weights 三、FP8到BF16权重转换 运行官方转换脚本(需从Hugging Face仓库获取): Bash python convert_fp8_to_bf16.py \ --input_dir ./deepseek-weights \ --output_dir ./bf16-weights \ --quant_bit 8 此脚本会将原始FP8权重转换为BF16格式,同时保留模型结构配置文件1。第三步没看懂,具体操作是什么
03-13
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值