forked from NovaSky-AI/SkyRL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvllm_engine.py
More file actions
729 lines (593 loc) · 31.1 KB
/
Copy pathvllm_engine.py
File metadata and controls
729 lines (593 loc) · 31.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
import os
from typing import TYPE_CHECKING, Any, Dict, List, Optional
if TYPE_CHECKING:
from skyrl.backends.skyrl_train.weight_sync.transfer_strategy import (
WeightSyncInitInfo,
)
import asyncio
import time
from dataclasses import dataclass
from http import HTTPStatus
from types import SimpleNamespace
from uuid import uuid4
import ray
import vllm
from loguru import logger
from vllm import SamplingParams
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionRequest,
ChatCompletionResponse,
)
from vllm.entrypoints.openai.chat_completion.serving import OpenAIServingChat
from vllm.entrypoints.openai.completion.protocol import (
CompletionRequest,
CompletionResponse,
)
from vllm.entrypoints.openai.completion.serving import OpenAIServingCompletion
from vllm.entrypoints.openai.engine.protocol import ErrorInfo, ErrorResponse
from vllm.entrypoints.openai.models.serving import (
BaseModelPath,
OpenAIModelRegistry,
OpenAIServingModels,
)
from vllm.entrypoints.serve.render.serving import OpenAIServingRender
from vllm.inputs import TokensPrompt
from vllm.lora.request import LoRARequest
from skyrl.backends.skyrl_train.inference_engines.base import (
InferenceEngineInput,
InferenceEngineInterface,
InferenceEngineOutput,
)
from skyrl.backends.skyrl_train.inference_engines.vllm.utils import pop_openai_kwargs
# Backward compatibility: WorkerWrap has moved to inference_servers.vllm_worker
# This alias preserves the old import path for existing scripts/configs.
# TODO (Kourosh): Remove this alias once all references are updated.
from skyrl.backends.skyrl_train.inference_servers.vllm_worker import (
WorkerWrap, # noqa: F401, E402
)
from skyrl.backends.skyrl_train.weight_sync import WeightLoader, WeightUpdateRequest
@dataclass
class Logprob:
logprob: float
rank: int
token_id: str
def setup_envvars_for_vllm(kwargs, bundle_indices):
noset_visible_devices = kwargs.pop("noset_visible_devices")
mp_cuda_visible_devices = kwargs.pop("mp_cuda_visible_devices", None)
if kwargs.get("distributed_executor_backend") == "mp" and mp_cuda_visible_devices is not None:
# For mp backend in colocated mode, set CUDA_VISIBLE_DEVICES to the
# pre-computed GPU IDs for this engine so spawned workers see the
# correct GPUs (not all GPUs on the node).
os.environ["CUDA_VISIBLE_DEVICES"] = mp_cuda_visible_devices
os.environ.pop("ROCR_VISIBLE_DEVICES", None)
os.environ.pop("HIP_VISIBLE_DEVICES", None)
logger.info(f"mp backend: setting CUDA_VISIBLE_DEVICES={mp_cuda_visible_devices}")
elif kwargs.get("distributed_executor_backend") in ("ray", "mp"):
# For ray backend (and non-colocate mp), clear CUDA_VISIBLE_DEVICES
# so vLLM workers can discover GPUs via their own scheduling.
os.environ.pop("CUDA_VISIBLE_DEVICES", None)
os.environ.pop("ROCR_VISIBLE_DEVICES", None)
os.environ.pop("HIP_VISIBLE_DEVICES", None)
elif noset_visible_devices:
# We need to set CUDA_VISIBLE_DEVICES to the ray assigned GPU
# when the distributed_executor_backend is not ray/mp and
# RAY_EXPERIMENTAL_NOSET_*_VISIBLE_DEVICES is set.
os.environ["CUDA_VISIBLE_DEVICES"] = str(ray.get_gpu_ids()[0])
num_gpus = kwargs.pop("num_gpus")
if bundle_indices is not None:
os.environ["VLLM_RAY_PER_WORKER_GPUS"] = str(num_gpus)
os.environ["VLLM_RAY_BUNDLE_INDICES"] = ",".join(map(str, bundle_indices))
logger.info(f"creating LLM with bundle_indices={bundle_indices}")
class BaseVLLMInferenceEngine(InferenceEngineInterface):
"""Base class containing shared logic between sync and async VLLM engines."""
def __init__(self, *args, bundle_indices: list = None, **kwargs):
# Redirect infrastructure output to log file before any engine initialization.
# Done here in the base class so all subclasses get it automatically.
from skyrl.train.utils.ray_logging import redirect_actor_output_to_file
redirect_actor_output_to_file()
setup_envvars_for_vllm(kwargs, bundle_indices)
vllm_v1_disable_multiproc = kwargs.pop("vllm_v1_disable_multiproc", False)
if vllm_v1_disable_multiproc:
os.environ["VLLM_ENABLE_V1_MULTIPROCESSING"] = "0"
# Store common attributes
self._tp_size = kwargs.get("tensor_parallel_size", 1)
self._pp_size = kwargs.get("pipeline_parallel_size", 1)
self._dp_size = kwargs.get("data_parallel_size", 1)
self._is_lora = kwargs.get("enable_lora", False)
# Let subclass create the appropriate engine
self.llm = self._create_engine(*args, **kwargs)
# Weight loader is created by subclass after engine initialization
self._weight_loader = None
def tp_size(self):
return self._tp_size
def pp_size(self):
return self._pp_size
def dp_size(self):
return self._dp_size
def _create_engine(self, *args, **kwargs):
"""Abstract method for subclasses to implement engine creation."""
raise NotImplementedError("Subclasses must implement _create_engine")
def _preprocess_prompts(self, input_batch: InferenceEngineInput):
"""Common prompt preprocessing logic."""
prompts = input_batch.get("prompts")
prompt_token_ids = input_batch.get("prompt_token_ids")
request_sampling_params = input_batch.get("sampling_params")
assert (
prompts is None and prompt_token_ids is not None
), "VLLMInferenceEngine only accepts `prompt_token_ids`, not `prompts`."
sampling_params = (
SamplingParams(**request_sampling_params) if request_sampling_params is not None else SamplingParams()
)
return prompt_token_ids, sampling_params
def _postprocess_outputs(self, outputs):
"""Common output processing logic."""
responses: List[str] = []
stop_reasons: List[str] = []
response_ids: List[List[int]] = []
response_logprobs: Optional[List[List[float]]] = []
rollout_expert_indices: Optional[List[List[List[List[int]]]]] = []
for output in outputs:
# TODO(tgriggs): Support n>1 sampling.
assert (
len(output.outputs) == 1
), "Each prompt should have only one responses. n>1 sampling is supported by copying prompts."
resp = output.outputs[0]
responses.append(resp.text)
stop_reasons.append(resp.finish_reason)
response_ids.append(resp.token_ids)
_logprobs = None
if resp.logprobs:
_logprobs = []
for i, token_logprobs in enumerate(resp.logprobs):
token_logprobs: Dict[str, Logprob]
token_id = resp.token_ids[i]
logprob = token_logprobs[token_id].logprob
_logprobs.append(logprob)
del token_logprobs
response_logprobs.append(_logprobs)
_routed_experts = None
if resp.routed_experts is not None:
if hasattr(resp.routed_experts, "tolist"):
_routed_experts = resp.routed_experts.tolist()
else:
_routed_experts = resp.routed_experts
rollout_expert_indices.append(_routed_experts)
if len(response_logprobs) and response_logprobs[0] is None:
response_logprobs = None # hack: assume uniform sampling params
if len(rollout_expert_indices) > 0 and rollout_expert_indices[0] is None:
rollout_expert_indices = None # hack: assume uniform sampling params
return InferenceEngineOutput(
responses=responses,
stop_reasons=stop_reasons,
response_ids=response_ids,
response_logprobs=response_logprobs,
rollout_expert_indices=rollout_expert_indices,
)
def _get_engine(self):
"""Get the underlying engine for RPC calls."""
return self.llm.engine if hasattr(self.llm, "engine") else self.llm
@staticmethod
def _get_unfinished_request_ids(output_processor) -> list:
"""Get unfinished request IDs suitable for abort/abort_request calls.
In vllm 0.16.0+, request_states is keyed by internal IDs (with a random suffix),
while abort() expects external IDs by default. We use external_req_ids when
available and fall back to request_states keys for older vllm versions.
"""
if hasattr(output_processor, "external_req_ids"):
return list(output_processor.external_req_ids.keys())
return list(output_processor.request_states.keys())
def reset_prefix_cache(self):
"""Reset the prefix cache. Subclasses override for async version."""
return self.llm.llm_engine.reset_prefix_cache()
async def pause_generation(self, lora_name: Optional[str] = None, clear_cache: bool = False) -> None:
raise NotImplementedError("pause_generation is only supported for AsyncVLLMInferenceEngine.")
async def resume_generation(self, lora_name: Optional[str] = None) -> None:
raise NotImplementedError("resume_generation is only supported for AsyncVLLMInferenceEngine.")
class VLLMInferenceEngine(BaseVLLMInferenceEngine):
"""Synchronous VLLM engine."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._weight_loader = VLLMWeightLoader(self.llm, is_async=False)
def _create_engine(self, *args, **kwargs):
# Pipeline parallelism requires AsyncLLMEngine
if kwargs.get("pipeline_parallel_size", 1) > 1:
raise ValueError(
"Pipeline parallelism is only supported with AsyncVLLMInferenceEngine. "
"Please set `generator.async_engine=true` in your config."
)
# Pop enable_ray_prometheus_stats - only supported for async engine
enable_ray_prometheus_stats = kwargs.pop("enable_ray_prometheus_stats", False)
if enable_ray_prometheus_stats:
logger.warning(
"enable_ray_prometheus_stats is only supported with AsyncVLLMInferenceEngine. "
"Set `generator.async_engine=true` to enable Ray Prometheus stats logging."
)
return vllm.LLM(*args, **kwargs)
async def generate(self, input_batch: InferenceEngineInput) -> InferenceEngineOutput:
prompt_token_ids, sampling_params = self._preprocess_prompts(input_batch)
# Check if LoRA is enabled and create LoRA requests
lora_requests = None
if self._is_lora:
lora_int_ids = list(self.llm.llm_engine.list_loras())
if len(lora_int_ids) > 0:
lora_int_id = lora_int_ids[0]
batch_size = len(prompt_token_ids)
# dummy_lora_path for placeholder (actual loading done in add_lora())
lora_requests = [
LoRARequest(lora_name=f"{lora_int_id}", lora_int_id=lora_int_id, lora_path="/dummy_lora_path")
] * batch_size
outputs = await asyncio.to_thread(
self.llm.generate,
prompts=[TokensPrompt(prompt_token_ids=r) for r in prompt_token_ids],
sampling_params=sampling_params,
lora_request=lora_requests,
)
return self._postprocess_outputs(outputs)
async def chat_completion(self, request_payload: Dict[str, Any]) -> Dict[str, Any]:
"""Only supported in AsyncVLLMInferenceEngine."""
raise NotImplementedError("`chat_completion` is only supported in AsyncVLLMInferenceEngine.")
async def completion(self, request_payload: Dict[str, Any]) -> Dict[str, Any]:
"""Only supported in AsyncVLLMInferenceEngine."""
raise NotImplementedError("`completion` is only supported in AsyncVLLMInferenceEngine.")
async def wake_up(self, *args: Any, **kwargs: Any):
await asyncio.to_thread(self.llm.wake_up, tags=kwargs.get("tags", None))
async def sleep(self, *args: Any, **kwargs: Any):
engine = self._get_engine().llm_engine
output_processor = engine.output_processor
if output_processor.has_unfinished_requests():
logger.warning(
"Calling sleep() with unfinished requests in vLLM engine. This is unexpected since all "
"generation should be done before sleep() is called. Check for potential failures or "
"dangling requests in your Generator/Env. Aborting all unfinished requests."
)
unfinished_request_ids = self._get_unfinished_request_ids(output_processor)
await asyncio.to_thread(engine.abort_request, unfinished_request_ids)
level = 1 if self._is_lora else kwargs.get("level", 2)
await asyncio.to_thread(self.llm.sleep, level=level)
async def init_weight_update_communicator(self, init_info: "WeightSyncInitInfo"):
import pickle
engine = self._get_engine()
# Pickle the init_info to preserve type through collective_rpc
pickled_init_info = pickle.dumps(init_info)
return await asyncio.to_thread(
engine.collective_rpc,
"init_weight_update_communicator",
args=(pickled_init_info,),
)
async def _load_lora_from_disk(self, lora_path: str, lora_name: str = ""):
"""Load LoRA adapters from disk using vLLM's native add_lora method.
When ``lora_name`` is empty (legacy single-tenant), a numeric name is
generated. Multi-tenant callers pass ``lora_name`` so subsequent
``model=<lora_name>`` sampling routes to the right adapter.
"""
lora_id = int(time.time_ns() % 0x7FFFFFFF)
name = lora_name or f"{lora_id}"
lora_request = LoRARequest(lora_name=name, lora_int_id=lora_id, lora_path=lora_path)
result = self.llm.llm_engine.add_lora(lora_request)
return result
async def update_named_weights(self, request: WeightUpdateRequest):
from skyrl.backends.skyrl_train.weight_sync import LoraLoadRequest
# Handle LoRA disk loading request
if isinstance(request, LoraLoadRequest):
return await self._load_lora_from_disk(request.lora_path, lora_name=request.lora_name)
if not len(request):
raise ValueError("Weight update request must not be empty")
# Use the weight loader to coordinate weight transfer
return await self._weight_loader.load_weights(request)
async def teardown(self):
await self._teardown_weight_receiver()
async def reset_prefix_cache(self):
return await asyncio.to_thread(self.llm.llm_engine.reset_prefix_cache)
async def _teardown_weight_receiver(self):
engine = self._get_engine()
return await asyncio.to_thread(engine.collective_rpc, "teardown_weight_receiver")
class AsyncVLLMInferenceEngine(BaseVLLMInferenceEngine):
"""Asynchronous VLLM engine."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._weight_loader = VLLMWeightLoader(self.llm, is_async=True)
def _create_engine(self, *args, **kwargs):
openai_kwargs = pop_openai_kwargs(kwargs)
# Logging kwargs
enable_ray_prometheus_stats = kwargs.pop("enable_ray_prometheus_stats", False)
enable_log_requests = kwargs.pop("enable_log_requests", False)
max_log_len = kwargs.pop("max_log_len", None)
engine_args = vllm.AsyncEngineArgs(enable_log_requests=enable_log_requests, kv_cache_metrics=True, **kwargs)
# Setup stat loggers for vLLM v1 if Ray Prometheus stats are enabled
stat_loggers = None
if enable_ray_prometheus_stats:
stat_loggers = self._create_ray_prometheus_stat_loggers()
engine = vllm.AsyncLLMEngine.from_engine_args(engine_args, stat_loggers=stat_loggers)
model_path = kwargs.get("model")
# Use served_model_name if provided (from generator.served_model_name config),
# otherwise fall back to model_path. This allows using a different model name
# in HTTP endpoint requests than the actual model path.
# See: https://github.com/NovaSky-AI/SkyRL/pull/238#discussion_r2326561295
served_model_name = kwargs.get("served_model_name", None)
model_name = served_model_name if served_model_name is not None else model_path
base_model_paths = [BaseModelPath(name=model_name, model_path=model_path)]
models = OpenAIServingModels(engine, base_model_paths)
# Build request logger for debugging (off by default).
# Enable via: generator.engine_init_kwargs.enable_log_requests=true
# Optionally limit logged chars: generator.engine_init_kwargs.max_log_len=256
request_logger = None
if enable_log_requests:
from vllm.entrypoints.logger import RequestLogger
request_logger = RequestLogger(max_log_len=max_log_len)
chat_template = openai_kwargs.pop("chat_template", None)
from vllm.renderers import renderer_from_config
model_registry = OpenAIModelRegistry(
model_config=engine.model_config,
base_model_paths=base_model_paths,
)
renderer = renderer_from_config(engine.vllm_config)
openai_serving_render = OpenAIServingRender(
model_config=engine.model_config,
renderer=renderer,
model_registry=model_registry,
request_logger=request_logger,
chat_template=chat_template,
chat_template_content_format="auto",
)
self.openai_serving_chat = OpenAIServingChat(
engine_client=engine,
models=models,
response_role="assistant",
openai_serving_render=openai_serving_render,
request_logger=request_logger,
chat_template=chat_template,
chat_template_content_format="auto",
**openai_kwargs,
)
# TODO(Charlie): revisit kwargs `return_tokens_as_token_ids`,
# `enable_prompt_tokens_details`, `enable_force_include_usage`.
self.openai_serving_completion = OpenAIServingCompletion(
engine_client=engine,
models=models,
openai_serving_render=openai_serving_render,
request_logger=request_logger,
)
return engine
def _create_ray_prometheus_stat_loggers(self):
"""Create Ray Prometheus stat loggers for vLLM metrics.
Returns stat_loggers in the format expected by vLLM's from_engine_args().
For vLLM v1 (0.9.0+), this returns a list of StatLoggerFactory callables.
For older versions where the v1 API is not available, this returns `None`.
See: https://docs.vllm.ai/en/latest/api/vllm/v1/metrics/ray_wrappers/
"""
try:
# Try vLLM v1 API first (0.9.0+)
from vllm.v1.metrics.ray_wrappers import RayPrometheusStatLogger
logger.info("Enabling RayPrometheusStatLogger for vLLM inference engine metrics")
# For v1, stat_loggers is a list of factory callables
return [RayPrometheusStatLogger]
except ImportError:
logger.warning(
"RayPrometheusStatLogger not available in this vLLM version. "
"For Ray-integrated metrics, upgrade to vLLM >= 0.9.0. "
"Stat logging will be disabled."
)
return None
async def _load_lora_from_disk(self, lora_path: str, lora_name: str = ""):
"""Load LoRA adapters from disk using vLLM's native add_lora method.
When ``lora_name`` is empty (legacy single-tenant), a numeric name is
generated. Multi-tenant callers pass ``lora_name`` so subsequent
``model=<lora_name>`` sampling routes to the right adapter.
"""
lora_id = int(time.time_ns() % 0x7FFFFFFF)
name = lora_name or f"{lora_id}"
lora_request = LoRARequest(lora_name=name, lora_int_id=lora_id, lora_path=lora_path)
result = await self.llm.add_lora(lora_request)
return result
async def _collect_outputs(self, prompt_token_ids, request_id: str, sampling_params: SamplingParams):
"""Collect outputs for a single prompt."""
# Check if LoRA is enabled and create LoRA request
final_output = None
lora_request = None
if self._is_lora:
lora_int_ids = list(await self.llm.list_loras())
if len(lora_int_ids) > 0:
lora_int_id = lora_int_ids[0]
# dummy_lora_path for placeholder (actual loading done in add_lora())
lora_request = LoRARequest(
lora_name=f"{lora_int_id}", lora_int_id=lora_int_id, lora_path="/dummy_lora_path"
)
async for request_output in self.llm.generate(
prompt=TokensPrompt(prompt_token_ids=prompt_token_ids),
sampling_params=sampling_params,
request_id=request_id,
lora_request=lora_request,
):
final_output = request_output
return final_output
async def generate(self, input_batch: InferenceEngineInput) -> InferenceEngineOutput:
"""Generate responses using vLLM's async engine."""
prompt_token_ids, sampling_params = self._preprocess_prompts(input_batch)
tasks = []
for prompt in prompt_token_ids:
# Schedule the collection of outputs for each prompt.
# Avoid duplicate request_ids
request_id = str(uuid4().hex)
task = asyncio.create_task(self._collect_outputs(prompt, request_id, sampling_params))
tasks.append(task)
outputs = await asyncio.gather(*tasks)
return self._postprocess_outputs(outputs)
async def wake_up(self, *args: Any, **kwargs: Any):
await self.llm.wake_up(tags=kwargs.get("tags", None))
async def sleep(self, *args: Any, **kwargs: Any):
engine = self._get_engine()
output_processor = engine.output_processor
# make sure that the engine is alive
engine.engine_core.ensure_alive()
if output_processor.has_unfinished_requests():
logger.warning(
"Calling sleep() with unfinished requests in vLLM engine. This is unexpected since all "
"generation should be done before sleep() is called. Check for potential failures or "
"dangling requests in your Generator/Env. Aborting all unfinished requests."
)
unfinished_request_ids = self._get_unfinished_request_ids(output_processor)
await engine.abort(unfinished_request_ids)
# TODO(team): remove once vllm fixes this
# otherwise waking it up will output gibberish: https://github.com/vllm-project/vllm/issues/17103
await self.reset_prefix_cache()
level = 1 if self._is_lora else kwargs.get("level", 2)
await self.llm.sleep(level=level)
async def init_weight_update_communicator(self, init_info: "WeightSyncInitInfo"):
import pickle
engine = self._get_engine()
# Pickle the init_info to preserve type through collective_rpc
pickled_init_info = pickle.dumps(init_info)
return await engine.collective_rpc(
"init_weight_update_communicator",
args=(pickled_init_info,),
)
async def update_named_weights(self, request: WeightUpdateRequest):
from skyrl.backends.skyrl_train.weight_sync import LoraLoadRequest
# Check for LoRA disk loading request
if isinstance(request, LoraLoadRequest):
return await self._load_lora_from_disk(request.lora_path, lora_name=request.lora_name)
if not len(request):
raise ValueError("Weight update request must not be empty")
# Use the weight loader to coordinate weight transfer
return await self._weight_loader.load_weights(request)
async def teardown(self):
await self._teardown_weight_receiver()
async def reset_prefix_cache(self):
engine = self._get_engine()
await engine.reset_prefix_cache()
async def _teardown_weight_receiver(self):
engine = self._get_engine()
return await engine.collective_rpc("teardown_weight_receiver")
# ----------------------------------------
# Methods for handling OpenAI API requests
# ----------------------------------------
async def _handle_openai_request(self, request_payload: Dict[str, Any], endpoint: str) -> Dict[str, Any]:
"""Handle OpenAI API request."""
assert endpoint in ["/chat/completions", "/completions"]
body = request_payload.get("json", {})
headers = request_payload.get("headers", {})
# 1. Build request
try:
if endpoint == "/chat/completions":
request = ChatCompletionRequest(**body)
else:
request = CompletionRequest(**body)
assert request.stream is False, "Streaming is not supported in SkyRL yet, please set stream to False."
except Exception as e:
return ErrorResponse(
error=ErrorInfo(
message=str(e),
type=HTTPStatus.BAD_REQUEST.phrase,
code=HTTPStatus.BAD_REQUEST.value,
),
).model_dump()
# 2. Call vllm engine
try:
# Create a minimal request-like object with attributes used by vLLM
minimal_request = _MinimalRequest(headers)
if endpoint == "/chat/completions":
generator = await self.openai_serving_chat.create_chat_completion(request, minimal_request)
assert isinstance(generator, (ChatCompletionResponse, ErrorResponse))
else:
generator = await self.openai_serving_completion.create_completion(request, minimal_request)
assert isinstance(generator, (CompletionResponse, ErrorResponse))
return generator.model_dump()
except Exception as e:
# Handle it here so we can surface the error from a ray worker.
# Determine appropriate HTTP status code based on error message to mimic vllm serve error
# handling. Here, we handle context length errors, which should return 400 according to
# vllm serve error handling, so that downstream users can handle these properly rather
# than seeing a 500 SkyRL INTERNAL_SERVER_ERROR. For instance, LiteLLM can wraps them as
# BadRequestError, enabling Harbor to detect ContextLengthExceededError.
# NOTE(Charlie): This is hacky. With the refactored inference stack, we
# should be able to directly reuse the error handling from the served vllm.
error_message = str(e).lower()
is_context_length_error = "context length" in error_message or "maximum input length" in error_message
if is_context_length_error:
http_status = HTTPStatus.BAD_REQUEST
else:
http_status = HTTPStatus.INTERNAL_SERVER_ERROR
return ErrorResponse(
error=ErrorInfo(
message=str(e),
type=http_status.phrase,
code=http_status.value,
),
).model_dump()
async def chat_completion(self, request_payload: Dict[str, Any]) -> Dict[str, Any]:
"""OpenAI-compatible HTTP endpoint for handling `/chat/completions` in Python vLLM engine.
Accepts a JSON-serializable payload: {"json": <request-body>, "headers": <headers-dict>}.
Constructs a minimal request-like object for vLLM's openai_serving_chat.
Returns a plain dict, either a ChatCompletionResponse or an ErrorResponse, both defined
in vllm.entrypoints.openai.protocol.
"""
return await self._handle_openai_request(request_payload, endpoint="/chat/completions")
async def completion(self, request_payload: Dict[str, Any]) -> Dict[str, Any]:
"""OpenAI-compatible HTTP endpoint for handling `/completions` in Python vLLM engine.
Accepts a JSON-serializable payload: {"json": <request-body>, "headers": <headers-dict>}.
Constructs a minimal request-like object for vLLM's openai_serving_completion.
Returns a plain dict, either a CompletionResponse or an ErrorResponse, both defined
in vllm.entrypoints.openai.protocol.
"""
return await self._handle_openai_request(request_payload, endpoint="/completions")
async def pause_generation(self, lora_name: Optional[str] = None, clear_cache: bool = False) -> None:
"""Pause generation using vLLM's native keep mode, freezing in-flight requests."""
if lora_name is not None:
raise NotImplementedError("targeted pause is HTTP-only")
engine = self._get_engine()
await engine.pause_generation(mode="keep", clear_cache=clear_cache)
logger.info("pause_generation(mode='keep') finished")
async def resume_generation(self, lora_name: Optional[str] = None) -> None:
"""Resume generation after a keep-mode pause."""
if lora_name is not None:
raise NotImplementedError("targeted pause is HTTP-only")
engine = self._get_engine()
await engine.resume_generation()
logger.info("resume_generation() finished")
class _MinimalRequest:
"""
Minimal request-like object for vLLM's openai_serving_chat and openai_serving_completion.
We cannot use the original user Request object because it cannot be serialized and hence
cannot be a ray method argument. Instead we take the original request's headers and
reconstruct an instance of _MinimalRequest to mimic the FastAPI Request object.
The fields depend on what vLLM accesses internally.
"""
def __init__(self, headers):
self.headers = headers # Expect a mapping with .get support
self.state = SimpleNamespace() # vLLM sets raw_request.state.request_metadata
class VLLMWeightLoader(WeightLoader):
"""Loads weights into vLLM engine, managing RPC coordination.
This loader encapsulates the collective_rpc calls to workers.
Workers create the appropriate receiver locally for the actual weight transfer.
"""
def __init__(self, engine: Any, is_async: bool = False) -> None:
"""Initialize the loader.
Args:
engine: The vLLM engine (LLM or AsyncLLMEngine).
is_async: Whether this is for AsyncVLLMInferenceEngine.
"""
self._engine = engine.engine if hasattr(engine, "engine") else engine
self._is_async = is_async
async def load_weights(self, request: WeightUpdateRequest) -> None:
"""Load weights by coordinating RPC to workers.
Sends the request to workers via collective_rpc. Workers create
the receiver locally and use it to receive and load weights.
Args:
request: Weight update request.
"""
import pickle
# Pickle the request to preserve type through collective_rpc
pickled_request = pickle.dumps(request)
if self._is_async:
await self._engine.collective_rpc(
"load_weights",
args=(pickled_request,),
)
else:
await asyncio.to_thread(
self._engine.collective_rpc,
"load_weights",
args=(pickled_request,),
)
VLLMRayActor = ray.remote(VLLMInferenceEngine)
AsyncVLLMRayActor = ray.remote(AsyncVLLMInferenceEngine)