llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

vllm

conda create --name vllm python=3.10
conda activate vllm

pip install vllm

from vllm import LLM, SamplingParams

prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

llm = LLM(model="/home/chuan/models/qwen/Qwen-7B-Chat", trust_remote_code=True)

outputs = llm.generate(prompts, sampling_params)

# Print the outputs.
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

建议将vllm 用在triton 引擎中,官方链接


fastllm

git clone https://github.com/ztxz16/fastllm
cd fastllm
mkdir build
cd build
cmake .. -DUSE_CUDA=ON
make -j
cd tools && python setup.py install

pip install tiktoken einops transformers_stream_generator

量化qwen模型

python3 tools/qwen2flm.py qwen-7b-int4.flm int4 

运行demo程序

# 这时在fastllm/build目录下
# 命令行聊天程序, 支持打字机效果 (只支持Linux)
./main -p model.flm 
# 简易webui, 使用流式输出 + 动态batch,可多路并发访问
./webui -p model.flm --port 1234 
# python版本的命令行聊天程序,使用了模型创建以及流式对话效果
python tools/cli_demo.py -p model.flm 
# python版本的简易webui,需要先安装streamlit-chat
streamlit run tools/web_demo.py model.flm 

在代码中使用

# 模型创建
from fastllm_pytools import llm
model = llm.model("model.flm")

# 生成回复
print(model.response("你好"))

# 流式生成回复
for response in model.stream_response("你好"):
    print(response, flush = True, end = "")


llama.cpp

环境安装和模型量化

git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make -j LLAMA_CUBLAS=1

python convert-hf-to-gguf.py /home/chuan/models/baichuan-inc/Baichuan2-13B-Chat
# quantize the model to 4-bits (using Q4_K_M method)
./quantize /home/chuan/models/baichuan-inc/Baichuan2-13B-Chat/ggml-model-f16.gguf /home/chuan/models/baichuan-inc/Baichuan2-13B-Chat/ggml-model-Q4_K_M.gguf Q4_K_M

关于llama.cpp 有两个非常火的github项目,分别是

  1. privateGPT
  2. ollama

privateGPT

git clone https://github.com/imartinez/privateGPT
cd privateGPT

conda create --name privateGPT python=3.11
conda activate privateGPT

curl -sSL https://install.python-poetry.org | python3 -

vi /home/chuan/.bashrc
export PATH=$PATH:/home/chuan/.local/bin

poetry install --with ui
poetry install --with local

make run

需要编辑配置文件

settings.yaml

# The default configuration file.
# More information about configuration can be found in the documentation: https://docs.privategpt.dev/
# Syntax in `private_pgt/settings/settings.py`
server:
  env_name: ${APP_ENV:prod}
  port: ${PORT:8001}
  cors:
    enabled: false
    allow_origins: ["*"]
    allow_methods: ["*"]
    allow_headers: ["*"]
  auth:
    enabled: false
    # python -c 'import base64; print("Basic " + base64.b64encode("secret:key".encode()).decode())'
    # 'secret' is the username and 'key' is the password for basic auth by default
    # If the auth is enabled, this value must be set in the "Authorization" header of the request.
    secret: "Basic c2VjcmV0OmtleQ=="

data:
  local_data_folder: local_data/private_gpt

ui:
  enabled: true
  path: /
  default_chat_system_prompt: >
    请根据instructions回答问题.
  default_query_system_prompt: >
    请根据context回答问题.
  delete_file_button_enabled: true
  delete_all_files_button_enabled: true

llm:
  mode: local
  # Should be matching the selected model
  max_new_tokens: 512
  context_window: 3900
  tokenizer: /home/chuan/models/baichuan-inc/Baichuan2-7B-Chat/

embedding:
  # Should be matching the value above in most cases
  mode: local
  ingest_mode: simple

vectorstore:
  database: qdrant

qdrant:
  path: local_data/private_gpt/qdrant

pgvector:
  host: localhost
  port: 5432
  database: postgres
  user: postgres
  password: postgres
  embed_dim: 384 # 384 is for BAAI/bge-small-en-v1.5
  schema_name: private_gpt
  table_name: embeddings

local:
  prompt_style: "llama2"
  llm_hf_repo_id: Baichuan2-7B-Chat
  llm_hf_model_file: /home/chuan/models/baichuan-inc/Baichuan2-7B-Chat/ggml-model-Q4_K_M.gguf
  embedding_hf_model_name: /home/chuan/models/BAAI/bge-large-zh-v1.5

