Skip to main content

Record execution graphs of PyTorch neural networks

Project description

torchrec

A small package to record execution graphs of neural networks in PyTorch. The package uses hooks and the grad_fn attribute to record information.
This can be used to generate visualizations at different scope depths.

Licensed under MIT License. View documentation at https://pytorchrec.readthedocs.io/

Installation

Requirements:

Install this package:

$ pip install torchrec

Usage

Consider the below example network:

import sys
import torch
import torchrec


class SampleNet(torch.nn.Module):
    def __init__(self):
        torch.nn.Module.__init__(self)

        self.linear_1 = torch.nn.Linear(in_features=3, out_features=3, bias=True)
        self.linear_2 = torch.nn.Linear(in_features=3, out_features=3, bias=True)
        self.linear_3 = torch.nn.Linear(in_features=6, out_features=1, bias=True)
        self.my_special_relu = torch.nn.ReLU()

    def forward(self, inputs):
        x = self.linear_1(inputs)
        y = self.linear_2(inputs)
        z = torch.cat([x, y], dim=1)
        z = self.my_special_relu(self.linear_3(z))
        return z


def main():
    i = int(sys.argv[1])
    net = SampleNet()
    torchrec.render_network(
        net,
        name="Sample Net",
        input_shapes=(1, 3),
        directory="./",
        fmt="svg",
        render_depth=i,
    )


if __name__ == "__main__":
    main()

And visualizations like these can be produced:

Acknowledgements

This is inspired from szagoruyko/pytorchviz. This package differs from pytorchviz as it provides rendering at multiple depths.

Note that for rendering a network via TensorBoard during training, you can use torch.utils.tensorboard.SummaryWriter.add_graph, which records and renders to a protobuf in a single step. The intended usage of pytorchrec is for presentation purposes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for torchrec, version 1.0.0a0
Filename, size File type Python version Upload date Hashes
Filename, size torchrec-1.0.0a0-py3-none-any.whl (13.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size torchrec-1.0.0a0.tar.gz (10.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page