Transformers documentation
SGLang
快速入门
基类
模型
预处理器
推理
Pipeline API
生成 API
优化
与模型对话
服务
训练
硬件
量化
生态系统集成
资源
API
You are viewing main version, which requires installation from source. If you'd like
regular pip install, checkout the latest stable version (v5.24.0).
SGLang
SGLang 是一款面向大语言模型(LLM)的低延迟、高吞吐量推理引擎。它还包含一种用于构建智能体工作流的前端语言。
设置 model_impl="transformers" 以加载 Transformers 建模后端。
import sglang as sgl
llm = sgl.Engine("meta-llama/Llama-3.2-1B-Instruct", model_impl="transformers")
print(llm.generate(["The capital of France is"], {"max_new_tokens": 20})[0])对于在线服务,请在 sglang.launch_server 命令中传入 --model-impl transformers。
python3 -m sglang.launch_server \ --model-path meta-llama/Llama-3.2-1B-Instruct \ --model-impl transformers \ --host 0.0.0.0 \ --port 30000
Transformers 集成
设置 model_impl="transformers" 可指示 SGLang 跳过其原生模型匹配,直接使用 Transformers 模型。
- PreTrainedConfig.from_pretrained() loads the model’s
config.jsonfrom the Hub or your Hugging Face cache. - AutoModel.from_config() 会根据 config 解析模型类。
- 在加载过程中,
_attn_implementation会被设置为"sglang"。这会将注意力调用路由到 SGLang 的 RadixAttention 内核。 - SGLang 的并行线性类用于替换线性层,以支持张量并行。
- load_weights 函数从 safetensors 文件中加载权重并填充到模型中。
该模型在使用 Transformers 模型结构的同时,受益于 SGLang 的所有优化。
兼容模型需要设置
_supports_attention_backend=True,以便 SGLang 能够控制注意力机制的执行。有关详细信息,请参阅 构建用于推理的兼容模型后端 指南。
资源
- SGLang 文档提供了更多使用 Transformers 作为后端的示例和技巧。
- Transformers 后端在 SGLang 中的集成博客文章介绍了该集成所实现的功能。