sagemaker:
  llm_endpoint_name: huggingface-pytorch-tgi-inference-2023-09-25-19-53-32-140
  embedding_endpoint_name: huggingface-pytorch-inference-2023-11-03-07-41-36-479

openai:
  api_key: ${OPENAI_API_KEY:}
  model: gpt-3.5-turbo

ollama:
  model: llama2-uncensored



poetry run python scripts/setup


www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

从上面的截图可以看出,知识库提问没有正确回答,是因为prompt不对,请修改源代码中的prompt

修改privateGPT/private_gpt/components/llm/prompt_helper.py文件,这里就不再赘述。

ollama

编译

go generate ./...
go build .


运行

  1. 运行ollama服务
./ollama serve


2. 编辑model文件

vi Modelfile


FROM /home/chuan/models/baichuan-inc/Baichuan2-7B-Chat/ggml-model-Q4_K_M.gguf


./ollama create example -f Modelfile
./ollama run example


www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结
curl http://localhost:11434/api/chat -d '{
  "model": "example",
  "messages": [
    { "role": "user", "content": "why is the sky blue?" }
  ]
}'


www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

可以看到流式回答结果。

MindSpore

华为的推理框架

OpenVINO

intel的推理框架

总结

大模型的推理加速方法有很多,大模型的聊天后端也有很多。可是聊天的准确性和后端没有太大的关系。字符生成快慢也和后端没有任何关系,综上所述,个人不建议把太多时间花在后端开发上面。

以下是个人觉得大模型值得优化的地方:

www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结
www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

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

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

1.学习路线图

www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

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

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

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

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

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

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

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

2.视频教程

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

www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结
www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

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

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

3.技术文档和电子书

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

www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

4.LLM面试题和面经合集

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

www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

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

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

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

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

www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结
1.AI大模型学习路线图
2.100套AI大模型商业化落地方案
3.100集大模型视频教程
4.200本大模型PDF书籍
5.LLM面试题合集
6.AI产品经理资源合集

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

www.zeeklog.com  - llm大模型教程:LLM大模型推理加速vllm 、fastllm、llama.cpp使用教程以及大模型推理的总结

Read more

前端防范 XSS(跨站脚本攻击)

目录 一、防范措施 1.layui util  核心转义的特殊字符 示例 2.js-xss.js库 安装 1. Node.js 环境(npm/yarn) 2. 浏览器环境 核心 API 基础使用 1. 基础过滤(默认规则) 2. 自定义过滤规则 (1)允许特定标签 (2)允许特定属性 (3)自定义标签处理 (4)自定义属性处理 (5)转义特定字符 常见场景示例 1. 过滤用户输入的评论内容 2. 允许特定富文本标签(如富文本编辑器内容) 注意事项 更多配置 XSS(跨站脚本攻击)是一种常见的网络攻击手段,它允许攻击者将恶意脚本注入到其他用户的浏览器中。

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

目录 1. 打开浏览器开发者工具 2. 使用 Network 面板 3. 查看具体的API请求 a. Headers b. Payload c. Response d. Preview e. Timing 4. 实际操作步骤 5. 常见问题及解决方法 a. 无法看到API请求 b. 请求失败 c. 跨域问题(CORS) 作为一名后端工程师,理解前端如何调用接口、传递参数以及接收返回值是非常重要的。下面将详细介绍如何通过浏览器开发者工具(F12)查看和分析这些信息,并附带图片案例帮助你更好地理解。 1. 打开浏览器开发者工具 按下 F12 或右键点击页面选择“检查”可以打开浏览器的开发者工具。常用的浏览器如Chrome、Firefox等都内置了开发者工具。下面是我选择我的一篇文章,打开开发者工具进行演示。 2. 使用

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例)

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例) 前端开发中最令人头疼的莫过于那些难以定位的UI问题——元素错位、样式冲突、响应式失效...传统调试方式往往需要反复修改代码、刷新页面、检查元素。现在,通过Cursor编辑器集成的Codex功能,你可以直接用截图交互快速定位和修复这些问题。本文将带你从零开始,掌握这套革命性的调试工作流。 1. 环境准备与基础配置 在开始之前,确保你已经具备以下环境: * Cursor编辑器最新版(v2.5+) * Node.js 18.x及以上版本 * React 18项目(本文以Chakra UI 2.x为例) 首先在Cursor中安装Codex插件: 1. 点击左侧扩展图标 2. 搜索"Codex"并安装 3. 登录你的OpenAI账户(需要ChatGPT Plus订阅) 关键配置项: // 在项目根目录创建.