Model Carving: Training Transformers That Optimize Their Own Topology
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.
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.
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.
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.
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.
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.
| Model | Inference GFLOPs / token | Val. bpb ↓ | CORE-22 ↑ | Compute multiplier \(k\) |
|---|---|---|---|---|
| Looped Transformer (four loops) | ||||
| 140M | 1.18 → 1.09 (−8%) | 0.899 → 0.883 | 18.9 → 19.9 | 1.33 |
| 370M | 3.04 → 2.36 (−22%) | 0.808 → 0.788 | 25.7 → 28.5 | 1.55 |
| 770M | 6.47 → 4.76 (−26%) | 0.743 → 0.723 | 31.1 → 33.5 | 1.62 |
| 1.3B | 10.67 → 8.17 (−23%) | 0.704 → 0.683 | 35.2 → 36.0 | 1.71 |
| Standard Transformer | ||||
| 140M | 0.35 → 0.31 (−13%) | 0.926 → 0.922 | 15.7 → 16.5 | 1.07 |
| 370M | 0.84 → 0.67 (−20%) | 0.835 → 0.832 | 22.1 → 22.0 | 1.07 |
| 770M | 1.74 → 1.37 (−22%) | 0.769 → 0.767 | 26.7 → 27.2 | 1.05 |
| 1.3B | 2.82 → 2.03 (−28%) | 0.729 → 0.727 | 29.8 → 31.2 | 1.05 |
| 2.7B | 5.62 → 3.74 (−34%) | 0.676 → 0.675 | 36.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.
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.
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!