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 引擎中
,官方链接github.com/triton-infe…
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项目,分别是
- privateGPT
- ollama
privateGPT
github.com/imartinez/p…
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
从上面的截图可以看出,知识库提问没有正确回答,是因为prompt不对,请修改源代码中的prompt
修改privateGPT/private_gpt/components/llm/prompt_helper.py文件,这里就不再赘述。
ollama
编译
go generate ./...
go build .
运行
- 运行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
curl http://localhost:11434/api/chat -d '{
"model": "example",
"messages": [
{ "role": "user", "content": "why is the sky blue?" }
]
}'
可以看到流式回答结果。
MindSpore
华为的推理框架
略
OpenVINO
intel的推理框架
略
总结
大模型的推理加速方法有很多,大模型的聊天后端也有很多。可是聊天的准确性和后端没有太大的关系。字符生成快慢也和后端没有任何关系,综上所述,个人不建议把太多时间花在后端开发上面。
以下是个人觉得大模型值得优化的地方:
大模型&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%免费】🆓