Files
Mini-Nav/mini-nav/compressors/training_metrics.py
SikongJueluo 4ea567adba feat(compressors): add JSONL training metrics logging with CLI controls
- Add write_training_metrics() in new compressors/training_metrics.py
  for appending epoch/step/lr/component rows as JSON Lines
- Wire --metrics-path and --log-every CLI options into train.py, passing
  them to the training loop so metrics rows are written every N steps
- Accept absolute metrics paths or paths relative to output directory
- Add quantization component to loss log alongside existing distill/contrastive
- Replace inline torch.device() with get_device() utility
- Add test_hash_training_metrics.py covering multi-row JSONL append

Infrastructure:
- Pin torch 2.7.1 + CUDA 12.8 index for Linux/Windows in pyproject.toml
- Add .justfile rsync upload recipe with .stignore exclusion
- Exclude **/__marimo__ from rsync in .stignore

Dependencies updated: numpy 2.4.5, pandas 3.0.3, black 26.5.0,
click 8.4.0, contourpy, etc.
2026-05-17 14:57:10 +08:00

38 lines
1.0 KiB
Python

"""Utilities for recording hash compressor training metrics."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Mapping
def write_training_metrics(
path: str | Path,
*,
epoch: int,
step: int,
lr: float,
components: Mapping[str, float],
) -> None:
"""Append one training metrics row as JSON Lines.
The JSONL format keeps training logging cheap and easy to resume: every
training step is an independent row that plotting scripts can stream later.
"""
metrics_path = Path(path)
metrics_path.parent.mkdir(parents=True, exist_ok=True)
row = {
"epoch": int(epoch),
"step": int(step),
"lr": float(lr),
"total": float(components["total"]),
"contrastive": float(components["contrastive"]),
"distill": float(components["distill"]),
"quantization": float(components["quantization"]),
}
with metrics_path.open("a", encoding="utf-8") as f:
f.write(json.dumps(row, ensure_ascii=False) + "\n")