Added VLLM Offline Serve working code.
#107
by
hrithiksagar-tih
- opened
README.md
CHANGED
|
@@ -104,6 +104,82 @@ vllm serve openai/gpt-oss-20b
|
|
| 104 |
|
| 105 |
[Learn more about how to use gpt-oss with vLLM.](https://cookbook.openai.com/articles/gpt-oss/run-vllm)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
## PyTorch / Triton
|
| 108 |
|
| 109 |
To learn about how to use this model with PyTorch and Triton, check out our [reference implementations in the gpt-oss repository](https://github.com/openai/gpt-oss?tab=readme-ov-file#reference-pytorch-implementation).
|
|
|
|
| 104 |
|
| 105 |
[Learn more about how to use gpt-oss with vLLM.](https://cookbook.openai.com/articles/gpt-oss/run-vllm)
|
| 106 |
|
| 107 |
+
Offline Serve Code:
|
| 108 |
+
- run this code after installing proper libraries as described, while additionally installing this:
|
| 109 |
+
- `uv pip install openai-harmony`
|
| 110 |
+
```python
|
| 111 |
+
# source .oss/bin/activate
|
| 112 |
+
|
| 113 |
+
import os
|
| 114 |
+
os.environ["VLLM_USE_FLASHINFER_SAMPLER"] = "0"
|
| 115 |
+
|
| 116 |
+
import json
|
| 117 |
+
from openai_harmony import (
|
| 118 |
+
HarmonyEncodingName,
|
| 119 |
+
load_harmony_encoding,
|
| 120 |
+
Conversation,
|
| 121 |
+
Message,
|
| 122 |
+
Role,
|
| 123 |
+
SystemContent,
|
| 124 |
+
DeveloperContent,
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
from vllm import LLM, SamplingParams
|
| 128 |
+
import os
|
| 129 |
+
|
| 130 |
+
# --- 1) Render the prefill with Harmony ---
|
| 131 |
+
encoding = load_harmony_encoding(HarmonyEncodingName.HARMONY_GPT_OSS)
|
| 132 |
+
|
| 133 |
+
convo = Conversation.from_messages(
|
| 134 |
+
[
|
| 135 |
+
Message.from_role_and_content(Role.SYSTEM, SystemContent.new()),
|
| 136 |
+
Message.from_role_and_content(
|
| 137 |
+
Role.DEVELOPER,
|
| 138 |
+
DeveloperContent.new().with_instructions("Always respond in riddles"),
|
| 139 |
+
),
|
| 140 |
+
Message.from_role_and_content(Role.USER, "What is the weather like in SF?"),
|
| 141 |
+
]
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
prefill_ids = encoding.render_conversation_for_completion(convo, Role.ASSISTANT)
|
| 145 |
+
|
| 146 |
+
# Harmony stop tokens (pass to sampler so they won't be included in output)
|
| 147 |
+
stop_token_ids = encoding.stop_tokens_for_assistant_actions()
|
| 148 |
+
|
| 149 |
+
# --- 2) Run vLLM with prefill ---
|
| 150 |
+
llm = LLM(
|
| 151 |
+
model="openai/gpt-oss-20b",
|
| 152 |
+
trust_remote_code=True,
|
| 153 |
+
gpu_memory_utilization = 0.95,
|
| 154 |
+
max_num_batched_tokens=4096,
|
| 155 |
+
max_model_len=5000,
|
| 156 |
+
tensor_parallel_size=1
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
sampling = SamplingParams(
|
| 160 |
+
max_tokens=128,
|
| 161 |
+
temperature=1,
|
| 162 |
+
stop_token_ids=stop_token_ids,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
outputs = llm.generate(
|
| 166 |
+
prompt_token_ids=[prefill_ids], # batch of size 1
|
| 167 |
+
sampling_params=sampling,
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# vLLM gives you both text and token IDs
|
| 171 |
+
gen = outputs[0].outputs[0]
|
| 172 |
+
text = gen.text
|
| 173 |
+
output_tokens = gen.token_ids # <-- these are the completion token IDs (no prefill)
|
| 174 |
+
|
| 175 |
+
# --- 3) Parse the completion token IDs back into structured Harmony messages ---
|
| 176 |
+
entries = encoding.parse_messages_from_completion_tokens(output_tokens, Role.ASSISTANT)
|
| 177 |
+
|
| 178 |
+
# 'entries' is a sequence of structured conversation entries (assistant messages, tool calls, etc.).
|
| 179 |
+
for message in entries:
|
| 180 |
+
print(f"{json.dumps(message.to_dict())}")
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
## PyTorch / Triton
|
| 184 |
|
| 185 |
To learn about how to use this model with PyTorch and Triton, check out our [reference implementations in the gpt-oss repository](https://github.com/openai/gpt-oss?tab=readme-ov-file#reference-pytorch-implementation).
|