On this put up, I discuss via the motivation, complexities and implementation particulars of constructing torchvista, an open-source bundle to interactively visualize the ahead go of any Pytorch mannequin from inside web-based notebooks.
To get a way of the workings of torchvista whereas studying this put up, you may try:
- Github page if you wish to set up it by way of
pip
and use it from web-based notebooks (Jupyter, Colab, Kaggle, VSCode, and so forth) - An interactive demo page with numerous well-known fashions visualized
- A Google Colab tutorial
- A video demo:
Motivation
Pytorch fashions can get very giant and complicated, and making sense of 1 from the code alone is usually a tiresome and even intractable train. Having a graph-like visualization of it’s simply what we have to make this simpler.
Whereas there exist instruments like Netron, pytorchviz, and torchview that make this simpler, my motivation for constructing torchvista was that I discovered that they had been missing in some or all of those necessities:
- Interplay assist: The visualized graph needs to be interactive and never a static picture. It needs to be a construction you may zoom, drag, broaden/collapse, and so forth. Fashions can get very giant, and if all you’re see is a big static picture of the graph, how will you actually discover it?
- Modular exploration: Massive Pytorch fashions are modular in thought and implementation. For instance, consider a module which has a
Sequential
module which accommodates a couple ofConsideration
blocks, which in flip every has Totally linked blocks which compriseLinear
layers with activation capabilities and so forth. The software ought to permit you to faucet into this modular construction, and never simply current a low-level tensor hyperlink graph.

- Pocket book assist: We are likely to prototype and construct our fashions in notebooks. If a software had been offered as a standalone software that required you to construct your mannequin and cargo it to visualise it, it’s simply too lengthy a suggestions loop. So the software has to ideally work from inside notebooks.

- Error debugging assist: Whereas constructing fashions from scratch, we frequently run into many errors till the mannequin is ready to run a full ahead go end-to-end. So the visualization software needs to be error tolerant and present you a partial visualization graph even when there are errors, so to debug the error.

