Xinference 内置模型部署指南:code-llama(Code Llama)多引擎启动与量化配置全解析
【免费下载链接】inferenceSwap GPT for any LLM by changing a single line of code. Xinference lets you run open-source, speech, and multimodal models on cloud, on-prem, or your laptop — all through one unified, production-ready inference API.项目地址: https://gitcode.com/GitHub_Trending/in/inference
适用场景:本文面向希望在 Xinference 上快速部署 Code Llama(code-llama)代码生成模型的开发者,覆盖其内置 Model Spec 的完整解读、pytorch 与 ggufv2 两种格式族的引擎选型、以及可直接复制的
xinference launch启动命令。读完本文,你将掌握如何针对 7B/13B/34B 三档参数规模与 12 种 GGUF 量化级别,在 vLLM、Transformers、SGLang、llama.cpp 四类引擎间做出正确选择并完成一键拉起。
一、模型概览:基于 LLaMA2 微调的代码生成模型
code-llama是 Xinference 内置模型目录(builtin models)中收录的 Code Llama 系列基础模型。根据内置模型元数据,它的核心定位如下:
- Model Name:
code-llama - Context Length:100000(10 万 token 上下文,见 xinference/model/llm/llm_family.json 中的
"context_length": 100000) - Languages:en
- Abilities:generate(仅生成,无对话/工具调用能力标记)
- Description:Code-Llama 是一个开源的 LLM,通过对 LLaMA2 进行微调(fine-tuning)得到,用于生成和讨论代码
从内置家族定义(xinference/model/llm/llm_family.json)可以进一步确认实现层面的信息:
- 架构(architectures):
LlamaForCausalLM,模型类型(model_type)为llama,即完整的 LLaMA CausalLM 解码器结构; - 能力标签:
["generate"],与文档中的 Abilities 一一对应; - 语言标签:
["en"],面向英文代码与注释场景。
注意:
code-llama与同目录下的code-llama-instruct(指令微调版)、code-llama-python(Python 专项版)属于同一个家族,三者同源于 Llama 系列架构,但本文聚焦文档指定的基础模型code-llama。
二、内置 Model Spec 全景:两个格式族、六种规格
Xinference 为code-llama内置了6 个 Model Spec,本质上分为两大格式族:
- pytorch 族(fp16 原始权重):7B / 13B / 34B,量化方式仅
none,可选引擎为vLLM、Transformers、SGLang; - ggufv2 族(GGUF 量化权重):7B / 13B / 34B,提供 12 种量化级别,引擎为llama.cpp。
下表完整汇总了文档中全部 6 个 Spec 的元数据(与 llm_family.json 中model_specs数据一致):
| Spec | Model Format | Size (in billions) | Quantizations | Engines | Model ID(Hugging Face) | Model ID(ModelScope) |
|---|---|---|---|---|---|---|
| 1 | pytorch | 7 | none | vLLM / Transformers / SGLang | TheBloke/CodeLlama-7B-fp16 | AI-ModelScope/CodeLlama-7b-hf |
| 2 | pytorch | 13 | none | vLLM / Transformers / SGLang | TheBloke/CodeLlama-13B-fp16 | AI-ModelScope/CodeLlama-13b-hf |
| 3 | pytorch | 34 | none | vLLM / Transformers / SGLang | TheBloke/CodeLlama-34B-fp16 | AI-ModelScope/CodeLlama-34b-hf |
| 4 | ggufv2 | 7 | Q2_K, Q3_K_L, Q3_K_M, Q3_K_S, Q4_0, Q4_K_M, Q4_K_S, Q5_0, Q5_K_M, Q5_K_S, Q6_K, Q8_0 | llama.cpp | TheBloke/CodeLlama-7B-GGUF | — |
| 5 | ggufv2 | 13 | 同上 12 种 | llama.cpp | TheBloke/CodeLlama-13B-GGUF | — |
| 6 | ggufv2 | 34 | 同上 12 种 | llama.cpp | TheBloke/CodeLlama-34B-GGUF | — |
关于 GGUF 文件名规则:ggufv2 族的三档规格在家族元数据中分别声明了model_file_name_template:codellama-7b.{quantization}.gguf、codellama-13b.{quantization}.gguf、codellama-34b.{quantization}.gguf(见 llm_family.json、llm_family.json、llm_family.json)。即选择Q4_K_M时,实际下载文件为codellama-7b.Q4_K_M.gguf等,Xinference 的下载与缓存逻辑会依据该模板自动拼接文件名。
关于固定 revision:pytorch 族在 Hugging Face / ModelScope 上均固定了版本号以保证可复现性,例如 7B 对应 HF revisionce09049eb9140a19cf78051cb5d849607b6fa8ec、ModelScope revisionv1.0.2;13B 与 34B 同样在 llm_family.json 中各有固定值。这意味着同一模型在不同时间启动,拉取的权重内容保持一致。
三、逐 Spec 启动命令(可直接复制)
以下 6 条命令完整继承自原文档。命令中的${engine}与${quantization}为占位符,请按下表给出的选项替换:pytorch 族将${engine}替换为vllm/transformers/sglang之一、${quantization}替换为none;ggufv2 族将${engine}替换为llama.cpp、${quantization}从 12 种量化级别中选择一种。
Spec 1 —— pytorch / 7B:
xinference launch --model-engine ${engine} --model-name code-llama --size-in-billions 7 --model-format pytorch --quantization ${quantization}Spec 2 —— pytorch / 13B:
xinference launch --model-engine ${engine} --model-name code-llama --size-in-billions 13 --model-format pytorch --quantization ${quantization}Spec 3 —— pytorch / 34B:
xinference launch --model-engine ${engine} --model-name code-llama --size-in-billions 34 --model-format pytorch --quantization ${quantization}Spec 4 —— ggufv2 / 7B:
xinference launch --model-engine ${engine} --model-name code-llama --size-in-billions 7 --model-format ggufv2 --quantization ${quantization}Spec 5 —— ggufv2 / 13B:
xinference launch --model-engine ${engine} --model-name code-llama --size-in-billions 13 --model-format ggufv2 --quantization ${quantization}Spec 6 —— ggufv2 / 34B:
xinference launch --model-engine ${engine} --model-name code-llama --size-in-billions 34 --model-format ggufv2 --quantization ${quantization}当不指定下载来源时,Xinference 会优先按 ModelScope → Hugging Face 的优先级解析权重源(详见下文
match_llm的 hub 优先级逻辑),国内网络环境下可自动走 ModelScope 镜像,也可在命令中显式控制。
四、launch 参数逐项拆解(对应命令行实现)
xinference launch的参数定义位于 xinference/deploy/cmdline.py,上述命令涉及的五个核心参数及常用扩展参数如下:
| 参数 | 短选项 | 类型 | 说明 |
|---|---|---|---|
--model-name | -n | str,必填 | 模型名,此处为code-llama |
--model-engine | -en | str | 推理引擎,pytorch 族可选 vLLM / Transformers / SGLang,ggufv2 族为 llama.cpp |
--size-in-billions | -s | str | 模型参数量级(十亿),7 / 13 / 34 |
--model-format | -f | str | 模型格式,pytorch或ggufv2 |
--quantization | -q | str | 量化方式,pytorch 族为none,ggufv2 族为 12 种 K-quant 之一 |
--model-type | -t | str | 模型类型,默认LLM |
--model-uid | -u | str | 模型实例 UID,默认 None(自动生成) |
--replica | -r | int | 模型副本数,默认 1 |
--n-gpu | — | str | 使用的 GPU 数量,默认auto;当 n_worker > 1 时表示每个 worker 的 GPU 数 |
--n-worker | — | int | worker 数量,默认 1 |
量化参数的大小写兼容:--quantization的匹配对大小写不敏感。源码中match_llm的量化匹配逻辑为q.lower() != quant.lower()(见 xinference/model/llm/llm_family.py),因此命令行中写q4_k_m与Q4_K_M等价,均能正确命中 Spec 中的量化选项。
五、引擎选型:pytorch 族 vs ggufv2 族
引擎与格式是绑定的,选择依据可以从家族元数据中的virtualenv依赖声明(llm_family.json)反向理解各引擎的运行时构成:
"virtualenv": { "packages": [ "#transformers_dependencies# ; #engine# == \"Transformers\"", "#llama_cpp_dependencies# ; #engine# == \"llama.cpp\"", "#vllm_dependencies# ; #engine# == \"vllm\"", "#system_numpy# ; #engine# == \"vllm\"" ] }- vLLM:依赖
#vllm_dependencies#与#system_numpy#,适合追求高吞吐的批量代码生成/补全服务,Xinference 会按需为 vLLM 创建隔离虚拟环境; - Transformers:依赖
#transformers_dependencies#,走原生 HF 生态,兼容性与调试便利性最佳,适合验证模型行为或小规模调用; - SGLang:与 vLLM 类似的性能导向引擎(pytorch 族第三个选项),适合需要 SGLang 特性(如结构化约束推理)的场景;
- llama.cpp:依赖
#llama_cpp_dependencies#,搭配 ggufv2 量化权重,是低显存/CPU 部署的标准路径。
选型建议(基于上述实现事实):显存充足、追求吞吐优先选 vLLM / SGLang + pytorch fp16;需要 CPU 推理或显存受限时,选 llama.cpp + GGUF 量化(如 Q4_K_M、Q5_K_M 这类速度与质量均衡的档位);只想快速验证模型效果则用 Transformers + pytorch。34B fp16 权重对显存需求显著高于 7B/13B,可以推断默认单卡n-gpu=auto场景下更依赖多卡或量化方案。
六、源码级验证:内置注册表与匹配逻辑
1. 内置家族注册表
code-llama的全部 Spec 数据来自 Xinference 内置的 LLM 家族注册表 xinference/model/llm/llm_family.json,运行时被加载为LLMFamilyV2对象。该类定义了context_length、model_name、model_lang、model_ability、model_specs、architectures、virtualenv等字段(见 xinference/model/llm/llm_family.py),并可通过has_architecture/matches_supported_architectures判断模型是否被某引擎支持(llm_family.py)。code-llama的LlamaForCausalLM架构正是各引擎判定兼容性的关键依据。
2. match_llm 的匹配优先级
match_llm(model_name, model_format, model_size_in_billions, quantization, download_hub)(xinference/model/llm/llm_family.py)负责把xinference launch传入的参数解析成具体 Spec:
- 先按
model_name命中家族,再按 hub 过滤 spec:当显式传入download_hub时,按该 hub 过滤;未指定时按download_from_modelscope()等环境判定,优先级为 ModelScope → Hugging Face; - 随后按 format / size / quantization 逐级匹配,量化匹配大小写不敏感;
- 若 GGUF 的
model_id中含有{}占位符,还会执行model_id.format(quantization=q)完成模型 ID 拼接(llm_family.py)。
3. 测试用例佐证
仓库测试 xinference/model/llm/tests/test_llm_family.py 直接覆盖了code-llama的匹配行为:
family = match_llm("code-llama", model_format="ggufv2", quantization="q4_0") assert family.model_name == "code-llama" family = match_llm("code-llama") assert family.model_name == "code-llama"其中第一例以小写q4_0成功命中Q4_0量化,验证了量化匹配的大小写容错;第二例不带任何限定参数也能解析到家族默认 Spec,说明code-llama在未指定 format/size/quantization 时同样具备合理的默认匹配路径。
七、部署实操建议与验证步骤
1. 快速验证(默认路径)
不指定任何 Spec 限定参数,让match_llm自行解析默认 Spec:
xinference launch --model-name code-llama2. 典型生产路径示例
场景 A:vLLM + fp16 7B,追求吞吐:
xinference launch --model-engine vllm --model-name code-llama --size-in-billions 7 --model-format pytorch --quantization none场景 B:llama.cpp + Q4_K_M 13B,节省显存:
xinference launch --model-engine llama.cpp --model-name code-llama --size-in-billions 13 --model-format ggufv2 --quantization Q4_K_M场景 C:多副本/多 GPU 扩展:
xinference launch --model-engine vllm --model-name code-llama --size-in-billions 34 --model-format pytorch --quantization none --replica 2 --n-gpu 23. 验证要点
- 启动成功后,可通过 Xinference 的模型列表接口查看实例状态与 cache 状态(
LLMFamilyV2.to_version_info会返回model_version、model_file_location、cache_status等信息,见 xinference/model/llm/llm_family.py); - 首次启动会触发权重下载,pytorch 族从 ModelScope/Hugging Face 拉取 fp16 权重,ggufv2 族按
codellama-{size}.{quantization}.gguf模板拉取 GGUF 文件; - 由于
code-llama的 Abilities 为generate(非chat),调用时应按生成式补全接口而非对话接口进行请求,这是由其内置能力标签决定的正确用法。
相关参考:本文核心数据源为内置模型文档 doc/source/models/builtin/llm/code-llama.rst,实现依据为 xinference/model/llm/llm_family.json、xinference/model/llm/llm_family.py、xinference/deploy/cmdline.py 及对应测试 xinference/model/llm/tests/test_llm_family.py。
【免费下载链接】inferenceSwap GPT for any LLM by changing a single line of code. Xinference lets you run open-source, speech, and multimodal models on cloud, on-prem, or your laptop — all through one unified, production-ready inference API.项目地址: https://gitcode.com/GitHub_Trending/in/inference
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考