Skip to main content

Static AST-based PyTorch tensor shape analysis.

Project description

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 (one-line, recommended):

/plugin marketplace add Davidxswang/torchshapeflow
/plugin install torchshapeflow

This installs a plugin that registers 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchshapeflow-0.7.0.tar.gz (103.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchshapeflow-0.7.0-py3-none-any.whl (60.4 kB view details)

Uploaded Python 3

File details

Details for the file torchshapeflow-0.7.0.tar.gz.

File metadata

  • Download URL: torchshapeflow-0.7.0.tar.gz
  • Upload date:
  • Size: 103.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torchshapeflow-0.7.0.tar.gz
Algorithm Hash digest
SHA256 3870d3f0a9d1c2d234b700ee42d5a135935a1f8c8b54df6b98097195bfa2a5c6
MD5 f93559cd5c17104c2d3f7c894f0dc5b6
BLAKE2b-256 5110810d2d5ffbbd206b4c7ea87c3a2f472b23d9d747632ac101a8a9340d7715

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchshapeflow-0.7.0.tar.gz:

Publisher: release.yml on Davidxswang/torchshapeflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchshapeflow-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: torchshapeflow-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 60.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torchshapeflow-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e864124f943b872fc5fe732e6fff4b8c35aeb11274938d5463480114d2a19b31
MD5 149b2eef84fa42f491aa6af72c8ac703
BLAKE2b-256 099d5f12287415e7143694a6dbf9b58c2c1745cf14ecbe4e071bc8237ca41dc8

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchshapeflow-0.7.0-py3-none-any.whl:

Publisher: release.yml on Davidxswang/torchshapeflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page