Views
No views yet
alpha is the trade-off between confidence and the cosine-similarity-based penalty.Qwen/Qwen2.5-0.5B-Instruct (example)top_k (int): Number of candidate tokens to consider each step (e.g., 4)penalty_alpha (float): Weight of the degeneration penalty (e.g., 0.6)top_k explores more candidates but increases computepenalty_alpha in [0.3, 0.8] often works well; 0.0 reduces to greedytransformers generation1from transformers import AutoModelForCausalLM, AutoTokenizer, infer_device
2
3device = infer_device()
4
5model_id = "Qwen/Qwen2.5-0.5B-Instruct"
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto").to(device)
8
9inputs = tokenizer(["DeepMind Company is"], return_tensors="pt").to(device)
10
11# Contrastive search
12gen_out = model.generate(
13 **inputs,
14 custom_generate="contrastive_search",
15 penalty_alpha=0.6,
16 top_k=4,
17 max_new_tokens=128,
18 trust_remote_code=True,
19)
20
21print(tokenizer.batch_decode(gen_out, skip_special_tokens=True))