Views
No views yet
| Parameter | Value |
|---|---|
| Mode | codi_uniform |
| Per-step distillation | True |
| Distillation γ | 2.0 |
| Learning rate | 0.0002 |
| Epochs | 3 |
| Batch size (per GPU) | 2 |
| Gradient accumulation | 8 |
| Effective batch size | 128 (across 8 GPUs) |
| Max answer length | 128 |
| Latent steps (K) | 6 |
| Task | GSM8k (7,473 training problems) |
| Rollouts per problem | 16 |
| GSM8k test accuracy | 80.3% |
pip install torch transformers1import torch
2import torch.nn as nn
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5# Load model
6model_name = "LakshyAAAgrawal/continuous-thought-r11_uniform_perstep_g2"
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8model = AutoModelForCausalLM.from_pretrained(
9 model_name, torch_dtype=torch.bfloat16, device_map="auto"
10)
11model.eval()
12
13# Load projection head
14class ProjectionHead(nn.Module):
15 def __init__(self, hidden_size):
16 super().__init__()
17 self.mlp = nn.Sequential(
18 nn.Linear(hidden_size, hidden_size),
19 nn.GELU(),
20 nn.Linear(hidden_size, hidden_size),
21 nn.LayerNorm(hidden_size),
22 )
23 def forward(self, x):
24 return self.mlp(x)
25
26proj = ProjectionHead(model.config.hidden_size)
27proj.load_state_dict(torch.load(
28 hf_hub_download(model_name, "projection_head.pt"), map_location="cpu"
29))
30proj = proj.to(model.dtype).to(model.device).eval()
31
32# Generate with latent reasoning
33question = "Natalia sold clips to 48 of her friends in April, and then she sold half as many clips in May. How many clips did Natalia sell altogether in April and May?"
34messages = [{"role": "user", "content": f"Solve the following math problem step by step. Show your work and put your final numerical answer after ####.\n\n{question}"}]
35prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
36inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
37
38# Step 1: Process prompt
39with torch.no_grad():
40 out = model(**inputs, output_hidden_states=True, use_cache=True)
41 past_kv = out.past_key_values
42 latent = out.hidden_states[-1][:, -1, :] # last token hidden state
43
44 # Step 2: K latent reasoning steps
45 mask = inputs["attention_mask"].clone()
46 for k in range(6):
47 latent = proj(latent)
48 mask = torch.cat([mask, torch.ones(1, 1, device=mask.device, dtype=mask.dtype)], dim=1)
49 out = model(inputs_embeds=latent.unsqueeze(1), attention_mask=mask,
50 past_key_values=past_kv, output_hidden_states=True, use_cache=True)
51 past_kv = out.past_key_values
52 latent = out.hidden_states[-1][:, -1, :]
53
54 # Step 3: Greedy text generation
55 next_token = out.logits[:, -1, :].argmax(dim=-1)
56 generated = [next_token]
57 for _ in range(2047):
58 if next_token.item() == tokenizer.eos_token_id:
59 break
60 mask = torch.cat([mask, torch.ones(1, 1, device=mask.device, dtype=mask.dtype)], dim=1)
61 out = model(input_ids=next_token.unsqueeze(0), attention_mask=mask,
62 past_key_values=past_kv, use_cache=True)
63 past_kv = out.past_key_values
64 next_token = out.logits[:, -1, :].argmax(dim=-1)
65 generated.append(next_token)
66
67 response = tokenizer.decode(torch.cat(generated), skip_special_tokens=True)
68 print(response)1python evaluate.py \
2 --model_dir LakshyAAAgrawal/continuous-thought-r11_uniform_perstep_g2 \
3 --mode codi_uniform \
4 --output results/r11_uniform_perstep_g2.json \
5 --max_new_tokens 2048 \
6 --num_latent 6max_new_tokens=2048 for evaluation. The model generates verbose
chain-of-thought text after the latent steps, requiring more tokens than standard models.| Model | Mode | Per-step | γ | ans_len | GSM8k Accuracy |
|---|---|---|---|---|---|
| This model | codi_uniform | True | 2.0 | 128 | 80.3% |
| Qwen3-1.7B (base) | — | — | — | — | 77.3% |
| Discrete SFT | sft | — | — | — | 80.7% |
| QThink RW final-step | rw | no | 1.0 | 128 | 81.0% |
| QThink Uniform per-step ans256 | uniform | yes | 2.0 | 256 | 83.2% |
| QThink RW per-step ans256 | rw | yes | 1.0 | 256 | 82.7% |
1@misc{continuous-thought-2025,
2 title={QThink: Parallel Latent Reasoning via Per-Step Distillation of Multiple Rollouts},
3 author={Lakshya Agrawal},
4 year={2025},
5 url={https://huggingface.co/LakshyAAAgrawal/continuous-thought-r11_uniform_perstep_g2}
6}