Views
No views yet
k) in a single forward pass. It utilizes a custom generate() implementation allowing for accelerated inference by predicting k tokens at once as well as an adaptive mode (ConfAdapt) that dynamically adjusts the number of predicted tokens based on model confidence.trust_remote_code=True to load the custom generation logic.do_mtp=True, the model defaults to standard Hugging Face generation behavior (1 token at a time).1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_id = "this/repo"
4
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6# trust_remote_code is required for the custom model class
7model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, device_map="auto")
8
9prompt = "Q: There are 15 trees in the grove..."
10inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
11
12# Decode using the ConfAdapt strategy with threshold 90%, max k = 16
13output = model.generate(
14 input_ids=inputs.input_ids,
15 max_returned_tokens=128, # Limits total length (prompt + gen).
16 do_mtp=True, # Enable custom MTP logic.
17 k_toks=16, # Maximum tokens to attempt per step.
18 mask_id=128259, # NOTE Must match actual mask token id for your model.
19 eos_id=[128001, 128009], # NOTE Must match actual stop token id, can handle multiple.
20 strategy=["conf_adapt", 0.9] # See examples below.
21)
22print(tokenizer.decode(output[0], skip_special_tokens=True))do_mtp=True to the generate() function.mask_id): The token used to mask future positions (e.g., 128259 for Llama3 models in this collection).eos_id): The token ID(s) that stop generation. Note that in the examples, sometimes multiple tokens, eg. eos_id=[128001, 128009] should be passed if the model is sometimes inconsistent about which stop token it emits.1output = model.generate(
2 input_ids=inputs.input_ids,
3 do_mtp=False, # Disable custom MTP logic
4 # other kwargs
5)strategy arg, i.e. = None, means that the Static strategy is used and k value is fixed at every step.1output = model.generate(
2 input_ids=inputs.input_ids,
3 max_returned_tokens=128, # Limits total length (prompt + gen)
4 do_mtp=True, # Enable custom MTP logic
5 k_toks=1, # Predict 1 token per step
6 mask_id=128259, # REPLACE with actual mask token id for your model
7 eos_id=128009, # REPLACE with actual eos token id
8)k tokens per step, fixed acceleration, possibly lossy.
Omitting the strategy arg, i.e. = None, means that the Static strategy is used and k value is fixed at every step.1output = model.generate(
2 input_ids=inputs.input_ids,
3 max_returned_tokens=128, # Limits total length (prompt + gen)
4 do_mtp=True, # Enable custom MTP logic
5 k_toks=3, # Predict 3 tokens per step
6 mask_id=128259, # REPLACE with actual mask token id for your model
7 eos_id=128009, # REPLACE with actual eos token id
8)conf_adapt)k tokens based on a confidence threshold, variable acceleration, nearly lossless. Check implementaton for other possible strategies, some of which are experimental and not discussed in paper.1# Strategy spec: ["conf_adapt", threshold_float]
2# Stops predicting k tokens if confidence drops below 0.9
3strategy = ["conf_adapt", 0.9]
4
5output = model.generate(
6 input_ids=inputs.input_ids,
7 max_returned_tokens=128,
8 do_mtp=True,
9 k_toks=16, # Maximum tokens to attempt per step
10 mask_id=128259, # REPLACE with actual mask token id for your model
11 eos_id=[128001, 128009], # Can handle a list of stop tokens
12 strategy=strategy
13)do_mtp=True, standard sampling arguments (like do_sample) are ignored.| Argument | Type | Description |
|---|---|---|
do_mtp | bool | Set to True to enable the MTP generation path. |
k_toks | int | The number of future tokens to predict per forward pass. |
mask_id | int | The token ID used to mask future positions. Required if k_toks > 1. |
eos_id | int or list | The End-of-Sequence token ID(s). |
strategy | tuple | Decoding strategy. Supports ("conf_adapt", threshold) or ("random", weights). |
include_prompt | bool | Whether to return the full sequence or just generated tokens. Default: True. |