FeatureStdMonitor#
- class FeatureStdMonitor(log_frequency=10, prefix='feature_std', monitor_student=True, monitor_teacher=True, track_channels=False)[source]#
Bases:
HookBaseMonitor feature standard deviation to detect representation collapse.
In
before_train(after unwrapping DDP) it locates the student/teacher module (Sonata, JEPA, etc.) and registers forward hooks on the selected backbones (and theirrepresentation_fusionif present). Each forward hook fires everylog_frequencycalls (training mode only) and computes, with cross-rank synchronization, the global feature std, per-sample batch std, and per-channel std summary (mean/min/max), logging them to the writer under{prefix}/<module>/...; withtrack_channels=Trueit also logs each channel’s std.after_trainremoves all hooks. Useful for catching feature collapse (std trending to zero). Registered asFeatureStdMonitor.- Parameters:
log_frequency (int) – Log statistics every this many forward passes. Defaults to
10.prefix (str) – Namespace prefix for the logged keys. Defaults to
"feature_std".monitor_student (bool) – Register hooks on the student branch. Defaults to
True.monitor_teacher (bool) – Register hooks on the teacher branch. Defaults to
True.track_channels (bool) – If
True, additionally log per-channel std. Defaults toFalse.
Note
Statistics are computed inside the forward pass to avoid retaining large feature tensors. Warns and does nothing if no student/teacher module is found.
Example
Add to
cfg.hooksto catch representation collapse during SSL; it watches the std of teacher/student backbone features:hooks = [dict(type="FeatureStdMonitor", log_frequency=20)] # → every 20 forward passes writes "feature_std/<student|teacher>/ # {global_std,batch_std,channel_mean_std,channel_min_std, # channel_max_std}" to the writer (std trending to 0 signals collapse)