Views
No views yet
Attention Residuals (Chen
et al., Moonshot AI).1hidden_size: 768
2num_layers: 12
3num_attention_heads: 12
4num_key_value_heads: 4
5vocab_size: 50304
6use_attn_res: True
7sublayers_per_block: 2transformers.AutoModelForCausalLM via our
AttnResLMForCausalLM wrapper (src/model/hf_wrapper.py). When you load
it through any of our training / inference scripts, the wrapper's
AutoConfig.register("attnres", ...) call has already run, so a plain
from_pretrained(repo_id) call just works - no trust_remote_code=True
required and no modeling files uploaded to the Hub. For fully external
usage from a fresh Python session that hasn't imported our wrapper, pass
trust_remote_code=True (or import src.model.hf_wrapper once before
loading):1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4repo_id = "oars344/attnres-phase1"
5
6tokenizer = AutoTokenizer.from_pretrained(repo_id)
7model = AutoModelForCausalLM.from_pretrained(
8 repo_id,
9 torch_dtype=torch.bfloat16, # 114M params -> ~228 MB in bf16
10 device_map="auto",
11)
12model.eval()
13
14prompt = "Once upon a time"
15inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
16with torch.no_grad():
17 output = model.generate(**inputs, max_new_tokens=32, do_sample=True, top_p=0.95)
18
19print(tokenizer.decode(output[0], skip_special_tokens=True))AttnResLMAttnResLM under model.model.
The learned pseudo-query projections inside each block's
BlockAttnRes live at model.model.layers[i].{attn_res,mlp_res}.proj:1# Probing the learned residual aggregation weights:
2projection = model.model.layers[0].attn_res.proj.weight # [1, hidden_size]
3print(f"Layer-0 attn_res pseudo-query shape: {tuple(projection.shape)}")
4# `mlp_res.proj` is the corresponding projection for the MLP-side residual.src/training/train_phase2.py. AttnRes-aware target modules include the
seven standard Llama-style linears plus the BlockAttnRes pseudo-query
projections (attn_res.proj, mlp_res.proj), so LoRA can re-route the
residual stream for downstream tasks:1from peft import LoraConfig, get_peft_model
2
3attnres_targets = [
4 "q_proj", "k_proj", "v_proj", "o_proj",
5 "gate_proj", "up_proj", "down_proj",
6 "attn_res.proj", "mlp_res.proj",
7]
8model = get_peft_model(
9 model,
10 LoraConfig(r=16, lora_alpha=32, task_type="CAUSAL_LM",
11 target_modules=attnres_targets),
12)
13model.print_trainable_parameters()