
- **Add SPDX license headers to python source files** - **Check for SPDX headers using pre-commit** commit 9d7ef44c3cfb72ca4c32e1c677d99259d10d4745 Author: Russell Bryant <rbryant@redhat.com> Date: Fri Jan 31 14:18:24 2025 -0500 Add SPDX license headers to python source files This commit adds SPDX license headers to python source files as recommended to the project by the Linux Foundation. These headers provide a concise way that is both human and machine readable for communicating license information for each source file. It helps avoid any ambiguity about the license of the code and can also be easily used by tools to help manage license compliance. The Linux Foundation runs license scans against the codebase to help ensure we are in compliance with the licenses of the code we use, including dependencies. Having these headers in place helps that tool do its job. More information can be found on the SPDX site: - https://spdx.dev/learn/handling-license-info/ Signed-off-by: Russell Bryant <rbryant@redhat.com> commit 5a1cf1cb3b80759131c73f6a9dddebccac039dea Author: Russell Bryant <rbryant@redhat.com> Date: Fri Jan 31 14:36:32 2025 -0500 Check for SPDX headers using pre-commit Signed-off-by: Russell Bryant <rbryant@redhat.com> --------- Signed-off-by: Russell Bryant <rbryant@redhat.com>
81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
import asyncio
|
|
import multiprocessing
|
|
from typing import Callable, Tuple, Union
|
|
|
|
from vllm import SamplingParams
|
|
from vllm.engine.arg_utils import AsyncEngineArgs
|
|
from vllm.engine.multiprocessing.client import MQLLMEngineClient
|
|
from vllm.engine.multiprocessing.engine import MQLLMEngine
|
|
from vllm.outputs import RequestOutput
|
|
from vllm.usage.usage_lib import UsageContext
|
|
|
|
|
|
async def generate(
|
|
client: MQLLMEngineClient,
|
|
request_id: str,
|
|
num_tokens: int,
|
|
return_output: bool = False) -> Union[RequestOutput, Tuple[int, str]]:
|
|
|
|
final_output = None
|
|
count = 0
|
|
async for out in client.generate(
|
|
request_id=request_id,
|
|
prompt="Hello my name is Robert and",
|
|
sampling_params=SamplingParams(max_tokens=num_tokens,
|
|
temperature=0)):
|
|
|
|
count += 1
|
|
final_output = out
|
|
await asyncio.sleep(0.)
|
|
|
|
if return_output:
|
|
return final_output
|
|
|
|
# Confirm we generated all the tokens we expected.
|
|
return count, request_id
|
|
|
|
|
|
def run_normal(engine_args: AsyncEngineArgs, ipc_path: str):
|
|
# Make engine.
|
|
engine = MQLLMEngine.from_engine_args(
|
|
engine_args=engine_args,
|
|
usage_context=UsageContext.UNKNOWN_CONTEXT,
|
|
ipc_path=ipc_path)
|
|
|
|
# Run engine.
|
|
engine.start()
|
|
|
|
|
|
class RemoteMQLLMEngine:
|
|
|
|
def __init__(self,
|
|
engine_args: AsyncEngineArgs,
|
|
ipc_path: str,
|
|
run_fn: Callable = run_normal) -> None:
|
|
|
|
self.engine_args = engine_args
|
|
self.ipc_path = ipc_path
|
|
context = multiprocessing.get_context("spawn")
|
|
self.proc = context.Process(target=run_fn,
|
|
args=(engine_args, ipc_path))
|
|
self.proc.start()
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
self.proc.kill()
|
|
|
|
async def make_client(self) -> MQLLMEngineClient:
|
|
engine_config = self.engine_args.create_engine_config()
|
|
client = MQLLMEngineClient(self.ipc_path, engine_config, self.proc.pid)
|
|
while True:
|
|
try:
|
|
await client.setup()
|
|
break
|
|
except TimeoutError:
|
|
assert self.proc.is_alive()
|
|
return client
|