Agent Conversation: gpt-5.4
Task: llm-pretrain-residual
LLM Pretraining: Residual Connection Strategy
Research Question
Improve the residual connection strategy in a GPT-style language model. The current architecture uses standard Pre-LN residual connections (x + sublayer(x)) in each transformer block. Your task is to redesign how information flows through the residual stream across layers to achieve lower validation loss.
Background
Standard Residual Connections
The default GPT architecture uses simple additive residual connections in each block:
x = x + self.attn(self.ln_1(x)) # attention sublayer
x = x + self.mlp(self.ln_2(x)) # MLP sublayer
While effective, this fixed accumulation pattern may not be optimal for deep networks. The residual stream is the primary information highway through the model, and its design critically affects gradient flow, feature reuse, and training dynamics.
Research Directions
Several recent works have proposed improvements to residual connections:
- Per-layer residual scaling: Learnable scalars that modulate the residual stream at each layer (inspired by modded-nanogpt, ReZero, SkipInit).
- Initial embedding blending: Blending the initial token embedding back at each layer to preserve token identity (x0 residual connections).
- Hyper-Connections: Maintaining m parallel residual streams with learned transition matrices for richer information flow across layers (Zhu et al., 2025).
- Attention Residuals: Using softmax attention over all previous layer outputs to dynamically select which representations to combine (Kimi Team, 2026).
What You Can Modify
Block Class (lines 88-99)
The Block class defines per-block residual behavior. You can change how attention and MLP outputs are combined with the residual stream within each block.
Residual Stream Parameters (lines 128-130)
Add custom parameters to GPT.__init__ for your residual connection strategy (e.g., per-layer scalars, transition matrices, query vectors).
Block Loop in GPT.forward (lines 162-164)
The main loop that iterates through transformer blocks. You can modify how blocks are called and how their outputs are accumulated (e.g., multi-stream processing, attention over layer outputs).
Optimizer Configuration (lines 175-192)
The configure_optimizers method. If you add new parameters, you may want to assign them to appropriate optimizer groups with custom learning rates and weight decay.
Training Hyperparameters (line 251)
The CONFIG_OVERRIDES dictionary for adjusting learning rate, weight decay, etc.
Note: The CausalSelfAttention, MLP, LayerNorm, and GPTConfig classes are fixed. The Block.forward signature must accept x and return a tensor of the same shape. The GPT.forward must accept (idx, targets=None) and return (logits, loss).
Evaluation
- Primary metric: Validation loss (
val_loss, lower is better) - Secondary metrics: Perplexity on WikiText-2 and LAM
Tool Input
1{
2 "op": "replace",
3 "filename": "nanoGPT/custom_pretrain.py",
4 "start_line": 88,
5 "end_line": 99,
6 "content": "class Block(nn.Module):\n def __init__(self, config):\n super().__init__()\n self.ln_1 = LayerNorm(config.n_embd, bias=config.bias)\n self.attn = CausalSelfAttention(config)\n self.ln_2 = LayerNorm(config.n_embd, bias=config.bias)\n self.mlp = MLP(config)\n\n def forward(self, x):\n # Return vanilla block output; cross-layer residual routing is handled in GPT.forward.\n x = x + self.attn(self.ln_1(x))\n x = x + self.mlp(self.ln_2(x))\n return x"
7}OK: Replaced lines 88..99 with 13 line(s) in nanoGPT/custom_pretrain.py. Editable range: 88–100, 129–131, 163–165, 176–193, 252–252.
[Current file: nanoGPT/custom_pretrain.py | editable: 88–100, 129–131, 163–165, 176–193, 252–252 | total: 442 lines]
88 class Block(nn.Module):
89 def __init__(self, config):
90 super().__init__()
... (7 more lines) ...
98 x = x + self.attn(self.ln_1(x))
99 x = x + self.mlp(self.ln_2(x))
100 return x
...
129 # ── Residual stream parameters ──
130 # (default: none — vanilla residual x + sublayer(x) is in Block.forward)
131 # Add custom residual parameters here if needed.
...
163 # ── Residual stream: iterate through transformer blocks ──
164 for block in self.transformer.h:
165 x = block(x)
...
176 def configure_optimizers(self, weight_decay, learning_rate, betas, device_type):
177 param_di
Tool Input
1{
2 "op": "replace",
3 "filename": "nanoGPT/custom_pretrain.py",
4 "start_line": 129,
5 "end_line": 131,
6 "content": " # ── ARX: Anchored Residual Mixture ──\n # Each layer mixes three information sources: previous state x,\n # current block update delta, and the initial embedding stream x0.\n # A learned depth-specific gate is normalized with softmax to keep\n # routing stable while still allowing dynamic residual reallocation.\n self.arx_logits = nn.Parameter(torch.zeros(config.n_layer, 3))\n self.arx_temp = 1.0"
7}OK: Replaced lines 129..131 with 7 line(s) in nanoGPT/custom_pretrain.py. Editable range: 88–100, 129–135, 167–169, 180–197, 256–256.
[Current file: nanoGPT/custom_pretrain.py | editable: 88–100, 129–135, 167–169, 180–197, 256–256 | total: 446 lines]
88 class Block(nn.Module):
89 def __init__(self, config):
90 super().__init__()
... (7 more lines) ...
98 x = x + self.attn(self.ln_1(x))
99 x = x + self.mlp(self.ln_2(x))
100 return x
...
129 # ── ARX: Anchored Residual Mixture ──
130 # Each layer mixes three information sources: previous state x,
131 # current block update delta, and the initial embedding stream x0.
132 # A learned depth-specific gate is normalized with softmax to keep
133 # routing stable while still allowing dynamic residual reallocation.
134 self.arx_logits = nn.Parameter(torch.zeros(config.n_layer, 3))
135 self.a
Tool Input
1{
2 "op": "replace",
3 "filename": "nanoGPT/custom_pretrain.py",
4 "start_line": 167,
5 "end_line": 169,
6 "content": " # ── ARX residual stream: softmax-normalized anchored mixture ──\n x0 = x\n for i, block in enumerate(self.transformer.h):\n block_out = block(x)\n delta = block_out - x\n mix = F.softmax(self.arx_logits[i] / self.arx_temp, dim=0)\n x = mix[0] * x + mix[1] * delta + mix[2] * x0"
7}OK: Replaced lines 167..169 with 7 line(s) in nanoGPT/custom_pretrain.py. Editable range: 88–100, 129–135, 167–173, 184–201, 260–260.
[Current file: nanoGPT/custom_pretrain.py | editable: 88–100, 129–135, 167–173, 184–201, 260–260 | total: 450 lines]
88 class Block(nn.Module):
89 def __init__(self, config):
90 super().__init__()
... (7 more lines) ...
98 x = x + self.attn(self.ln_1(x))
99 x = x + self.mlp(self.ln_2(x))
100 return x
...
129 # ── ARX: Anchored Residual Mixture ──
130 # Each layer mixes three information sources: previous state x,
131 # current block update delta, and the initial embedding stream x0.
132 # A learned depth-specific gate is normalized with softmax to keep
133 # routing stable while still allowing dynamic residual reallocation.
134 self.arx_logits = nn.Parameter(torch.zeros(config.n_layer, 3))
135 self.a
Tool Input
1{
2 "op": "replace",
3 "filename": "nanoGPT/custom_pretrain.py",
4 "start_line": 184,
5 "end_line": 201,
6 "content": " def configure_optimizers(self, weight_decay, learning_rate, betas, device_type):\n arx_ids = {id(self.arx_logits)}\n param_dict = {pn: p for pn, p in self.named_parameters() if p.requires_grad}\n decay_params = [p for n, p in param_dict.items() if p.dim() >= 2 and id(p) not in arx_ids]\n nodecay_params = [p for n, p in param_dict.items() if p.dim() < 2 and id(p) not in arx_ids]\n arx_params = [p for n, p in param_dict.items() if id(p) in arx_ids]\n optim_groups = [\n {'params': decay_params, 'weight_decay': weight_decay},\n {'params': nodecay_params, 'weight_decay': 0.0},\n {'params': arx_params, 'weight_decay': 0.0},\n ]\n num_decay_params = sum(p.numel() for p in decay_params)\n num_nodecay_params = sum(p.numel() for p in nodecay_params)\n num_arx_params = sum(p.numel() for p in arx_params)\n print(f\"num decayed parameter tensors: {len(decay_params)}, with {num_decay_params:,} parameters\")\n print(f\"num non-decayed parameter tensors: {len(nodecay_params)}, with {num_nodecay_params:,} parameters\")\n print(f\"num ARX parameter tensors: {len(arx_params)}, with {num_arx_params:,} parameters\")\n fused_available = 'fused' in inspect.signature(torch.optim.AdamW).parameters\n use_fused = fused_available and device_type == 'cuda'\n extra_args = dict(fused=True) if use_fused else dict()\n optimizer = torch.optim.AdamW(optim_groups, lr=learning_rate, betas=betas, **extra_args)\n print(f\"using fused AdamW: {use_fused}\")\n return optimizer"
7}OK: Replaced lines 184..201 with 23 line(s) in nanoGPT/custom_pretrain.py. Editable range: 88–100, 129–135, 167–173, 184–206, 265–265.
[Current file: nanoGPT/custom_pretrain.py | editable: 88–100, 129–135, 167–173, 184–206, 265–265 | total: 455 lines]
88 class Block(nn.Module):
89 def __init__(self, config):
90 super().__init__()
... (7 more lines) ...
98 x = x + self.attn(self.ln_1(x))
99 x = x + self.mlp(self.ln_2(x))
100 return x
...
129 # ── ARX: Anchored Residual Mixture ──
130 # Each layer mixes three information sources: previous state x,
131 # current block update delta, and the initial embedding stream x0.
132 # A learned depth-specific gate is normalized with softmax to keep
133 # routing stable while still allowing dynamic residual reallocation.
134 self.arx_logits = nn.Parameter(torch.zeros(config.n_layer, 3))
135 self.
Tool Input
1{
2 "op": "replace",
3 "filename": "nanoGPT/custom_pretrain.py",
4 "start_line": 265,
5 "end_line": 265,
6 "content": " CONFIG_OVERRIDES = {}"
7}OK: Replaced lines 265..265 with 1 line(s) in nanoGPT/custom_pretrain.py. Editable range: 88–100, 129–135, 167–173, 184–206, 265–265.
[Current file: nanoGPT/custom_pretrain.py | editable: 88–100, 129–135, 167–173, 184–206, 265–265 | total: 455 lines]
88 class Block(nn.Module):
89 def __init__(self, config):
90 super().__init__()
... (7 more lines) ...
98 x = x + self.attn(self.ln_1(x))
99 x = x + self.mlp(self.ln_2(x))
100 return x
...
129 # ── ARX: Anchored Residual Mixture ──
130 # Each layer mixes three information sources: previous state x,
131 # current block update delta, and the initial embedding stream x0.
132 # A learned depth-specific gate is normalized with softmax to keep
133 # routing stable while still allowing dynamic residual reallocation.
134 self.arx_logits = nn.Parameter(torch.zeros(config.n_layer, 3))
135 self.a
[Test #1] (2 tests remaining; call submit(n=N) to choose which test result to submit as final)
### gpt-345m (scripts/gpt_345m.sh)
Training metrics (last steps):
TRAIN_METRICS: step=9000, train_loss=3.8976, val_loss=3.6259
TRAIN_METRICS: step=10000, train_loss=3.8540, val_loss=3.5959
TRAIN_METRICS: step=11000, train_loss=6.4850, val_loss=6.3646
TRAIN_METRICS: step=12000, train_loss=3.8737, val_loss=3.6065
TRAIN_METRICS: step=13000, train_loss=3.9084, val_loss=3.6579
Final metrics (gpt-345m): val_loss_gpt-345m=3.5848, wikitext2_ppl_gpt-345m=315.1900, lambada_ppl_gpt-345m=267.5300
### lm-eval-345m (scripts/lm_eval_345m.sh)
Final metrics (lm-eval-345m): arc_easy_lm-eval-345m=40.0700, hellaswag_lm-eval-345m=26.2200, piqa_lm-eval-345m=57.1800, winogrande_lm-eval-345m=51.2200
[Leaderboard] Results saved: {'val_loss_gpt-345m': 3.5848, 'wikitext2_ppl_gpt-345m': 315.19, 'lambada_ppl_gpt-345m': 267.53, 'elapsed_gpt-345m': 21133, 'arc_easy_lm-eval-345m': 40.07, 'hellaswag_lm-eval-345m': 26.22,
Tool Input
1{
2 "op": "replace",
3 "filename": "nanoGPT/custom_pretrain.py",
4 "start_line": 129,
5 "end_line": 135,
6 "content": " # ── DARC: Depth-Adaptive Residual Competition ──\n # Layer i chooses among carry x, local delta, and anchor x0 using\n # content-aware gates from a tiny shared controller. The controller sees\n # pooled statistics of x and delta, producing token-independent but\n # input-adaptive routing that can increase update strength only when a\n # layer has a confident transform to contribute.\n self.darc_controller = nn.Linear(2, 3, bias=True)\n self.darc_depth_bias = nn.Parameter(torch.zeros(config.n_layer, 3))"
7}OK: Replaced lines 129..135 with 8 line(s) in nanoGPT/custom_pretrain.py. Editable range: 88–100, 129–136, 168–174, 185–207, 266–266.
[Current file: nanoGPT/custom_pretrain.py | editable: 88–100, 129–136, 168–174, 185–207, 266–266 | total: 456 lines]
88 class Block(nn.Module):
89 def __init__(self, config):
90 super().__init__()
... (7 more lines) ...
98 x = x + self.attn(self.ln_1(x))
99 x = x + self.mlp(self.ln_2(x))
100 return x
...
129 # ── DARC: Depth-Adaptive Residual Competition ──
130 # Layer i chooses among carry x, local delta, and anchor x0 using
131 # content-aware gates from a tiny shared controller. The controller sees
132 # pooled statistics of x and delta, producing token-independent but
133 # input-adaptive routing that can increase update strength only when a
134 # layer has a confident transform to contribute.
135
Tool Input
1{
2 "op": "replace",
3 "filename": "nanoGPT/custom_pretrain.py",
4 "start_line": 168,
5 "end_line": 174,
6 "content": " # ── DARC residual stream: content-adaptive depth-wise routing ──\n x0 = x\n for i, block in enumerate(self.transformer.h):\n block_out = block(x)\n delta = block_out - x\n stats = torch.stack([\n x.float().pow(2).mean(dim=(1, 2, 3)).sqrt(),\n delta.float().pow(2).mean(dim=(1, 2, 3)).sqrt(),\n ], dim=-1)\n gate_logits = self.darc_controller(stats).mean(dim=0) + self.darc_depth_bias[i]\n mix = F.softmax(gate_logits, dim=-1).to(dtype=x.dtype)\n x = mix[0] * x + mix[1] * delta + mix[2] * x0"
7}OK: Replaced lines 168..174 with 12 line(s) in nanoGPT/custom_pretrain.py. Editable range: 88–100, 129–136, 168–179, 190–212, 271–271.
[Current file: nanoGPT/custom_pretrain.py | editable: 88–100, 129–136, 168–179, 190–212, 271–271 | total: 461 lines]
88 class Block(nn.Module):
89 def __init__(self, config):
90 super().__init__()
... (7 more lines) ...
98 x = x + self.attn(self.ln_1(x))
99 x = x + self.mlp(self.ln_2(x))
100 return x
...
129 # ── DARC: Depth-Adaptive Residual Competition ──
130 # Layer i chooses among carry x, local delta, and anchor x0 using
131 # content-aware gates from a tiny shared controller. The controller sees
132 # pooled statistics of x and delta, producing token-independent but
133 # input-adaptive routing that can increase update strength only when a
134 # layer has a confident transform to contribute.
135
Tool Input
1{
2 "op": "replace",
3 "filename": "nanoGPT/custom_pretrain.py",
4 "start_line": 190,
5 "end_line": 212,
6 "content": " def configure_optimizers(self, weight_decay, learning_rate, betas, device_type):\n darc_ids = {id(self.darc_controller.weight), id(self.darc_controller.bias), id(self.darc_depth_bias)}\n param_dict = {pn: p for pn, p in self.named_parameters() if p.requires_grad}\n decay_params = [p for n, p in param_dict.items() if p.dim() >= 2 and id(p) not in darc_ids]\n nodecay_params = [p for n, p in param_dict.items() if p.dim() < 2 and id(p) not in darc_ids]\n darc_params = [p for n, p in param_dict.items() if id(p) in darc_ids]\n optim_groups = [\n {'params': decay_params, 'weight_decay': weight_decay},\n {'params': nodecay_params, 'weight_decay': 0.0},\n {'params': darc_params, 'weight_decay': 0.0},\n ]\n num_decay_params = sum(p.numel() for p in decay_params)\n num_nodecay_params = sum(p.numel() for p in nodecay_params)\n num_darc_params = sum(p.numel() for p in darc_params)\n print(f\"num decayed parameter tensors: {len(decay_params)}, with {num_decay_params:,} parameters\")\n print(f\"num non-decayed parameter tensors: {len(nodecay_params)}, with {num_nodecay_params:,} parameters\")\n print(f\"num DARC parameter tensors: {len(darc_params)}, with {num_darc_params:,} parameters\")\n fused_available = 'fused' in inspect.signature(torch.optim.AdamW).parameters\n use_fused = fused_available and device_type == 'cuda'\n extra_args = dict(fused=True) if use_fused else dict()\n optimizer = torch.optim.AdamW(optim_groups, lr=learning_rate, betas=betas, **extra_args)\n print(f\"using fused AdamW: {use_fused}\")\n return optimizer"
7}OK: Replaced lines 190..212 with 23 line(s) in nanoGPT/custom_pretrain.py. Editable range: 88–100, 129–136, 168–179, 190–212, 271–271.
[Current file: nanoGPT/custom_pretrain.py | editable: 88–100, 129–136, 168–179, 190–212, 271–271 | total: 461 lines]
88 class Block(nn.Module):
89 def __init__(self, config):
90 super().__init__()
... (7 more lines) ...
98 x = x + self.attn(self.ln_1(x))
99 x = x + self.mlp(self.ln_2(x))
100 return x
...
129 # ── DARC: Depth-Adaptive Residual Competition ──
130 # Layer i chooses among carry x, local delta, and anchor x0 using
131 # content-aware gates from a tiny shared controller. The controller sees
132 # pooled statistics of x and delta, producing token-independent but
133 # input-adaptive routing that can increase update strength only when a
134 # layer has a confident transform to contribute.
135