sage.core.logger
Filename : logger.py Description : Short description of the file
Created on 2025-11-07 18:53:42
__author__ = Narenraju Nagarajan __copyright__ = Copyright 2025, ProjectName __license__ = MIT Licence __version__ = 0.0.1 __maintainer__ = Narenraju Nagarajan __affiliation__ = N/A __email__ = N/A __status__ = [‘inProgress’, ‘Archived’, ‘inUsage’, ‘Debugging’]
GitHub Repository: NULL
Documentation: NULL
Classes
Fixed-capacity ring buffer that stores named tensor fields. |
|
Non-blocking logger that offloads disk I/O to a background thread. |
|
Accumulate tensors in memory and flush to disk in fixed-size chunks. |
|
Persistent, epoch-indexed loss logger backed by an HDF5 file. |
Functions
|
Configure global and per-module logging. |
|
Get a logger for a specific module. |
Module Contents
- get_logger(module_name, log_dir='logs')[source]
Get a logger for a specific module. Each module has its own log file + logs also go to the main file.
- Parameters:
- Returns:
Configured logger instance
- Return type:
- class TensorRingBuffer(capacity, schema, device='cpu')[source]
Fixed-capacity ring buffer that stores named tensor fields.
Pre-allocates a contiguous tensor for each named field and overwrites the oldest entries once the buffer is full. All data is kept on
device(use"cpu"to avoid GPU memory pressure).- Parameters:
Example
buffer = TensorRingBuffer( capacity=10000, schema={"loss": (1,), "params": (P,), "output": (C,), "target": (C,)}, device="cpu", ) # inside training loop buffer.push( loss=loss.detach().cpu(), params=signal_targets.detach().cpu(), output=out.detach().cpu(), target=targets.detach().cpu(), )
- push(**kwargs)[source]
Write one entry to the buffer, advancing the write pointer.
- Parameters:
**kwargs (torch.Tensor) – One keyword per field defined in
schema. Each tensor is detached before copying so no gradient is accidentally stored.
- class AsyncLogger(maxsize=1000, filepath='log.pt')[source]
Non-blocking logger that offloads disk I/O to a background thread.
Incoming data dicts are placed on an in-memory queue; a daemon thread drains the queue in batches of 100 and serialises them to
filepathwithtorch.save. Excess entries are silently dropped when the queue is full, so the training loop is never blocked.- Parameters:
Example
logger = AsyncLogger() logger.log({ "loss": loss.detach().cpu(), "params": signal_targets.detach().cpu(), "output": out.detach().cpu(), "target": targets.detach().cpu(), }) logger.close() # flush remaining entries and join the thread
- class ChunkedTensorLogger(chunk_size, path)[source]
Accumulate tensors in memory and flush to disk in fixed-size chunks.
Each flush writes a Python list of tensors to
{path}_{idx}.pt(viatorch.save()) and increments the chunk index.- Parameters:
- class HDF5LossLogger(path, num_epochs, num_components, dtype='float32')[source]
Persistent, epoch-indexed loss logger backed by an HDF5 file.
Pre-allocates datasets of shape
(num_epochs, num_components)for both the"training"and"validation"splits at construction time, then writes one row per epoch vialog().The resulting file can be read directly with
h5py:with h5py.File("losses.h5", "r") as f: train_loss = f["training"]["loss"][:] # (E, C) float32 val_loss = f["validation"]["loss"][:]
- Parameters:
path (str) – File path for the HDF5 output (created fresh at init; any existing file at that path is overwritten).
num_epochs (int) – Total number of training epochs (pre-allocates the dataset).
num_components (int) – Number of scalar loss components logged per epoch (e.g. 4 for
BCEWithPEsigmaLoss: total, BCE, reg, coupling).dtype (str) – NumPy dtype string for the stored values (default
"float32").
- log(loss_tensor, epoch, split)[source]
Write one epoch’s loss vector to the HDF5 file.
- Parameters:
loss_tensor (torch.Tensor) – Shape
(num_epochs, num_components). Only rowepochis written; the rest are ignored. This matches theloss_componentstensor stored on training/validation objects.epoch (int) – Zero-based epoch index selecting the row to write.
split (str) – Either
"training"or"validation".