N-Gram 스페큘레이션

N-Gram 스페큘레이션 (N-Gram Speculation)

N-Gram 스페큘레이션은 프롬프트 안에서 n-gram(연속된 n개 토큰 조합)을 찾아 매칭해서 제안(proposal)을 만드는 스페큘레이티브 디코딩 방식이에요. 별도의 드래프트 모델이 필요 없고, 입력 프롬프트 자체의 패턴을 활용해서 다음 토큰들을 추측하는 방식이죠. 더 자세한 내용은 관련 스레드에서 확인할 수 있어요.

아래 코드는 vLLM에서 n-gram 기반 스페큘레이티브 디코딩을 설정하는 예시입니다.

출처: vLLM 공식 문서 — features-speculative_decoding-n_gram

설정 예시 (Configuration Example)

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}")

설정 값 풀어보기

  • "method": "ngram" — n-gram 스페큘레이션 방식을 지정해요.
  • "num_speculative_tokens": 5 — 한 번에 제안할 스페큘레이티브 토큰의 개수예요.
  • "prompt_lookup_max": 4 — 프롬프트에서 패턴을 찾을 때 사용할 최대 n-gram 크기를 의미해요.

이 방식은 반복적인 텍스트나 규칙성이 강한 시퀀스에서 잘 작동해요. 모델 자체가 아닌 프롬프트의 통계적 패턴에 의존하기 때문에, 추가 모델을 띄우는 오버헤드 없이 레이턴시를 줄일 수 있다는 장점이 있죠.

더 알아보기 (Learn more)