VisualTorch

repository·main·Indexed 43 days ago

https://github.com/willyfh/visualtorch

A visualization library for PyTorch neural network architectures. It supports generating flow, graph, and LeNet-style diagrams, as well as animated architecture reveals. The library works with torch.nn.Sequential and custom PyTorch models by tracing a forward pass. It also provides MCP (Model Context Protocol) integration for use with coding assistants via the visualtorch-mcp server.

Tokens
5.3K
Snippets
19
Records
31
Agent score
55%

What's inside VisualTorch

  1. Overview of VisualTorch

    main

    VisualTorch is a tool designed to visualize PyTorch neural network architectures. It supports generating several visual styles, including:

    • flow: Flow-style architectures.
    • graph: Graph-style architectures.
    • lenet: LeNet-style architectures.

    It works with both nn.Sequential and custom PyTorch models by tracing a real forward pass.

    Important Limitations:

    • Data-dependent control flow: Because it uses tracing, models with branches that depend on tensor values (e.g., if x > 0:) will only visualize the path taken by the specific dummy input used during the trace.
    • Multiple outputs: For layers returning multiple tensors (like nn.LSTM), the node size is based on the first tensor. However, if show_dimension=True is used, the labels will display the shapes of all output tensors.
  2. Get Started with VisualTorch

    main

    VisualTorch is a tool for visualizing Torch-based neural network architectures. It supports generating three main visualization styles:

    • Flow-style architectures
    • Graph-style architectures
    • LeNet-style architectures

    It works with both torch.nn.Sequential and custom PyTorch models. To begin using the library, you can follow the installation guide or explore usage examples for specific styles.

  3. Security warning: VisualTorch MCP is not a sandbox

    main

    ⚠️ Critical Security Notice

    VisualTorch executes the provided source and model_expression as Python code. The worker subprocess is a reliability boundary, not a security sandbox.

    Supplied code can:

    • Read or modify files.
    • Access the network.
    • Start new processes.
    • Use the current user's permissions.

    Best Practices:

    1. Only connect to MCP hosts you trust.
    2. Review tool arguments before approving execution.
    3. For high-security environments, run the MCP server inside a container, virtual machine, or a dedicated OS account with restricted access.
  4. Build the VisualTorch documentation

    main

    Once dependencies are installed, you can generate the HTML version of the documentation using the sphinx-build command. This command takes the source directory as input and outputs the built files into the build directory.

    sphinx-build -b html source build
  5. Install VisualTorch documentation dependencies

    main

    To set up the environment for building the VisualTorch documentation, install the required dependencies using pip from the requirements.txt file. This project uses sphinx-design and myst-parser for its documentation structure.

    pip install -r requirements.txt
  6. Set up a development environment for VisualTorch

    main

    To contribute to VisualTorch, set up a local development environment using Conda. This involves creating a dedicated environment, installing the package in editable mode with development dependencies, and configuring pre-commit hooks for automated code quality checks.

    Environment Setup Steps

    1. Create and activate a Conda environment:

      conda create -n visualtorch_env python=3.10
      conda activate visualtorch_env
    2. Install development requirements: Install the package in editable mode including the [dev] extra:

      pip install -e .[dev]
    3. Install pre-commit hooks:

      pre-commit install
    conda create -n visualtorch_env python=3.10
    conda activate visualtorch_env
    pip install -e .[dev]
    pre-commit install
  7. Connect an MCP host to VisualTorch

    main

    Register visualtorch-mcp as a stdio server in your MCP host configuration.

    If the script is on your PATH:

    {
      "mcpServers": {
        "visualtorch": {
          "command": "visualtorch-mcp"
        }
      }
    }

    If the script is NOT on your PATH: You must use the absolute path to your Python interpreter and run the module via -m visualtorch_mcp.

    {
      "mcpServers": {
        "visualtorch": {
          "command": "/absolute/path/to/python",
          "args": ["-m", "visualtorch_mcp"]
        }
      }
    }

    Note: On Windows, the interpreter path typically ends in Scripts/python.exe; on POSIX, it ends in bin/python.