在华为 Atlas 800I A2 服务器上搭建大模型推理服务

NPU GPU
CANN CUDA
MindSpore PyTorch
MindFormer Transformers
MindIE vLLM
  • Qwen1.5-7B
git clone https://www.modelscope.cn/Qwen/Qwen1.5-7B-Chat.git
  • Qwen2-7B ❌
git clone https://www.modelscope.cn/Qwen/Qwen2-7B-Instruct.git
  • Qwen2-72B
git clone https://www.modelscope.cn/Qwen/Qwen2-72B-Instruct.git
  • DeepSeek-Coder-6.7B
git clone https://www.modelscope.cn/deepseek-ai/deepseek-coder-6.7b-instruct.git
  • StarCoder2-15B ❌
git clone https://www.modelscope.cn/AI-ModelScope/starcoder2-15b.git
  • CodeGeeX2-6B ❌
git clone https://www.modelscope.cn/ZhipuAI/codegeex2-6b.git

缺少软件包 sentencepiece

因为 MindIE 不支持 bin 格式的模型,需要将模型转换为

OpenAI API Compatibility

  • Ollama: 11434
  • LiteLLM: 4000
  • XInference: 9997
  • MindIE: 1025
  • curl -s: -s 选项表示静默模式,不输出进度信息。
  • jq -r: -r 选项表示以原始格式输出,去掉了引号。

