Tutorial 7

Compressed View for Repeated Structures

When your model has many repeated layers (like a stack of identical blocks), the visualization can become very long and repetitive.

TorchVista can detect these repeated patterns and compress them into a single "repeated" node. But this is possible only when the repeated structures are within nn.ModuleList or nn.Sequential as a chain.

Set show_compressed_view=True to enable this feature.

Code

import torch
import torch.nn as nn
from torchvista import trace_model

class DeepModel(nn.Module):
    def __init__(self):
        super().__init__()
        # 10 identical Sequential blocks, each with 10 Linear layers
        block = nn.Sequential(*[nn.Linear(64, 64) for _ in range(10)])
        self.layers = nn.ModuleList([block] * 10)

    def forward(self, x):
        for seq in self.layers:
            x = seq(x)
        return x

model = DeepModel()
example_input = torch.randn(2, 64)

# Compress repeated structures into a single representation
trace_model(
    model,
    example_input,
    ###############################
    show_compressed_view=True  # <-- compresses repeated layers
    ###############################
)

Interactive Visualization

You've now completed the TorchVista tutorial series! Check out the demos page for more examples.