Transformers documentation

SGLang

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).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

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 模型。

  1. PreTrainedConfig.from_pretrained() loads the model’s config.json from the Hub or your Hugging Face cache.
  2. AutoModel.from_config() 会根据 config 解析模型类。
  3. 在加载过程中,_attn_implementation 会被设置为 "sglang"。这会将注意力调用路由到 SGLang 的 RadixAttention 内核。
  4. SGLang 的并行线性类用于替换线性层,以支持张量并行。
  5. load_weights 函数从 safetensors 文件中加载权重并填充到模型中。

该模型在使用 Transformers 模型结构的同时,受益于 SGLang 的所有优化。

兼容模型需要设置 _supports_attention_backend=True,以便 SGLang 能够控制注意力机制的执行。有关详细信息,请参阅 构建用于推理的兼容模型后端 指南。

资源

Update on GitHub