LLM大模型学习:LLM大模型推理加速llm推理框架 TensorRT-LLM

TensorRT-LLM

TensorRT-LLM由nvidia发布。

TensorRT-LLM 为用户提供了易于使用的 Python API 来定义大型语言模型 (LLM) 并构建 包含最先进优化的[TensorRT引擎,以便在 NVIDIA GPU 上高效地执行推理。]

pip3 install tensorrt_llm -U --pre --extra-index-url https://pypi.nvidia.com
python3 -c "import tensorrt_llm"

安装(建议直接使用docker)
  1. 先安装tensort

    官网连接:docs.nvidia.com/deeplearnin…

sudo dpkg -i nv-tensorrt-local-repo-ubuntu2204-8.6.1-cuda-12.0_1.0-1_amd64.deb
sudo cp /var/nv-tensorrt-local-repo-ubuntu2204-8.6.1-cuda-12.0/nv-tensorrt-local-42B2FC56-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get install tensorrt

  1. 安装tensort llm

    pip3 install tensorrt_llm -U --pre --extra-index-url https://pypi.nvidia.com
    python3 -c "import tensorrt_llm"
    
    

    警告:tensort llm 和 TGI 软件版本冲突

  2. 安装docker nvidia

  3. 安装Triton Inference Server

    docker run --gpus=1 --rm --net=host -v ${PWD}/model_repository:/models nvcr.io/nvidia/tritonserver:24.01-py3 tritonserver --model-repository=/models
    
    docker pull nvcr.io/nvidia/tritonserver:24.01-trtllm-python-py3
    
    
    
    git clone https://github.com/triton-inference-server/tensorrtllm_backend.git
    cd tensorrtllm_backend
    cp ../TensorRT-LLM/examples/llama/out/*   all_models/inflight_batcher_llm/tensorrt_llm/1/
    
    
要为现有模型创建 TensorRT 引擎,需要 3 个步骤:
  1. 下载预先训练的权重,

  2. 构建完全优化的模型引擎,

    由于目前还不支持qwen2,因此使用qwen-7b 模型,这些问题可以自己编码解决。。。

    构建的时候,可以在仓库的examples目录下看到支持的所有模型,其他模型需要自己写代码去实现模型转化的功能。

    python build.py --hf_model_dir /home/chuan/models/qwen/Qwen-7B-Chat-Int4 \
                    --quant_ckpt_path /home/chuan/models/qwen/Qwen-7B-Chat-Int4 \
                    --dtype float16 \
                    --remove_input_padding \
                    --use_gpt_attention_plugin float16 \
                    --enable_context_fmha \
                    --use_gemm_plugin float16 \
                    --use_weight_only \
                    --weight_only_precision int4_gptq \
                    --per_group \
                    --world_size 1 \
                    --tp_size 1 \
                    --output_dir /home/chuan/models/qwen/Qwen-7B-Chat-Int4/trt_engines/int4-gptq/1-gpu
    
    
  3. 部署引擎,换句话说,运行完全优化的模型。

    python3 ../run.py --input_text "你好,请问你叫什么?" \
                      --max_output_len=50 \
                      --tokenizer_dir /home/chuan/models/qwen/Qwen-7B-Chat-Int4 \
                      --engine_dir=/home/chuan/models/qwen/Qwen-7B-Chat-Int4/trt_engines/int4-gptq/1-gpu
    
    

2024-02-17_22-55.png

tensortllm 在 Triton 上运行

在tensorrtllm_backend仓库中,打开目录all_models/inflight_batcher_llm/。该目录中现在有四个子文件夹,其中保存模型执行过程不同部分的工件:

  • preprocessing tokenizer模型,用于将文本(prompt)转换为token(input_ids)
  • postprocessing tokenizer模型,用于将模型的 output 转换为文本
  • tensorrt_llm 模型,即tensort llm 转换后的模型
  • ensemble 整体流程,将preprocessing、tensorrt_llm、postprocessing 串起来
  • tensorrt_llm_bls 和tensorrt_llm 功能一致,多了accumulate_tokens 的功能
  1. 编写tensorrt_llm
# 复制上一步生成的trensort llm 模型
cp /home/chuan/models/qwen/Qwen-7B-Chat-Int4/trt_engines/int4-gptq/1-gpu/* /home/chuan/github/tensorrtllm_backend/all_models/inflight_batcher_llm/tensorrt_llm/1

参数名含义
gpt_model_type(必选)inflight_fused_batching支持连续批处理,V1禁止
gpt_model_path(必选)model的路径
batch_scheduler_policy(必选)max_utilization:请求批处理;guaranteed_no_evict请求不中断
decoupled(可选)流式
max_beam_width(可选)beam search的配置
max_tokens_in_paged_kv_cache(可选)
max_attention_window_size(可选)
kv_cache_free_gpu_mem_fraction(可选)
enable_trt_overlap(可选)
exclude_input_in_output(可选)
normalize_log_probs(可选)
enable_chunked_context(可选)
python3 tools/fill_template.py --in_place \
      all_models/inflight_batcher_llm/tensorrt_llm/config.pbtxt \
      decoupled_mode:true,engine_dir:/all_models/inflight_batcher_llm/tensorrt_llm/1,\
gpt_model_type:inflight_fused_batching


  1. 编写preprocessing
参数名含义
tokenizer_typetokenizer的类型,包括:t5、auto、llama
tokenizer_dirtokenizer的路径
python tools/fill_template.py --in_place \
    all_models/inflight_batcher_llm/preprocessing/config.pbtxt \
    tokenizer_type:auto,tokenizer_dir:/all_models/inflight_batcher_llm/tensorrt_llm/1


  1. 编写postprocessing

和preprocessing参数一致

python tools/fill_template.py --in_place \
    all_models/inflight_batcher_llm/postprocessing/config.pbtxt \
    tokenizer_type:auto,tokenizer_dir:/all_models/inflight_batcher_llm/tensorrt_llm/1


  1. 运行tritonserver

重新编译模型

python3 build.py --hf_model_dir /home/chuan/models/qwen/Qwen-7B-Chat-Int4 \
                --quant_ckpt_path /home/chuan/models/qwen/Qwen-7B-Chat-Int4 \
                --dtype float16 \
                --remove_input_padding \
                --use_gpt_attention_plugin float16 \
                --enable_context_fmha \
                --use_gemm_plugin float16 \
                --use_weight_only \
                --weight_only_precision int4_gptq \
                --per_group \
                --world_size 1 \
                --tp_size 1 \
                --output_dir /home/chuan/models/qwen/Qwen-7B-Chat-Int4/trt_engines/int4-gptq/1-gpu \
                --use_inflight_batching \
	            --paged_kv_cache


docker run -it --gpus all --network host --shm-size=2g \
-v /home/chuan/github/tensorrtllm_backend:/opt/tensorrtllm_backend \
-v /home/chuan/github/TensorRT-LLM:/opt/TensorRT-LLM \
-p 8000:8000 \
triton_trt_llm

pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install sentencepiece protobuf

python3 /opt/tensorrtllm_backend/scripts/launch_triton_server.py --model_repo /opt/tensorrtllm_backend/all_models/inflight_batcher_llm --world_size 1


本地访问

curl -X POST localhost:800"text_input": "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n你好,你叫什么?<|im_end|>\n<|im_start|>assistant\n", "max_tokens": 50, "bad_words": "\n", "stop_words": "", "end_id": [151643], "pad_id": [151643]}'id": [151643], "pad_id": [151643]


结果

2024-02-18_15-37.png

qwen模型出现的错误处理

qwen需要单独安装的包,其他模型可能需要安装别的包

pip install tiktoken


2024-02-18_14-25.png

需要制定batch_size

vi tensorrt_llm/config.pbtxt

max_batch_size: 2


2024-02-18_14-28.png

在上面构建模型的过程中 python3 build.py 需要加上以下两个参数

--use_inflight_batching
--paged_kv_cache


本来的代码是为了llama 的token设计的,现在我们的模型是qwen,需要修改

preprocessing的model.py需要修改

postprocessing的model.py也需要修改

代码详见:github.com/night-is-yo…

本地编译 tritonserver
# Update the submodules
cd tensorrtllm_backend
git lfs install
git submodule update --init --recursive

# Use the Dockerfile to build the backend in a container
# For x86_64
DOCKER_BUILDKIT=1 docker build -t triton_trt_llm -f dockerfile/Dockerfile.trt_llm_backend .


有可能出现网络问题

docker 代理设置

下面的代理ip指的是docker0网卡的ip ,因为docker正在运行的容器,仅能访问到docker0网卡

vi ~/.docker/config.json

{
 "proxies": {
   "default": {
     "httpProxy": "http://172.17.0.1:3128",
     "httpsProxy": "https://172.17.0.1:3129",
     "noProxy": "*.test.example.com,.example.org,127.0.0.0/8"
   }
 }
}


sudo mkdir -p /etc/systemd/system/docker.service.d

sudo vi /etc/systemd/system/docker.service.d/http-proxy.conf

[Service]
Environment="HTTP_PROXY=http://172.17.0.1:3128"
Environment="HTTPS_PROXY=https://172.17.0.1:3129"
Environment="NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"


sudo systemctl daemon-reload
sudo systemctl restart docker


在这里插入图片描述

大模型&AI产品经理如何学习

求大家的点赞和收藏,我花2万买的大模型学习资料免费共享给你们,来看看有哪些东西。

1.学习路线图

在这里插入图片描述

第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;

第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;

第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;

第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;

第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;

第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;

第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。

2.视频教程

网上虽然也有很多的学习资源,但基本上都残缺不全的,这是我自己整理的大模型视频教程,上面路线图的每一个知识点,我都有配套的视频讲解。

在这里插入图片描述

在这里插入图片描述

(都打包成一块的了,不能一一展开,总共300多集)

因篇幅有限,仅展示部分资料,需要点击下方图片前往获取

3.技术文档和电子书

这里主要整理了大模型相关PDF书籍、行业报告、文档,有几百本,都是目前行业最新的。
在这里插入图片描述

4.LLM面试题和面经合集

这里主要整理了行业目前最新的大模型面试题和各种大厂offer面经合集。
在这里插入图片描述

👉学会后的收获:👈
• 基于大模型全栈工程实现(前端、后端、产品经理、设计、数据分析等),通过这门课可获得不同能力;

• 能够利用大模型解决相关实际项目需求: 大数据时代,越来越多的企业和机构需要处理海量数据,利用大模型技术可以更好地处理这些数据,提高数据分析和决策的准确性。因此,掌握大模型应用开发技能,可以让程序员更好地应对实际项目需求;

• 基于大模型和企业数据AI应用开发,实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能, 学会Fine-tuning垂直训练大模型(数据准备、数据蒸馏、大模型部署)一站式掌握;

• 能够完成时下热门大模型垂直领域模型训练能力,提高程序员的编码能力: 大模型应用开发需要掌握机器学习算法、深度学习框架等技术,这些技术的掌握可以提高程序员的编码能力和分析能力,让程序员更加熟练地编写高质量的代码。
在这里插入图片描述

1.AI大模型学习路线图
2.100套AI大模型商业化落地方案
3.100集大模型视频教程
4.200本大模型PDF书籍
5.LLM面试题合集
6.AI产品经理资源合集

👉获取方式:
😝有需要的小伙伴,可以保存图片到wx扫描二v码免费领取【保证100%免费】🆓

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值