# Memdlm Parametric Memory

> Enhance diffusion language model performance on long-context tasks by embedding simulated denoising into training via bi-level optimization. Fast weights capture local trajectory experience; base model optimized with accumulated parametric memory. Achieves +17.0% on RULER Variable Tracking (8K) and +9.6% on BABILong with gains primarily from training-stage improvements.

- Skill: `adu2021/memdlm-parametric-memory` (Agent Skill)
- Install (CLI): `npx skillmds@latest add adu2021/memdlm-parametric-memory`
- Raw SKILL.md: https://api.skillmd.com/api/skills/adu2021/memdlm-parametric-memory/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- License: MIT
- Author: adu2021 (https://skillmd.com/u/adu2021)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/adu2021/memdlm-parametric-memory

---


## Component ID
Bi-level parametric memory optimization for diffusion language model training.

## Motivation
Long-context language tasks require models to maintain accurate information over extended sequences. Diffusion language models struggle on needle-in-haystack tasks where critical information appears sparsely. Adding explicit parametric memory that captures local trajectory experience during training offloads memorization pressure from token representations, improving long-context capability.

## What Was Modified

### Bi-Level Optimization Framework
Insert a simulated denoising process into DLM training through two nested optimization loops:

```python
# Bi-level optimization: fast weights + base model optimization
class MemDLMTraining:
    """
    Inner loop updates fast weights (parametric memory).
    Outer loop updates base model conditioned on memory.
    """
    def __init__(self, base_model, device="cuda"):
        self.base_model = base_model
        self.fast_weight_optimizer = None

    def bi_level_step(self, batch_data, anchor_state, target_state):
        """
        Two-stage trajectory: pre-anchor alignment → anchor-to-target prediction.
        Fast weights act as Parametric Memory capturing local trajectory experience.
        """
        # ========== INNER LOOP: Update Fast Weights ==========
        # Stage 1: Denoise from noisier state toward anchor state
        current_state = batch_data
        for denoising_step in range(self.inner_steps):
            # Compute residual between current and anchor
            residual = current_state - anchor_state
            # Update fast weights to predict residual
            loss_pre_anchor = self.fast_weights.predict_residual(
                current_state, residual
            )
            loss_pre_anchor.backward()
            self.fast_weight_optimizer.step()

        # Stage 2: Predict clean output from anchor state
        anchor_embedding = self.base_model.encode(anchor_state)
        loss_anchor_to_target = self.fast_weights.predict_clean(
            anchor_embedding, target_state
        )
        loss_anchor_to_target.backward()
        self.fast_weight_optimizer.step()

        # ========== OUTER LOOP: Update Base Model ==========
        # Condition base model update on accumulated parametric memory
        memory_repr = self.fast_weights.get_memory()
        base_loss = self.base_model.compute_loss(
            batch_data, target_state, memory_conditioning=memory_repr
        )
        base_loss.backward()
        self.base_model.optimizer.step()

        return loss_pre_anchor + loss_anchor_to_target + base_loss
```

### Key Modifications

**1. Fast Weights (Parametric Memory)**:
- Parameter-efficient adapters trained on sample-specific trajectories
- Explicitly capture local experience rather than dispersing it across token embeddings
- Allow base model to focus on global patterns

**2. Two-Stage Anchor-Consistent Trajectory**:
- **Pre-anchor alignment**: Model learns to map from noisy state toward reference state
- **Anchor-to-target**: Model learns to complete task from anchor to clean output
- Provides natural curriculum: first stage teaches representation refinement, second teaches task completion

**3. First-Order Outer Loop Approximation**:
- Efficient gradient update of base model conditioned on fast weights
- Avoids expensive second-order derivatives
- Scales to large language model sizes

## Performance Delta

### Long-Context Benchmark Results

**RULER Variable Tracking (8K context, LLaDA-MoE)**:
```
Baseline:              78.84%
MemDLM (Train+Inf):    95.80%
Improvement:           +17.0% absolute
```

**BABILong (8K context, LLaDA2.1)**:
```
Baseline:              47.40%
MemDLM (Train+Inf):    57.00%
Improvement:           +9.6% absolute
```

**Key Finding**: "Train-Only" variant shows most gains derive from training-stage improvements rather than inference-time adaptation alone.

### Generalization

Performance improvements persist across different model architectures and long-context task families, suggesting the mechanism captures fundamental improvements in memorization capacity rather than task-specific artifacts.

## Conditions

### Effective Scenarios
- Long-context language tasks (8K+ effective context)
- Tasks requiring precise information retention (needle-in-haystack, fact recall, context-dependent reasoning)
- Diffusion language models as primary architecture
- Training-time budget available (bi-level optimization adds moderate overhead)

### Model Scales
- Tested on: LLaDA-MoE, LLaDA2.1 (medium to large language models)
- Expected to benefit: Any language model struggling with long-context information density
- Overhead: Single bi-level step adds ~2× inner loop cost; manageable with checkpointing

### Training Infrastructure Requirements
- Support for gradient checkpointing (required for memory efficiency)
- Backward compatibility with existing DLM training pipelines
- Ability to define anchor states (typically intermediate denoising steps)

## Drop-In Checklist

- [ ] **Identify integration point**: Add MemDLM module to DLM training loop after forward pass
- [ ] **Initialize fast weights**: Create parameter-efficient adapters for parametric memory (LoRA, prefix, adapter-based)
- [ ] **Define anchor states**: Choose intermediate denoising step as reference (empirically around 50% noise schedule)
- [ ] **Implement inner loop**: Pre-anchor alignment stage + anchor-to-target stage (2–4 steps each)
- [ ] **Set outer loop**: First-order approximation gradient step for base model
- [ ] **Validate memory formation**: Inspect fast weights—confirm they capture sample-specific trajectories
- [ ] **Benchmark on long-context task**: Test on needle-in-haystack or variable tracking (target: +15% improvement)
- [ ] **Profile overhead**: Measure wall-clock training time increase (expect 1.5–2.5× per step)
- [ ] **Tune anchor position**: Experiment with 25%, 50%, 75% noise schedule points
- [ ] **Verify generalization**: Test on multiple long-context task families (not just training task)


