sglang 0.3.4.post1__py3-none-any.whl → 0.3.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sglang/api.py +1 -1
- sglang/bench_latency.py +3 -3
- sglang/bench_server_latency.py +2 -3
- sglang/bench_serving.py +92 -0
- sglang/global_config.py +9 -3
- sglang/lang/chat_template.py +50 -25
- sglang/lang/interpreter.py +9 -1
- sglang/lang/ir.py +11 -2
- sglang/launch_server.py +1 -1
- sglang/srt/configs/model_config.py +76 -15
- sglang/srt/constrained/__init__.py +18 -0
- sglang/srt/constrained/bnf_cache.py +61 -0
- sglang/srt/constrained/fsm_cache.py +10 -3
- sglang/srt/constrained/grammar.py +190 -0
- sglang/srt/hf_transformers_utils.py +20 -5
- sglang/srt/layers/attention/flashinfer_backend.py +5 -5
- sglang/srt/layers/attention/triton_ops/decode_attention.py +110 -30
- sglang/srt/layers/attention/triton_ops/prefill_attention.py +1 -1
- sglang/srt/layers/fused_moe/fused_moe.py +4 -3
- sglang/srt/layers/fused_moe/layer.py +28 -0
- sglang/srt/layers/logits_processor.py +5 -5
- sglang/srt/layers/quantization/base_config.py +16 -1
- sglang/srt/layers/rotary_embedding.py +15 -48
- sglang/srt/layers/sampler.py +51 -39
- sglang/srt/layers/vocab_parallel_embedding.py +486 -0
- sglang/srt/managers/data_parallel_controller.py +8 -7
- sglang/srt/managers/detokenizer_manager.py +11 -9
- sglang/srt/managers/image_processor.py +4 -3
- sglang/srt/managers/io_struct.py +80 -78
- sglang/srt/managers/schedule_batch.py +46 -52
- sglang/srt/managers/schedule_policy.py +24 -13
- sglang/srt/managers/scheduler.py +145 -82
- sglang/srt/managers/tokenizer_manager.py +236 -334
- sglang/srt/managers/tp_worker.py +5 -5
- sglang/srt/managers/tp_worker_overlap_thread.py +58 -21
- sglang/srt/mem_cache/flush_cache.py +1 -1
- sglang/srt/mem_cache/memory_pool.py +10 -3
- sglang/srt/model_executor/cuda_graph_runner.py +34 -23
- sglang/srt/model_executor/forward_batch_info.py +6 -9
- sglang/srt/model_executor/model_runner.py +10 -19
- sglang/srt/models/baichuan.py +4 -4
- sglang/srt/models/chatglm.py +4 -4
- sglang/srt/models/commandr.py +1 -1
- sglang/srt/models/dbrx.py +5 -5
- sglang/srt/models/deepseek.py +4 -4
- sglang/srt/models/deepseek_v2.py +4 -4
- sglang/srt/models/exaone.py +4 -4
- sglang/srt/models/gemma.py +1 -1
- sglang/srt/models/gemma2.py +1 -1
- sglang/srt/models/gpt2.py +287 -0
- sglang/srt/models/gpt_bigcode.py +1 -1
- sglang/srt/models/grok.py +4 -4
- sglang/srt/models/internlm2.py +4 -4
- sglang/srt/models/llama.py +15 -7
- sglang/srt/models/llama_embedding.py +2 -10
- sglang/srt/models/llama_reward.py +5 -0
- sglang/srt/models/minicpm.py +4 -4
- sglang/srt/models/minicpm3.py +4 -4
- sglang/srt/models/mixtral.py +7 -5
- sglang/srt/models/mixtral_quant.py +4 -4
- sglang/srt/models/mllama.py +5 -5
- sglang/srt/models/olmo.py +4 -4
- sglang/srt/models/olmoe.py +4 -4
- sglang/srt/models/qwen.py +4 -4
- sglang/srt/models/qwen2.py +4 -4
- sglang/srt/models/qwen2_moe.py +4 -4
- sglang/srt/models/qwen2_vl.py +4 -8
- sglang/srt/models/stablelm.py +4 -4
- sglang/srt/models/torch_native_llama.py +4 -4
- sglang/srt/models/xverse.py +4 -4
- sglang/srt/models/xverse_moe.py +4 -4
- sglang/srt/openai_api/adapter.py +52 -66
- sglang/srt/sampling/penaltylib/penalizers/min_new_tokens.py +6 -3
- sglang/srt/sampling/sampling_batch_info.py +7 -13
- sglang/srt/sampling/sampling_params.py +5 -7
- sglang/srt/server.py +41 -33
- sglang/srt/server_args.py +34 -5
- sglang/srt/utils.py +40 -56
- sglang/test/run_eval.py +2 -0
- sglang/test/runners.py +2 -1
- sglang/test/srt/sampling/penaltylib/utils.py +1 -0
- sglang/test/test_utils.py +151 -6
- sglang/utils.py +62 -1
- sglang/version.py +1 -1
- sglang-0.3.5.dist-info/METADATA +344 -0
- sglang-0.3.5.dist-info/RECORD +152 -0
- {sglang-0.3.4.post1.dist-info → sglang-0.3.5.dist-info}/WHEEL +1 -1
- sglang-0.3.4.post1.dist-info/METADATA +0 -900
- sglang-0.3.4.post1.dist-info/RECORD +0 -148
- {sglang-0.3.4.post1.dist-info → sglang-0.3.5.dist-info}/LICENSE +0 -0
- {sglang-0.3.4.post1.dist-info → sglang-0.3.5.dist-info}/top_level.txt +0 -0
@@ -1,900 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: sglang
|
3
|
-
Version: 0.3.4.post1
|
4
|
-
Summary: SGLang is yet another fast serving framework for large language models and vision language models.
|
5
|
-
License: Apache License
|
6
|
-
Version 2.0, January 2004
|
7
|
-
http://www.apache.org/licenses/
|
8
|
-
|
9
|
-
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
10
|
-
|
11
|
-
1. Definitions.
|
12
|
-
|
13
|
-
"License" shall mean the terms and conditions for use, reproduction,
|
14
|
-
and distribution as defined by Sections 1 through 9 of this document.
|
15
|
-
|
16
|
-
"Licensor" shall mean the copyright owner or entity authorized by
|
17
|
-
the copyright owner that is granting the License.
|
18
|
-
|
19
|
-
"Legal Entity" shall mean the union of the acting entity and all
|
20
|
-
other entities that control, are controlled by, or are under common
|
21
|
-
control with that entity. For the purposes of this definition,
|
22
|
-
"control" means (i) the power, direct or indirect, to cause the
|
23
|
-
direction or management of such entity, whether by contract or
|
24
|
-
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
25
|
-
outstanding shares, or (iii) beneficial ownership of such entity.
|
26
|
-
|
27
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
28
|
-
exercising permissions granted by this License.
|
29
|
-
|
30
|
-
"Source" form shall mean the preferred form for making modifications,
|
31
|
-
including but not limited to software source code, documentation
|
32
|
-
source, and configuration files.
|
33
|
-
|
34
|
-
"Object" form shall mean any form resulting from mechanical
|
35
|
-
transformation or translation of a Source form, including but
|
36
|
-
not limited to compiled object code, generated documentation,
|
37
|
-
and conversions to other media types.
|
38
|
-
|
39
|
-
"Work" shall mean the work of authorship, whether in Source or
|
40
|
-
Object form, made available under the License, as indicated by a
|
41
|
-
copyright notice that is included in or attached to the work
|
42
|
-
(an example is provided in the Appendix below).
|
43
|
-
|
44
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
45
|
-
form, that is based on (or derived from) the Work and for which the
|
46
|
-
editorial revisions, annotations, elaborations, or other modifications
|
47
|
-
represent, as a whole, an original work of authorship. For the purposes
|
48
|
-
of this License, Derivative Works shall not include works that remain
|
49
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
50
|
-
the Work and Derivative Works thereof.
|
51
|
-
|
52
|
-
"Contribution" shall mean any work of authorship, including
|
53
|
-
the original version of the Work and any modifications or additions
|
54
|
-
to that Work or Derivative Works thereof, that is intentionally
|
55
|
-
submitted to Licensor for inclusion in the Work by the copyright owner
|
56
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
57
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
58
|
-
means any form of electronic, verbal, or written communication sent
|
59
|
-
to the Licensor or its representatives, including but not limited to
|
60
|
-
communication on electronic mailing lists, source code control systems,
|
61
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
62
|
-
Licensor for the purpose of discussing and improving the Work, but
|
63
|
-
excluding communication that is conspicuously marked or otherwise
|
64
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
65
|
-
|
66
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
67
|
-
on behalf of whom a Contribution has been received by Licensor and
|
68
|
-
subsequently incorporated within the Work.
|
69
|
-
|
70
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
71
|
-
this License, each Contributor hereby grants to You a perpetual,
|
72
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
73
|
-
copyright license to reproduce, prepare Derivative Works of,
|
74
|
-
publicly display, publicly perform, sublicense, and distribute the
|
75
|
-
Work and such Derivative Works in Source or Object form.
|
76
|
-
|
77
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
78
|
-
this License, each Contributor hereby grants to You a perpetual,
|
79
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
80
|
-
(except as stated in this section) patent license to make, have made,
|
81
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
82
|
-
where such license applies only to those patent claims licensable
|
83
|
-
by such Contributor that are necessarily infringed by their
|
84
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
85
|
-
with the Work to which such Contribution(s) was submitted. If You
|
86
|
-
institute patent litigation against any entity (including a
|
87
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
88
|
-
or a Contribution incorporated within the Work constitutes direct
|
89
|
-
or contributory patent infringement, then any patent licenses
|
90
|
-
granted to You under this License for that Work shall terminate
|
91
|
-
as of the date such litigation is filed.
|
92
|
-
|
93
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
94
|
-
Work or Derivative Works thereof in any medium, with or without
|
95
|
-
modifications, and in Source or Object form, provided that You
|
96
|
-
meet the following conditions:
|
97
|
-
|
98
|
-
(a) You must give any other recipients of the Work or
|
99
|
-
Derivative Works a copy of this License; and
|
100
|
-
|
101
|
-
(b) You must cause any modified files to carry prominent notices
|
102
|
-
stating that You changed the files; and
|
103
|
-
|
104
|
-
(c) You must retain, in the Source form of any Derivative Works
|
105
|
-
that You distribute, all copyright, patent, trademark, and
|
106
|
-
attribution notices from the Source form of the Work,
|
107
|
-
excluding those notices that do not pertain to any part of
|
108
|
-
the Derivative Works; and
|
109
|
-
|
110
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
111
|
-
distribution, then any Derivative Works that You distribute must
|
112
|
-
include a readable copy of the attribution notices contained
|
113
|
-
within such NOTICE file, excluding those notices that do not
|
114
|
-
pertain to any part of the Derivative Works, in at least one
|
115
|
-
of the following places: within a NOTICE text file distributed
|
116
|
-
as part of the Derivative Works; within the Source form or
|
117
|
-
documentation, if provided along with the Derivative Works; or,
|
118
|
-
within a display generated by the Derivative Works, if and
|
119
|
-
wherever such third-party notices normally appear. The contents
|
120
|
-
of the NOTICE file are for informational purposes only and
|
121
|
-
do not modify the License. You may add Your own attribution
|
122
|
-
notices within Derivative Works that You distribute, alongside
|
123
|
-
or as an addendum to the NOTICE text from the Work, provided
|
124
|
-
that such additional attribution notices cannot be construed
|
125
|
-
as modifying the License.
|
126
|
-
|
127
|
-
You may add Your own copyright statement to Your modifications and
|
128
|
-
may provide additional or different license terms and conditions
|
129
|
-
for use, reproduction, or distribution of Your modifications, or
|
130
|
-
for any such Derivative Works as a whole, provided Your use,
|
131
|
-
reproduction, and distribution of the Work otherwise complies with
|
132
|
-
the conditions stated in this License.
|
133
|
-
|
134
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
135
|
-
any Contribution intentionally submitted for inclusion in the Work
|
136
|
-
by You to the Licensor shall be under the terms and conditions of
|
137
|
-
this License, without any additional terms or conditions.
|
138
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
139
|
-
the terms of any separate license agreement you may have executed
|
140
|
-
with Licensor regarding such Contributions.
|
141
|
-
|
142
|
-
6. Trademarks. This License does not grant permission to use the trade
|
143
|
-
names, trademarks, service marks, or product names of the Licensor,
|
144
|
-
except as required for reasonable and customary use in describing the
|
145
|
-
origin of the Work and reproducing the content of the NOTICE file.
|
146
|
-
|
147
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
148
|
-
agreed to in writing, Licensor provides the Work (and each
|
149
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
150
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
151
|
-
implied, including, without limitation, any warranties or conditions
|
152
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
153
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
154
|
-
appropriateness of using or redistributing the Work and assume any
|
155
|
-
risks associated with Your exercise of permissions under this License.
|
156
|
-
|
157
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
158
|
-
whether in tort (including negligence), contract, or otherwise,
|
159
|
-
unless required by applicable law (such as deliberate and grossly
|
160
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
161
|
-
liable to You for damages, including any direct, indirect, special,
|
162
|
-
incidental, or consequential damages of any character arising as a
|
163
|
-
result of this License or out of the use or inability to use the
|
164
|
-
Work (including but not limited to damages for loss of goodwill,
|
165
|
-
work stoppage, computer failure or malfunction, or any and all
|
166
|
-
other commercial damages or losses), even if such Contributor
|
167
|
-
has been advised of the possibility of such damages.
|
168
|
-
|
169
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
170
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
171
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
172
|
-
or other liability obligations and/or rights consistent with this
|
173
|
-
License. However, in accepting such obligations, You may act only
|
174
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
175
|
-
of any other Contributor, and only if You agree to indemnify,
|
176
|
-
defend, and hold each Contributor harmless for any liability
|
177
|
-
incurred by, or claims asserted against, such Contributor by reason
|
178
|
-
of your accepting any such warranty or additional liability.
|
179
|
-
|
180
|
-
END OF TERMS AND CONDITIONS
|
181
|
-
|
182
|
-
APPENDIX: How to apply the Apache License to your work.
|
183
|
-
|
184
|
-
To apply the Apache License to your work, attach the following
|
185
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
186
|
-
replaced with your own identifying information. (Don't include
|
187
|
-
the brackets!) The text should be enclosed in the appropriate
|
188
|
-
comment syntax for the file format. We also recommend that a
|
189
|
-
file or class name and description of purpose be included on the
|
190
|
-
same "printed page" as the copyright notice for easier
|
191
|
-
identification within third-party archives.
|
192
|
-
|
193
|
-
Copyright [yyyy] [name of copyright owner]
|
194
|
-
|
195
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
196
|
-
you may not use this file except in compliance with the License.
|
197
|
-
You may obtain a copy of the License at
|
198
|
-
|
199
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
200
|
-
|
201
|
-
Unless required by applicable law or agreed to in writing, software
|
202
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
203
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
204
|
-
See the License for the specific language governing permissions and
|
205
|
-
limitations under the License.
|
206
|
-
|
207
|
-
Project-URL: Homepage, https://github.com/sgl-project/sglang
|
208
|
-
Project-URL: Bug Tracker, https://github.com/sgl-project/sglang/issues
|
209
|
-
Classifier: Programming Language :: Python :: 3
|
210
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
211
|
-
Requires-Python: >=3.8
|
212
|
-
Description-Content-Type: text/markdown
|
213
|
-
License-File: LICENSE
|
214
|
-
Requires-Dist: requests
|
215
|
-
Requires-Dist: tqdm
|
216
|
-
Requires-Dist: numpy
|
217
|
-
Provides-Extra: all
|
218
|
-
Requires-Dist: sglang[srt]; extra == "all"
|
219
|
-
Requires-Dist: sglang[openai]; extra == "all"
|
220
|
-
Requires-Dist: sglang[anthropic]; extra == "all"
|
221
|
-
Requires-Dist: sglang[litellm]; extra == "all"
|
222
|
-
Provides-Extra: all_xpu
|
223
|
-
Requires-Dist: sglang[srt_xpu]; extra == "all-xpu"
|
224
|
-
Requires-Dist: sglang[openai]; extra == "all-xpu"
|
225
|
-
Requires-Dist: sglang[anthropic]; extra == "all-xpu"
|
226
|
-
Requires-Dist: sglang[litellm]; extra == "all-xpu"
|
227
|
-
Provides-Extra: anthropic
|
228
|
-
Requires-Dist: anthropic>=0.20.0; extra == "anthropic"
|
229
|
-
Provides-Extra: dev
|
230
|
-
Requires-Dist: sglang[all]; extra == "dev"
|
231
|
-
Requires-Dist: sglang[test]; extra == "dev"
|
232
|
-
Provides-Extra: dev_xpu
|
233
|
-
Requires-Dist: sglang[all_xpu]; extra == "dev-xpu"
|
234
|
-
Requires-Dist: sglang[test]; extra == "dev-xpu"
|
235
|
-
Provides-Extra: litellm
|
236
|
-
Requires-Dist: litellm>=1.0.0; extra == "litellm"
|
237
|
-
Provides-Extra: openai
|
238
|
-
Requires-Dist: openai>=1.0; extra == "openai"
|
239
|
-
Requires-Dist: tiktoken; extra == "openai"
|
240
|
-
Provides-Extra: runtime_common
|
241
|
-
Requires-Dist: aiohttp; extra == "runtime-common"
|
242
|
-
Requires-Dist: decord; extra == "runtime-common"
|
243
|
-
Requires-Dist: fastapi; extra == "runtime-common"
|
244
|
-
Requires-Dist: hf-transfer; extra == "runtime-common"
|
245
|
-
Requires-Dist: huggingface-hub; extra == "runtime-common"
|
246
|
-
Requires-Dist: interegular; extra == "runtime-common"
|
247
|
-
Requires-Dist: orjson; extra == "runtime-common"
|
248
|
-
Requires-Dist: packaging; extra == "runtime-common"
|
249
|
-
Requires-Dist: pillow; extra == "runtime-common"
|
250
|
-
Requires-Dist: psutil; extra == "runtime-common"
|
251
|
-
Requires-Dist: pydantic; extra == "runtime-common"
|
252
|
-
Requires-Dist: python-multipart; extra == "runtime-common"
|
253
|
-
Requires-Dist: torchao; extra == "runtime-common"
|
254
|
-
Requires-Dist: uvicorn; extra == "runtime-common"
|
255
|
-
Requires-Dist: uvloop; extra == "runtime-common"
|
256
|
-
Requires-Dist: zmq; extra == "runtime-common"
|
257
|
-
Requires-Dist: outlines>=0.0.44; extra == "runtime-common"
|
258
|
-
Requires-Dist: modelscope; extra == "runtime-common"
|
259
|
-
Provides-Extra: srt
|
260
|
-
Requires-Dist: sglang[runtime_common]; extra == "srt"
|
261
|
-
Requires-Dist: torch; extra == "srt"
|
262
|
-
Requires-Dist: vllm==0.6.3.post1; extra == "srt"
|
263
|
-
Provides-Extra: srt_xpu
|
264
|
-
Requires-Dist: sglang[runtime_common]; extra == "srt-xpu"
|
265
|
-
Provides-Extra: test
|
266
|
-
Requires-Dist: jsonlines; extra == "test"
|
267
|
-
Requires-Dist: matplotlib; extra == "test"
|
268
|
-
Requires-Dist: pandas; extra == "test"
|
269
|
-
Requires-Dist: sentence-transformers; extra == "test"
|
270
|
-
Requires-Dist: accelerate; extra == "test"
|
271
|
-
Requires-Dist: peft; extra == "test"
|
272
|
-
|
273
|
-
<div align="center" id="sglangtop">
|
274
|
-
<img src="https://raw.githubusercontent.com/sgl-project/sglang/main/assets/logo.png" alt="logo" width="400" margin="10px"></img>
|
275
|
-
|
276
|
-
[](https://pypi.org/project/sglang)
|
277
|
-

|
278
|
-
[](https://github.com/sgl-project/sglang/tree/main/LICENSE)
|
279
|
-
[](https://github.com/sgl-project/sglang/issues)
|
280
|
-
[](https://github.com/sgl-project/sglang/issues)
|
281
|
-
|
282
|
-
</div>
|
283
|
-
|
284
|
-
--------------------------------------------------------------------------------
|
285
|
-
|
286
|
-
| [**Blog**](https://lmsys.org/blog/2024-07-25-sglang-llama3/) | [**Paper**](https://arxiv.org/abs/2312.07104) | [**Slides**](https://github.com/sgl-project/sgl-learning-materials/blob/main/slides/amd_dev_day_v2.pdf) | [**Learn More**](https://github.com/sgl-project/sgl-learning-materials) | [**Join Slack**](https://join.slack.com/t/sgl-fru7574/shared_invite/zt-2ngly9muu-t37XiH87qvD~6rVBTkTEHw) |
|
287
|
-
[**Join Bi-Weekly Development Meeting**](https://docs.google.com/document/d/1xEow4eIM152xNcRxqZz9VEcOiTQo8-CEuuQ5qTmkt-E/edit?usp=sharing) |
|
288
|
-
|
289
|
-
## News
|
290
|
-
- [2024/10] 🔥 The First SGLang Online Meetup ([slides](https://github.com/sgl-project/sgl-learning-materials?tab=readme-ov-file#the-first-sglang-online-meetup)).
|
291
|
-
- [2024/09] SGLang v0.3 Release: 7x Faster DeepSeek MLA, 1.5x Faster torch.compile, Multi-Image/Video LLaVA-OneVision ([blog](https://lmsys.org/blog/2024-09-04-sglang-v0-3/)).
|
292
|
-
- [2024/07] Faster Llama3 Serving with SGLang Runtime (vs. TensorRT-LLM, vLLM) ([blog](https://lmsys.org/blog/2024-07-25-sglang-llama3/)).
|
293
|
-
|
294
|
-
<details>
|
295
|
-
<summary>More</summary>
|
296
|
-
|
297
|
-
- [2024/02] SGLang enables **3x faster JSON decoding** with compressed finite state machine ([blog](https://lmsys.org/blog/2024-02-05-compressed-fsm/)).
|
298
|
-
- [2024/04] SGLang is used by the official **LLaVA-NeXT (video)** release ([blog](https://llava-vl.github.io/blog/2024-04-30-llava-next-video/)).
|
299
|
-
- [2024/01] SGLang provides up to **5x faster inference** with RadixAttention ([blog](https://lmsys.org/blog/2024-01-17-sglang/)).
|
300
|
-
- [2024/01] SGLang powers the serving of the official **LLaVA v1.6** release demo ([usage](https://github.com/haotian-liu/LLaVA?tab=readme-ov-file#demo)).
|
301
|
-
|
302
|
-
</details>
|
303
|
-
|
304
|
-
## About
|
305
|
-
SGLang is a fast serving framework for large language models and vision language models.
|
306
|
-
It makes your interaction with models faster and more controllable by co-designing the backend runtime and frontend language.
|
307
|
-
The core features include:
|
308
|
-
|
309
|
-
- **Fast Backend Runtime**: Provides efficient serving with RadixAttention for prefix caching, jump-forward constrained decoding, continuous batching, token attention (paged attention), tensor parallelism, FlashInfer kernels, chunked prefill, and quantization (INT4/FP8/AWQ/GPTQ).
|
310
|
-
- **Flexible Frontend Language**: Offers an intuitive interface for programming LLM applications, including chained generation calls, advanced prompting, control flow, multi-modal inputs, parallelism, and external interactions.
|
311
|
-
- **Extensive Model Support**: Supports a wide range of generative models (Llama, Gemma, Mistral, QWen, DeepSeek, LLaVA, etc.) and embedding models (e5-mistral), with easy extensibility for integrating new models.
|
312
|
-
- **Active Community**: SGLang is open-source and backed by an active community with industry adoption.
|
313
|
-
|
314
|
-
## Contents
|
315
|
-
- [Install](#install)
|
316
|
-
- [Backend: SGLang Runtime (SRT)](#backend-sglang-runtime-srt)
|
317
|
-
- [Frontend: Structured Generation Language (SGLang)](#frontend-structured-generation-language-sglang)
|
318
|
-
- [Benchmark And Performance](#benchmark-and-performance)
|
319
|
-
- [Roadmap](#roadmap)
|
320
|
-
- [Citation And Acknowledgment](#citation-and-acknowledgment)
|
321
|
-
|
322
|
-
## Install
|
323
|
-
|
324
|
-
You can install SGLang using any of the methods below.
|
325
|
-
|
326
|
-
### Method 1: With pip
|
327
|
-
```
|
328
|
-
pip install --upgrade pip
|
329
|
-
pip install "sglang[all]"
|
330
|
-
|
331
|
-
# Install FlashInfer CUDA kernels
|
332
|
-
pip install flashinfer -i https://flashinfer.ai/whl/cu121/torch2.4/
|
333
|
-
```
|
334
|
-
|
335
|
-
### Method 2: From source
|
336
|
-
```
|
337
|
-
# Use the last release branch
|
338
|
-
git clone -b v0.3.4.post1 https://github.com/sgl-project/sglang.git
|
339
|
-
cd sglang
|
340
|
-
|
341
|
-
pip install --upgrade pip
|
342
|
-
pip install -e "python[all]"
|
343
|
-
|
344
|
-
# Install FlashInfer CUDA kernels
|
345
|
-
pip install flashinfer -i https://flashinfer.ai/whl/cu121/torch2.4/
|
346
|
-
```
|
347
|
-
|
348
|
-
### Method 3: Using docker
|
349
|
-
The docker images are available on Docker Hub as [lmsysorg/sglang](https://hub.docker.com/r/lmsysorg/sglang/tags), built from [Dockerfile](https://github.com/sgl-project/sglang/tree/main/docker).
|
350
|
-
Replace `<secret>` below with your huggingface hub [token](https://huggingface.co/docs/hub/en/security-tokens).
|
351
|
-
|
352
|
-
```bash
|
353
|
-
docker run --gpus all \
|
354
|
-
-p 30000:30000 \
|
355
|
-
-v ~/.cache/huggingface:/root/.cache/huggingface \
|
356
|
-
--env "HF_TOKEN=<secret>" \
|
357
|
-
--ipc=host \
|
358
|
-
lmsysorg/sglang:latest \
|
359
|
-
python3 -m sglang.launch_server --model-path meta-llama/Llama-3.1-8B-Instruct --host 0.0.0.0 --port 30000
|
360
|
-
```
|
361
|
-
|
362
|
-
### Method 4: Using docker compose
|
363
|
-
|
364
|
-
<details>
|
365
|
-
<summary>More</summary>
|
366
|
-
|
367
|
-
> This method is recommended if you plan to serve it as a service.
|
368
|
-
> A better approach is to use the [k8s-sglang-service.yaml](docker/k8s-sglang-service.yaml).
|
369
|
-
|
370
|
-
1. Copy the [compose.yml](docker/compose.yaml) to your local machine
|
371
|
-
2. Execute the command `docker compose up -d` in your terminal.
|
372
|
-
</details>
|
373
|
-
|
374
|
-
### Method 5: Run on Kubernetes or Clouds with SkyPilot
|
375
|
-
|
376
|
-
<details>
|
377
|
-
<summary>More</summary>
|
378
|
-
|
379
|
-
To deploy on Kubernetes or 12+ clouds, you can use [SkyPilot](https://github.com/skypilot-org/skypilot).
|
380
|
-
|
381
|
-
1. Install SkyPilot and set up Kubernetes cluster or cloud access: see [SkyPilot's documentation](https://skypilot.readthedocs.io/en/latest/getting-started/installation.html).
|
382
|
-
2. Deploy on your own infra with a single command and get the HTTP API endpoint:
|
383
|
-
<details>
|
384
|
-
<summary>SkyPilot YAML: <code>sglang.yaml</code></summary>
|
385
|
-
|
386
|
-
```yaml
|
387
|
-
# sglang.yaml
|
388
|
-
envs:
|
389
|
-
HF_TOKEN: null
|
390
|
-
|
391
|
-
resources:
|
392
|
-
image_id: docker:lmsysorg/sglang:latest
|
393
|
-
accelerators: A100
|
394
|
-
ports: 30000
|
395
|
-
|
396
|
-
run: |
|
397
|
-
conda deactivate
|
398
|
-
python3 -m sglang.launch_server \
|
399
|
-
--model-path meta-llama/Llama-3.1-8B-Instruct \
|
400
|
-
--host 0.0.0.0 \
|
401
|
-
--port 30000
|
402
|
-
```
|
403
|
-
</details>
|
404
|
-
|
405
|
-
```bash
|
406
|
-
# Deploy on any cloud or Kubernetes cluster. Use --cloud <cloud> to select a specific cloud provider.
|
407
|
-
HF_TOKEN=<secret> sky launch -c sglang --env HF_TOKEN sglang.yaml
|
408
|
-
|
409
|
-
# Get the HTTP API endpoint
|
410
|
-
sky status --endpoint 30000 sglang
|
411
|
-
```
|
412
|
-
3. To further scale up your deployment with autoscaling and failure recovery, check out the [SkyServe + SGLang guide](https://github.com/skypilot-org/skypilot/tree/master/llm/sglang#serving-llama-2-with-sglang-for-more-traffic-using-skyserve).
|
413
|
-
</details>
|
414
|
-
|
415
|
-
|
416
|
-
### Common Notes
|
417
|
-
- [FlashInfer](https://github.com/flashinfer-ai/flashinfer) is the default attention kernel backend. It only supports sm75 and above. If you encounter any FlashInfer-related issues on sm75+ devices (e.g., T4, A10, A100, L4, L40S, H100), please switch to other kernels by adding `--attention-backend triton --sampling-backend pytorch` and open an issue on GitHub.
|
418
|
-
- If you only need to use the OpenAI backend, you can avoid installing other dependencies by using `pip install "sglang[openai]"`.
|
419
|
-
|
420
|
-
## Backend: SGLang Runtime (SRT)
|
421
|
-
The SGLang Runtime (SRT) is an efficient serving engine.
|
422
|
-
|
423
|
-
### Quick Start
|
424
|
-
Launch a server
|
425
|
-
```
|
426
|
-
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3-8B-Instruct --port 30000
|
427
|
-
```
|
428
|
-
|
429
|
-
Send a request
|
430
|
-
```
|
431
|
-
curl http://localhost:30000/generate \
|
432
|
-
-H "Content-Type: application/json" \
|
433
|
-
-d '{
|
434
|
-
"text": "Once upon a time,",
|
435
|
-
"sampling_params": {
|
436
|
-
"max_new_tokens": 16,
|
437
|
-
"temperature": 0
|
438
|
-
}
|
439
|
-
}'
|
440
|
-
```
|
441
|
-
|
442
|
-
Learn more about the argument specification, streaming, and multi-modal support [here](docs/en/sampling_params.md).
|
443
|
-
|
444
|
-
### OpenAI Compatible API
|
445
|
-
In addition, the server supports OpenAI-compatible APIs.
|
446
|
-
|
447
|
-
```python
|
448
|
-
import openai
|
449
|
-
client = openai.Client(
|
450
|
-
base_url="http://127.0.0.1:30000/v1", api_key="EMPTY")
|
451
|
-
|
452
|
-
# Text completion
|
453
|
-
response = client.completions.create(
|
454
|
-
model="default",
|
455
|
-
prompt="The capital of France is",
|
456
|
-
temperature=0,
|
457
|
-
max_tokens=32,
|
458
|
-
)
|
459
|
-
print(response)
|
460
|
-
|
461
|
-
# Chat completion
|
462
|
-
response = client.chat.completions.create(
|
463
|
-
model="default",
|
464
|
-
messages=[
|
465
|
-
{"role": "system", "content": "You are a helpful AI assistant"},
|
466
|
-
{"role": "user", "content": "List 3 countries and their capitals."},
|
467
|
-
],
|
468
|
-
temperature=0,
|
469
|
-
max_tokens=64,
|
470
|
-
)
|
471
|
-
print(response)
|
472
|
-
|
473
|
-
# Text embedding
|
474
|
-
response = client.embeddings.create(
|
475
|
-
model="default",
|
476
|
-
input="How are you today",
|
477
|
-
)
|
478
|
-
print(response)
|
479
|
-
```
|
480
|
-
|
481
|
-
It supports streaming, vision, and almost all features of the Chat/Completions/Models/Batch endpoints specified by the [OpenAI API Reference](https://platform.openai.com/docs/api-reference/).
|
482
|
-
|
483
|
-
### Additional Server Arguments
|
484
|
-
- To enable multi-GPU tensor parallelism, add `--tp 2`. If it reports the error "peer access is not supported between these two devices", add `--enable-p2p-check` to the server launch command.
|
485
|
-
```
|
486
|
-
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3-8B-Instruct --tp 2
|
487
|
-
```
|
488
|
-
- To enable multi-GPU data parallelism, add `--dp 2`. Data parallelism is better for throughput if there is enough memory. It can also be used together with tensor parallelism. The following command uses 4 GPUs in total.
|
489
|
-
```
|
490
|
-
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3-8B-Instruct --dp 2 --tp 2
|
491
|
-
```
|
492
|
-
- If you see out-of-memory errors during serving, try to reduce the memory usage of the KV cache pool by setting a smaller value of `--mem-fraction-static`. The default value is `0.9`.
|
493
|
-
```
|
494
|
-
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3-8B-Instruct --mem-fraction-static 0.7
|
495
|
-
```
|
496
|
-
- See [hyperparameter_tuning.md](docs/en/hyperparameter_tuning.md) on tuning hyperparameters for better performance.
|
497
|
-
- If you see out-of-memory errors during prefill for long prompts, try to set a smaller chunked prefill size.
|
498
|
-
```
|
499
|
-
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3-8B-Instruct --chunked-prefill-size 4096
|
500
|
-
```
|
501
|
-
- To enable torch.compile acceleration, add `--enable-torch-compile`. It accelerates small models on small batch sizes.
|
502
|
-
- To enable torchao quantization, add `--torchao-config int4wo-128`. It supports various quantization strategies.
|
503
|
-
- To enable fp8 weight quantization, add `--quantization fp8` on a fp16 checkpoint or directly load a fp8 checkpoint without specifying any arguments.
|
504
|
-
- To enable fp8 kv cache quantization, add `--kv-cache-dtype fp8_e5m2`.
|
505
|
-
- If the model does not have a chat template in the Hugging Face tokenizer, you can specify a [custom chat template](docs/en/custom_chat_template.md).
|
506
|
-
- To run tensor parallelism on multiple nodes, add `--nnodes 2`. If you have two nodes with two GPUs on each node and want to run TP=4, let `sgl-dev-0` be the hostname of the first node and `50000` be an available port, you can use the following commands. If you meet deadlock, please try to add `--disable-cuda-graph`
|
507
|
-
```
|
508
|
-
# Node 0
|
509
|
-
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3-8B-Instruct --tp 4 --nccl-init sgl-dev-0:50000 --nnodes 2 --node-rank 0
|
510
|
-
|
511
|
-
# Node 1
|
512
|
-
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3-8B-Instruct --tp 4 --nccl-init sgl-dev-0:50000 --nnodes 2 --node-rank 1
|
513
|
-
```
|
514
|
-
|
515
|
-
### Engine Without HTTP Server
|
516
|
-
|
517
|
-
We also provide an inference engine **without a HTTP server**. For example,
|
518
|
-
|
519
|
-
```python
|
520
|
-
import sglang as sgl
|
521
|
-
|
522
|
-
|
523
|
-
def main():
|
524
|
-
prompts = [
|
525
|
-
"Hello, my name is",
|
526
|
-
"The president of the United States is",
|
527
|
-
"The capital of France is",
|
528
|
-
"The future of AI is",
|
529
|
-
]
|
530
|
-
sampling_params = {"temperature": 0.8, "top_p": 0.95}
|
531
|
-
llm = sgl.Engine(model_path="meta-llama/Meta-Llama-3.1-8B-Instruct")
|
532
|
-
|
533
|
-
outputs = llm.generate(prompts, sampling_params)
|
534
|
-
for prompt, output in zip(prompts, outputs):
|
535
|
-
print("===============================")
|
536
|
-
print(f"Prompt: {prompt}\nGenerated text: {output['text']}")
|
537
|
-
|
538
|
-
if __name__ == "__main__":
|
539
|
-
main()
|
540
|
-
```
|
541
|
-
|
542
|
-
This can be used for:
|
543
|
-
|
544
|
-
1. **Offline Batch Inference**
|
545
|
-
2. **Building Custom Servers**
|
546
|
-
|
547
|
-
You can view the full example [here](https://github.com/sgl-project/sglang/tree/main/examples/runtime/engine)
|
548
|
-
|
549
|
-
### Supported Models
|
550
|
-
|
551
|
-
**Generative Models**
|
552
|
-
- Llama / Llama 2 / Llama 3 / Llama 3.1
|
553
|
-
- Mistral / Mixtral / Mistral NeMo
|
554
|
-
- Gemma / Gemma 2
|
555
|
-
- Qwen / Qwen 2 / Qwen 2 MoE
|
556
|
-
- DeepSeek / DeepSeek 2
|
557
|
-
- OLMoE
|
558
|
-
- [LLaVA-OneVision](https://llava-vl.github.io/blog/2024-08-05-llava-onevision/)
|
559
|
-
- `python3 -m sglang.launch_server --model-path lmms-lab/llava-onevision-qwen2-7b-ov --port=30000 --chat-template=chatml-llava`
|
560
|
-
- `python3 -m sglang.launch_server --model-path lmms-lab/llava-onevision-qwen2-72b-ov --port=30000 --tp-size=8 --chat-template=chatml-llava`
|
561
|
-
- Query the server with the [OpenAI Vision API](https://platform.openai.com/docs/guides/vision). See examples at [test/srt/test_vision_openai_server.py](test/srt/test_vision_openai_server.py)
|
562
|
-
- LLaVA 1.5 / 1.6 / NeXT
|
563
|
-
- `python -m sglang.launch_server --model-path lmms-lab/llama3-llava-next-8b --port=30000 --tp-size=1 --chat-template=llava_llama_3`
|
564
|
-
- `python -m sglang.launch_server --model-path lmms-lab/llava-next-72b --port=30000 --tp-size=8 --chat-template=chatml-llava`
|
565
|
-
- Query the server with the [OpenAI Vision API](https://platform.openai.com/docs/guides/vision). See examples at [test/srt/test_vision_openai_server.py](test/srt/test_vision_openai_server.py)
|
566
|
-
- Yi-VL
|
567
|
-
- StableLM
|
568
|
-
- Command-R
|
569
|
-
- DBRX
|
570
|
-
- Grok
|
571
|
-
- ChatGLM
|
572
|
-
- InternLM 2
|
573
|
-
- Exaone 3
|
574
|
-
- BaiChuan2
|
575
|
-
- MiniCPM / MiniCPM 3
|
576
|
-
- XVERSE / XVERSE MoE
|
577
|
-
- SmolLM
|
578
|
-
- GLM-4
|
579
|
-
|
580
|
-
**Embedding Models**
|
581
|
-
|
582
|
-
- e5-mistral
|
583
|
-
- gte-Qwen2
|
584
|
-
- `python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct --is-embedding`
|
585
|
-
|
586
|
-
Instructions for supporting a new model are [here](docs/en/model_support.md).
|
587
|
-
|
588
|
-
#### Use Models From ModelScope
|
589
|
-
<details>
|
590
|
-
<summary>More</summary>
|
591
|
-
|
592
|
-
To use a model from [ModelScope](https://www.modelscope.cn), set the environment variable SGLANG_USE_MODELSCOPE.
|
593
|
-
```
|
594
|
-
export SGLANG_USE_MODELSCOPE=true
|
595
|
-
```
|
596
|
-
Launch [Qwen2-7B-Instruct](https://www.modelscope.cn/models/qwen/qwen2-7b-instruct) Server
|
597
|
-
```
|
598
|
-
SGLANG_USE_MODELSCOPE=true python -m sglang.launch_server --model-path qwen/Qwen2-7B-Instruct --port 30000
|
599
|
-
```
|
600
|
-
|
601
|
-
Or start it by docker.
|
602
|
-
```bash
|
603
|
-
docker run --gpus all \
|
604
|
-
-p 30000:30000 \
|
605
|
-
-v ~/.cache/modelscope:/root/.cache/modelscope \
|
606
|
-
--env "SGLANG_USE_MODELSCOPE=true" \
|
607
|
-
--ipc=host \
|
608
|
-
lmsysorg/sglang:latest \
|
609
|
-
python3 -m sglang.launch_server --model-path Qwen/Qwen2.5-7B-Instruct --host 0.0.0.0 --port 30000
|
610
|
-
```
|
611
|
-
|
612
|
-
</details>
|
613
|
-
|
614
|
-
#### Run Llama 3.1 405B
|
615
|
-
<details>
|
616
|
-
<summary>More</summary>
|
617
|
-
|
618
|
-
```bash
|
619
|
-
# Run 405B (fp8) on a single node
|
620
|
-
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3.1-405B-Instruct-FP8 --tp 8
|
621
|
-
|
622
|
-
# Run 405B (fp16) on two nodes
|
623
|
-
## on the first node, replace the `172.16.4.52:20000` with your own first node ip address and port
|
624
|
-
GLOO_SOCKET_IFNAME=eth0 python3 -m sglang.launch_server --model-path meta-llama/Meta-Llama-3.1-405B-Instruct --tp 16 --nccl-init-addr 172.16.4.52:20000 --nnodes 2 --node-rank 0 --disable-cuda-graph
|
625
|
-
|
626
|
-
## on the first node, replace the `172.16.4.52:20000` with your own first node ip address and port
|
627
|
-
GLOO_SOCKET_IFNAME=eth0 python3 -m sglang.launch_server --model-path meta-llama/Meta-Llama-3.1-405B-Instruct --tp 16 --nccl-init-addr 172.16.4.52:20000 --nnodes 2 --node-rank 1 --disable-cuda-graph
|
628
|
-
```
|
629
|
-
|
630
|
-
</details>
|
631
|
-
|
632
|
-
### Benchmark Performance
|
633
|
-
|
634
|
-
- Benchmark a single static batch by running the following command without launching a server. The arguments are the same as for `launch_server.py`.
|
635
|
-
Note that this is not a dynamic batching server, so it may run out of memory for a batch size that a real server can handle.
|
636
|
-
A real server truncates the prefill into several batches, while this unit test does not. For accurate large batch testing, please use `sglang.bench_serving` instead.
|
637
|
-
```
|
638
|
-
python -m sglang.bench_latency --model-path meta-llama/Meta-Llama-3-8B-Instruct --batch 32 --input-len 256 --output-len 32
|
639
|
-
```
|
640
|
-
- Benchmark online serving. Launch a server first and run the following command.
|
641
|
-
```
|
642
|
-
python3 -m sglang.bench_serving --backend sglang --num-prompt 10
|
643
|
-
```
|
644
|
-
|
645
|
-
## Frontend: Structured Generation Language (SGLang)
|
646
|
-
The frontend language can be used with local models or API models. It is an alternative to the OpenAI API. You may found it easier to use for complex prompting workflow.
|
647
|
-
|
648
|
-
### Quick Start
|
649
|
-
The example below shows how to use sglang to answer a multi-turn question.
|
650
|
-
|
651
|
-
#### Using Local Models
|
652
|
-
First, launch a server with
|
653
|
-
```
|
654
|
-
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3-8B-Instruct --port 30000
|
655
|
-
```
|
656
|
-
|
657
|
-
Then, connect to the server and answer a multi-turn question.
|
658
|
-
|
659
|
-
```python
|
660
|
-
from sglang import function, system, user, assistant, gen, set_default_backend, RuntimeEndpoint
|
661
|
-
|
662
|
-
@function
|
663
|
-
def multi_turn_question(s, question_1, question_2):
|
664
|
-
s += system("You are a helpful assistant.")
|
665
|
-
s += user(question_1)
|
666
|
-
s += assistant(gen("answer_1", max_tokens=256))
|
667
|
-
s += user(question_2)
|
668
|
-
s += assistant(gen("answer_2", max_tokens=256))
|
669
|
-
|
670
|
-
set_default_backend(RuntimeEndpoint("http://localhost:30000"))
|
671
|
-
|
672
|
-
state = multi_turn_question.run(
|
673
|
-
question_1="What is the capital of the United States?",
|
674
|
-
question_2="List two local attractions.",
|
675
|
-
)
|
676
|
-
|
677
|
-
for m in state.messages():
|
678
|
-
print(m["role"], ":", m["content"])
|
679
|
-
|
680
|
-
print(state["answer_1"])
|
681
|
-
```
|
682
|
-
|
683
|
-
#### Using OpenAI Models
|
684
|
-
Set the OpenAI API Key
|
685
|
-
```
|
686
|
-
export OPENAI_API_KEY=sk-******
|
687
|
-
```
|
688
|
-
|
689
|
-
Then, answer a multi-turn question.
|
690
|
-
```python
|
691
|
-
from sglang import function, system, user, assistant, gen, set_default_backend, OpenAI
|
692
|
-
|
693
|
-
@function
|
694
|
-
def multi_turn_question(s, question_1, question_2):
|
695
|
-
s += system("You are a helpful assistant.")
|
696
|
-
s += user(question_1)
|
697
|
-
s += assistant(gen("answer_1", max_tokens=256))
|
698
|
-
s += user(question_2)
|
699
|
-
s += assistant(gen("answer_2", max_tokens=256))
|
700
|
-
|
701
|
-
set_default_backend(OpenAI("gpt-3.5-turbo"))
|
702
|
-
|
703
|
-
state = multi_turn_question.run(
|
704
|
-
question_1="What is the capital of the United States?",
|
705
|
-
question_2="List two local attractions.",
|
706
|
-
)
|
707
|
-
|
708
|
-
for m in state.messages():
|
709
|
-
print(m["role"], ":", m["content"])
|
710
|
-
|
711
|
-
print(state["answer_1"])
|
712
|
-
```
|
713
|
-
|
714
|
-
#### More Examples
|
715
|
-
|
716
|
-
Anthropic and VertexAI (Gemini) models are also supported.
|
717
|
-
You can find more examples at [examples/quick_start](examples/frontend_language/quick_start).
|
718
|
-
|
719
|
-
### Language Feature
|
720
|
-
To begin with, import sglang.
|
721
|
-
```python
|
722
|
-
import sglang as sgl
|
723
|
-
```
|
724
|
-
|
725
|
-
`sglang` provides some simple primitives such as `gen`, `select`, `fork`, `image`.
|
726
|
-
You can implement your prompt flow in a function decorated by `sgl.function`.
|
727
|
-
You can then invoke the function with `run` or `run_batch`.
|
728
|
-
The system will manage the state, chat template, parallelism and batching for you.
|
729
|
-
|
730
|
-
The complete code for the examples below can be found at [readme_examples.py](examples/frontend_language/usage/readme_examples.py)
|
731
|
-
|
732
|
-
#### Control Flow
|
733
|
-
You can use any Python code within the function body, including control flow, nested function calls, and external libraries.
|
734
|
-
|
735
|
-
```python
|
736
|
-
@sgl.function
|
737
|
-
def tool_use(s, question):
|
738
|
-
s += "To answer this question: " + question + ". "
|
739
|
-
s += "I need to use a " + sgl.gen("tool", choices=["calculator", "search engine"]) + ". "
|
740
|
-
|
741
|
-
if s["tool"] == "calculator":
|
742
|
-
s += "The math expression is" + sgl.gen("expression")
|
743
|
-
elif s["tool"] == "search engine":
|
744
|
-
s += "The key word to search is" + sgl.gen("word")
|
745
|
-
```
|
746
|
-
|
747
|
-
#### Parallelism
|
748
|
-
Use `fork` to launch parallel prompts.
|
749
|
-
Because `sgl.gen` is non-blocking, the for loop below issues two generation calls in parallel.
|
750
|
-
|
751
|
-
```python
|
752
|
-
@sgl.function
|
753
|
-
def tip_suggestion(s):
|
754
|
-
s += (
|
755
|
-
"Here are two tips for staying healthy: "
|
756
|
-
"1. Balanced Diet. 2. Regular Exercise.\n\n"
|
757
|
-
)
|
758
|
-
|
759
|
-
forks = s.fork(2)
|
760
|
-
for i, f in enumerate(forks):
|
761
|
-
f += f"Now, expand tip {i+1} into a paragraph:\n"
|
762
|
-
f += sgl.gen(f"detailed_tip", max_tokens=256, stop="\n\n")
|
763
|
-
|
764
|
-
s += "Tip 1:" + forks[0]["detailed_tip"] + "\n"
|
765
|
-
s += "Tip 2:" + forks[1]["detailed_tip"] + "\n"
|
766
|
-
s += "In summary" + sgl.gen("summary")
|
767
|
-
```
|
768
|
-
|
769
|
-
#### Multi-Modality
|
770
|
-
Use `sgl.image` to pass an image as input.
|
771
|
-
|
772
|
-
```python
|
773
|
-
@sgl.function
|
774
|
-
def image_qa(s, image_file, question):
|
775
|
-
s += sgl.user(sgl.image(image_file) + question)
|
776
|
-
s += sgl.assistant(sgl.gen("answer", max_tokens=256)
|
777
|
-
```
|
778
|
-
|
779
|
-
See also [srt_example_llava.py](examples/frontend_language/quick_start/local_example_llava_next.py).
|
780
|
-
|
781
|
-
#### Constrained Decoding
|
782
|
-
Use `regex` to specify a regular expression as a decoding constraint.
|
783
|
-
This is only supported for local models.
|
784
|
-
|
785
|
-
```python
|
786
|
-
@sgl.function
|
787
|
-
def regular_expression_gen(s):
|
788
|
-
s += "Q: What is the IP address of the Google DNS servers?\n"
|
789
|
-
s += "A: " + sgl.gen(
|
790
|
-
"answer",
|
791
|
-
temperature=0,
|
792
|
-
regex=r"((25[0-5]|2[0-4]\d|[01]?\d\d?).){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)",
|
793
|
-
)
|
794
|
-
```
|
795
|
-
|
796
|
-
#### JSON Decoding
|
797
|
-
Use `regex` to specify a JSON schema with a regular expression.
|
798
|
-
|
799
|
-
```python
|
800
|
-
character_regex = (
|
801
|
-
r"""\{\n"""
|
802
|
-
+ r""" "name": "[\w\d\s]{1,16}",\n"""
|
803
|
-
+ r""" "house": "(Gryffindor|Slytherin|Ravenclaw|Hufflepuff)",\n"""
|
804
|
-
+ r""" "blood status": "(Pure-blood|Half-blood|Muggle-born)",\n"""
|
805
|
-
+ r""" "occupation": "(student|teacher|auror|ministry of magic|death eater|order of the phoenix)",\n"""
|
806
|
-
+ r""" "wand": \{\n"""
|
807
|
-
+ r""" "wood": "[\w\d\s]{1,16}",\n"""
|
808
|
-
+ r""" "core": "[\w\d\s]{1,16}",\n"""
|
809
|
-
+ r""" "length": [0-9]{1,2}\.[0-9]{0,2}\n"""
|
810
|
-
+ r""" \},\n"""
|
811
|
-
+ r""" "alive": "(Alive|Deceased)",\n"""
|
812
|
-
+ r""" "patronus": "[\w\d\s]{1,16}",\n"""
|
813
|
-
+ r""" "bogart": "[\w\d\s]{1,16}"\n"""
|
814
|
-
+ r"""\}"""
|
815
|
-
)
|
816
|
-
|
817
|
-
@sgl.function
|
818
|
-
def character_gen(s, name):
|
819
|
-
s += name + " is a character in Harry Potter. Please fill in the following information about this character.\n"
|
820
|
-
s += sgl.gen("json_output", max_tokens=256, regex=character_regex)
|
821
|
-
```
|
822
|
-
|
823
|
-
See also [json_decode.py](examples/frontend_language/usage/json_decode.py) for an additional example of specifying formats with Pydantic models.
|
824
|
-
|
825
|
-
#### Batching
|
826
|
-
Use `run_batch` to run a batch of requests with continuous batching.
|
827
|
-
|
828
|
-
```python
|
829
|
-
@sgl.function
|
830
|
-
def text_qa(s, question):
|
831
|
-
s += "Q: " + question + "\n"
|
832
|
-
s += "A:" + sgl.gen("answer", stop="\n")
|
833
|
-
|
834
|
-
states = text_qa.run_batch(
|
835
|
-
[
|
836
|
-
{"question": "What is the capital of the United Kingdom?"},
|
837
|
-
{"question": "What is the capital of France?"},
|
838
|
-
{"question": "What is the capital of Japan?"},
|
839
|
-
],
|
840
|
-
progress_bar=True
|
841
|
-
)
|
842
|
-
```
|
843
|
-
|
844
|
-
#### Streaming
|
845
|
-
Add `stream=True` to enable streaming.
|
846
|
-
|
847
|
-
```python
|
848
|
-
@sgl.function
|
849
|
-
def text_qa(s, question):
|
850
|
-
s += "Q: " + question + "\n"
|
851
|
-
s += "A:" + sgl.gen("answer", stop="\n")
|
852
|
-
|
853
|
-
state = text_qa.run(
|
854
|
-
question="What is the capital of France?",
|
855
|
-
temperature=0.1,
|
856
|
-
stream=True
|
857
|
-
)
|
858
|
-
|
859
|
-
for out in state.text_iter():
|
860
|
-
print(out, end="", flush=True)
|
861
|
-
```
|
862
|
-
|
863
|
-
#### Roles
|
864
|
-
|
865
|
-
Use `sgl.system`, `sgl.user` and `sgl.assistant` to set roles when using Chat models. You can also define more complex role prompts using begin and end tokens.
|
866
|
-
|
867
|
-
```python
|
868
|
-
@sgl.function
|
869
|
-
def chat_example(s):
|
870
|
-
s += sgl.system("You are a helpful assistant.")
|
871
|
-
# Same as: s += s.system("You are a helpful assistant.")
|
872
|
-
|
873
|
-
with s.user():
|
874
|
-
s += "Question: What is the capital of France?"
|
875
|
-
|
876
|
-
s += sgl.assistant_begin()
|
877
|
-
s += "Answer: " + sgl.gen(max_tokens=100, stop="\n")
|
878
|
-
s += sgl.assistant_end()
|
879
|
-
```
|
880
|
-
|
881
|
-
#### Tips and Implementation Details
|
882
|
-
- The `choices` argument in `sgl.gen` is implemented by computing the [token-length normalized log probabilities](https://blog.eleuther.ai/multiple-choice-normalization/) of all choices and selecting the one with the highest probability.
|
883
|
-
- The `regex` argument in `sgl.gen` is implemented through autoregressive decoding with logit bias masking, according to the constraints set by the regex. It is compatible with `temperature=0` and `temperature != 0`.
|
884
|
-
|
885
|
-
## Benchmark And Performance
|
886
|
-
Learn more in our release blogs: [v0.2](https://lmsys.org/blog/2024-07-25-sglang-llama3/), [v0.3](https://lmsys.org/blog/2024-09-04-sglang-v0-3/).
|
887
|
-
|
888
|
-
## Roadmap
|
889
|
-
[Development Roadmap (2024 Q4)](https://github.com/sgl-project/sglang/issues/1487)
|
890
|
-
|
891
|
-
## Citation And Acknowledgment
|
892
|
-
Please cite our paper, [SGLang: Efficient Execution of Structured Language Model Programs](https://arxiv.org/abs/2312.07104), if you find the project useful.
|
893
|
-
We also learned from the design and reused code from the following projects: [Guidance](https://github.com/guidance-ai/guidance), [vLLM](https://github.com/vllm-project/vllm), [LightLLM](https://github.com/ModelTC/lightllm), [FlashInfer](https://github.com/flashinfer-ai/flashinfer), [Outlines](https://github.com/outlines-dev/outlines), and [LMQL](https://github.com/eth-sri/lmql).
|
894
|
-
|
895
|
-
|
896
|
-
<p align="center">
|
897
|
-
<a href="#sglangtop" target="_blank">
|
898
|
-
<bold>Back To Top </bold>
|
899
|
-
</a>
|
900
|
-
</p>
|