tensorial.reaxkit.utils package#

Submodules#

tensorial.reaxkit.utils.instantiators module#

tensorial.reaxkit.utils.instantiators.instantiate_listeners(listeners_cfg)[source]#

Instantiates listeners from config.

Parameters:
  • listeners_cfg – A DictConfig object containing listener configurations.

  • listeners_cfg (DictConfig)

Return type:

list[TrainerListener]

Returns:

A list of instantiated listeners.

tensorial.reaxkit.utils.instantiators.instantiate_loggers(logger_cfg)[source]#

Instantiates loggers from config.

Parameters:
  • logger_cfg – A DictConfig object containing logger configurations.

  • logger_cfg (DictConfig)

Return type:

list[Logger]

Returns:

A list of instantiated loggers.

tensorial.reaxkit.utils.logging_utils module#

tensorial.reaxkit.utils.logging_utils.log_hyperparameters(object_dict)[source]#

Controls which config parts are saved by Lightning loggers.

Additionally, it saves:
  • Number of model parameters

Parameters:
  • object_dict – A dictionary containing the following objects: - “cfg”: A DictConfig object containing the main config. - “model”: The Lightning model. - “trainer”: The Lightning trainer.

  • object_dict (dict[str, Any])

Return type:

None

tensorial.reaxkit.utils.pylogger module#

class tensorial.reaxkit.utils.pylogger.RankFilter(keep_rank=0)[source]#

Bases: Filter

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

Parameters:

keep_rank (int | None)

UNKNOWN = 'N/A'#
filter(record)[source]#

Determine if the specified record is to be logged.

Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.

Return type:

bool

class tensorial.reaxkit.utils.pylogger.RankedLogger(name='tensorial.reaxkit.utils.pylogger', rank_zero_only=False, extra=None)[source]#

Bases: LoggerAdapter

A multi-GPU-friendly python command line logger.

Parameters:
  • name (str)

  • rank_zero_only (bool)

  • extra (Mapping[str, object] | None)

Initializes a multi-GPU-friendly python command line logger that logs on all processes with their rank prefixed in the log message.

Parameters:
  • name – The name of the logger. Default is __name__.

  • rank_zero_only – Whether to force all logs to only occur on the rank zero process. Default is False.

  • extra – (Optional) A dict-like object which provides contextual information. See logging.LoggerAdapter.

  • name (str)

  • rank_zero_only (bool)

  • extra (Mapping[str, object] | None)

isEnabledFor(level)[source]#

Is this logger enabled for level ‘level’?

log(level, msg, *args, rank=None, **kwargs)[source]#

Delegate a log call to the underlying logger, after prefixing its message with the rank of the process it’s being logged from. If ‘rank’ is provided, then the log will only occur on that rank/process.

Parameters:
  • level – The level to log at. Look at logging.__init__.py for more information.

  • msg – The message to log.

  • rank – The rank to log at.

  • *args – Additional args to pass to the underlying logging function.

  • **kwargs – Any additional keyword args to pass to the underlying logging function.

  • level (int)

  • msg (str)

  • rank (int | None)

Return type:

None

tensorial.reaxkit.utils.rich_utils module#

tensorial.reaxkit.utils.rich_utils.enforce_tags(cfg, save_to_file=False)[source]#

Prompts user to input tags from command line if no tags are provided in config.

Parameters:
  • cfg – A DictConfig composed by Hydra.

  • save_to_file – Whether to export tags to the hydra output folder. Default is False.

  • cfg (DictConfig)

  • save_to_file (bool)

Return type:

None

tensorial.reaxkit.utils.rich_utils.print_config_tree(cfg, print_order=('data', 'model', 'listeners', 'logger', 'trainer', 'paths', 'extras'), resolve=False, save_to_file=False)[source]#

Prints the contents of a DictConfig as a tree structure using the Rich library.

Parameters:
  • cfg – A DictConfig composed by Hydra.

  • print_order – Determines in what order config components are printed. Default is ("data", "model", "listeners", "logger", "trainer", "paths", "extras").

  • resolve – Whether to resolve reference fields of DictConfig. Default is False.

  • save_to_file – Whether to export config to the hydra output folder. Default is False.

  • cfg (DictConfig)

  • print_order (Sequence[str])

  • resolve (bool)

  • save_to_file (bool)

Return type:

None

tensorial.reaxkit.utils.rich_utils.print_tree(root, name)[source]#
Parameters:
  • root (Mapping)

  • name (str)

tensorial.reaxkit.utils.utils module#

tensorial.reaxkit.utils.utils.extras(cfg)[source]#

Applies optional utilities before the task is started.

Utilities:
  • Ignoring python warnings

  • Setting tags from command line

  • Rich config printing

Parameters:
  • cfg – A DictConfig object containing the config tree.

  • cfg (DictConfig)

Return type:

None

tensorial.reaxkit.utils.utils.get_metric_value(metric_dict, metric_name)[source]#

Safely retrieves value of the metric logged in reax.Module.

Parameters:
  • metric_dict – A dict containing metric values.

  • metric_name – If provided, the name of the metric to retrieve.

  • metric_dict (dict[str, Any])

  • metric_name (str | None)

