nanlogger

package
v0.28.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 1 module, 6 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package nanlogger collects `graph.Node` objects to monitor for `NaN` ("not-a-number") or `Inf` (infinity) values.

It does that by implementing `graph.LoggerFn` and hooking to the `graph.Exec` that executes the graph -- it can also `AttachToTrainer`, so it automatically attaches to every graph the trainer creates.

If at the end of a graph.Exec call, if a `NaN` value is found on the traced computation nodes, the first node where it appears (often `NaN` values spread through the graph) is reported back.

The report includes a stack trace and an optional user set scoped model.

Example: create a `NanLogger` and attaches it to the trainer, to it gets attached to every graph created (if more than one is created by the trainer).

func train() {
	…
	nanLogger := nanlogger.New()
	scope.SetParam(optimizer.ParamNanLogger, nanLogger)  // Gradients with NaNs get reported.
	trainer := train.NewTrainer(…)
	nanLogger.AttachToTrainer(trainer)
	…
}

func ModelGraph(scope *model.Scope, spec any, inputs []*Node) []*Node {
	…
	for ii := range numBlocks {
		x = ResidualBlock(scope.In(name), x, lastNumChannels)
		nanLogger.TraceFirstNaN(x, fmt.Sprintf("Residual-%d", ii+1))
	}
	…
}

Index

Constants

View Source
const (
	TracedMessageId = "#nanlogger(%d)"
)

Variables

View Source
var DefaultHandler = ReportScopeHandler

DefaultHandler for a new NanLogger.

Functions

func DefaultBreakHandler

func DefaultBreakHandler(info *Trace)

DefaultBreakHandler is the default handler called when a `NaN` or `Inf` is observed in a break traced node.

It prints all out all the information about the `NaN` trace.

func ReportAllHandler

func ReportAllHandler(info *Trace)

ReportAllHandler is a handler that reports the stack trace and scope of a traced node, and then panic.

func ReportAndPanicHandler

func ReportAndPanicHandler(info *Trace)

ReportAndPanicHandler is a handler that reports the stack trace and scope of a traced node, and then panic.

func ReportScopeHandler

func ReportScopeHandler(info *Trace)

ReportScopeHandler reports the scope when a traced node with NaN/Inf values is found.

Types

type ExecWithLogger

type ExecWithLogger interface {
	SetNodeLogger(loggerFn graph.LoggerFn)
	GetNodeLogger() graph.LoggerFn
}

ExecWithLogger represents any of the executors in GoMLX (or future): `graph.Exec` and `model.Exec`. What is required is that it supports setting the logger and reading the current logger.

type HandlerFn

type HandlerFn func(info *Trace)

HandlerFn is the type of function to handle NaN traces.

type NanLogger

type NanLogger struct {
	// contains filtered or unexported fields
}

NanLogger uses the logger infrastructure to monitor for NaN (and Inf) values in your graph. You manually select the nodes you want to monitor, and it saves the stack where it was called along with user-provided scope information.

If during the execution any NaN appears, it calls your handler with the stack trace where the monitor was set, along with the scope.

Alternatively, instead of panicking, one can set a custom handler to be called if/when a NaN is observed.

See an example in the package documentation.

func New

func New() *NanLogger

New creates a NanLogger that can be used to debug where NaN happen in graphs.

You manually select the nodes you want to monitor, and it saves the stack where it was called along with user-provided scope information.

If during the execution any NaN appears, it calls your handler with the stack trace where the monitor was set, along with the scope.

Alternatively, instead of panicking, one can set a custom handler to be called if/when a NaN is observed.

See an example in the package documentation.

func (*NanLogger) AttachToExec

func (l *NanLogger) AttachToExec(exec ExecWithLogger)

AttachToExec will set the NanLogger as the default logger in exec. NanLogger acts as a pass-through logger, anything that is not marked as nanlogger.TracedMessageId is passed through to whatever the previous logger configured in exec was.

A nil NanLogger is valid, and it will simply be a no-op.

func (*NanLogger) AttachToTrainer

func (l *NanLogger) AttachToTrainer(trainer *train.Trainer)

AttachToTrainer makes sure that the logger is attached to every graph created by the trainer.

A nil NanLogger is valid, and it will simply be a no-op.

func (*NanLogger) PopScope

func (l *NanLogger) PopScope()

PopScope removes the last entry in the current scope stack. These values are added by default to any new Trace.

A nil NanLogger is valid, and it will simply be a no-op.

func (*NanLogger) PushScope

func (l *NanLogger) PushScope(scope string)

PushScope to the current scope stack. These values are added by default to any new Trace.

A nil NanLogger is valid, and it will simply be a no-op.

func (*NanLogger) Trace

func (l *NanLogger) Trace(node *graph.Node, scope ...string)

Trace sets a trace on the given node. and if it ever becomes NaN, the scope is printed -- but the execution continues normally.

Args:

Remember to attach the NanLogger once to the train.Trainer or to the graph.Exec/model.Exec you are running.

A nil NanLogger is valid, and it will simply be a no-op.

func (*NanLogger) TraceAndContinue

func (l *NanLogger) TraceAndContinue(node *graph.Node, scope ...string)

TraceAndContinue sets a trace on the given node, and if it ever becomes NaN, the scope is printed -- but the execution continues normally.

Remember to attach the NanLogger once to the train.Trainer or to the graph.Exec/model.Exec you are running.

A nil NanLogger is valid, and it will simply be a no-op.

func (*NanLogger) TraceFirstNaN

func (l *NanLogger) TraceFirstNaN(node *graph.Node, scope ...string)

TraceFirstNaN sets a trace on the given node, and if it ever becomes NaN, the trace of the first node the graph (closer to the input in graph ordering) is logged (if the DefaultHandler is used).

Remember to attach the NanLogger once to the train.Trainer or to the graph.Exec/model.Exec you are running.

A nil NanLogger is valid, and it will simply be a no-op.

func (*NanLogger) WithHandler

func (l *NanLogger) WithHandler(handler HandlerFn) *NanLogger

WithHandler sets the function called when a `NaN` is observed.

The default is DefaultHandler, that is initialized with ReportScopeHandler. There are also ReportAllHandler and ReportAndPanicHandler handler predefined that can be used.

If the handler is nil, set it to the default value.

func (*NanLogger) WithStopAtFirst

func (l *NanLogger) WithStopAtFirst(stopAtFirst bool) *NanLogger

WithStopAtFirst defines the behavior of Trace.

If stopAtFirst is true, traces will stop at the first NaN occurrence. Otherwise, they report the NaN, but keep on running.

The default is true.

A nil NanLogger is valid, and it will simply be a no-op.

type Trace

type Trace struct {
	// StackTrace of where the monitored node was created, stored as an error that can be printed.
	StackTrace errors.StackTrace

	// Scope saved when the monitor node was created.
	Scope []string

	// FirstOnly traces only the first occurrence of a NaN: that means if more than one traced node
	// is or has a NaN/Inf value, only the handler is called only for the first one.
	FirstOnly bool
}

Trace information of a node that is set to monitor. This is what is printed out when a `NaN` is found or passed to a handler function if one is set.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL