English
File size: 3,717 Bytes
76de374
 
a542584
76de374
 
a542584
76de374
a542584
 
 
 
 
 
 
 
 
76de374
 
 
 
 
 
 
 
a542584
76de374
 
2a4b0d9
76de374
2a4b0d9
 
 
76de374
 
a542584
76de374
a542584
76de374
ef174fa
76de374
2a4b0d9
 
 
 
 
76de374
2a4b0d9
76de374
 
2a4b0d9
 
 
76de374
 
a542584
76de374
 
 
a542584
76de374
 
 
a542584
76de374
 
2a4b0d9
 
 
76de374
 
 
 
 
 
a542584
0988d89
 
 
 
 
 
 
 
 
 
 
 
 
 
a542584
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 混元-DiT TensorRT 加速

语言: [**English**](https://huggingface.co/Tencent-Hunyuan/TensorRT-libs/blob/main/README.md) | **中文**

我们提供了将 [混元-DiT](https://github.com/Tencent/HunyuanDiT) 中的文生图模型转换为 TensorRT 的代码和相关依赖用于推理加速
(比 Flash Attention 更快). 您可以使用以下步骤使用我们 TensorRT 模型, 基于 **TensorRT-9.2.0.5****cuda (11.7 或 11.8)**.

> ⚠️ **重要提醒 (关于TensorRT加速版本测试的建议)**:   
> 我们建议用户在 Compute Capability>=8.0 的 NVIDIA 显卡上测试 TensorRT 版本, (例如, RTX4090, RTX3090, H800, A10/A100/A800 等)
> 您可以从[这里](https://developer.nvidia.com/cuda-gpus#compute)查询到您显卡对应的 Compute Capability。
> 对于 Compute Capability<8.0 的 NVIDIA 显卡,如果您要尝试 TensorRT 版本,有可能遭遇 TensorRT Engine 文件无法生成的错误或推理性能较差
> 的问题,主要原因在于 TensorRT 没有在该架构上支持 fused mha kernel。

## 🛠 构建步骤

### 1. 从 Huggingface 下载 TensorRT 的依赖文件

```shell

cd HunyuanDiT



# Download the dependencies

huggingface-cli download Tencent-Hunyuan/TensorRT-libs --local-dir ./ckpts/t2i/model_trt

```

### 2. 安装 TensorRT 依赖

```shell

# 使用下面的命令解压并安装 TensorRT 依赖

sh trt/install.sh



# 设置使用或构建 TensorRT 相关的环境变量,我们提供了一个脚本来一键设置

source trt/activate.sh

```

### 3. 构建 TensorRT engine

#### 方法1: 使用预构建的 engine

我们提供了一些预构建的 [TensorRT Engines](https://huggingface.co/Tencent-Hunyuan/TensorRT-engine), 需要从 Huggingface 下载.

|     支持的 GPU      |               远程地址                |
|:----------------:|:---------------------------------:|
| GeForce RTX 3090 | `engines/RTX3090/model_onnx.plan` |
| GeForce RTX 4090 | `engines/RTX4090/model_onnx.plan` |
|       A100       |  `engines/A100/model_onnx.plan`   |

可以使用以下命令下载并放置在指定的位置. *注意: 请将 `<远程地址>` 替换为上表中对应 GPU 的远程地址.*

```shell

export REMOTE_PATH=<远程地址>

huggingface-cli download Tencent-Hunyuan/TensorRT-engine ${REMOTE_PATH} ./ckpts/t2i/model_trt/engine/

ln -s ${REMOTE_PATH} ./ckpts/t2i/model_trt/engine/model_onnx.plan

```

#### 方法2: 自行构建 engine
如果您使用不同于上面表格中的 GPU, 可以使用以下命令构建适配于当前 GPU 的 engine.

```shell

# 构建 TensorRT engine. 默认会读取当前目录下的 ckpts 文件夹

sh trt/build_engine.sh

```

### 4. 使用 TensorRT 模型进行推理.

```shell

# 重要: 如果您在使用 TensorRT 模型之前没有设置环境变量, 请使用以下命令设置

source trt/activate.sh



# 使用 prompt 强化 + 文生图 TensorRT 模型进行推理

python sample_t2i.py --prompt "渔舟唱晚" --infer-mode trt



# 关闭 prompt 强化 (可以在显存不足时使用)

python sample_t2i.py --prompt "渔舟唱晚" --infer-mode trt --no-enhance

```

### 5. 提示

为了性能考虑, 我们限制 TensorRT engine 只支持以下输入的形状. 未来我们会验证并尝试支持任意的形状.

```python

STANDARD_SHAPE = [

    [(768, 768), (1024, 1024), (1280, 1280)],   # 1:1

    [(1024, 768), (1152, 864), (1280, 960)],    # 4:3

    [(768, 1024), (864, 1152), (960, 1280)],    # 3:4

    [(1280, 768)],                              # 16:9

    [(768, 1280)],                              # 9:16

]

```

## ❓ Q&A

参考 [Q&A](./QA.md) 获取更多关于构建 TensorRT Engine 的问题解答.