Return type:

float | None

Returns:

If a metric name was provided, the value of the metric.

tensorial.reaxkit.utils.utils.task_wrapper(task_func)[source]#

Optional decorator that controls the failure behavior when executing the task function.

This wrapper can be used to:
  • make sure loggers are closed even if the task function raises an exception (prevents

    multirun failure)

  • save the exception to a .log file

  • mark the run as failed with a dedicated file in the logs/ folder (so we can find and

    rerun it later)

  • etc. (adjust depending on your needs)

Example: .. code-block:: python

@utils.task_wrapper def train(cfg: DictConfig) -> tuple[dict[str, Any], dict[str, Any]]:

# … return metric_dict, object_dict

Parameters:
  • task_func – The task function to be wrapped.

  • task_func (Callable)

Return type:

Callable

Returns:

The wrapped task function.

Module contents#

class tensorial.reaxkit.utils.RankFilter(keep_rank=0)[source]#

Bases: Filter

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

Parameters:

keep_rank (int | None)

UNKNOWN = 'N/A'#
filter(record)[source]#

Determine if the specified record is to be logged.

Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.

Return type:

bool

class tensorial.reaxkit.utils.RankedLogger(name='tensorial.reaxkit.utils.pylogger', rank_zero_only=False, extra=None)[source]#

Bases: LoggerAdapter

A multi-GPU-friendly python command line logger.

Parameters:
  • name (str)

  • rank_zero_only (bool)

  • extra (Mapping[str, object] | None)

Initializes a multi-GPU-friendly python command line logger that logs on all processes with their rank prefixed in the log message.

Parameters:
  • name – The name of the logger. Default is __name__.

  • rank_zero_only – Whether to force all logs to only occur on the rank zero process. Default is False.

  • extra – (Optional) A dict-like object which provides contextual information. See logging.LoggerAdapter.

  • name (str)

  • rank_zero_only (bool)

  • extra (Mapping[str, object] | None)

isEnabledFor(level)[source]#

Is this logger enabled for level ‘level’?

log(level, msg, *args, rank=None, **kwargs)[source]#

Delegate a log call to the underlying logger, after prefixing its message with the rank of the process it’s being logged from. If ‘rank’ is provided, then the log will only occur on that rank/process.

Parameters:
  • level – The level to log at. Look at logging.__init__.py for more information.

  • msg – The message to log.

  • rank – The rank to log at.

  • *args – Additional args to pass to the underlying logging function.

  • **kwargs – Any additional keyword args to pass to the underlying logging function.

  • level (int)

  • msg (str)

  • rank (int | None)

Return type:

None

tensorial.reaxkit.utils.extras(cfg)[source]#

Applies optional utilities before the task is started.

Utilities:
  • Ignoring python warnings

  • Setting tags from command line

  • Rich config printing

Parameters:
  • cfg – A DictConfig object containing the config tree.

  • cfg (DictConfig)

Return type:

None

tensorial.reaxkit.utils.get_metric_value(metric_dict, metric_name)[source]#

Safely retrieves value of the metric logged in reax.Module.

Parameters:
  • metric_dict – A dict containing metric values.

  • metric_name – If provided, the name of the metric to retrieve.

  • metric_dict (dict[str, Any])

  • metric_name (str | None)

Return type:

float | None

Returns:

If a metric name was provided, the value of the metric.

tensorial.reaxkit.utils.instantiate_listeners(listeners_cfg)[source]#

Instantiates listeners from config.

Parameters:
  • listeners_cfg – A DictConfig object containing listener configurations.

  • listeners_cfg (DictConfig)

Return type:

list[TrainerListener]

Returns:

A list of instantiated listeners.

tensorial.reaxkit.utils.instantiate_loggers(logger_cfg)[source]#

Instantiates loggers from config.

Parameters:
  • logger_cfg – A DictConfig object containing logger configurations.

  • logger_cfg (DictConfig)

Return type:

list[Logger]

Returns:

A list of instantiated loggers.

tensorial.reaxkit.utils.log_hyperparameters(object_dict)[source]#

Controls which config parts are saved by Lightning loggers.

Additionally, it saves:
  • Number of model parameters

Parameters:
  • object_dict – A dictionary containing the following objects: - “cfg”: A DictConfig object containing the main config. - “model”: The Lightning model. - “trainer”: The Lightning trainer.

  • object_dict (dict[str, Any])

Return type:

None

tensorial.reaxkit.utils.task_wrapper(task_func)[source]#

Optional decorator that controls the failure behavior when executing the task function.

This wrapper can be used to:
  • make sure loggers are closed even if the task function raises an exception (prevents

    multirun failure)

  • save the exception to a .log file

  • mark the run as failed with a dedicated file in the logs/ folder (so we can find and

    rerun it later)

  • etc. (adjust depending on your needs)

Example: .. code-block:: python

@utils.task_wrapper def train(cfg: DictConfig) -> tuple[dict[str, Any], dict[str, Any]]:

# … return metric_dict, object_dict

Parameters:
  • task_func – The task function to be wrapped.

  • task_func (Callable)

Return type:

Callable

Returns:

The wrapped task function.