API Reference¶
Convenience Functions¶
-
torchrecorder.render_network(net, name, input_shapes, directory, filename=None, fmt='svg', input_data=None, render_depth=1, **styler_args)[source]¶ Render the structure of a
torch.nn.Moduleto an image viagraphviz.- Parameters
net (
torch.nn.Module) –name (str) – name of the network
input_shapes (None, tuple or list(tuple)) –
tupleifnethas a single input,list(tuple),Noneifinput_datais provideddirectory (str) – directory to store the rendered image
fmt (str, optional) – image format
input_data (
torch.Tensorortuple(torch.Tensor), optional) – ifnetrequires normalized inputs, provide them here instead of settinginput_shapes.render_depth (int, optional) – Default
1.**styler_args – node attributes to pass to
graphviz
-
torchrecorder.record(net, name, input_shapes, input_data=None)[source]¶ Record the graph by running a single pass of a
torch.nn.Module.- Parameters
net (
torch.nn.Module) –name (str) – name of the network
input_shapes (None, tuple or list(tuple)) –
tupleifnethas a single input,list(tuple),Noneifinput_datais providedinput_data (
torch.Tensorortuple(torch.Tensor), optional) – ifnetrequires normalized inputs, provide them here instead of settinginput_shapes.
- Returns
a
Recorderobject containing the execution graph
-
torchrecorder.make_dot(rec, render_depth=256, styler_cls=None, **styler_args)[source]¶ Produces Graphviz representation from a
Recorderobject- Parameters
rec (
Recorder) –render_depth (int) – depth until which nodes should be rendered
styler_cls – styler class to instantiate when styling nodes. If
None, defaults toGraphvizStyler.
- Kwargs:
styler_args (optional): styler properties to be set for all nodes
- Returns
a
graphviz.Digraphwith the rendered nodes
Custom graphviz styling¶
-
class
torchrecorder.renderer.GraphvizStyler(**styler_args)[source]¶ Bases:
objectProvide styling options before rendering to graphviz.
The style_node and style_edge methods read the properties
BaseNode objects, so any subclass of GraphvizStyler would need the same.
-
class
torchrecorder.nodes.TensorNode(name='', fn=None, depth=-1, parent=None)[source]¶ Bases:
torchrecorder.nodes.BaseNodeNode to encapsulate a
torch.Tensor.-
fn¶ - Type
-
-
class
torchrecorder.nodes.OpNode(name='', fn=None, depth=-1, parent=None)[source]¶ Bases:
torchrecorder.nodes.BaseNodeNode to encapsulate an Op, a
grad_fnattribute of atorch.Tensor.-
fn¶ - Type
-
-
class
torchrecorder.nodes.LayerNode(name='', fn=None, depth=-1, parent=None)[source]¶ Bases:
torchrecorder.nodes.BaseNodeNode to encapsulate a
torch.nn.Module.-
fn¶ - Type
-
-
class
torchrecorder.nodes.ParamNode(name='', fn=None, depth=-1, parent=None)[source]¶ Bases:
torchrecorder.nodes.TensorNodeNode to encapsulate a
torch.nn.Parameter.-
fn¶ - Type
torch.nn.Parameter
-
parent¶ a
Modulewhoseparameterscontainsfn- Type
-
Custom Rendering¶
If you are creating a new format to render information from a Recorder,
you would need to subclass the following methods in BaseRenderer,
as done in GraphvizRenderer:
-
class
torchrecorder.renderer.GraphvizRenderer(rec, render_depth=256, styler_cls=None, **styler_args)[source]¶ Bases:
torchrecorder.renderer.base.BaseRendererRender information from a
Recorderinto agraphviz.Digraph.-
styler¶ GraphvizStyleror a subclass- Type
class
-
render_node(g, node)[source]¶ Render a node in
graphvizRenders
nodeinto theDigraphg, after applying appropriate styling. Ifnodeis aLayerNode, checksrender_depthto see if itssubnetshave to rendered.- Parameters
g (
graphviz.Digraph) –node (
BaseNode) –
-
render_recursive_node(g, node)[source]¶ Render a
LayerNodeand its subnets.- Parameters
g (
graphviz.Digraph) –node (
LayerNode) – has adepthgreater thanrender_depth
The
nodeis rendered as a separateDigraphand then is added as agraphviz.Digraph.subgraphtog.
-
render_edge(g, fnode, tnode)[source]¶ Render an edge in
graphviz- Parameters
g (
graphviz.Digraph) –fnode (
BaseNode) –tnode (
BaseNode) –
-
Custom Recording¶
Subclassing Recorder should be unnecessary in most cases.
-
class
torchrecorder.recorder.Recorder[source]¶ Bases:
objectRecord and store execution graph information
-
add_node(net, depth=0, parent=None, name=None)[source]¶ Construct a node of recording graph.
Construct a
BaseNodethat will store information related tonetas the neural network is run.
-
add_dummy(dummy, fn)[source]¶ Point to an existing node to assist recording.
Instead of creating a separate node, the
dummyobject is used to point to an existing node containingfn. Used for dummy ops andAccumulateGradients (seeleaf_dummy).- Parameters
dummy – a dummy
torch.Tensoror op that should not be recordedfn – a recorded object that will be connected to further ops
-
add_edge(_from, _to)[source]¶ Construct an edge of the recording graph.
Records an edge between two
fnobjects to be used while rendering. This will be used along with thenodesdictionary to map edges properly.
-
register_hooks(net, depth=0, parent=None, name=None)[source]¶ Register the hooks of the
Recorderrecursively on atorch.nn.Module.The hooks registered are
partialversions ofprehookandposthookcorresponding to each node.- Parameters
net (
Module) –depth (int) –
parent (
torch.nn.Module) – the parent ofnetname (str) – name of
net
- Returns
-
-
torchrecorder.recorder.op_acc(gf, rec, node)[source]¶ Operator Accumulator.
Creates an
OpNodeto record the newly-performed operationgf, if not already recorded. Ifgfis an initialization op (AccumulateGradient), then pointsgfto its connectedtorch.Tensorinstead of creating anOpNode. Otherwise recursively checks all operations that are connected togfand adds them if necessary.- Parameters
gf – current operation, a
grad_fnobject obtained from atorch.Tensorrec – a
Recorderobject whose nodes are updatednode –
LayerNodewhosefnthe current operation is a part of
- Returns
-
torchrecorder.recorder.tensor_acc(tensor, rec, node)[source]¶ Tensor Accumulator.
Creates a
TensorNodeto record the newly-created tensor, if not already recorded. Note that the resultingTensorNodehas the same parent asnode, because thetensoris the output of/input tonode.fn.- Parameters
tensor – a
torch.Tensorrec – a
Recorderobject whose nodes are updatednode – a
LayerNodewhosefnoutputs/inputstensor
- Returns
-
torchrecorder.recorder.param_acc(param, rec, node)[source]¶ Parameter Accumulator.
Creates a
ParamNodeto record the parameterparamofnode.fn, if not already recorded. Note thatnode.fnis the parent ofparam.
-
torchrecorder.recorder.leaf_dummy(tensor, rec)[source]¶ Performs a dummy operation (adding 0) to a leaf
Tensor.This ensures that the (possibly in-place) operations performed on
tensorhereafter can be correctly mapped. The dummy tensor (and operation) are not recorded separately, they merely point to the original tensor.- Parameters
tensor – a newly-formed leaf
torch.Tensorrec – the
Recorderobject whose nodes are updated
- Returns
tensorafter adding 0
-
torchrecorder.recorder.prehook(module, inputs, rec, node)[source]¶ hook to record BEFORE the given
moduleis run.Records parameters contained in
module, then checks each tensor ininputsfor any operations that may have run after the end of the previousmodule. Theinputsare then converted to leaf tensors and recorded before being passed off to themodule.- Parameters
module – a
torch.nn.Moduleinputs – a
torch.Tensoror atupleoftorch.Tensorsrec – a
Recorderobject for global informationnode (
LayerNode) –node.fnismodule.
- Returns
the
leaf-equivalent ofinputs.
-
torchrecorder.recorder.posthook(module, inputs, outputs, rec, node)[source]¶ hook to record AFTER the given
modulehas run and returned.Records any operations that may have run as part of
module, then checks if each tensor in theoutputshas already been recorded by a submoduleof the currentmodule(the submodule’sposthookwould execute first!). If necessary, theoutputsare converted to leaf tensors to record operations afresh.- Parameters
module – a
torch.nn.Moduleinputs – a
torch.Tensoror a tuple oftorch.Tensorsoutputs – a
torch.Tensoror a tuple oftorch.Tensorsrec – a
Recorderobject for global informationnode (
LayerNode) –node.fnismodule.
- Returns
the
leaf-equivalent ofoutputs.