torch.cat
failed on account of mismatched tensor shapes- Ahead go tracing: Pytorch natively exposes a backward go graph via its autograd system, which the bundle pytorchviz exposes as a graph, however that is completely different from the ahead go. Once we construct, examine and picture fashions, we expect extra concerning the ahead go, and this may be very helpful to visualise.
Constructing torchvista
Primary API
The aim was to have a easy API that works with nearly any Pytorch mannequin.
import torch
from transformers import XLNetModel
from torchvista import trace_model
mannequin = XLNetModel.from_pretrained("xlnet-base-cased")
example_input = torch.randint(0, 32000, (1, 10))
# Hint it!
trace_model(mannequin, example_input)
With one line of code calling trace_model(
it ought to simply produce an interactive visualization of the ahead go.
Steps concerned
Behind the scenes, torchvista, when referred to as, works in two phases:
- Tracing: That is the place torchvista extracts a graph information construction from the ahead go of the mannequin. Pytorch doesn’t inherently expose this graph construction (regardless that it does expose a graph for the backward go), so torchvista has to construct this information construction by itself.
- Visualization: As soon as the graph is extracted, torchvista has to provide the precise visualization as an interactive graph. torchvista’s tracer does this by loading a template HTML file (with JS embedded inside it), and injecting serialized graph information construction objects as strings into the template to be subsequently loaded by the browser engine.

Tracing
Tracing is basically carried out by (briefly) wrapping all of the vital and identified tensor operations, and normal Pytorch modules. The aim of wrapping is to switch the capabilities in order that when referred to as, they moreover do the bookkeeping crucial for tracing.
Construction of the graph
The graph we extract from the mannequin is a directed graph the place:
- The nodes are the varied Tensor operations and the varied inbuilt Pytorch modules that get referred to as throughout the ahead go
- Moreover, enter and output tensors, and fixed valued tensors are additionally nodes within the graph.
- An edge exists from one node to the opposite for every tensor despatched from the previous to the latter.
- The sting label is the dimension of the related tensor.

However, the construction of our graph might be extra sophisticated as a result of most Pytorch modules name tensor operations and generally different modules’ ahead
methodology. This implies we have now to keep up a graph construction that holds data to visually discover it at any degree of depth.

Subsequently, the construction that torchvista extracts contains two most important information constructions:
- Adjacency record of the bottom degree operations/modules that get referred to as.
input_0 -> [ linear ]
linear -> [ __add__ ]
__getitem__ -> [ __add__ ]
__add__ -> [ multi_head_attention_forward ]
multi_head_attention_forward -> [ dropout ]
dropout -> [ __add__ ]
- Hierarchy map that maps every node to its dad or mum module container (if current)
linear -> Linear
multi_head_attention_forward -> MultiheadAttention
MultiheadAttention -> TransformerEncoderLayer
TransformerEncoderLayer -> TransformerEncoder
With each of those, we’re capable of assemble any desired views of the ahead go within the visualization layer.
Wrapping operations and modules
The entire concept behind wrapping is to do some bookkeeping earlier than and after the precise operation, in order that when the operation is known as, our wrapped operate as an alternative will get referred to as, and the bookkeeping is carried out. The targets of bookkeeping are:
- Document connections between nodes primarily based on tensor references.
- Document tensor dimensions to point out as edge labels.
- Document module hierarchy for modules within the case the place modules are nested inside each other
Here’s a simplified code snippet of how wrapping works:
original_operations = {}
def wrap_operation(module, operation):
original_operations[get_hashable_key(module, operation)] = operation
def wrapped_operation(*args, **kwargs):
# Do the mandatory pre-call bookkeeping
do_pre_call_bookkeeping()
# Name the unique operation
consequence = operation(*args, **kwargs)
do_post_call_bookkeeping()
return consequence
setattr(module, func_name, wrapped_operation)
for module, operation in LONG_LIST_OF_PYTORCH_OPS:
wrap_operation(module, operation)
And when trace_model is about to finish, we should reset the whole lot again to its unique state:
for module, operation in LONG_LIST_OF_PYTORCH_OPS:
setattr(module, func_name, original_operations[get_hashable_key(module,
operation)])
That is carried out in the identical approach for the ahead()
strategies of inbuilt Pytorch modules like Linear
, Conv2d
and so forth.
Connections between nodes
As said beforehand, an edge exists between two nodes if a tensor was despatched from one to the opposite. This types the premise of making connections between nodes whereas constructing the graph.
Here’s a simplified code snippet of how this works:
adj_list = {}
def do_post_call_bookkeeping(module, operation, tensor_output):
# Set a "marker" on the output tensor in order that whoever consumes it
# is aware of which operation produced it
tensor_output._source_node = get_hashable_key(module, operation)
def do_pre_call_bookkeeping(module, operation, tensor_input):
source_node = tensor_input._source_node
# Add a hyperlink from the producer of the tensor to this node (the patron)
adj_list[source_node].append(get_hashable_key(module, operation))

Module hierarchy map
Once we wrap modules, issues need to be carried out somewhat in a different way to construct the module hierarchy map. The thought is to keep up a stack of modules at the moment being referred to as in order that the highest of the stack all the time represents within the rapid dad or mum within the hierarchy map.
Here’s a simplified code snippet of how this works:
hierarchy_map = {}
module_call_stack = []
def do_pre_call_bookkeeping_for_module(bundle, module, tensor_output):
# Add it to the stack
module_call_stack.append(get_hashable_key(bundle, module))
def do_post_call_bookkeeping_for_module(module, operation, tensor_input):
module_call_stack.pop()
# High of the stack now could be the dad or mum node
hierarchy_map[get_hashable_key(package, module)] = module_call_stack[-1]
Visualization
This half is solely dealt with in Javscript as a result of the visualization occurs in web-based notebooks. The important thing libraries which are used listed below are:
- graphviz: for producing the format for the graph (viz-js is the JS port)
- d3: for drawing the interactive graph on a canvas
- iPython: to render HTML contents inside a pocket book
Graph Structure
Getting the format for the graph proper is a particularly complicated downside. The principle aim is for the graph to have a top-to-bottom “stream” of edges, and most significantly, for there to not be an overlap between the varied nodes, edges, and edge labels.
That is made all of the extra complicated once we are working with a “hierarchical” graph the place there are “container” containers for modules inside which the underlying nodes and subcomponents are proven.

Fortunately, graphviz (viz-js) involves the rescue for us. graphviz makes use of a language referred to as “DOT language” via which we specify how we require the graph format to be constructed.
Here’s a pattern of the DOT syntax for the above graph:
# Edges and nodes
"input_0" [width=1.2, height=0.5];
"output_0" [width=1.2, height=0.5];
"input_0" -> "linear_1"[label="(1, 16)", fontsize="10", edge_data_id="5623840688" ];
"linear_1" -> "layer_norm_1"[label="(1, 32)", fontsize="10", edge_data_id="5801314448" ];
"linear_1" -> "layer_norm_2"[label="(1, 32)", fontsize="10", edge_data_id="5801314448" ];
...
# Module hierarchy specified utilizing clusters
subgraph cluster_FeatureEncoder_1 {
label="FeatureEncoder_1";
fashion=rounded;
subgraph cluster_MiddleBlock_1 {
label="MiddleBlock_1";
fashion=rounded;
subgraph cluster_InnerBlock_1 {
label="InnerBlock_1";
fashion=rounded;
subgraph cluster_LayerNorm_1 {
label="LayerNorm_1";
fashion=rounded;
"layer_norm_1";
}
subgraph cluster_TinyBranch_1 {
label="TinyBranch_1";
fashion=rounded;
subgraph cluster_MicroBranch_1 {
label="MicroBranch_1";
fashion=rounded;
subgraph cluster_Linear_2 {
label="Linear_2";
fashion=rounded;
"linear_2";
}
...
As soon as this DOT illustration is generated from our adjacency record and hierarchy map, graphviz produces a format with positions and sizes of all nodes and paths for edges.
Rendering
As soon as the format is generated, d3 is used to render the graph visually. Every little thing is drawn on a canvas (which is simple to make draggable and zoomable), and we set numerous occasion handlers to detect person clicks.
When the person makes these two sorts of broaden/collapse clicks on modules (utilizing the ‘+’ ‘-‘ buttons), torchvista information which node the motion was carried out on, and simply re-renders the graph as a result of the format must be reconstructed, after which routinely drags and zooms in to an applicable degree primarily based on the recorded pre-click place.
Rendering a graph utilizing d3 is a really detailed matter and in any other case to not distinctive to torchvista, and therefore I pass over the small print from this put up.
[Bonus] Dealing with errors in Pytorch fashions
When customers hint their Pytorch fashions (particularly whereas growing the fashions), generally the fashions throw errors. It might have been straightforward for torchvista to only surrender when this occurs and let the person repair the error first earlier than they might use torchvista. However torchvista as an alternative lends a hand at debugging these errors by doing best-effort tracing of the mannequin. The thought is straightforward – simply hint the utmost it might till the error occurs, after which render the graph with simply a lot (with visible indicators exhibiting the place the error occurred), after which simply increase the exception in order that the person may also see the stacktrace like they usually would.


Here’s a simplified code snippet of how this works:
def trace_model(...):
exception = None
strive:
# All of the tracing code
besides Exception as e:
exception = e
lastly:
# do all the mandatory cleanups (unwrapping all of the operations and modules)
if exception shouldn't be None:
increase exception
Wrapping up
This put up shed some gentle on the journey of constructing a Pytorch visualization bundle. We first talked concerning the very particular motivation for constructing such a software by evaluating with different comparable instruments. Then, we mentioned the design and implementation of torchvista in two elements. The primary half was concerning the strategy of tracing the ahead go of a Pytorch mannequin utilizing (short-term) wrapping of operations and modules to extract detailed details about the mannequin’s ahead go, together with not solely the connections between numerous operations, but in addition the module hierarchy. Then, within the second half, we went over the visualization layer, and the complexities of format technology, which had been solved utilizing the best selection of libraries.
torchvista is open supply, and all contributions, together with suggestions, points and pull requests, are welcome. I hope torchvista helps folks of all ranges of experience in constructing and visualizing their fashions (no matter mannequin dimension), showcasing their work, and as a software for educating others about machine studying fashions.
Future instructions
Potential future enhancements to torchvista embody:
- Including assist for “rolling”, the place if the identical substructure of a mannequin is repeated a number of instances, it’s proven simply as soon as with a depend of what number of instances it repeats
- Systematic exploration of state-of-the-art fashions to make sure all their tensor operations are adequately coated
- Help for exporting static pictures of fashions as png or pdf information
- Effectivity and velocity enhancements
References
- Open supply libraries used:
- Dot language from graphviz
- Different comparable visualization instruments:
- torchvista:
All pictures except in any other case said are by the writer.