Model Carving: Training Transformers That Optimize Their Own Topology

1Harvard University 2New York University

Model Carving learns a Transformer's topology while it trains. Every sublayer writes to the residual stream through a learned gate, and when a gate falls to zero, we remove that computation.

Abstract

Machine learning has largely kept separate the choice of architecture and the process of training. A classic Transformer, for instance, trains a fixed set of attention and MLP sublayers at every layer, in the same order, and optimizes the weights within. This uniformity is a convention rather than a principled design choice, and it is likely suboptimal: the best architecture for a given task may be smaller, more efficient, and far from uniform. We believe that, ideally, training itself should shape the model’s topology. To this end, we introduce Model Carving, which learns both a Transformer’s weights and its architecture in a single run. By applying learnable, scalar gates to each sublayer, we allow the training loss to remove the model’s own components during training – freeing a smaller heterogeneous network inside. We evaluate Model Carving by pre-training standard Transformers from 140M to 2.7B parameters and looped Transformers from 140M to 1.3B. At equal training compute, carved models reach a lower validation loss than dense models trained with up to 1.7 times more compute. Furthermore, carved models also outperform size-matched models produced by pruning or distillation, suggesting that the best way to obtain small models for edge devices is to carve them during training, not to compress a large one afterwards. This advantage both grows with scale and carries over to downstream tasks such as few-shot evaluation and generation. Because the removed sublayers no longer run, carved models also cost up to 34% fewer FLOPs per token and decode up to 1.6 times faster without cost in quality. Our results show that architecture, like weights, should be learned and not fixed in advance, and that inside every large transformer is a smaller, faster, stronger one waiting to be carved out.

How Model Carving works

Every sublayer writes through a gate

Every attention and MLP sublayer \(u\) writes its output \(\mathbf y_u\) to the residual stream \(\mathbf z\) through a learned gate \(g_u \ge 0\), and learned skip connections allow the model to reuse the outputs of earlier layers in the current residual stream.

\[ \mathbf z \;\leftarrow\; \mathbf z + g_u\,\frac{\operatorname{rms}(\mathbf z)}{\operatorname{rms}(\mathbf y_u)}\,\mathbf y_u \]

We normalize each write to let the gate alone scale the write to the stream. In a looped Transformer, each sublayer per loop has its own gate, so a layer's sublayers are kept or removed separately in each loop.

Model 0 of 192 removed
Attention MLP
Gates
Kept, middle 80% Removed
update 0 of 78,840
Left: the looped 1.3B model has 24 layers looped four times, with sublayer computations being carved away when its gates shrink to zero. Right: gates and closures on a log scale over training.

The pressure on each gate

Each sublayer application that stays in the graph carries a pressure \(\pi_u\), which we construct as the loss its FLOPs would save if they were spent on more training tokens instead. We estimate it online from the run's own rate of learning, forecasting that rate to the end of training with a known power law.

\[ \pi_u(t) \;=\; v(T)\,\ln\!\Big(1 + \rho_t\,\frac{\phi_u}{1-\phi_u}\Big) \]

Here \(v(T)\) is the forecast rate of learning at the end of the run (the loss reduction every e-fold of tokens), \(\phi_u\) is the sublayer application's share of the training FLOPs per token, and \(\rho_t\) is the fraction of tokens still to come. Weights and gates train jointly on the same loss.

\[ \mathcal L_t \;=\; \mathcal L(\theta, c, g;\, B_t) \;+\; \sum_u \pi_u(t)\, \mathbf 1[g_u > 0] \]

The first term is the next-token loss on batch \(B_t\), with weights \(\theta\), skip-connection coefficients \(c\) and gates \(g\). The second adds the pressure \(\pi_u(t)\) for each sublayer application whose gate is above zero.

A gate falls while its sublayer application's gain is below its pressure, and holds or rises when the gain is back above it. Therefore, Model Carving does not require a penalty sweep! From 770M up, the pressure on an MLP sublayer application peaks between 6% and 12% of training and then falls as fewer tokens remain. The pressure is withheld during the cooldown.

Gain and pressure
Gate
update 0
Left: we derive the gain (the loss a sublayer's application saves) and the pressure (the loss its FLOPs would save as more training tokens) from the current training curve, averaging over the previous 1,000 updates. Right: the same sublayer application's gate.

Results

Better models at lower inference cost

Carved models reach lower validation loss than larger, dense models trained with the same FLOPs and shallower models with the same inference cost. The compute multiplier \(k\) is the training compute at which regular training reaches the carved loss, divided by the carved run's training compute. In looped Transformers, \(k\) grows from 1.33 at 140M to 1.71 at 1.3B.

Validation loss
Compute multiplier
0.0 × 10²⁰ FLOPs
Left: the looped 1.3B models train with the same compute budget, compared with Carving. Right: we fit L = E + A·C−α to the final losses of the regular looped models at 140M, 370M and 1.3B. Standard looped 1.3B models reach carved 1.3B model's loss at 1.71 times the compute.
Model Inference GFLOPs / token Val. bpb ↓ CORE-22 ↑ Compute multiplier \(k\)
Looped Transformer (four loops)
140M1.18 → 1.09 (−8%)0.899 → 0.88318.9 → 19.91.33
370M3.04 → 2.36 (−22%)0.808 → 0.78825.7 → 28.51.55
770M6.47 → 4.76 (−26%)0.743 → 0.72331.1 → 33.51.62
1.3B10.67 → 8.17 (−23%)0.704 → 0.68335.2 → 36.01.71
Standard Transformer
140M0.35 → 0.31 (−13%)0.926 → 0.92215.7 → 16.51.07
370M0.84 → 0.67 (−20%)0.835 → 0.83222.1 → 22.01.07
770M1.74 → 1.37 (−22%)0.769 → 0.76726.7 → 27.21.05
1.3B2.82 → 2.03 (−28%)0.729 → 0.72729.8 → 31.21.05
2.7B5.62 → 3.74 (−34%)0.676 → 0.67536.0 → 37.0–

Regular → carved, trained on the same data with the same training FLOPs.

Cheaper to run

Carved models use 8–34% fewer inference FLOPs per token. They decode up to 1.45× faster at equal batch size and keep 5–31% less KV cache per token.

Carving 0.00 s
Regular 0.00 s
Carved vs. regular looped 1.3B models on 200 tokens from the same prompt with batch 1, greedy decoding, one H100.

Higher scores with less training compute

We rescore released models with our own evaluation harness for fairness. The carved standard 1.3B scores higher on CORE-22 than OPT-1.3B and Pythia-1.4B, while using only 37% and 22% of their respective training compute.

CORE-22 against training compute for carved standard Transformers (blue) and released standard models of similar scale (gray), all scored with our evaluation harness.

Synthetic tasks

We also carve a looped Transformer with three layers and four loops on S5 composition, ListOps and modular addition modulo 113 to evaluate whether it finds minimal circuits.

On S5, we keep 11 of 24 sublayer applications and reach 100% test exact match. On modular addition, we keep only 2 of 24, both attention!

Model 0 of 24 removed
Attention MLP Removed
Validation exact match
Left: the sublayer applications and removal during training. Right: validation exact match accuracy.

BibTeX

@article{zheng2026modelcarving,
  title   = {Model Carving: Training Transformers That Optimize Their Own Topology},
  author  = {Zheng, Kaden and Sharma, Pratyusha},
  journal = {arXiv preprint},
  year    = {2026},
  url     = {https://kadenzheng.com/model-carving/}
}