-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide an offline engine API (#1567)
- Loading branch information
Showing
6 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import sglang as sgl | ||
|
||
|
||
def main(): | ||
# Sample prompts. | ||
prompts = [ | ||
"Hello, my name is", | ||
"The president of the United States is", | ||
"The capital of France is", | ||
"The future of AI is", | ||
] | ||
# Create a sampling params object. | ||
sampling_params = {"temperature": 0.8, "top_p": 0.95} | ||
|
||
# Create an LLM. | ||
llm = sgl.Engine(model_path="meta-llama/Meta-Llama-3.1-8B-Instruct") | ||
|
||
outputs = llm.generate(prompts, sampling_params) | ||
# Print the outputs. | ||
for prompt, output in zip(prompts, outputs): | ||
print("===============================") | ||
print(f"Prompt: {prompt}\nGenerated text: {output['text']}") | ||
|
||
|
||
# The __main__ condition is necessary here because we use "spawn" to create subprocesses | ||
# Spawn starts a fresh program every time, if there is no __main__, it will run into infinite loop to keep spawning processes from sgl.Engine | ||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import json | ||
import unittest | ||
|
||
import sglang as sgl | ||
from sglang.test.test_utils import DEFAULT_MODEL_NAME_FOR_TEST | ||
|
||
|
||
class TestSRTBackend(unittest.TestCase): | ||
|
||
def test_engine_runtime_consistency(self): | ||
prompt = "Today is a sunny day and I like" | ||
model_path = DEFAULT_MODEL_NAME_FOR_TEST | ||
|
||
sampling_params = {"temperature": 0, "max_new_tokens": 8} | ||
|
||
engine = sgl.Engine(model_path=model_path, random_seed=42) | ||
out1 = engine.generate(prompt, sampling_params)["text"] | ||
engine.shutdown() | ||
|
||
runtime = sgl.Runtime(model_path=model_path, random_seed=42) | ||
out2 = json.loads(runtime.generate(prompt, sampling_params))["text"] | ||
runtime.shutdown() | ||
|
||
print("==== Answer 1 ====") | ||
print(out1) | ||
|
||
print("==== Answer 2 ====") | ||
print(out2) | ||
assert out1 == out2, f"{out1} != {out2}" | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |