{"id":50719105,"url":"https://github.com/syvb/coconut-oracle-2","last_synced_at":"2026-06-09T22:01:23.274Z","repository":{"id":349651485,"uuid":"1203292566","full_name":"syvb/coconut-oracle-2","owner":"syvb","description":null,"archived":false,"fork":false,"pushed_at":"2026-04-07T00:57:26.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-04-07T01:11:54.485Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syvb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-06T22:59:37.000Z","updated_at":"2026-04-07T00:57:30.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/syvb/coconut-oracle-2","commit_stats":null,"previous_names":["syvb/coconut-oracle-2"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/syvb/coconut-oracle-2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syvb%2Fcoconut-oracle-2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syvb%2Fcoconut-oracle-2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syvb%2Fcoconut-oracle-2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syvb%2Fcoconut-oracle-2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syvb","download_url":"https://codeload.github.com/syvb/coconut-oracle-2/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syvb%2Fcoconut-oracle-2/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34127345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-09T02:00:06.510Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-06-09T22:01:22.405Z","updated_at":"2026-06-09T22:01:23.269Z","avatar_url":"https://github.com/syvb.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# coconut-oracle-2\n\nExperiments with [CoDi](https://arxiv.org/abs/2502.21074) (Compressing Chain-of-Thought into Continuous Space via Self-Distillation) using the [`bcywinski/codi_llama1b-answer_only`](https://huggingface.co/bcywinski/codi_llama1b-answer_only) checkpoint.\n\nCoDi compresses explicit chain-of-thought reasoning into continuous latent vectors. Instead of generating reasoning tokens in natural language, the model \"thinks\" in embedding space through a series of latent steps, then decodes just the final answer.\n\n## Model\n\n- **Base**: LLaMA 3.2 1B Instruct\n- **Fine-tuning**: LoRA (r=128, alpha=32) on all attention + MLP modules\n- **Projection layer**: 2-layer MLP with GELU + LayerNorm, maps hidden states back to input space between latent steps\n- **Latent steps**: 6 continuous reasoning tokens per inference\n- **Training data**: GSM8K-Aug (augmented GSM8K)\n- **Eval data**: GSM8K test split (1319 questions, not in training set)\n\nThe checkpoint stores base weights + LoRA weights separately. Our loader merges them at load time, so no gated HF access to the original LLaMA repo is needed.\n\n## Scripts\n\n### `chat_codi.py` — Interactive chat\n\n```bash\npython chat_codi.py               # with latent reasoning visualization\npython chat_codi.py --no-reasoning  # skip latent steps for comparison\n```\n\nEach latent thinking step is visualized with:\n- A heatmap of the 2048-dim hidden state (blue=negative, gray=zero, red=positive)\n- A delta heatmap showing what changed from the previous step\n- Cosine similarity drift, vector norm, sparsity stats\n\n### `bench_codi.py` — GSM8K benchmark\n\n```bash\npython bench_codi.py                     # full test set, both modes\npython bench_codi.py -n 100              # first 100 examples\npython bench_codi.py --mode reasoning    # reasoning only\npython bench_codi.py --mode none         # no-reasoning only\npython bench_codi.py --output results.json  # save detailed results\n```\n\n### `codi_model.py` — Shared library\n\nModel loading and inference used by both scripts. Key functions:\n- `load_model()` — reconstruct LlamaForCausalLM with merged LoRA weights\n- `generate(model, prj, tokenizer, bot_id, eot_id, prompt, num_latent=6)` — run inference (set `num_latent=0` to skip reasoning)\n- `extract_number(text)` — pull the last number from model output\n\n## Results (full GSM8K test set, 1319 questions)\n\n| Mode | Accuracy | Throughput | Time |\n|------|----------|------------|------|\n| Reasoning (6 latent steps) | **36.7%** | 7.0 q/s | 188s |\n| No reasoning (0 steps) | 28.7% | 7.7 q/s | 171s |\n\nReasoning advantage: **+8.0%** (helped 134 questions, hurt 29).\n\n## How CoDi inference works\n\n1. **Encode**: Tokenize the question, append `\u003cbot\u003e` token, run through the model\n2. **Think**: Take the last hidden state, project through the MLP, feed back as input — repeat 6 times (no text generated, just latent vectors cycling through the transformer)\n3. **Decode**: Append `\u003ceot\u003e` token, then autoregressively generate the answer\n\nThe `--no-reasoning` flag skips step 2, going straight from encoding to decoding.\n\n## Latent Oracle: Interpreting CoDI's Reasoning Tokens\n\nInspired by [Activation Oracles](https://arxiv.org/abs/2512.15674), we train a small LM (SmolLM-360M) to answer natural language questions about what CoDI's latent reasoning tokens are doing. All training data is generated via **task-agnostic unsupervised methods** — no knowledge of the downstream task (GSM8K math) is assumed.\n\n### Training Data Generation (5 strategies, all task-agnostic)\n\n| Strategy | Method | Yield |\n|----------|--------|-------|\n| **Perturbation/Ablation** | Zero out or skip individual latent steps, observe output changes | 11,148 |\n| **Early Decoding** | Fork KV cache after each step, decode partial answer — reveals what model \"knows\" at each point | 11,871 |\n| **Contrastive** | Compare reasoning-on (6 steps) vs reasoning-off (0 steps) outputs | 1,866 |\n| **Token Prediction Stats** | Project hidden states to vocab space via `lm_head`; extract entropy, top-k tokens, convergence | 26,380 |\n| **Summary** | Composite questions combining signals from all strategies | 1,319 |\n| **Total** | | **52,584** |\n\n### Oracle Training\n\n- **Base model**: [SmolLM-360M-Instruct](https://huggingface.co/HuggingFaceTB/SmolLM-360M-Instruct)\n- **Fine-tuning**: LoRA (r=32, alpha=64) on all attention + MLP modules\n- **Data**: 47,326 train / 5,258 val QA pairs\n- **Input format**: Structured text encoding the question, per-step top-k tokens/probs/entropy, and model output\n\n| Epoch | Train Loss | Val Loss |\n|-------|-----------|----------|\n| 1 | 0.1146 | 0.0488 |\n| 2 | 0.0432 | 0.0352 |\n| 3 | 0.0283 | 0.0307 |\n\n### Oracle Evaluation (100 held-out questions)\n\n| Metric | Accuracy | What it measures |\n|--------|----------|-----------------|\n| Ablation prediction | **76%** | Can oracle identify which step is most critical? |\n| Convergence prediction | **72%** | Can oracle predict when output stabilizes? (±1 step) |\n| Contrastive | **73%** | Can oracle predict whether reasoning changes output? |\n| Redundancy consistency | **88%** | Do oracle's redundancy claims match actual ablation data? |\n\n### Oracle Scripts\n\n```bash\n# 1. Collect traces (latent states + ablations + early decodes)\npython collect_traces.py -n 1319 --output traces_test.jsonl\n\n# 2. Generate task-agnostic QA training data\npython generate_oracle_data.py --input traces_test.jsonl\n\n# 3. Train oracle (SmolLM-360M + LoRA)\npython train_oracle.py --train oracle_data.jsonl --val oracle_val.jsonl\n\n# 4. Evaluate\npython eval_oracle.py --traces traces_test.jsonl --oracle-dir oracle_model/best\n\n# 5. Interactive: ask questions about CoDI's reasoning\npython oracle_inference.py --prompt \"Janet has 16 ducks...\"\n\n# 6. Integrated chat with oracle explanations\npython chat_codi.py --oracle\n```\n\n### Key Findings\n\n- **Early decoding is the most revealing strategy**: partial answers show progressive refinement (e.g., pre-reasoning outputs \"36\", after step 2 corrects to \"18\")\n- **CoDI is robust to individual step ablation**: for many questions, zeroing a single step doesn't change the output, suggesting distributed/redundant representations\n- **The oracle achieves 88% consistency on redundancy claims** against ground-truth ablation data, demonstrating that the text-based trace representation (top-k tokens + entropy) captures meaningful information about latent reasoning\n\n## Reference\n\n- [CoDi paper](https://arxiv.org/abs/2502.21074) (EMNLP 2025)\n- [Activation Oracles paper](https://arxiv.org/abs/2512.15674)\n- [Official CoDi repo](https://github.com/zhenyi4/codi) (cloned at `/workspace/codi`)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyvb%2Fcoconut-oracle-2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyvb%2Fcoconut-oracle-2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyvb%2Fcoconut-oracle-2/lists"}