determinism#


format_log_message(logger: Logger, level: int, msg: str, formatter: Formatter, stacklevel: int = 1) str[source]#

Formats a log message as it would have been created by logger.log(level, msg) with the given formatter.

Parameters:
  • logger – the logger

  • level – the log level

  • msg – the message

  • formatter – the formatter

  • stacklevel – the stack level of the function to report as the generator

Returns:

the formatted log message (not including trailing newline)

class TraceLogger[source]#

Bases: object

Supports the collection of behavioural trace logs, which can, in particular, be used for determinism tests.

is_enabled = False#

whether the trace logger is enabled.

NOTE: The preferred way to enable this is via the context manager.

verbose = False#

whether to print trace log messages to stdout.

MESSAGE_TAG = '[TRACE]'#

a tag which is added at the beginning of log messages generated by this logger

LOG_LEVEL = 10#
log_buffer: StringIO | None = None#
log_formatter: Formatter | None = None#
classmethod log(logger: Logger, message_generator: Callable[[], str]) None[source]#

Logs a message intended for tracing agent-env interaction, which is enabled via TraceAgentEnvLoggerContext.

Parameters:
  • logger – the logger to use for the actual logging

  • message_generator – function which generates the log message (which may be expensive); if logging is disabled, the function will not be called.

class TraceLog(log_lines: list[str])[source]#

Bases: object

log_lines: list[str]#
save_log(path: str) None[source]#
print_log() None[source]#
get_full_log() str[source]#
reduce_log_to_messages() TraceLog[source]#

Removes logger names and function names from the log entries, such that each log message contains only the main text message itself (starting with the content after the logger’s tag).

Returns:

the result with reduced log messages

filter_messages(required_messages: Sequence[str] = (), optional_messages: Sequence[str] = (), ignored_messages: Sequence[str] = ()) TraceLog[source]#

Applies inclusion and or exclusion filtering to the log messages. If either required_messages or optional_messages is empty, inclusion filtering is applied. If ignored_messages is empty, exclusion filtering is applied. If both inclusion and exclusion filtering are applied, the exclusion filtering takes precedence.

Parameters:
  • required_messages – required message substrings to filter for; each message is required to appear at least once (triggering exception otherwise)

  • optional_messages – additional messages fragments to filter for; these are not required

  • ignored_messages – message fragments that result in exclusion; takes precedence over required_messages and optional_messages

Returns:

the result with reduced log messages

class TraceLoggerContext(enable_log_buffer: bool = True, log_format: str = '%(name)s:%(funcName)s - %(message)s')[source]#

Bases: object

A context manager which enables the trace logger. Apart from enabling the logging, it can optionally create a memory log buffer, such that getting the trace log is not strictly dependent on the logging system.

Parameters:
  • enable_log_buffer – whether to enable the dedicated log buffer for trace logs, whose contents can, within the context of this manager, be accessed via method get_log.

  • log_format – the logger format string to use for the dedicated log buffer

get_log() TraceLog[source]#
Returns:

the full trace log that was captured if enable_log_buffer was enabled at construction

torch_param_hash(module: Module) str[source]#

Computes a hash of the parameters of the given module; parameters not requiring gradients are ignored.

Parameters:

module – a torch module

Returns:

a hex digest of the parameters of the module

class TraceDeterminismTest(base_path: Path, core_messages: Sequence[str] = (), ignored_messages: Sequence[str] = (), log_filename: str | None = None)[source]#

Bases: object

Parameters:
  • base_path – the directory where the reference results are stored (will be created if necessary)

  • core_messages – message fragments that make up the core of a trace; if empty, all messages are considered core

  • ignored_messages – message fragments to ignore in the trace log (if any); takes precedence over core_messages

  • log_filename – the name of the log file to which results are to be written (if any)

class Result(*, git_status: sensai.util.git.GitStatus, log: TraceLog)[source]#

Bases: object

git_status: GitStatus#
log: TraceLog#
check(current_log: TraceLog, name: str, create_reference_result: bool = False, pass_if_core_messages_unchanged: bool = False) None[source]#

Checks the given log against the reference result for the given name.

Parameters:
  • current_log – the result to check

  • name – the name of the reference result; must be unique among all tests!

  • create_reference_result – whether update the reference result with the given result