Labsco
Athe-kunal logo

PyTorch-FX Shaper

โ˜… 1

from Athe-kunal

The MCP server provides shape of tensors to convert PyTorch code to einsum and einops

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeQuick setup

Agent Shaper

Agent Shaper extracts per-module tensor shape metadata from any PyTorch nn.Module and uses it to annotate source files โ€” either with descriptive shape comments or by rewriting operations as torch.einsum / einops.

How it works

  1. Shape extraction (fx_utils/get_fx_data.py) โ€” runs torch.export + ShapeProp on your module to capture the shape of every intermediate tensor in every workspace-defined module's forward pass, not just the inputs.
  2. Manual annotation (fx_utils/manual_annotate.py) โ€” inserts the extracted shapes as inline comments at the end of each relevant source line.
  3. LLM annotation (fx_utils/llm_annotate.py) โ€” feeds each manually-annotated module class to an LLM in parallel. Two modes:
    • COMMENT โ€” rewrites comments with descriptive dimension names (e.g. batch_size, seq_len, n_embd) and plain-English explanations of each transformation.
    • EINSUM โ€” rewrites the entire module replacing matmuls and attention operations with torch.einsum, collapsing intermediate reshapes where possible.
  4. Diff viewer โ€” review LLM changes before accepting them, either in a Streamlit UI or directly in VS Code's native diff editor.
  5. MCP server (agent_shaper/mcp_server.py) โ€” exposes the full shape-extraction and rewrite-validation pipeline as MCP tools consumable by Claude Code or any MCP-compatible AI assistant.

All modules in a file are processed in parallel via asyncio. Everything outside the module classes (imports, dataclasses, config objects) is preserved unchanged in the output file.

Project structure

agent_shaper/
  fx_utils/
    get_fx_data.py       # shape extraction via torch.export + ShapeProp
    manual_annotate.py   # inline shape comment insertion
    llm_annotate.py      # LLM-powered rewrite (COMMENT or EINSUM mode)
    diff_viewer.py       # Streamlit diff UI
  mcp_server.py          # MCP tool server for AI-assisted rewrites

examples/
  transformer/           # GPT-2, LLaMA, Qwen3, Swin Transformer
    model.py             # nanoGPT reference implementation
    model_einsum.py      # einsum-rewritten version
    llama.py / llama_einsum.py
    qwen3.py / qwen3_einsum.py
    swin_transformer.py / swin_transformer_einsum.py
    run_transformer.py   # example forward pass
  alignment/             # standalone alignment loss references
    alignment_losses.py  # DPO/IPO/SimPO losses (reference einsum style)
    dpo_losses_einsum.py
    jsd.py / jsd_einsum.py
    opd.py / opd_einsum.py
  direct_alignment/      # full training-ready RLHF loss implementations
    loss.py              # DPO, IPO, SimPO, ORPO, KTO, APO-zero, APO-down
    train.py             # training loop
    data.py              # preference dataset loading
    config.py            # training configuration

Each *_einsum.py file is the einsum/einops-rewritten counterpart of the original, validated to produce numerically identical outputs (atol=1e-5).

Examples

The examples/ folder contains worked rewrites across several model families, each paired with a validated einsum/einops version:

  • examples/transformer/ โ€” GPT-2, LLaMA, Qwen3, Swin Transformer. Each model has a *_einsum.py counterpart rewritten with torch.einsum and einops.
  • examples/alignment/ โ€” standalone alignment loss functions (DPO, IPO, SimPO, JSD, OPD) in both original and einsum form.
  • examples/direct_alignment/ โ€” production-style RLHF loss implementations (DPO, cDPO, IPO, SimPO, ORPO, KTO, APO-zero, APO-down) as nn.Module classes with a full training loop. The loss.py file uses einops.einsum and einops.reduce throughout, validated against the gather-based originals at atol=1e-5.

All *_einsum.py rewrites were validated using the validate_rewrite / validate_rewrite_function MCP tools.