File size: 8,476 Bytes
c2d8a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
PC-SHO-DLM Ablation Study Runner

Runs the critical ablations defined in the paper:
1. First-order vs second-order settling
2. Fixed vs learned precision
3. Cold-start vs warm-start (state continuation)
4. Uniform vs adaptive settling
5. Forward-only vs bidirectional PC
6. Tied vs learned feedback
"""

import json
import os
import sys
import time
from dataclasses import dataclass, field, asdict
from pathlib import Path
from typing import Optional

import torch

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
from model import PCSHODLM, PCSHOConfig, LocalParameterUpdater, count_parameters
from train import CharLevelDataset, Trainer


@dataclass
class AblationConfig:
    """Configuration for an ablation experiment."""
    name: str
    description: str

    # Architecture overrides
    use_second_order: bool = True
    use_learned_precision: bool = True
    use_state_continuation: bool = True
    use_adaptive_settling: bool = True
    use_feedback: bool = True
    tied_feedback: bool = False

    # Training
    training_mode: str = "local"
    max_steps: int = 2000
    batch_size: int = 16
    lr: float = 1e-4
    seq_len: int = 128
    n_settling: int = 8

    # Model
    d_model: int = 256
    n_layers: int = 4
    n_heads: int = 4
    n_diffusion: int = 100


# =============================================================================
# Define ablation experiments
# =============================================================================

ABLATIONS = [
    # Full model (baseline)
    AblationConfig(
        name="full_model",
        description="Full PC-SHO-DLM with all components",
    ),

    # Ablation 1: First-order settling (no momentum)
    AblationConfig(
        name="first_order",
        description="First-order settling (gamma=1, no velocity)",
        use_second_order=False,
    ),

    # Ablation 2: Fixed precision (no learned precision heads)
    AblationConfig(
        name="fixed_precision",
        description="Fixed uniform precision (no learned heads)",
        use_learned_precision=False,
    ),

    # Ablation 3: No state continuation (cold restart each step)
    AblationConfig(
        name="no_continuation",
        description="Cold restart at each diffusion step",
        use_state_continuation=False,
    ),

    # Ablation 4: No adaptive settling (all tokens always active)
    AblationConfig(
        name="no_adaptive",
        description="Uniform settling (no token freezing)",
        use_adaptive_settling=False,
    ),

    # Ablation 5: No feedback (forward-only, no top-down)
    AblationConfig(
        name="no_feedback",
        description="Forward-only stack (no top-down predictions)",
        use_feedback=False,
    ),

    # Ablation 6: Tied feedback (transpose of forward weights)
    AblationConfig(
        name="tied_feedback",
        description="Tied feedback (weight transpose)",
        tied_feedback=True,
    ),

    # Ablation 7: Standard backprop training (for comparison)
    AblationConfig(
        name="backprop_baseline",
        description="Standard backprop training (not local)",
        training_mode="backprop",
    ),

    # Ablation 8: Varying settling steps
    AblationConfig(
        name="settling_k2",
        description="Only 2 settling steps",
        n_settling=2,
    ),
    AblationConfig(
        name="settling_k16",
        description="16 settling steps",
        n_settling=16,
    ),
]


def create_model_from_ablation(ablation: AblationConfig) -> PCSHODLM:
    """Create a model configured for a specific ablation."""
    config = PCSHOConfig(
        vocab_size=257,
        max_seq_len=ablation.seq_len,
        d_model=ablation.d_model,
        n_heads=ablation.n_heads,
        n_layers=ablation.n_layers,
        d_ff=ablation.d_model * 4,
        n_diffusion_steps=ablation.n_diffusion,
        n_settling_steps=ablation.n_settling,
        mask_token_id=0,
    )

    # Apply ablation modifications
    if not ablation.use_second_order:
        # Force first-order by setting gamma_min = gamma_max = 1
        config.gamma_min = 1.0
        config.gamma_max = 1.0

    if not ablation.use_state_continuation:
        config.velocity_decay = 0.0  # No velocity carry-over

    if not ablation.use_adaptive_settling:
        config.settling_threshold = float("inf")  # Never freeze tokens

    if not ablation.use_feedback:
        config.feedback_rank = 0  # Will need model modification

    model = PCSHODLM(config)

    if not ablation.use_learned_precision:
        # Replace precision heads with fixed uniform precision
        for ph in list(model.precision_up) + list(model.precision_down):
            for param in ph.parameters():
                param.requires_grad = False

    return model


def run_ablation(
    ablation: AblationConfig,
    data_text: str,
    output_dir: Path,
    device: str = "cpu",
):
    """Run a single ablation experiment."""
    print(f"\n{'='*60}")
    print(f"Ablation: {ablation.name}")
    print(f"Description: {ablation.description}")
    print(f"{'='*60}")

    # Create model
    model = create_model_from_ablation(ablation)
    print(f"Parameters: {count_parameters(model):,}")

    # Create dataset
    dataset = CharLevelDataset(data_text, ablation.seq_len, vocab_size=257)
    n_val = max(1, len(dataset) // 10)
    n_train = len(dataset) - n_val
    train_dataset, val_dataset = torch.utils.data.random_split(
        dataset, [n_train, n_val]
    )

    # Train
    save_dir = output_dir / ablation.name
    trainer = Trainer(
        model=model,
        train_dataset=train_dataset,
        val_dataset=val_dataset,
        training_mode=ablation.training_mode,
        lr=ablation.lr,
        batch_size=ablation.batch_size,
        max_steps=ablation.max_steps,
        log_interval=100,
        eval_interval=500,
        save_dir=str(save_dir),
        device=device,
    )

    start = time.time()
    trainer.train()
    elapsed = time.time() - start

    # Save ablation config and results
    results = {
        "config": asdict(ablation),
        "n_params": count_parameters(model),
        "wall_time": elapsed,
        "training_log": trainer.log,
    }
    with open(save_dir / "ablation_results.json", "w") as f:
        json.dump(results, f, indent=2)

    print(f"Completed in {elapsed:.1f}s")
    return results


def run_all_ablations(
    data_path: Optional[str] = None,
    output_dir: str = "ablation_results",
    device: str = "cpu",
    ablation_names: Optional[list] = None,
):
    """Run all ablation experiments."""
    output_path = Path(output_dir)
    output_path.mkdir(parents=True, exist_ok=True)

    # Load data
    if data_path and os.path.exists(data_path):
        with open(data_path, "r") as f:
            text = f.read()
    else:
        print("Using synthetic data for ablation experiments.")
        text = "The quick brown fox jumps over the lazy dog. " * 5000

    # Filter ablations if specified
    if ablation_names:
        ablations = [a for a in ABLATIONS if a.name in ablation_names]
    else:
        ablations = ABLATIONS

    all_results = {}
    for ablation in ablations:
        results = run_ablation(ablation, text, output_path, device)
        all_results[ablation.name] = results

    # Summary
    print(f"\n{'='*60}")
    print("ABLATION SUMMARY")
    print(f"{'='*60}")
    for name, results in all_results.items():
        log = results["training_log"]
        final_loss = log["loss"][-1] if log["loss"] else "N/A"
        print(f"{name:25s} | Loss: {final_loss:>8} | Time: {results['wall_time']:.1f}s")

    # Save summary
    with open(output_path / "summary.json", "w") as f:
        json.dump(
            {name: {"final_loss": r["training_log"]["loss"][-1] if r["training_log"]["loss"] else None,
                     "wall_time": r["wall_time"]}
             for name, r in all_results.items()},
            f, indent=2,
        )

    return all_results


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--data", type=str, default=None)
    parser.add_argument("--output", type=str, default="ablation_results")
    parser.add_argument("--device", type=str, default="cpu")
    parser.add_argument("--ablations", nargs="+", default=None,
                        help="Specific ablation names to run")
    args = parser.parse_args()

    run_all_ablations(args.data, args.output, args.device, args.ablations)