배치 불변성

배치 불변성 (Batch Invariance)

배치 불변성(batch invariance)은 스케줄링 방식과 무관하게 일관된 결과를 얻기 위한 vLLM 기능입니다. 기본적으로 멀티프로세싱을 끄거나 VLLM_BATCH_INVARIANT=1을 설정하면 요청이 어떤 배치로 묶이든 재현 가능한 출력을 얻을 수 있습니다.

출처: 문서

본문

소스: https://github.com/vllm-project/vllm/tree/main/examples/features/batch_invariance

오프라인 재현성 (Reproducibility Offline)

이 스크립트는 vLLM에서 재현성(reproducibility)을 달성하는 방법을 보여줍니다.

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Demonstrates how to achieve reproducibility in vLLM.

Main article: https://docs.vllm.ai/en/latest/usage/reproducibility.html
"""

import os
import random

from vllm import LLM, SamplingParams

# Either:
## Turn off multiprocessing to make the scheduling deterministic, or
os.environ["VLLM_ENABLE_V1_MULTIPROCESSING"] = "0"
## Enable batch invariance to get consistent results regardless of scheduling.
os.environ["VLLM_BATCH_INVARIANT"] = "1"

prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

def main():
    llm = LLM(model="facebook/opt-125m")
    outputs = llm.generate(prompts, sampling_params)
    print("-" * 50)
    for output in outputs:
        prompt = output.prompt
        generated_text = output.outputs[0].text
        print(f"Prompt: {prompt!r}\nGenerated text: {generated_text!r}")
        print("-" * 50)

    # Try generating random numbers outside vLLM
    # The same number is output across runs, meaning that the random state
    # in the user code has been updated by vLLM
    print(random.randint(0, 100))

if __name__ == "__main__":
    main()

동작 요약:

  • 재현 가능한 결과를 위해 두 방법 중 하나를 선택합니다.
    • VLLM_ENABLE_V1_MULTIPROCESSING=0 — 멀티프로세싱을 꺼 스케줄링이 결정적(deterministic)이게 합니다.
    • VLLM_BATCH_INVARIANT=1 — 스케줄링과 무관하게 일관된 결과를 얻는 배치 불변성을 켭니다.
  • 실행 후 vLLM 밖에서 random.randint를 호출해도 같은 값이 나오는데, 이는 vLLM이 사용자 코드의 난수 상태도 갱신했음을 보여줍니다.

더 알아보기 (Learn more)

  • Reproducibility 문서 — 메인 개념 문서
  • 환경 변수 VLLM_BATCH_INVARIANT, VLLM_ENABLE_V1_MULTIPROCESSING