Skip to main content

TorchShapeFlow

CI PyPI Python License: MIT

TorchShapeFlow is a static, AST-based shape analyzer for PyTorch. It reads your Python source, infers tensor shapes from Annotated[..., Shape(...)] contracts, and reports mismatches as structured diagnostics. No execution required.

from typing import Annotated
import torch
from torchshapeflow import Shape

def attention_scores(
    q: Annotated[torch.Tensor, Shape("B", "H", "T", "D")],
    k: Annotated[torch.Tensor, Shape("B", "H", "T", "D")],
) -> Annotated[torch.Tensor, Shape("B", "H", "T", "T")]:
    return q @ k.transpose(-2, -1)
$ tsf check mymodel.py
All clean (1 file checked)

Philosophy

TorchShapeFlow is annotation-first and symbolic-first.

  • You declare tensor shape contracts with Annotated[torch.Tensor, Shape(...)].
  • Symbolic dimensions like "B", "T", and "D" are the default path for config-driven model code.
  • Integer dimensions are still useful for fixed semantics like RGB channels or known embedding widths.
  • When inference is not possible, the analyzer degrades visibly instead of guessing.

If Pydantic gives structure to data boundaries, TorchShapeFlow aims to do the same for tensor-shape boundaries in deep learning code.

Install

In Claude Code (two commands, no config-file editing):

/plugin marketplace add Davidxswang/torchshapeflow
/plugin install torchshapeflow@torchshapeflow

The first command registers this repo as a plugin marketplace (pulling from main by default). The second installs the torchshapeflow plugin from that marketplace, which wires in an MCP server, an agent skill, and a post-edit hook — your Claude Code then knows how to run tsf check, interpret the structured diagnostics, and propose annotations. No manual .mcp.json editing required.

As a plain Python package (for CLI use or other agent runtimes):

pip install torchshapeflow

Documentation

Full docs at davidxswang.github.io/torchshapeflow

Contributing

git clone https://github.com/Davidxswang/torchshapeflow
cd torchshapeflow
make install   # uv sync --extra dev
make check     # format + lint + typecheck + tests

If you want to execute the example PyTorch scripts in examples/, install the separate examples extra:

uv sync --extra dev --extra examples

See docs/development.md for the full development guide: all make targets, CI workflow descriptions, and how to add new operators.

Metadata

Release files for torchshapeflow 0.7.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchshapeflow 0.7.3
File Size Uploaded
torchshapeflow-0.7.3.tar.gz 114.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torchshapeflow 0.7.3
File Interpreter ABI Platform
torchshapeflow-0.7.3-py3-none-any.whl Python 3 none any Details

Total release size: 187.2 kB

Release files / torchshapeflow-0.7.3.tar.gz

Download URL torchshapeflow-0.7.3.tar.gz
Size 114.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8b27c716c509e39461e61196c3736406410e70d96560233e3eae9278395bbf77
BLAKE2b-256 checksum
How to use checksums
4b05199dfb10c63962cc4cab788042ea22a10ea308dbc002401a18886adc4fec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 22, 2026.

Transparency log

Release files / torchshapeflow-0.7.3-py3-none-any.whl

Download URL torchshapeflow-0.7.3-py3-none-any.whl
Size 73.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab30419e71c60e283523a24d4ef51fcbf37b5dc01674c1613ce3617782b35392
BLAKE2b-256 checksum
How to use checksums
5f9062b2f0c9f2e2f71b5fc7b7f5e6d95a94e2a3e1aab85d94dd7e98869e8fc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 22, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.7.3 This release

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page