Speculative Decoding in LLMs
Before diving into speculative decoding, it's important to understand the concept of decoding in NLP. Decoding refers to the process by…
Speculative Decoding in LLMs
Before diving into speculative decoding, it's important to understand the concept of decoding in NLP. Decoding refers to the process by which a trained model generates text by predicting one token at a time, based on a given input prompt. After every token is generated, it is added to the input sequence, and the model continues generating until it reaches a stopping condition (such as a maximum length or an end-of-sequence token).
Decoding strategies can vary, such as:
- Greedy decoding: Selects the token with the highest probability at each step.
- Beam search: Considers multiple possible sequences to find the most probable one.
- Top-k sampling: Chooses from the top-k most likely tokens at each step.
What is Speculative Decoding?
Speculative decoding is an advanced technique designed to accelerate the decoding process in large language models, which can be computationally expensive and slow due to their size. The main idea is to use a draft model (a smaller, faster model) to quickly generate tokens, and then verify and adjust these tokens using the larger, more accurate model. The goal is to minimize the heavy computational work done by the larger model while maintaining the quality of the generated text.
In speculative decoding, the smaller model (the draft model) generates a sequence of tokens quickly. The larger model (the verifier model) then either accepts these tokens or suggests changes based on its own understanding. This two-step approach offers a compromise between speed and quality, allowing for more efficient text generation without sacrificing too much accuracy.
Speculative Decoding Techniques
There are several speculative decoding strategies, but let's focus on the draft-model-based speculative decoding approach, which is demonstrated in your code. The idea is that the smaller draft model can generate the first part of the text quickly, and the larger model will then complete or refine the rest of the text. The large model is called later, thereby reducing the overall load.
How the Code Accomplishes Speculative Decoding
The code you provided implements a simulated version of speculative decoding using two models: a smaller, faster model (assistant_model) and a larger, more accurate model (model). The smaller model generates part of the output, and then the larger model refines or continues the generation.
Let's break down the steps:
-
Import Libraries and Set Seed import torch from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed, BitsAndBytesConfig set_seed(42) # For reproducibility
-
Load Models and Tokenizer checkpoint = "Qwen/Qwen2.5-3B-Instruct" assistant_checkpoint = "Qwen/Qwen2.5-0.5B-Instruct" tokenizer = AutoTokenizer.from_pretrained(checkpoint)
-
Configure Quantization for the Large Model compute_dtype = getattr(torch, "float16") bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=compute_dtype, bnb_4bit_use_double_quant=True, )
-
Load Models with Configurations model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto", quantization_config=bnb_config) assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint, device_map="auto")
-
Prompt Input and Tokenization prompt = "What is the purpose of life?" model_inputs = tokenizer(prompt, return_tensors="pt").to("cuda:0")
-
Text Generation Without Speculative Decoding start_time = time.time() output = model.generate(**model_inputs, max_length=500)[0] end_time = time.time()
output_decoded = tokenizer.decode(output) print("Output without speculative decoding:") print(output_decoded) print(f"Time taken without speculative decoding: {end_time - start_time:.2f} seconds")
- Simulating Speculative Decoding start_time = time.time()
First use the smaller model (assistant_model) to generate part of the text.
assistant_output = assistant_model.generate(**model_inputs, max_length=250)[0]
Decode the assistant model's output and re-encode for the larger model.
intermediate_text = tokenizer.decode(assistant_output, skip_special_tokens=True) intermediate_inputs = tokenizer(intermediate_text, return_tensors="pt").to("cuda:0")
Then use the larger model to complete the remaining part.
final_output = model.generate(**intermediate_inputs, max_length=500)[0]
end_time = time.time()
final_output_decoded = tokenizer.decode(final_output) print("\nOutput with simulated speculative decoding:") print(final_output_decoded) print(f"Time taken with speculative decoding (simulated): {end_time - start_time:.2f} seconds")
Advantages of Draft Model-Based Speculative Decoding
- Faster Inference: Since the draft model handles the initial generation, which is usually faster, the overall latency is reduced.
- Balanced Quality: The larger model still ensures that the final output is high-quality, compensating for the draft model's lower accuracy.
- Flexibility: This approach allows for fine-tuning. You can adjust the amount of text generated by the draft model based on speed and quality trade-offs.
Here is colab for trying it out on QWen models https://colab.research.google.com/drive/11fOTlBO8gdivLsykI2uaqD3OLru5EQA4?usp=sharing
Conclusion
Speculative decoding, particularly using a draft model, offers a practical way to reduce the time taken for text generation without sacrificing the quality of the generated text. In the provided example, the large model is called only for the latter half of the generation process, saving computational resources and speeding up the response. The code demonstrates how speculative decoding can be implemented with two models, showing the clear advantage in time while retaining the power of the larger model for refinement.
A Message from AI Mind

Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the AI Mind Publication
- 🧠 Improve your AI prompts effortlessly and FREE
- 🧰 Discover Intuitive AI Tools
메타데이터
- post_id
- 76d66a879a3d
- slug
- speculative-decoding-in-llms-76d66a879a3d
- url
- https://pub.aimind.so/speculative-decoding-in-llms-76d66a879a3d
- canonical_url
- https://pub.aimind.so/speculative-decoding-in-llms-76d66a879a3d
- author_url
- https://medium.com/@jain.sm
- status
- ok
- fetched_at
- 2026-07-31 19:19:38