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
- Variables
- func DefaultBreakHandler(info *Trace)
- func ReportAllHandler(info *Trace)
- func ReportAndPanicHandler(info *Trace)
- func ReportScopeHandler(info *Trace)
- type ExecWithLogger
- type HandlerFn
- type NanLogger
- func (l *NanLogger) AttachToExec(exec ExecWithLogger)
- func (l *NanLogger) AttachToTrainer(trainer *train.Trainer)
- func (l *NanLogger) PopScope()
- func (l *NanLogger) PushScope(scope string)
- func (l *NanLogger) Trace(node *graph.Node, scope ...string)
- func (l *NanLogger) TraceAndContinue(node *graph.Node, scope ...string)
- func (l *NanLogger) TraceFirstNaN(node *graph.Node, scope ...string)
- func (l *NanLogger) WithHandler(handler HandlerFn) *NanLogger
- func (l *NanLogger) WithStopAtFirst(stopAtFirst bool) *NanLogger
- type Trace
Constants ¶
const (
TracedMessageId = "#nanlogger(%d)"
)
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.