Labsco
MCP SERVER

PyTorch-FX Shaper

by Athe-kunal

Get the real tensor shape at every step of a PyTorch module, then rewrite the maths as einsum with the rewrite checked before you keep it.

Code Generation, Scaffolding & Migration
Summary
Shapes read off a real trace, then the rewrite checked.

Reading a transformer's forward pass usually means holding shapes in your head. This puts the measured shape on every line, and when it rewrites matmuls and attention as einsum it validates the result before you take it.

What it is

A shape-extraction and rewrite pipeline for PyTorch, exposed over MCP. It runs torch.export and ShapeProp across your nn.Module to capture the shape of every intermediate tensor, then annotates the source or rewrites operations as torch.einsum.

What you get
  • Per-module tensor shapes for every intermediate in the forward pass, not just the inputs
  • Your source files back with those shapes inserted as inline comments on the lines they belong to
  • Comment mode: shapes rewritten with descriptive dimension names such as batch_size, seq_len and n_embd, plus an explanation of each transformation
  • Einsum mode: matmuls and attention operations replaced with torch.einsum, collapsing intermediate reshapes where possible
  • A rewrite validated before you accept it — whole file, single module or single function
Requirements

The repository checked out locally with PyTorch, and a module that torch.export can trace — the shapes come from a real forward pass, not from static analysis. The comment and einsum rewrites run through an LLM, so those two modes need a model behind them.

Setup effort

One command — pip install -e .