在 Bash 中,单引号和双引号的使用有一些重要的区别:

  • 单引号 (')
    • 完全字面值:单引号内的内容被视为字面值,不会对其中的任何字符进行扩展或解析。
    • 变量不扩展:在单引号内,变量不会被解析。例如,'LITELLMAPIKEY会被视为字符LITELLM_API_KEY' 会被视为字符串 'LITELLM_API_KEY',而不是变量的值。
    echo '$LITELLM_API_KEY'  # 输出: $LITELLM_API_KEY
    
  • 双引号 (")
    • 允许扩展:双引号内的内容会进行解析和扩展。
    • 变量扩展:在双引号内,变量会被解析。例如,"$LITELLM_API_KEY" 会被替换为该变量的值。
    • 特殊字符处理:某些字符(如 $ 和 \)在双引号内需要用反斜杠转义。
    echo "$LITELLM_API_KEY"  # 输出: 变量的实际值(例如: sk-1234)
    

部署 LLM 多 LoRA 适配器的推理服务

conda create -n text-generation-inference python=3.9
conda activate text-generation-inference

git clone https://github.com/huggingface/text-generation-inference.git && cd text-generation-inference
BUILD_EXTENSIONS=True make install
conda create -n vllm python=3.10 -y
conda activate vllm
pip install vllm
cd ~/HuggingFace/mistralai/Mistral-7B-v0.1
git clone https://huggingface.co/predibase/magicoder adapters/magicoder
vllm serve `pwd` \
    --enable-lora \
    --lora-modules magicoder=`pwd`/adapters/magicoder

推测解码 (Speculative Decoding)

  1. 初步生成:使用一个小而快速的模型(称为Mq),生成一系列初步的 tokens。这个模型很高效,所以能快速得到结果。
  2. 并行评估:接着,使用一个更大的目标模型(称为Mp)来同时评估Mq生成的所有 tokens。Mp会判断每个 token 的概率,选择那些可能性高的结果。
  3. 修正输出:对于那些被Mq生成但被Mp拒绝的低概率 token,Mp会提供新的替代 token。这一步确保了输出的质量,同时提高了生成的速度。
  • Serving AI models faster with speculative decoding
    1. 生成多个猜测候选: 使用一个更小更高效的"草稿"模型或者是主模型本身的最后一层,生成多个可能的下一个token作为猜测。
    2. 并行评估猜测: 利用主要的大型语言模型(LLM)并行地对这些猜测进行评估,计算每个猜测的概率分布。
    3. 接受或拒绝猜测: 通过比较每个猜测在 LLM 和草稿模型下的概率,以及生成一个随机数进行判断,决定是否接受该猜测。
    4. 调整并重采样: 如果所有猜测都被接受,则直接从 LLM 采样下一个token。如果有猜测被拒绝,则从调整后的概率分布中重新采样被拒绝的猜测。
    5. 输出结果: 最终输出包括所有被接受的猜测以及从 LLM 采样或重采样得到的token。

Reader-LM: 用于清理和转换 HTML 到 Markdown 的小型语言模型

不能简单地将 HTML 把输入给模型(Reader-LM),因为效果不理想。

Reader-LM-0.5B 和 Reader-LM-1.5B 是受 Jina Reader 启发的两个新型小型语言模型,旨在将来自开放网络的原始、嘈杂的 HTML 转换为干净的 markdown。

使用小型语言模型替换了 readability + turndown + regex 启发式的管道。

Jina Reader 是一个简单的 API,只需一个简单的前缀:r.jina.ai,就可以将任何 URL 转换为 LLM 友好的 markdown。

jina-embeddings-v3 的架构基于 XLM-RoBERTa 模型,并进行了几项关键修改。集成了 FlashAttention 2 以提高计算效率,同时 RoPE 扩展了对最长 8192 个 token 序列的支持。引入了任务特定的 LoRA 适配器,以优化各种任务的 embeddings。模型的输入由两部分组成:文本,即要嵌入的长文档,以及任务类型。jina-embeddings-v3 支持四种任务,并实现了五种适配器可供选择:retrieval.query 和 retrieval.

LLM 的合成数据

围绕在 Phi 数据集上的谜团除了我们对其如何创建的不甚了了之外,还有一个问题是其数据集的生成使用的是私有模型。为了解决这些问题,我们引入了 Cosmopedia,这是由 Mixtral-8x7B-Instruct-v0.1 生成的包含教科书、博文、故事、帖子以及 WikiHow 文章等各种体裁的合成数据集。其中有超过 3000 万个文件、250 亿个词元,是迄今为止最大的开放合成数据集。

实际上 Cosmopedia 的大部分时间都花在了细致的提示词工程上了。

少儿专业人士和研究人员以及高中生生成相同主题的教科书的提示

  • 提示
    • 为一本关于“为什么去太空?”的教科书写一篇详细的课程单元。面向(少儿 / 专业人士和研究人员 / 高中生
    • 我们目前正在编写第一章:“1. 引言”。你将编写本章的第一个子单元。写一个标题为“1.1. 为什么我们花费数十亿美元探索太空?”的新子单元,同时尽量做到:
  • 生成提示的三个主要目标受众是:
    • yong children (少儿)
      • 记住这个单元是为儿童书籍准备的,所以使用非常简单、日常的词汇和短语,让一个10岁的孩子能够轻松理解。讲一个有趣和愉快的故事,避免任何复杂的概念或技术术语。
    • professionals and researchers in the field (专业人士和研究人员)
      • 内容应旨在吸引对该主题有深入了解的高知识水平的受众。包括对最新研究成果和领域内争论的批判性分析。
    • high school students (高中生)
      • 使用能够与青少年学生产生共鸣的语言和例子,平衡教育严谨性与可及性。目标是使该主题变得易于理解且有趣,激发他们对其在日常生活中应用的好奇心。

Letta (fka MemGPT) 是用于创建有状态 LLM 服务的框架

git clone https://github.com/cpacker/MemGPT
  • Ollama
LETTA_LLM_ENDPOINT=http://host.docker.internal:11434
LETTA_LLM_ENDPOINT_TYPE=ollama
LETTA_LLM_MODEL=qwen2.5:7b-q6_K
LETTA_LLM_CONTEXT_WINDOW=32000
LETTA_EMBEDDING_ENDPOINT=http://host.docker.internal:11434
LETTA_EMBEDDING_ENDPOINT_TYPE=ollama
LETTA_EMBEDDING_MODEL=bge-m3
LETTA_EMBEDDING_DIM=1024

在下载 Ollama 模型时,请确保使用标签!

不要执行 ollama pull dolphin2.2-mistral,而是执行 ollama pull dolphin2.2-mistral:7b-q6_K。

如果您没有指定标签,Ollama 可能会默认使用高度压缩的模型变体(例如 Q4)。

Qwen2 Technical Report

This report introduces the Qwen2 series, the latest addition to our large language models and large multimodal models. We release a comprehensive suite of foundational and instruction-tuned language models, encompassing a parameter range from 0.5 to 72 billion, featuring dense models and a Mixture-of-Experts model. Qwen2 surpasses most prior open-weight models, including its predecessor Qwen1.5, and exhibits competitive performance relative to proprietary models across diverse benchmarks on language understanding, generation, multilingual proficiency, coding, mathematics, and reasoning.

Qwen2.5-Coder Technical Report

In this report, we introduce the Qwen2.5-Coder series, a significant upgrade from its predecessor, CodeQwen1.5. This series includes two models: Qwen2.5-Coder-1.5B and Qwen2.5-Coder-7B. As a code-specific model, Qwen2.5-Coder is built upon the Qwen2.5 architecture and continues pretrained on a vast corpus of over 5.5 trillion tokens. Through meticulous data cleaning, scalable synthetic data generation, and balanced data mixing, Qwen2.

Continue 源码分析 - 向量数据库存储位置

向量数据库存储目录名字的组成部分:

  • IndexTag.directory: /Users/junjian/GitHub/continuedev/continue-0.9.191-vscode/extensions/vscode
  • IndexTag.branch: NONE
  • IndexTag.artifactId: vectordb::_TransformersJsEmbeddingsProvider::bge-small-zh-v1.5

变量IDE ExtensionsBranchEmbedding ProviderEmbedding Model

源代码:core/indexing/refreshIndex.ts

export function tagToString(tag: IndexTag): string {
  return `${tag.directory}::${tag.branch}::${tag.artifactId}`;
}

Langfuse: Open Source LLM Engineering Platform

LLM 可观察性(LLM Observability)、提示管理(Prompt Management)、LLM 评估(LLM Evaluations)、数据集(Datasets)、LLM 指标(LLM Metrics)和提示游乐场(Prompt Playground)

# Clone repository
git clone https://github.com/langfuse/langfuse.git
cd langfuse

# Run server and database
docker compose up -d

安装

pip install langfuse>=2.0.0 litellm

main.py import os import litellm os.environ['LITELLM_LOG'] = 'DEBUG' # Langfuse os.environ["LANGFUSE_HOST"]="http://localhost:3000" os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-fd5d8fba-5134-4037-884d-d6780894a65a" os.

LiteLLM: [Python SDK] [Proxy Server (LLM Gateway)]

model_list:
  - model_name: qwen-coder
    litellm_params:
      model: ollama/qwen2.5-coder:7b
  - model_name: bge-m3
    litellm_params:
      model: ollama/bge-m3
  - model_name: llava
    litellm_params:
      model: ollama/llava:7b
      api_base: "http://localhost:11434"
      # api_base: http://127.0.0.1:11434/v1 # ❌ 500 Internal Server Error
  - model_name: gpt-4
    litellm_params:
      model: openai/gpt-4-32k
// ...
docker run --name litellm \
    -v $(pwd)/litellm_config.yaml:/app/config.yaml \
    -p 4000:4000 \
    ghcr.io/berriai/litellm:main-stable \
    --config /app/config.yaml \
    --detailed_debug

Kong AI Gateway

  • 创建
curl -X POST http://localhost:8001/services \
    --data "name=ai-proxy" \
    --data "url=http://localhost:32000"

请记住,上游 URL 可以指向任何空的地方,因为插件不会使用它。

  • 查看
curl -X GET http://localhost:8001/services/ai-proxy | jq                   
  • 删除
curl -X DELETE http://localhost:8001/services/ai-proxy
  • 创建
curl -X POST http://localhost:8001/services/ai-proxy/routes \
    --data "name=openai-chat" \
    --data "paths[]=~/openai-chat$"
  • 查看
curl -X GET http://localhost:8001/routes/openai-chat | jq

SGLang 大模型服务框架

SGLang 是用于大型语言模型和视觉语言模型的快速服务框架。通过协同设计后端运行时和前端语言,使您与模型的交互更快速、更可控。

The core features include:

核心功能包括: Fast Backend Runtime: Efficient serving with RadixAttention for prefix caching, jump-forward constrained decoding, continuous batching, token attention (paged attention), tensor parallelism, FlashInfer kernels, and quantization (AWQ/FP8/GPTQ/Marlin). 快速后端运行时:通过 RadixAttention 实现高效的服务,支持前缀缓存(prefix caching)、受限跳转前缀解码(jump-forward constrained decoding)、连续批处理(continuous batching)、令牌注意力(分页注意力)(token attention (paged attention))、张量并行(tensor parallelism)、FlashInfer 内核和量化(AWQ/FP8/GPTQ/Marlin)。