File size: 24,986 Bytes
e9c8366 | 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | """Teacher cache β universal caching of teacher/model reference outputs.
Used by QAT, clustered quantization, and any teacher-student workflow.
All scripts import from this module β no duplication of cache logic.
Cache format (directory-based, streaming to disk during capture):
{dataset_dir}/teacher_cache-{teacher_name}/{source_name}/
__meta__.pt β {"model_input": {...}, "model_output": {...}, "layer_names": [...]}
blocks.0.norm1.pt β {"input": tensor_or_tuple, "output": tensor_or_tuple}
blocks.0.attn.q_proj.pt
...
Streaming: during capture_with_hooks, each hook writes its layer's I/O to a
temporary directory IMMEDIATELY (torch.save + del tensor), so RAM/VRAM never
accumulates more than one layer's data at a time. After forward completes, the
temp directory is renamed to the final cache path.
Model-level I/O stored in __meta__.pt (small tensors β model input + output).
Per-layer I/O stored in individual {layer_name}.pt files.
Validation: cache is valid if:
1. Directory exists with __meta__.pt
2. cache_mtime >= source_mtime (not stale)
3. All required layer names have corresponding .pt files
Capture granularity is configurable via CaptureConfig:
- mode="final": only model input + model output (minimal disk)
- mode="all": every selected module (all leaves, or recursive by depth)
- mode="named": explicit module names
- mode="types": modules matching isinstance(types)
- depth: None=unlimited, 0=root, N=N levels deep
- leaves_only: True=only terminal modules (no children)
"""
import os
import shutil
import tempfile
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
import torch
import torch.nn as nn
# ---------------------------------------------------------------------------
# Path helpers
# ---------------------------------------------------------------------------
def get_cache_dir(dataset_dir: str | Path, teacher_name: str) -> Path:
"""Cache directory: {dataset_dir}/teacher_cache-{teacher_name}/"""
return Path(dataset_dir) / f"teacher_cache-{teacher_name}"
def get_cache_path(source_path: str | Path, dataset_dir: str | Path, teacher_name: str) -> Path:
"""Cache directory path for a given source (screenshot/dataset file).
Returns a DIRECTORY path (not a single .pt file). Inside:
__meta__.pt + {layer_name}.pt per layer.
"""
base = Path(source_path).stem
return get_cache_dir(dataset_dir, teacher_name) / base
def _safe_layer_filename(name: str) -> str:
"""Convert module qualified name to a safe filename.
Module names use dots (blocks.0.attn.q_proj) which are fine as filenames.
"""
return name.replace("/", "__")
def _meta_path(cache_dir: Path) -> Path:
"""Path to __meta__.pt inside a cache directory."""
return cache_dir / "__meta__.pt"
def _layer_path(cache_dir: Path, layer_name: str) -> Path:
"""Path to a single layer's .pt file inside a cache directory."""
return cache_dir / f"{_safe_layer_filename(layer_name)}.pt"
# ---------------------------------------------------------------------------
# CaptureConfig β what to capture
# ---------------------------------------------------------------------------
@dataclass
class CaptureConfig:
"""Configuration for which modules to capture during teacher forward.
mode:
"final" β only model input + model output (minimal disk)
"all" β all modules selected by depth/leaves_only
"named" β explicit module names in `names`
"types" β modules matching isinstance(types)
names: list of qualified module names (for "named" mode)
types: list of nn.Module subclasses (for "types" mode)
depth: None=unlimited recursion, 0=root only, N=down to N levels
leaves_only: if True, only modules with no children are captured
"""
mode: str = "final"
names: list[str] = field(default_factory=list)
types: list[type] = field(default_factory=list)
depth: int | None = None
leaves_only: bool = False
# ---------------------------------------------------------------------------
# select_modules β build list of module names to capture
# ---------------------------------------------------------------------------
def select_modules(
model: nn.Module,
config: CaptureConfig,
) -> list[str]:
"""Build the list of qualified module names to capture based on config.
Returns list of names like ["blocks.0.norm1", "blocks.0.attn.q_proj", ...].
Model-level I/O (__model_input__/__model_output__) is always captured separately.
"""
if config.mode == "final":
return []
if config.mode == "named":
# Validate names exist in model
available = dict(model.named_modules())
result = []
for name in config.names:
if name in available:
result.append(name)
else:
# Name not found β skip silently (or could raise)
pass
return result
# "all" and "types" modes β walk module tree with depth/leaves filters
result = []
types_filter = config.types if config.mode == "types" else None
for name, module in model.named_modules():
# Skip the root module itself (empty name)
if name == "":
continue
# Depth filter: count dots in name = depth level
if config.depth is not None:
level = name.count(".")
if level > config.depth:
continue
# Leaves-only filter
if config.leaves_only:
has_children = len(list(module.children())) > 0
if has_children:
continue
# Types filter
if types_filter is not None:
if not isinstance(module, tuple(types_filter)):
continue
result.append(name)
return result
# ---------------------------------------------------------------------------
# capture_with_hooks β streaming to disk, single forward pass
# ---------------------------------------------------------------------------
def capture_with_hooks(
model: nn.Module,
inputs: dict[str, Any],
config: CaptureConfig,
output_extractor=None,
tmp_dir: str | Path | None = None,
) -> dict:
"""Run a single forward pass through model, capturing I/O of selected modules.
Streaming: each hook writes its layer's I/O to a temp directory IMMEDIATELY
(torch.save + del tensor). After forward, the returned dict contains only
__model_input__ and __model_output__ (small), plus a 'tmp_dir' key pointing
to the directory with per-layer .pt files.
Use save_capture_result() to move the temp dir to the final cache location,
or pass the returned dict to save_layer_cache() which handles this.
Args:
model: the teacher model (will be set to eval mode, no_grad)
inputs: dict of forward kwargs, e.g. {"pixel_values": pv, "grid_thw": gt}
config: CaptureConfig specifying which modules to capture
output_extractor: optional callable(model_output) -> dict of output parts
If None, stores the raw output under __model_output__["raw"]
tmp_dir: directory where per-layer .pt files are written during capture.
If None, a temp dir is created (caller must move it to final location).
Returns:
dict with keys:
"__model_input__": inputs (copy, CPU tensors)
"__model_output__": extracted output parts (CPU tensors)
"__layer_names__": list of captured layer names
"__tmp_dir__": path to temp directory containing per-layer .pt files
"""
module_names = select_modules(model, config)
if tmp_dir is None:
tmp_dir = Path(tempfile.mkdtemp(prefix="teacher_cache_"))
else:
tmp_dir = Path(tmp_dir)
tmp_dir.mkdir(parents=True, exist_ok=True)
captured_layer_names: list[str] = []
hooks = []
def _make_hook(name: str):
def _hook(module, inp, out):
# Detach + move to CPU WITHOUT .clone() β we save then immediately del.
# torch.save serializes the tensor; after save the tensor is freed.
inp_clean = tuple(t.detach().cpu() if isinstance(t, torch.Tensor) else t for t in inp)
if isinstance(out, torch.Tensor):
out_clean = out.detach().cpu()
elif isinstance(out, (tuple, list)):
out_clean = tuple(t.detach().cpu() if isinstance(t, torch.Tensor) else t for t in out)
else:
out_clean = out
# Write immediately to disk β no accumulation in RAM
layer_file = tmp_dir / f"{_safe_layer_filename(name)}.pt"
torch.save({"input": inp_clean, "output": out_clean}, str(layer_file))
# Free references β let GC reclaim memory
del inp_clean, out_clean
captured_layer_names.append(name)
return _hook
for name in module_names:
module = model.get_submodule(name)
h = module.register_forward_hook(_make_hook(name))
hooks.append(h)
try:
model.eval()
with torch.no_grad():
output = model(**inputs)
# Extract model output (small β kept in RAM)
if output_extractor is not None:
captured_output = output_extractor(output)
else:
if isinstance(output, torch.Tensor):
captured_output = {"raw": output.detach().cpu()}
else:
captured_output = {"raw": output}
# Model input β move to CPU (small tensors)
captured_input = {
k: v.detach().cpu() if isinstance(v, torch.Tensor) else v
for k, v in inputs.items()
}
result = {
"__model_input__": captured_input,
"__model_output__": captured_output,
"__layer_names__": captured_layer_names,
"__tmp_dir__": str(tmp_dir),
}
finally:
for h in hooks:
h.remove()
return result
# ---------------------------------------------------------------------------
# Save / load per-layer cache (directory-based)
# ---------------------------------------------------------------------------
def save_layer_cache(
source_path: str | Path,
dataset_dir: str | Path,
teacher_name: str,
data: dict,
) -> Path:
"""Save capture result to a cache DIRECTORY on disk.
If data contains '__tmp_dir__', per-layer .pt files are moved from the temp
directory into the final cache directory. __model_input__ / __model_output__
/ __layer_names__ are saved to __meta__.pt.
If data does NOT contain '__tmp_dir__' (legacy/old format with inline tensors),
each layer entry is saved as a separate .pt file, and model I/O to __meta__.pt.
Returns the cache directory path.
"""
cache_dir = get_cache_path(source_path, dataset_dir, teacher_name)
# Remove old cache if exists
if cache_dir.exists():
shutil.rmtree(cache_dir)
cache_dir.mkdir(parents=True, exist_ok=True)
# Save model-level I/O + layer name list to __meta__.pt
meta = {
"__model_input__": data.get("__model_input__", {}),
"__model_output__": data.get("__model_output__", {}),
"__layer_names__": data.get("__layer_names__", []),
}
torch.save(meta, str(_meta_path(cache_dir)))
# Handle per-layer files
if "__tmp_dir__" in data:
# Streaming capture β move .pt files from temp dir to cache dir
tmp_dir = Path(data["__tmp_dir__"])
if tmp_dir.exists():
for pt_file in tmp_dir.glob("*.pt"):
shutil.copy2(str(pt_file), str(cache_dir / pt_file.name))
# Clean up temp dir
shutil.rmtree(tmp_dir, ignore_errors=True)
else:
# Legacy format β layer data is inline in dict
for key in data:
if key.startswith("__"):
continue
layer_data = data[key]
torch.save(layer_data, str(_layer_path(cache_dir, key)))
return cache_dir
def load_layer_cache(
source_path: str | Path,
dataset_dir: str | Path,
teacher_name: str,
) -> dict:
"""Load full per-layer cache from disk into a single dict.
Reads __meta__.pt (model I/O) and all per-layer .pt files, merging them
into a flat dict:
{
"__model_input__": {...},
"__model_output__": {...},
"blocks.0.norm1": {"input": tensor, "output": tensor},
...
}
Note: this loads ALL layers into RAM. For large caches, prefer load_layer_io()
to read a single layer at a time.
"""
cache_dir = get_cache_path(source_path, dataset_dir, teacher_name)
# Load meta
meta = torch.load(str(_meta_path(cache_dir)), weights_only=False)
result = {
"__model_input__": meta.get("__model_input__", {}),
"__model_output__": meta.get("__model_output__", {}),
}
# Load all per-layer .pt files (excluding __meta__.pt)
layer_names = meta.get("__layer_names__", [])
if not layer_names:
# Fallback: discover layer files by listing directory
layer_names = [
f.stem for f in cache_dir.glob("*.pt")
if f.name != "__meta__.pt"
]
for name in layer_names:
layer_file = _layer_path(cache_dir, name)
if layer_file.exists():
result[name] = torch.load(str(layer_file), weights_only=False)
return result
def load_layer_io(
source_path: str | Path,
dataset_dir: str | Path,
teacher_name: str,
layer_name: str,
) -> tuple:
"""Load input and output of a single layer from cache.
Reads ONLY the requested layer's .pt file β no full cache load.
Returns: (input, output) where each may be a tensor, tuple, or other.
"""
cache_dir = get_cache_path(source_path, dataset_dir, teacher_name)
layer_file = _layer_path(cache_dir, layer_name)
if not layer_file.exists():
# Try listing available layers from meta
meta = torch.load(str(_meta_path(cache_dir)), weights_only=False)
available = meta.get("__layer_names__", [])
raise KeyError(f"Layer '{layer_name}' not in cache. Available: {available}")
entry = torch.load(str(layer_file), weights_only=False)
return entry["input"], entry["output"]
def list_cached_names(data: dict) -> list[str]:
"""List all layer names in a loaded cache dict (excluding __model_*__).
Works with both loaded dicts (from load_layer_cache) and meta dicts.
"""
keys = [k for k in data.keys() if not k.startswith("__")]
# If data is a meta dict (has __layer_names__), use that
if "__layer_names__" in data:
keys = data["__layer_names__"]
return keys
# ---------------------------------------------------------------------------
# Validation β mtime + contents check (directory-based)
# ---------------------------------------------------------------------------
def validate_cache_contents(
source_path: str | Path,
dataset_dir: str | Path,
teacher_name: str,
required_names: list[str],
) -> list[str]:
"""Check if cache directory has all required layer .pt files.
Returns list of MISSING names (empty = all present = valid).
Does NOT check mtime β use is_cache_valid for full validation.
"""
cache_dir = get_cache_path(source_path, dataset_dir, teacher_name)
if not _meta_path(cache_dir).exists():
return list(required_names)
# Check each required name has a .pt file
missing = []
for name in required_names:
if not _layer_path(cache_dir, name).exists():
missing.append(name)
return missing
def is_cache_valid(
source_path: str | Path,
dataset_dir: str | Path,
teacher_name: str,
required_names: list[str] | None = None,
) -> bool:
"""Full validation: directory exists with __meta__.pt, mtime fresh, contents present.
Args:
required_names: list of layer names that must have .pt files.
If None, only mtime+existence is checked (backward compat).
"""
cache_dir = get_cache_path(source_path, dataset_dir, teacher_name)
if not _meta_path(cache_dir).exists():
return False
# mtime of cache dir (or __meta__.pt) must be >= source mtime
cache_mtime = cache_dir.stat().st_mtime
if cache_mtime < Path(source_path).stat().st_mtime:
return False
if required_names is not None:
missing = validate_cache_contents(source_path, dataset_dir, teacher_name, required_names)
if missing:
return False
return True
def get_sources_needing_cache(
source_paths: list[str],
dataset_dir: str | Path,
teacher_name: str,
required_names: list[str] | None = None,
) -> list[str]:
"""Filter source paths that need (re)caching: missing, stale, or incomplete."""
return [
s for s in source_paths
if not is_cache_valid(s, dataset_dir, teacher_name, required_names)
]
# ---------------------------------------------------------------------------
# Cleanup β remove old-format cache files
# ---------------------------------------------------------------------------
def clean_old_cache(dataset_dir: str | Path, teacher_name: str) -> int:
"""Remove old-format cache files (single .pt files instead of directories).
Old format: {source_name}.pt (single file with all layers inline)
New format: {source_name}/ directory with per-layer .pt files
Also removes corrupted cache directories (missing __meta__.pt).
Returns count of removed items (files + directories).
"""
cache_dir = get_cache_dir(dataset_dir, teacher_name)
if not cache_dir.exists():
return 0
removed = 0
for item in cache_dir.iterdir():
if item.is_file() and item.suffix == ".pt":
# Old format β single .pt file (new format uses directories)
try:
data = torch.load(str(item), weights_only=False)
if isinstance(data, dict) and "__model_input__" not in data:
# Old format β remove
item.unlink()
removed += 1
except (RuntimeError, EOFError, Exception):
# Corrupted β remove
item.unlink()
removed += 1
elif item.is_dir():
# New format directory β check if it has __meta__.pt
if not _meta_path(item).exists():
# Corrupted/incomplete β remove
shutil.rmtree(item, ignore_errors=True)
removed += 1
return removed
# ---------------------------------------------------------------------------
# TeacherCache β OOP wrapper, eliminates dataset_dir/teacher_name duplication
# ---------------------------------------------------------------------------
class TeacherCache:
"""Bound cache for a specific teacher model + dataset directory.
Encapsulates dataset_dir and teacher_name so scripts create ONE instance
and call methods without passing these every time. All read/write/validation
goes through this class β no duplication in scripts.
Usage:
cache = TeacherCache(dataset_dir, "qwen3_vl_vision_fp32")
cache.clean_old()
need = cache.get_sources_needing_cache(screenshots, required_names)
cache.save(source_path, data)
data = cache.load(source_path)
inp, out = cache.load_layer_io(source_path, "blocks.0.norm")
"""
def __init__(self, dataset_dir: str | Path, teacher_name: str):
self.dataset_dir = Path(dataset_dir)
self.teacher_name = teacher_name
self.cache_dir = get_cache_dir(dataset_dir, teacher_name)
def get_path(self, source_path: str | Path) -> Path:
"""Cache directory path for a source file (directory, not .pt file)."""
return get_cache_path(source_path, self.dataset_dir, self.teacher_name)
def is_valid(
self,
source_path: str | Path,
required_names: list[str] | None = None,
) -> bool:
"""Full validation: exists, mtime fresh, contents present."""
return is_cache_valid(
source_path, self.dataset_dir, self.teacher_name, required_names
)
def validate_contents(
self,
source_path: str | Path,
required_names: list[str],
) -> list[str]:
"""Returns list of MISSING layer names (empty = all present)."""
return validate_cache_contents(
source_path, self.dataset_dir, self.teacher_name, required_names
)
def get_sources_needing_cache(
self,
source_paths: list[str],
required_names: list[str] | None = None,
) -> list[str]:
"""Filter sources that need (re)caching."""
return get_sources_needing_cache(
source_paths, self.dataset_dir, self.teacher_name, required_names
)
def save(self, source_path: str | Path, data: dict) -> Path:
"""Save capture result to cache directory. Returns cache directory path.
If data has '__tmp_dir__', per-layer .pt files are moved from temp dir.
Otherwise, layer entries in data are saved as individual .pt files.
"""
return save_layer_cache(
source_path, self.dataset_dir, self.teacher_name, data
)
def load(self, source_path: str | Path) -> dict:
"""Load full per-layer cache dict from disk (ALL layers into RAM).
For large caches, prefer load_layer_io() to read one layer at a time.
"""
return load_layer_cache(
source_path, self.dataset_dir, self.teacher_name
)
def load_layer_io(
self,
source_path: str | Path,
layer_name: str,
) -> tuple:
"""Load (input, output) of a single layer from cache (lazy, one file)."""
return load_layer_io(
source_path, self.dataset_dir, self.teacher_name, layer_name
)
def list_names(self, data: dict) -> list[str]:
"""List layer names in a loaded cache dict or meta dict (excluding __model_*__)."""
return list_cached_names(data)
def clean_old(self) -> int:
"""Remove old-format cache files/directories. Returns count removed."""
return clean_old_cache(self.dataset_dir, self.teacher_name)
def capture(
self,
model: nn.Module,
inputs: dict[str, Any],
config: CaptureConfig,
output_extractor=None,
tmp_dir: str | Path | None = None,
) -> dict:
"""Run forward pass with hooks, capture I/O to temp dir. Does NOT save.
Returns dict with __model_input__, __model_output__, __layer_names__,
__tmp_dir__. Use save() to persist to final cache location.
"""
return capture_with_hooks(model, inputs, config, output_extractor, tmp_dir=tmp_dir)
def capture_and_save(
self,
source_path: str | Path,
model: nn.Module,
inputs: dict[str, Any],
config: CaptureConfig,
output_extractor=None,
tmp_dir: str | Path | None = None,
) -> Path:
"""Capture I/O via hooks (streaming to temp dir) AND save to final cache.
Hooks write each layer to disk immediately (no RAM accumulation).
After forward, temp dir is moved to final cache location.
Returns cache directory path.
"""
data = capture_with_hooks(model, inputs, config, output_extractor, tmp_dir=tmp_dir)
return self.save(source_path, data)
# ---------------------------------------------------------------------------
# Backward-compat wrappers (old API, delegates to new directory-based cache)
# ---------------------------------------------------------------------------
def save_cache(
source_path: str | Path,
dataset_dir: str | Path,
teacher_name: str,
pixel_values,
grid_thw,
teacher_parts: list[torch.Tensor],
) -> Path:
"""Backward-compat: save model-level I/O only (final mode).
Constructs a minimal cache dict with __model_input__ and __model_output__,
then saves as directory-based cache (meta only, no per-layer files).
"""
data = {
"__model_input__": {"pixel_values": pixel_values, "grid_thw": grid_thw},
"__model_output__": {"teacher_parts": teacher_parts},
"__layer_names__": [],
}
return save_layer_cache(source_path, dataset_dir, teacher_name, data)
def load_cache(
source_path: str | Path,
dataset_dir: str | Path,
teacher_name: str,
) -> tuple:
"""Backward-compat: load model-level I/O (final mode).
Returns: (pixel_values, grid_thw, teacher_parts)
"""
data = load_layer_cache(source_path, dataset_dir, teacher_name)
mi = data["__model_input__"]
mo = data["__model_output__"]
return mi["pixel_values"], mi["grid_thw"], mo["teacher_parts"] |