analogvnn.utils.render_autograd_graph
#
Module Contents#
Classes#
Stores and manages Graphviz representation of PyTorch autograd graph. |
Functions#
|
Convert a tensor size to a string. |
|
Compile Graphviz representation of PyTorch autograd graph from output tensors. |
|
Compile Graphviz representation of PyTorch autograd graph from forward pass. |
|
Produces graphs of torch.jit.trace outputs. |
|
Runs and make Graphviz representation of PyTorch autograd graph from output tensors. |
|
Runs and make Graphviz representation of PyTorch autograd graph from forward pass. |
Save Graphviz representation of PyTorch autograd graph from output tensors. |
|
Save Graphviz representation of PyTorch autograd graph from forward pass. |
|
Save Graphviz representation of PyTorch autograd graph from trace. |
- analogvnn.utils.render_autograd_graph.size_to_str(size)[source]#
Convert a tensor size to a string.
- Parameters:
size (torch.Size) – the size to convert.
- Returns:
the string representation of the size.
- Return type:
- class analogvnn.utils.render_autograd_graph.AutoGradDot[source]#
Stores and manages Graphviz representation of PyTorch autograd graph.
- Variables:
dot (graphviz.Digraph) – Graphviz representation of the autograd graph.
_module (nn.Module) – The module to be traced.
_inputs (List[Tensor]) – The inputs to the module.
_inputs_kwargs (Dict[str, Tensor]) – The keyword arguments to the module.
_outputs (Sequence[Tensor]) – The outputs of the module.
param_map (Dict[int, str]) – A map from parameter values to their names.
_seen (set) – A set of nodes that have already been added to the graph.
show_attrs (bool) – whether to display non-tensor attributes of backward nodes (Requires PyTorch version >= 1.9)
show_saved (bool) – whether to display saved tensor nodes that are not by custom autograd functions. Saved tensor nodes for custom functions, if present, are always displayed. (Requires PyTorch version >= 1.9)
max_attr_chars (int) – if show_attrs is True, sets max number of characters to display for any given attribute.
_called (bool) – the module has been called.
- property inputs: Sequence[torch.Tensor][source]#
The arg inputs to the module.
- Returns:
the arg inputs to the module.
- Return type:
Sequence[Tensor]
- property inputs_kwargs: Dict[str, torch.Tensor][source]#
The keyword inputs to the module.
- Parameters:
Dict[str – the keyword inputs to the module.
Tensor] – the keyword inputs to the module.
- property outputs: Optional[Sequence[torch.Tensor]][source]#
The outputs of the module.
- Returns:
the outputs of the module.
- Return type:
Optional[Sequence[Tensor]]
- property module: torch.nn.Module[source]#
The module.
- Returns:
the module to be traced.
- Return type:
nn.Module
- _module: torch.nn.Module[source]#
- _inputs: Sequence[torch.Tensor][source]#
- _inputs_kwargs: Dict[str, torch.Tensor][source]#
- _outputs: Sequence[torch.Tensor][source]#
- __post_init__()[source]#
Create the graphviz graph.
- Raises:
ImportError – if graphviz (https://pygraphviz.github.io/) is not available.
- add_ignore_tensor(tensor: torch.Tensor)[source]#
Add a tensor to the ignore tensor dict.
- Parameters:
tensor (Tensor) – the tensor to ignore.
- Returns:
self.
- Return type:
- del_ignore_tensor(tensor: torch.Tensor)[source]#
Delete a tensor from the ignore tensor dict.
- Parameters:
tensor (Tensor) – the tensor to delete.
- Returns:
self.
- Return type:
- get_tensor_name(tensor: torch.Tensor, name: Optional[str] = None) Tuple[str, str] [source]#
Get the name of the tensor.
- add_tensor(tensor: torch.Tensor, name: Optional[str] = None, _attributes=None, **kwargs)[source]#
Add a tensor to the graph.
- Parameters:
- Returns:
self.
- Return type:
- add_fn(fn: Any, _attributes=None, **kwargs)[source]#
Add a function to the graph.
- Parameters:
- Returns:
self.
- Return type:
- add_edge(u: Any, v: Any, label: Optional[str] = None, _attributes=None, **kwargs)[source]#
Add an edge to the graph.
- Parameters:
- Returns:
self.
- Return type:
- analogvnn.utils.render_autograd_graph.make_autograd_obj_from_outputs(outputs: Union[torch.Tensor, Sequence[torch.Tensor]], named_params: Union[Dict[str, Any], Iterator[Tuple[str, torch.nn.Parameter]]], additional_params: Optional[dict] = None, show_attrs: bool = True, show_saved: bool = True, max_attr_chars: int = 50) AutoGradDot [source]#
Compile Graphviz representation of PyTorch autograd graph from output tensors.
- Parameters:
outputs (Union[Tensor, Sequence[Tensor]]) – output tensor(s) of forward pass
named_params (Union[Dict[str, Any], Iterator[Tuple[str, Parameter]]]) – dict of params to label nodes with
additional_params (dict) – dict of additional params to label nodes with
show_attrs (bool) – whether to display non-tensor attributes of backward nodes (Requires PyTorch version >= 1.9)
show_saved (bool) – whether to display saved tensor nodes that are not by custom autograd functions. Saved tensor nodes for custom functions, if present, are always displayed. (Requires PyTorch version >= 1.9)
max_attr_chars (int) – if show_attrs is True, sets max number of characters to display for any given attribute.
- Returns:
graphviz representation of autograd graph
- Return type:
- analogvnn.utils.render_autograd_graph.make_autograd_obj_from_module(module: torch.nn.Module, *args: torch.Tensor, additional_params: Optional[dict] = None, show_attrs: bool = True, show_saved: bool = True, max_attr_chars: int = 50, from_forward: bool = False, **kwargs: torch.Tensor) AutoGradDot [source]#
Compile Graphviz representation of PyTorch autograd graph from forward pass.
- Parameters:
module (nn.Module) – PyTorch model
*args (Tensor) – input to the model
additional_params (dict) – dict of additional params to label nodes with
show_attrs (bool) – whether to display non-tensor attributes of backward nodes (Requires PyTorch version >= 1.9)
show_saved (bool) – whether to display saved tensor nodes that are not by custom autograd functions. Saved tensor nodes for custom functions, if present, are always displayed. (Requires PyTorch version >= 1.9)
max_attr_chars (int) – if show_attrs is True, sets max number of characters to display for any given attribute.
from_forward (bool) – if True then use autograd graph otherwise analogvvn graph
**kwargs (Tensor) – input to the model
- Returns:
graphviz representation of autograd graph
- Return type:
- analogvnn.utils.render_autograd_graph.get_autograd_dot_from_trace(trace) graphviz.Digraph [source]#
Produces graphs of torch.jit.trace outputs.
Example: >>> trace, = torch.jit.trace(model, args=(x,)) >>> dot = get_autograd_dot_from_trace(trace)
- Parameters:
trace (torch.jit.trace) – the trace object to visualize.
- Returns:
the resulting graph.
- Return type:
graphviz.Digraph
- analogvnn.utils.render_autograd_graph.get_autograd_dot_from_outputs(outputs: Union[torch.Tensor, Sequence[torch.Tensor]], named_params: Union[Dict[str, Any], Iterator[Tuple[str, torch.nn.Parameter]]], additional_params: Optional[dict] = None, show_attrs: bool = True, show_saved: bool = True, max_attr_chars: int = 50) graphviz.Digraph [source]#
Runs and make Graphviz representation of PyTorch autograd graph from output tensors.
- Parameters:
outputs (Union[Tensor, Sequence[Tensor]]) – output tensor(s) of forward pass
named_params (Union[Dict[str, Any], Iterator[Tuple[str, Parameter]]]) – dict of params to label nodes with
additional_params (dict) – dict of additional params to label nodes with
show_attrs (bool) – whether to display non-tensor attributes of backward nodes (Requires PyTorch version >= 1.9)
show_saved (bool) – whether to display saved tensor nodes that are not by custom autograd functions. Saved tensor nodes for custom functions, if present, are always displayed. (Requires PyTorch version >= 1.9)
max_attr_chars (int) – if show_attrs is True, sets max number of characters to display for any given attribute.
- Returns:
graphviz representation of autograd graph
- Return type:
Digraph
- analogvnn.utils.render_autograd_graph.get_autograd_dot_from_module(module: torch.nn.Module, *args: torch.Tensor, additional_params: Optional[dict] = None, show_attrs: bool = True, show_saved: bool = True, max_attr_chars: int = 50, from_forward: bool = False, **kwargs: torch.Tensor) graphviz.Digraph [source]#
Runs and make Graphviz representation of PyTorch autograd graph from forward pass.
- Parameters:
module (nn.Module) – PyTorch model
*args (Tensor) – input to the model
additional_params (dict) – dict of additional params to label nodes with
show_attrs (bool) – whether to display non-tensor attributes of backward nodes (Requires PyTorch version >= 1.9)
show_saved (bool) – whether to display saved tensor nodes that are not by custom autograd functions. Saved tensor nodes for custom functions, if present, are always displayed. (Requires PyTorch version >= 1.9)
max_attr_chars (int) – if show_attrs is True, sets max number of characters to display for any given attribute.
from_forward (bool) – if True then use autograd graph otherwise analogvvn graph
**kwargs (Tensor) – input to the model
- Returns:
graphviz representation of autograd graph
- Return type:
Digraph
- analogvnn.utils.render_autograd_graph.save_autograd_graph_from_outputs(filename: Union[str, pathlib.Path], outputs: Union[torch.Tensor, Sequence[torch.Tensor]], named_params: Union[Dict[str, Any], Iterator[Tuple[str, torch.nn.Parameter]]], additional_params: Optional[dict] = None, show_attrs: bool = True, show_saved: bool = True, max_attr_chars: int = 50) str [source]#
Save Graphviz representation of PyTorch autograd graph from output tensors.
- Parameters:
filename (Union[str, Path]) – filename to save the graph to
outputs (Union[Tensor, Sequence[Tensor]]) – output tensor(s) of forward pass
named_params (Union[Dict[str, Any], Iterator[Tuple[str, Parameter]]]) – dict of params to label nodes with
additional_params (dict) – dict of additional params to label nodes with
show_attrs (bool) – whether to display non-tensor attributes of backward nodes (Requires PyTorch version >= 1.9)
show_saved (bool) – whether to display saved tensor nodes that are not by custom autograd functions. Saved tensor nodes for custom functions, if present, are always displayed. (Requires PyTorch version >= 1.9)
max_attr_chars (int) – if show_attrs is True, sets max number of characters to display for any given attribute.
- Returns:
The (possibly relative) path of the rendered file.
- Return type:
- analogvnn.utils.render_autograd_graph.save_autograd_graph_from_module(filename: Union[str, pathlib.Path], module: torch.nn.Module, *args: torch.Tensor, additional_params: Optional[dict] = None, show_attrs: bool = True, show_saved: bool = True, max_attr_chars: int = 50, from_forward: bool = False, **kwargs: torch.Tensor) str [source]#
Save Graphviz representation of PyTorch autograd graph from forward pass.
- Parameters:
filename (Union[str, Path]) – filename to save the graph to
module (nn.Module) – PyTorch model
*args (Tensor) – input to the model
additional_params (dict) – dict of additional params to label nodes with
show_attrs (bool) – whether to display non-tensor attributes of backward nodes (Requires PyTorch version >= 1.9)
show_saved (bool) – whether to display saved tensor nodes that are not by custom autograd functions. Saved tensor nodes for custom functions, if present, are always displayed. (Requires PyTorch version >= 1.9)
max_attr_chars (int) – if show_attrs is True, sets max number of characters to display for any given attribute.
from_forward (bool) – if True then use autograd graph otherwise analogvvn graph
**kwargs (Tensor) – input to the model
- Returns:
The (possibly relative) path of the rendered file.
- Return type:
- analogvnn.utils.render_autograd_graph.save_autograd_graph_from_trace(filename: Union[str, pathlib.Path], trace) str [source]#
Save Graphviz representation of PyTorch autograd graph from trace.