# TensorRT-LLM

[TensorRT-LLM](https://github.com/NVIDIA/TensorRT-LLM) 可优化 NVIDIA GPU 上的 LLM 推理。它将模型编译为 TensorRT 引擎，支持 in-flight batching、paged KV caching 和 tensor parallelism。[AutoDeploy](https://nvidia.github.io/TensorRT-LLM/torch/auto_deploy/auto-deploy.html) 可直接接受 Transformers 模型，无需任何修改。它会自动将模型转换为优化后的运行时。

将来自 Hub 的模型 ID 传递给 `build_and_run_ad.py`，以运行 Transformers 模型。

```bash
cd examples/auto_deploy
python build_and_run_ad.py --model meta-llama/Llama-3.2-1B
```

Under the hood, AutoDeploy creates an [LLM](https://nvidia.github.io/TensorRT-LLM/llm-api/reference.html#tensorrt_llm.llmapi.LLM) class. It loads the model configuration with [AutoConfig.from_pretrained()](/docs/transformers/main/zh/model_doc/auto#transformers.AutoConfig.from_pretrained) and extracts any parallelism metadata stored in `tp_plan`. [AutoModelForCausalLM.from_pretrained()](/docs/transformers/main/zh/model_doc/auto#transformers.AutoModel.from_pretrained) loads the model with the config and enables Transformers' built-in tensor parallelism.

```py
from tensorrt_llm._torch.auto_deploy import LLM

llm = LLM(model="meta-llama/Llama-3.2-1B")
```

TensorRT-LLM 使用 `torch.export` 提取模型图并应用优化。它将 Transformers 的注意力机制替换为 TensorRT-LLM 的 [注意力内核](https://github.com/NVIDIA/TensorRT-LLM/tree/main/tensorrt_llm/_torch/attention_backend)，并将模型编译为优化后的执行后端。

## 资源

- 有关更详细的使用指南，请参阅 [TensorRT-LLM 文档](https://nvidia.github.io/TensorRT-LLM/)。
- [AutoDeploy 指南](https://nvidia.github.io/TensorRT-LLM/torch/auto_deploy/auto-deploy.html)通过高级示例介绍了其工作原理。

