PrototypeUsageLogger#
- class PrototypeUsageLogger(log_frequency=10, prefix='prototypes', log_pooling_levels=True)[source]#
Bases:
HookBaseMonitor prototype utilization in Sonata-style clustering heads.
In
before_train(after unwrapping DDP) it finds the Sonata submodule and registers a forward hook on each teacher/student head whose name contains"head". Each forward hook fires everylog_frequencycalls (training mode only) and, from the head’s output logits, computes prototype-usage statistics via argmax assignment counts (synchronized across ranks): number of used prototypes, percent unused, average tokens per active prototype, and the assignment-distribution entropy. These are written to the writer under{prefix}/<head>/....after_trainremoves all registered hooks. Registered asPrototypeUsageLogger.When
log_pooling_levelsis set it ALSO hooks each teacher/studentbackboneand, by walking thepooling_parentchain of the returnedPoint(read-only, so the model’s later up-cast is untouched), logs the cross-rank average number of points per event at each hierarchy stage under{prefix}/<backbone>/points_per_event/level{k}(level0= finest input resolution, increasing with pooling depth). This surfaces redundant pooling stages – e.g. a deepest level that collapses to ~1 point per event after many pooling operations.- Parameters:
log_frequency (int) – Log statistics every this many head/backbone forward passes. Defaults to
10.prefix (str) – Namespace prefix for the logged keys. Defaults to
"prototypes".log_pooling_levels (bool) – Also log average points-per-event at each backbone pooling stage. Defaults to
True.
Note
Specific to models exposing a Sonata-like
teacher/studentModuleDictof heads; warns and does nothing if none is found. Counts are all-reduced so the reported usage is global across GPUs.Example
Add to
cfg.hooksfor Sonata-style SSL; it watches each clustering head’s prototype assignments:hooks = [dict(type="PrototypeUsageLogger", log_frequency=20)] # → every 20 head forward passes writes, per head, the cross-rank # "prototypes/<teacher|student>/<head>/{used_count,unused_percent, # tokens_per_prototype,assignment_entropy}" to the writer, plus # "prototypes/<teacher|student>/backbone/points_per_event/level{k}" # for each pooling stage