N-Gram Speculation

N-Gram Speculation (N-gram 추측)

N-gram 추측은 프롬프트에서 n-gram을 매칭해 제안(proposal)을 생성하는 추측 디코딩 방법이에요. 별도의 드래프트 모델 없이 가볍고 쉽게 활성화할 수 있어서, 특히 반복적인 구조가 많은 텍스트에서 효과적이에요. 자세한 내용은 관련 스레드를 참고하세요.

출처: 문서

본문

오프라인 예시

아래 코드는 vLLM이 프롬프트에서 n-gram 매칭으로 제안을 생성하는 추측 디코딩을 구성해요:

from vllm import LLM, SamplingParams

prompts = ["The future of AI is"]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

llm = LLM(
    model="Qwen/Qwen3-8B",
    tensor_parallel_size=1,
    speculative_config={
        "method": "ngram",
        "num_speculative_tokens": 5,
        "prompt_lookup_max": 4,
    },
)

outputs = llm.generate(prompts, sampling_params)

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

설정 키

--speculative-config에서 method: "ngram"(또는 "ngram_gpu")으로 설정하면 다음 키를 사용할 수 있어요:

Key Type Default 의미
num_speculative_tokens integer > 0 None 스텝마다 제안할 추측 토큰 수
prompt_lookup_max integer >= 1 조회 범위가 둘 다 생략되면 5, 아니면 prompt_lookup_min을 따름 최대 n-gram 윈도우 크기
prompt_lookup_min integer >= 1 조회 범위가 둘 다 생략되면 5, 아니면 prompt_lookup_max를 따름 최소 n-gram 윈도우 크기

prompt_lookup_min/prompt_lookup_max는 프롬프트에서 매칭할 n-gram 윈도우의 크기 범위를 정해요. n-gram 모델로는 model 키가 보통 필요 없어요.

온라인 예시

vllm serve <target-model> \
  --speculative-config '{
    "method": "ngram",
    "num_speculative_tokens": 4,
    "prompt_lookup_min": 2,
    "prompt_lookup_max": 5
  }'

특징

  • 드래프트 모델이 필요 없어 추가 GPU 메모리와 로딩 비용이 없어요.
  • 피크 트래픽 동안 워크로드를 늘리지 않으면서 적당한 속도 향상을 제공해요.
  • 반복적인 코드나 구조적 텍스트에서 수락율이 특히 좋아요.

더 알아보기 (Learn more)