LoopedPhai-A1-35B: Phai-A1-35B with a looped middle core

ramachandrajoshi/Phai-A1-35B-1M-FP8 (a Qwen3.6-35B-A3B post-train, qwen3_5_moe) plus a small trained adapter (~6.5 M parameters, 26 MB) that runs the model's middle layers T times per token (default T = 3) inside vLLM. The design is ported from jasontitus/loopedqwen (dense Qwen3.8-27B) and adapted to sparse MoE.

This repository holds everything: the FP8 base weights (unchanged), a config.json that selects the looped architecture and points at the bundled adapter, the adapter, the vLLM plugin source and wheel, the trainer, and the data/eval scripts.

tokens โ”€โ–บ prelude L0-15 โ”€โ–บ core L16-27 (pass 0 = base) โ”€โ–บ core ร—(T-1) with adapter โ”€โ–บ coda L28-39 โ”€โ–บ logits
                    โ”‚                                      โ–ฒ  workspace entry, cross-pass mixer,
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ anchor โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  block adapters, MoE-router LoRA

Status: +11 points on LiveCodeBench v6 (33.5% โ†’ 44.5%, 95% CI +6.5 to +16). In a thinking-on A/B on 2ร— L40S, adapter v1 at T=3 beat the same deployment at T=1 (the unchanged base model). Most of the gain comes from thinking that finishes within the 32k token budget. KodCode and a small visual set point the same way but are within noise (results). Not yet shown: whether this comes from the loop or from the fine-tuning alone. A matched no-loop control adapter is trained and its eval is next. There is no claim of matching Qwen3.8-27B.

Phai-specific. The adapter was trained and tested only on Phai-A1-35B-1M-FP8. It loads on any Qwen3.6-35B-A3B fine-tune (same architecture), but it learned corrections to Phai's own activations and expert routing. On other fine-tunes it is untested and may not help, or may hurt. Measure before using it elsewhere, e.g. python scripts/eval_local.py --model <fine-tune> --depths 1 3.

Quick start (vLLM, 2ร— L40S)

pip install "vllm>=0.34.0"
pip install https://huggingface.co/ramachandrajoshi/LoopedPhai-A1-35B/resolve/main/dist/looped_qwen_moe-0.2.1-py3-none-any.whl

vllm serve ramachandrajoshi/LoopedPhai-A1-35B \
  --tensor-parallel-size 2 --max-model-len 131072 \
  --reasoning-parser qwen3 --tool-call-parser qwen3_coder --enable-auto-tool-choice \
  --served-model-name looped-phai

The wheel registers a vLLM plugin (vllm.general_plugins). config.json selects LoopedQwen3_5MoeForConditionalGeneration (keeps the vision tower) with "looped": {"adapter": "./adapter", "num_passes": 3}. The adapter is fetched from this repo.

  • Base model behaviour from the same deployment: LOOPED_NUM_PASSES=1.
  • More thinking per token: LOOPED_NUM_PASSES=4. The adapter was trained on T = 2โ€“4.
  • Text-only: --hf-overrides '{"architectures": ["LoopedQwen3_5MoeForCausalLM"]}'.
  • --cpu-offload-gb is not supported when T>1. The FP8 model (38 GB) has to fit in GPU memory.
Env var Default Meaning
LOOPED_NUM_PASSES 3 (from config) total core passes T (1 = plain base model)
LOOPED_ADAPTER ./adapter (from config) another adapter: local dir or Hub org/repo[@rev][:subfolder]; none = identity
LOOPED_CD_OMEGA 0.0 LoopCD-Hidden extrapolation ฯ‰ (try 0.1โ€“0.2 at Tโ‰ฅ3)
LOOPED_CD_REF prev prev (M_{T-2}) or first (M_0)

The same keys work as --hf-overrides '{"looped": {...}}'. The adapter can be hot-swapped without a restart; see looped_qwen_moe/vllm_plugin/worker_ext.py.

Cost at T=3
Extra weights 0 GB (passes share the base weights by reference) + 26 MB adapter
Extra compute โ‰ˆ1.6ร— per token (12 of 40 layers run twice more)
Extra KV cache โ‰ˆ1.6ร— full-attention KV (3 of 10 attention layers repeated twice)
vLLM features continuous batching, prefix caching, CUDA graphs, torch.compile, tensor parallel

