Files
OBLITERATUS/obliteratus/runner.py
Joseph Magly c21275ae5f fix(models): never let snapshot cleanup replace an in-flight error; close interrupt windows
Muse review r1 and the Muse BT6 audit for #659: _close_restore_point stores a
plain closed/unavailable policy naming the snapshot path right after
detaching, and reports closed only after close() returns; DiskSnapshot.close()
marks the snapshot close_failed before removing anything, so an interrupted
removal can never leave a snapshot that is accepted again. New
cleanup_on_exit/cleanup_during_error helpers attach a cleanup failure as a
note while an error is propagating and raise it otherwise; every production
ModelHandle cleanup site in a finally or failure path uses them. Removal clears
Windows read-only bits once and reports a count and the first failing paths;
the entry-point test registers cleanup before summoning and checks its own
paths.
2026-11-25 03:47:19 -05:00

176 lines
6.4 KiB
Python

"""Main ablation runner — orchestrates the full pipeline."""
from __future__ import annotations
from pathlib import Path
from datasets import load_dataset
from rich.console import Console
from obliteratus.config import StudyConfig
from obliteratus.evaluation.evaluator import Evaluator
from obliteratus.models.loader import cleanup_on_exit, load_model
from obliteratus.reporting.report import AblationReport, AblationResult
from obliteratus.strategies import get_strategy
console = Console()
def run_study(config: StudyConfig) -> AblationReport:
"""Execute a full ablation study from a StudyConfig.
Steps:
1. Load model from HuggingFace.
2. Load evaluation dataset.
3. Compute baseline metrics.
4. For each strategy, enumerate ablation specs and evaluate each.
5. Collect everything into an AblationReport.
"""
output_dir = Path(config.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
# --- 1. Load model ---
console.print(f"\n[bold cyan]Loading model:[/bold cyan] {config.model.name}")
handle = load_model(
model_name=config.model.name,
task=config.model.task,
device=config.model.device,
dtype=config.model.dtype,
trust_remote_code=config.model.trust_remote_code,
num_labels=config.model.num_labels,
quantization=config.model.quantization,
skip_snapshot=True,
)
# A cleanup failure is raised, but never replaces a study failure.
with cleanup_on_exit(handle):
handle.ensure_snapshot(
directory=config.snapshot.directory,
max_bytes=config.snapshot.max_bytes,
staging_bytes=config.snapshot.staging_bytes,
)
return _run_loaded_study(config, handle, output_dir)
def _run_loaded_study(config, handle, output_dir):
console.print(f" Architecture: {handle.architecture}")
console.print(f" Layers: {handle.num_layers} Heads: {handle.num_heads}")
console.print(f" Hidden: {handle.hidden_size} Params: {handle.summary()['total_params']:,}")
# --- 2. Load dataset ---
console.print(f"\n[bold cyan]Loading dataset:[/bold cyan] {config.dataset.name}")
ds_kwargs = {"path": config.dataset.name, "split": config.dataset.split}
if config.dataset.subset:
ds_kwargs["name"] = config.dataset.subset
dataset = load_dataset(**ds_kwargs)
console.print(f" Samples: {len(dataset)}")
# --- 3. Baseline evaluation ---
console.print("\n[bold green]Computing baseline metrics...[/bold green]")
evaluator = Evaluator(
handle=handle,
dataset=dataset,
metrics=config.metrics,
batch_size=config.batch_size,
max_length=config.max_length,
max_samples=config.dataset.max_samples,
text_column=config.dataset.text_column,
label_column=config.dataset.label_column,
)
baseline = evaluator.evaluate()
console.print(f" Baseline: {baseline}")
report = AblationReport(
model_name=config.model.name,
primary_metric=config.metrics[0] if config.metrics else None,
)
report.add_baseline(baseline)
# --- 4. Run ablation strategies ---
for strat_cfg in config.strategies:
console.print(f"\n[bold magenta]Strategy:[/bold magenta] {strat_cfg.name}")
strategy = get_strategy(strat_cfg.name)
specs = strategy.enumerate(handle, **strat_cfg.params)
console.print(f" Ablation specs: {len(specs)}")
if getattr(strategy, "batch_apply", False):
console.print(f" [dim]Applying {len(specs)} specs as one batch...[/dim]", end=" ")
try:
summary = strategy.apply_batch(handle, specs)
ablated_eval = Evaluator(
handle=handle,
dataset=dataset,
metrics=config.metrics,
batch_size=config.batch_size,
max_length=config.max_length,
max_samples=config.dataset.max_samples,
text_column=config.dataset.text_column,
label_column=config.dataset.label_column,
)
metrics = ablated_eval.evaluate()
console.print(f"{metrics}")
finally:
handle.restore()
report.add_result(
AblationResult(
strategy=strat_cfg.name,
component="batch",
description=f"Applied {len(specs)} {strat_cfg.name} specs together",
metrics=metrics,
metadata={"summary": summary, "components": [spec.component for spec in specs]},
)
)
continue
for spec in specs:
console.print(f" [dim]Ablating {spec.component}...[/dim]", end=" ")
try:
# Apply ablation
strategy.apply(handle, spec)
# Evaluate
ablated_eval = Evaluator(
handle=handle,
dataset=dataset,
metrics=config.metrics,
batch_size=config.batch_size,
max_length=config.max_length,
max_samples=config.dataset.max_samples,
text_column=config.dataset.text_column,
label_column=config.dataset.label_column,
)
metrics = ablated_eval.evaluate()
console.print(f"{metrics}")
finally:
handle.restore()
report.add_result(
AblationResult(
strategy=spec.strategy_name,
component=spec.component,
description=spec.description,
metrics=metrics,
metadata=spec.metadata,
)
)
# --- 5. Save outputs ---
report.save_json(output_dir / "results.json")
report.save_csv(output_dir / "results.csv")
# Try to generate plots (may fail in headless environments). Each plot is
# attempted independently and only files actually written are reported.
try:
outcome = report.save_plots(output_dir)
console.print()
for line in outcome.lines():
console.print(line, markup=False)
except Exception as e:
console.print(f"\n[yellow]Could not generate plots: {e}[/yellow]")
console.print(f"\n[bold green]Results saved to {output_dir}/[/bold green]")
report.print_summary()
return report