When to Adapt: Conditional Memory Adapters for Retention-Preserving Domain Specialization
Organizations: University of Electronic Science and Technology of China, Chengdu, China
Abstract
Large language models deployed in specialized domains must improve in-domain performance without sacrificing general capabilities. Existing parameter-efficient fine-tuning methods are typically always on: their learned perturbations are applied to every input, which can degrade out-of-domain (OOD) performance. We propose Engram Adapter, a framework that repurposes pretraining-time conditional memory as a post-hoc adapter for frozen LLMs. It uses multi-channel matching over local n-gram patterns with explicit occupancy tracking as a lightweight selectivity prior, making residual injection more likely on in-domain inputs while a learned scalar gate suppresses incoherent OOD retrievals. We evaluate on Qwen3-4B and Qwen3-8B with AG-News and MedMCQA as adaptation tasks and OOD benchmarks spanning reasoning, translation, code generation, and legal reasoning. Engram Adapter improves in-domain accuracy while preserving 99.4%--100.1% of average OOD performance; on LegalBench it slightly exceeds the frozen base model on average, whereas comparable always-on baselines degrade sharply. Mechanistic analyses show that although OOD activations are non-zero, gate and projection attenuation reduce residuals to approximately 0.08% of hidden-state norm, yielding small KL drift and negligible accuracy change. These results suggest conditional activation is a promising route toward modular, retention-preserving domain specialization over frozen backbones.
Figures & tables
| In-Domain | Out-of-Domain (General Capabilities) | |||||
| Method | AG-News | ARC-C | MedMCQA | FLORES | FLORES | MBPP |
| Acc.% | Acc.% | Acc.% | BLEU | chrF | pass@1 | |
| Base Model | 85.14 | 88.99 | 53.65 | 29.85 | 58.85 | 66.80 |
| LoRA | 89.89 0.50 | 88.14 0.49 | 53.72 0.33 | 28.96 0.44 | 58.13 0.18 | 56.67 12.21 |
| LoRA | 90.15 0.14 | 88.11 0.38 | 54.90 0.27 | 29.55 0.19 | 58.61 0.06 | 64.93 0.84 |
| Adapter | 90.54 | 87.88 | 53.41 | 29.91 | 58.56 | 56.00 |
| In-Domain | Out-of-Domain (General Capabilities) | |||||
| Method | MedMCQA | ARC-C | AG-News | FLORES | FLORES | MBPP |
| Acc.% | Acc.% | Acc.% | BLEU | chrF | pass@1 | |
| Base Model | 53.65 | 88.99 | 85.14 | 29.85 | 58.85 | 66.80 |
| LoRA | 54.38 0.33 | 88.22 0.19 | 86.30 0.24 | 30.06 0.29 | 58.56 0.18 | 64.07 0.34 |
| LoRA | 57.10 0.27 | 88.14 0.14 | 86.13 0.12 | 30.17 0.10 | 58.97 0.01 | 65.13 0.09 |
| Adapter | 55.49 | 87.97 | 84.61 | 29.23 | 58.82 | 56.40 |
| AG Med | Med AG | |||
|---|---|---|---|---|
| Metric | All | Active | All | Active |
| Mean act. | 28.26% | 26.28% | ||
| Mean gate | 0.599 | 0.640 | 0.656 | 0.690 |
| Mean | 0.078% | 0.275% | 0.076% | 0.290% |
| P95 | 0.371% | 1.124% | 0.348% | 1.233% |
| Metric | AG Med | Med AG |
|---|---|---|
| Base accuracy | 53.65% | 85.14% |
| Adapted accuracy | 53.60% | 86.57% |
| Prediction changed | 7.55% | 2.16% |
| Negative flip rate | 3.80% | 0.36% |
| Positive flip rate | 3.75% | 1.79% |
| Mean KL to base | 0.088 | 0.040 |
| In-Domain | OOD | OOD Avg. | |||
| Method | AG-News | MedMCQA | ARC-C | LegalBench | Ret.% |
| Acc.% | Acc.% | Acc.% | Avg. Acc.% | ||
| Base Model | 79.49 | 55.51 | 89.77 | 71.91 | 100.00 |
| LoRA | 90.29 | 57.92 | 90.53 | 44.67 | 89.1 |
| LoRA (lr= ) | 86.96 | 57.33 | 89.68 | 61.08 | 96.0 |
| LoRA (lr= ) | 82.18 | 53.60 | 89.52 | 68.54 | 97.2 |
Appendix figures & tables11 assets
Supplementary material from the paper’s appendix.
Appendix
| Model | Train task | Train size | Eval | OOD evaluation | Baselines |
|---|---|---|---|---|---|
| Qwen3-4B | AG-News | 1,000 | 0-shot; MBPP 3-shot | ARC-C, MedMCQA, FLORES, MBPP | LoRA (3 seeds), DoRA, PiSSA, Adapter, |
| Qwen3-4B | MedMCQA | 1,000 | 0-shot; MBPP 3-shot | ARC-C, AG-News, FLORES, MBPP | LoRA (3 seeds), DoRA, PiSSA, Adapter, |
| Qwen3-8B | AG-News | 10,000 | zero-shot | MedMCQA, ARC-C, LegalBench | LoRA , LoRA lr= , LoRA lr= , TALR |
| Hyperparameter | Default | Description |
|---|---|---|
| inject_layers | Layer indices with adapters | |
| ngram_orders | Suffix -gram sizes | |
| prev_ngram_orders | Previous-context sizes | |
| num_hashes | Hash functions per lookup | |
| table_size | Buckets per hash table | |
| per_hash_dim | / | Embedding dim per hash (4B / 8B) |
| Property | LoRA | Engram Adapter |
|---|---|---|
| Adapted path | Always-on | Additive residual over frozen |
| Activation condition | Always on | Occupancy-conditioned -gram matches |
| OOD behavior | Full applied | Gate-attenuated ( 0.1% residual) |
| Retention mechanism | None (empirical only) | Gate + mask (empirical) |
| False positive control | N/A | Selectivity prior tunable via and |
| Composability | Requires merging / switching | Separate adapter module over frozen backbone |
| Method | ARC-C | MedMCQA | FLORES (BLEU) | MBPP | Avg. Ret. |
|---|---|---|---|---|---|
| LoRA | 99.04 0.55 | 100.12 0.62 | 97.01 1.48 | 84.83 18.28 | 95.25 4.95 |
| LoRA | 99.01 0.43 | 102.32 0.50 | 98.98 0.62 | 97.21 1.25 | 99.38 0.51 |
| Adapter | 98.75 | 99.55 | 100.20 | 83.83 | 95.58 |
| DoRA | 99.04 | 101.73 | 100.77 | 89.52 | 97.77 |
| PiSSA | 22.34 | 60.07 | 0.00 | 0.00 | 20.60 |
| 96.65 | 99.27 | 71.09 | 84.43 | 87.87 |
| Method | ARC-C | AG-News | FLORES (BLEU) | MBPP | Avg. Ret. |
|---|---|---|---|---|---|
| LoRA | 99.14 0.21 | 101.36 0.28 | 100.69 0.96 | 95.91 0.51 | 99.28 0.20 |
| LoRA | 99.04 0.16 | 101.16 0.14 | 101.06 0.34 | 97.50 0.14 | 99.69 0.06 |
| Adapter | 98.85 | 99.38 | 97.92 | 84.43 | 95.15 |
| DoRA | 99.23 | 99.55 | 100.23 | 85.33 | 96.09 |
| PiSSA | 30.68 | 0.00 | 0.00 | 0.00 | 7.67 |
| 70.66 | 99.34 | 70.10 | 86.83 | 81.74 |
| Task | Base | Engram | LoRA64 | lr= | lr= | TALR |
|---|---|---|---|---|---|---|
| abercrombie | 36.84 | 40.00 | 36.84 | 23.16 | 37.89 | 32.63 |
| consumer_contracts_qa | 90.40 | 90.91 | 69.70 | 92.68 | 92.17 | 92.68 |
| contract_nli_confid. | 78.05 | 78.05 | 74.39 | 79.27 | 78.05 | 89.02 |
| contract_nli_limited | 82.69 | 83.17 | 65.87 | 81.25 | 83.17 | 81.25 |
| contract_nli_return | 81.82 | 81.82 | 75.76 | 83.33 | 81.82 | 87.88 |
| contract_qa | 97.50 | 97.50 | 97.50 | 97.50 | 97.50 | 98.75 |
| Signal | Med. | ARC-C | Legal |
|---|---|---|---|
| Engram -gram density | 0.60 | 0.17 | 0.27 |
| TF-IDF max cosine | 16.90 | 24.06 | 31.53 |
| Qwen embedding retrieval | 0.00 | 0.00 | 0.10 |
| AG MedMCQA | Med AG-News | ||||
| Type | Variant | Mean% | P95% | Mean% | P95% |
| — | Baseline | 0.275 | 1.124 | 0.290 | 1.233 |
| Gate | (disabled) | 0.505 | 2.124 | 0.509 | 1.990 |
| no_dot | 0.273 | 1.086 | 0.286 | 1.105 | |
| Method | Params | Train profile | Peak mem. |
|---|---|---|---|
| Base Model | 0 | — | — |
| LoRA | 132.1M | 20.88s / 47.88 ex/s | 23.2GB |
| LoRA | 66.1M | 19.85s / 50.38 ex/s | 22.4GB |
| Engram Adapter | 154.0M | 19.80s / 50.50 ex/s | 17.5GB |
| Latency Overhead | Activation Rate | |||||
|---|---|---|---|---|---|---|
| Adapter | Eval Set | Forward | Generation | n2 | n3 | p3 |
| AG-News | AG-News (in-domain) | +12.3% | — | 0.500 | 0.356 | 0.376 |
| AG-News | MedMCQA | +39.9% | — | 0.219 | 0.126 | 0.137 |
| AG-News | ARC-C | +34.5% | — | 0.194 | 0.052 | 0.062 |
| AG-News | MBPP | +1.45% | +21.8% | 0.397 | 0.221 | 0.221 |
| MedMCQA | MedMCQA (in-domain) | +36.6% | — | 0.717 | 0.601 | 0.610 |
Domain Adaptation for LLMs
Domain adaptation for large language models tailors their knowledge and behavior to specialized fields, languages, and tasks.
See all 93 papers