Qwen/Qwen2.5-0.5B (mqa) — NeuralOps Qwen Attention Zoo

Module Summary (MultiQueryAttention): Multi-Query Attention (MQA) — Shazeer 2019. MQA is a special case of GQA with exactly ONE key/value head shared across all H_Q query heads. This yields: - KV-cache reduction by factor of H_Q (e.g., 12×) - Faster inference (fewer memory reads) - Slight quality drop that can be recovered via KD Architecture: q = q_proj(x) # (B, N, H*D) k = k_proj(x) # (B, N, D) — single KV head v = v_proj(x) # (B, N, D) attn = softmax(q @ k^T / √D) @ v # broadcast k, v across H out = out_proj(attn)
This repository contains a distilled Qwen/Qwen2.5-0.5B checkpoint whose decoder attention blocks have undergone surgical replacement with mqa from the NeuralOps ATTENTION_REGISTRY, trained on WikiText-2 (Salesforce/wikitext / wikitext-2-raw-v1).
Module Capability Profile
| Property | Value |
|---|
| Registry Key | mqa |
| Implementation Class | MultiQueryAttention |
Causal Autoregressive Support (supports_causal) | Yes |
Bidirectional Support (supports_bidirectional) | Yes |
Cross-Attention Support (supports_cross_attention) | Yes |
Native GQA Support (supports_gqa) | Yes |
Rotary Position Support (supports_rope) | Yes |
Custom Attention Operator (is_custom_attention) | Yes |
Surgery Topology
- Pattern:
hybrid_3_1
- Layer Layout: 3:1 interleaved hybrid (
hybrid_3_1): 18/24 layers transplanted with mqa via QwenAttentionAdapter (14:2 GQA expansion, Single-o_proj contract), and 6/24 full quadratic anchor layers preserved at indices [3, 7, 11, 15, 19, 23].
- Adapter Contract:
QwenAttentionAdapter maps Qwen2Attention projections (14 query heads, 2 KV heads, head_dim=64, hidden_size=896) with automatic 7x KV-head expansion where required and enforces the Single-o_proj bypass contract.
2-Stage Knowledge Distillation Telemetry
| Metric / Stage | Value | Notes |
|---|
| Pre-KD Causal PPL (Stage 0) | 541890.55 | Zero-shot post-surgery baseline on wikitext-2-raw-v1 |
| Post-KD Causal PPL | 205.89 | Final validation perplexity |
| Perplexity Delta ($\Delta$) | -541684.67 | Negative delta indicates recovery |
| Stage 1 (Frozen-Backbone Alignment) | 157 steps (54.3s) | lm_head bypassed; layerwise hidden MSE + cosine (0.25) |
| Stage 2 (Multi-Layer Representation KD) | 360 steps (125.8s) | Layerwise hidden MSE + cosine (0.50) |
| Total Optimization Steps / Time | 517 steps / 180.3s | seq_len=512, grad_accum=4, topk=64 |
| Peak GPU VRAM | 12234 MB | FP16 autocast with FP32 master student parameters |
Weight Artifacts
| Artifact | Size |
|---|
attention_weights.safetensors | 82.27 MB |
config.json | 2.2 KB |
tokenizer.json | 10.89 MB |
training_metrics.json | 362 B |
Quick Start (Attention-Only Weights)
import torch
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
from transformers import AutoModelForCausalLM, AutoTokenizer
from scripts.benchmarks.qwen_zoo_train_mix import apply_qwen_attention_surgery
base_model_id = "Qwen/Qwen2.5-0.5B"
repo_id = "tahamajs/qwen-2.5-0.5b-mqa-wikitext2"
tokenizer = AutoTokenizer.from_pretrained(repo_id)
model = AutoModelForCausalLM.from_pretrained(base_model_id, torch_dtype=torch.float16)
model = apply_qwen_attention_surgery(model, attn_key="mqa", pattern="hybrid_3_1")
ckpt_path = hf_hub_download(repo_id=repo_id, filename="attention_weights.safetensors")
model.load_state_dict(load_file(ckpt_path), strict=False)
model = model.eval()
inputs = tokenizer("NeuralOps subquadratic attention enables", return_tensors="pt")
with torch.no_grad():
out = model.generate(**inputs, max_new_tokens=32)
print(tokenizer.decode(out[0], skip_special_tokens=True))
Quick Start (Universal NeuralOps Hub API)
import torch
from neuralops.hub import load_surgeried_model
# Load model and tokenizer/processor in one line directly from Hugging Face Hub
model, tokenizer = load_surgeried_model("tahamajs/qwen-2.5-0.5b-mqa-wikitext2")
model.eval()
print(f"Successfully loaded tahamajs/qwen-2.5-0.5b-mqa-wikitext2 with NeuralOps modular surgical weights!")