vLLM 텍스트 생성 배포 예시 (Deployment)

vLLM 텍스트 생성 배포 예시 (Deployment)

vLLM의 AsyncLLMEngine을 이용해 언어 모델을 배포하고 텍스트 생성 요청을 처리하는 예시예요. Vast Deployments의 @context로 엔진을 워커 시작 시 한 번만 로드해 두고, @remote 함수로 추론을 수행하는 패턴을 보여 줘요. 이 구조를 그대로 가져가면 고성능 LLM 서빙을 시작하는 데 바로 쓸 수 있어요.

출처: Vast.ai 공식 문서 — vLLM Text Generation 예시

배포 코드 (deploy.py)

# deploy.py
from vastai import Deployment
from vastai.data.query import gpu_name, RTX_4090, RTX_5090

app = Deployment(name="vllm")

MODEL = "Qwen/Qwen3-0.6B"

@app.context()
class VLLMEngine:
    async def __aenter__(self):
        from vllm import AsyncLLMEngine, AsyncEngineArgs, SamplingParams

        args = AsyncEngineArgs(model=MODEL, max_model_len=512)
        self.engine = AsyncLLMEngine.from_engine_args(args)

        # Warmup: 모델이 완전히 로드됐는지 더미 생성으로 확인
        async for _ in self.engine.generate(
            "warmup", SamplingParams(max_tokens=1), request_id="warmup"
        ):
            pass
        return self

    async def __aexit__(self, *exc):
        self.engine.shutdown_background_loop()


@app.remote(benchmark_dataset=[{"prompt": "Hello"}])
async def generate(prompt: str, max_tokens: int = 128) -> str:
    from vllm import SamplingParams
    import uuid

    engine = app.get_context(VLLMEngine)
    params = SamplingParams(max_tokens=max_tokens, temperature=0.7)
    request_id = str(uuid.uuid4())
    result = None
    async for output in engine.engine.generate(prompt, params, request_id=request_id):
        result = output
    return result.outputs[0].text


image = app.image("vastai/vllm:v0.11.0-cuda-12.8-mvc-cuda-12.0", 32)
image.use_system_python()
image.pip_install("vllm==0.11.0", "transformers==4.57.0")
image.require(gpu_name.in_([RTX_4090, RTX_5090]))
app.configure_autoscaling(min_load=100)
app.ensure_ready()

클라이언트 (client.py)

# client.py
import asyncio
from deploy import app, generate

async def main():
    result = await generate("Explain quantum computing in one sentence.")
    print(f"Response: {result}")

if __name__ == "__main__":
    asyncio.run(main())

이 예시가 보여 주는 것

  • @context와 vLLM의 AsyncLLMEngine을 결합해 고성능 LLM 서빙을 구성하는 법
  • __aenter__에서 warmup 생성을 수행해 벤치마킹 전에 KV 캐시를 미리 할당하는 법
  • __aexit__에서 shutdown_background_loop()로 제대로 정리하는 법
  • image.use_system_python()으로 이미지의 내장 파이썬 환경을 사용하는 법
  • 재현성을 위해 정확한 패키지 버전을 지정하는 법
  • 짧은 프롬프트로 구성한 간단한 벤치마크 데이터셋

더 알아보기 (Learn more)