Adapter v1

Data (1,603 examples). Only complete answers that fit in 2,048 tokens; nothing is truncated. Coding and instruction following come first:

Source Examples Licence
bigcode/self-oss-instruct-sc2-exec-filter-50k (execution-verified Python) 600 ODC-BY
nvidia/Nemotron-Post-Training-Dataset-v1 code (reasoning traces) 300 CC-BY-4.0
allenai/tulu-3-sft-personas-instruction-following 300 ODC-BY
nvidia/Nemotron-Post-Training-Dataset-v1 STEM 150 CC-BY-4.0
Teacher-verified (inkling-2-mini): KodCode tasks passing their pytest tests in a sandbox, RLVR-IFeval with constraints checked, UltraChat 103 โ€”
Teacher-verified GSM8K (math retention) 150 MIT

Training. One epoch, 401 steps ร— 4 examples, depth T sampled from {2, 3, 4} per example. Loss: cross-entropy plus a top-K KL anchor to the base model (ฮป = 0.05). Only the adapter is trained (base frozen). It ran on a single RTX 5060 Ti 16 GB with FP8 experts streamed from CPU RAM, ~11 h. Arguments and full log: adapter/train_args.json, adapter/train_log.jsonl.

Held-out loss (37 examples from the same sources, never trained on; lower is better). T=1 is the unchanged base model:

Step T=1 T=2 T=3
0 0.680 0.680 0.680
100 0.680 0.619 0.616
200 0.680 0.600 0.597
401 0.680 0.582 0.580

Generation eval (small, local). Greedy decoding, thinking off. Code is run against unit tests in a sandbox and instruction constraints are machine-checked (scripts/eval_local.py, results in eval/):

T=1 (base) T=3 (looped)
HumanEval (20) 20/20 19/20
KodCode held-out (10) 7/10 5/10
RLVR-IFeval (20) 11/20 12/20

These differences are within noise. This small eval, with thinking off, showed no gain. The larger thinking-on eval below did.

Generation A/B (thinking on, 2ร— L40S)

One vLLM deployment (TP=2, FP8) serves the model. LOOPED_NUM_PASSES=1 gives the unchanged base model, and 3 gives adapter v1 at T=3. Both use the same sampling settings and seeds, thinking on, and a 32k-token cap. Code runs against all tests in a sandbox (scripts/eval_code_api.py).

Benchmark Base (T=1) Looped (T=3) Diff 95% CI (paired) Capped at 32k (T=1 โ†’ T=3)
LiveCodeBench v6 (100 problems ร— 2) 33.5% 44.5% +11.0 [+6.5, +16.0] 65% โ†’ 48%
KodCode held-out (50 ร— 2) 47.0% 49.0% +2.0 [โˆ’8.0, +11.0] 20% โ†’ 12%
Web/3D pages, render without errors (12 ร— 2) 62.5% 70.8% +8.3 [โˆ’16.7, +33.3] 21% โ†’ 8%

On LiveCodeBench the looped model solved 21 problems the base model did not, and the base model solved 1 that the looped model did not. Most of the gain comes from thinking that finishes. The base model ran out of its 32k budget mid-thought on 65% of samples, against 48% for the looped model, and mean output fell from 24.6k to 20.1k tokens. The looped model is also โ‰ˆ1.6ร— slower per token (about 34 vs 55 tok/s per stream at 16 concurrent requests).

Is it the loop or just the tuning? On held-out loss, the matched no-loop control (below) gets almost as far as the looped adapter (0.588 vs 0.580; base 0.680). So the generation gain may come mostly from fine-tuning. The control's generation eval decides it, and its results will be added here.

--mode flat trains this no-loop control. The core runs once, with the same block adapters, router LoRA and coda adapters, and a matched parameter count (--block-rank 96: 6.77 M vs 6.51 M). It is trained on exactly v1's data (scripts/train_control_16gb.sh). scripts/l40s_ab.sh compares base, looped T=3 and the control with thinking on:

  • LiveCodeBench v6: 100 problems ร— 2 samples, run in a sandbox.
  • KodCode held-out: 50 problems.
  • 12 web/3D pages (data/visual_prompts.jsonl), rendered headless and judged blind A/B.

scripts/eval_code_api.py --compare reports paired differences with 95% CIs. See docs/TRAINING.md. For visual tasks, scripts/visual_refine.py runs a render-and-look loop: the model sees a screenshot of its page and fixes it.

Train your own adapter

pip install "looped_qwen_moe[train] @ https://huggingface.co/ramachandrajoshi/LoopedPhai-A1-35B/resolve/main/dist/looped_qwen_moe-0.2.1-py3-none-any.whl"
hf download ramachandrajoshi/LoopedPhai-A1-35B --exclude "*.safetensors" --local-dir LoopedPhai-src   # scripts + docs
cd LoopedPhai-src
python scripts/build_open_mix.py --out data/open_mix.jsonl --eval-out data/open_mix_eval.jsonl
bash scripts/train_overnight_16gb.sh            # 16 GB GPU + ~64 GB RAM; 1x L40S: --expert-device cuda
python scripts/eval_local.py --adapter runs/adapter-v1/final --depths 1 3   # 16 GB, no vLLM
python scripts/eval_vllm.py --tp 2 --looped --tag looped_t3                 # 2x L40S

Teacher-distilled data (scripts/build_mix_dataset_api.py) needs an OpenAI-compatible endpoint (OPENAI_BASE_URL, OPENAI_API_KEY). See docs/TRAINING.md and docs/DESIGN.md.

Tests (no 35B download needed)

pip install -e ".[train,dev]"
python scripts/make_tiny_model.py --out runs/tiny-qwen35moe     # 150M-param random qwen3_5_moe
pytest -q                                                        # adapter maths, trainer parity, cached decode
RUN_VLLM_TESTS=1 pytest -q tests/test_vllm_plugin.py           # vLLM plugin checks (GPU, ~10 min)

Validation results so far: docs/VALIDATION.md.

Repository layout

config.json, model-*.safetensors, tokenizer*, ...   base Phai-A1-35B FP8 (unchanged) + looped config
adapter/          trained loop adapter + training args/log
eval/             generation eval results
dist/             pip wheel of the vLLM plugin + trainer
looped_qwen_moe/
  adapter/        framework-free adapter maths, config, checkpoint I/O (single source of truth)
  vllm_plugin/    vLLM model classes + plugin registration + worker extension
  training/       low-VRAM FP8 trainer (HF Transformers layers + streamed experts, cached decode)
scripts/          dataset building, training, eval, publishing
tests/            pytest suites
docs/             design, training and validation notes

Roadmap

  • vLLM plugin (TP-safe, CUDA graphs, per-pass KV/GDN caches, router LoRA, LoopCD-Hidden)
  • Trainer with FP8 expert streaming (16 GB) and an all-GPU mode (48 GB)
  • Adapter v1: coding / instruction following / chat mix, 15% lower held-out loss at T=3
  • Small local generation eval (HumanEval / KodCode / IFEval): no gain from v1
  • Tensor-parallel (TP=2) serving on 2ร— L40S
  • LiveCodeBench / KodCode / visual A/B: +11 points on LiveCodeBench v6 at T=3 vs base
  • No-loop control adapter trained (held-out loss 0.588 vs looped 0.580)
  • Control generation eval: does the loop add anything beyond fine-tuning?
  • Adapter v2 (in training): 18.9 M parameters, 4k context, plus verified concise LiveCodeBench v1โ€“v5 solutions
  • Generation evals vs Qwen3.8-27B (only if the A/B shows a real gain)
  • Larger data mix, optional RLVR stage, MTP speculative decoding with the looped model

Licence & credits

Base weights: Apache-2.0, from PhaiModelLab/Phai-A1-35B-1M, FP8 by ramachandrajoshi. Adapter and code: MIT (LICENSE). Adapter design (gated workspace entry, cross-pass mixer, pass/remaining conditioning, LoopCD-Hidden) from jasontitus/loopedqwen (MIT). Training data licences are listed above.

Downloads last month
176
Safetensors
Model size
36B params
Tensor type
BF16
ยท
F8_E4M3
ยท
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Model tree for ramachandrajoshi/LoopedPhai-A1-35B

Quantized
(1)
this model