Metadata-Version: 2.4
Name: torchshapeflow
Version: 0.7.2
Summary: Static AST-based PyTorch tensor shape analysis.
Project-URL: Homepage, https://github.com/Davidxswang/torchshapeflow
Project-URL: Repository, https://github.com/Davidxswang/torchshapeflow
Project-URL: Issues, https://github.com/Davidxswang/torchshapeflow/issues
Project-URL: Documentation, https://davidxswang.github.io/torchshapeflow/
Author: TorchShapeFlow Contributors
License: MIT License
        
        Copyright (c) 2026 Xuesong Wang
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: mkdocs-material>=9.7.5; extra == 'dev'
Requires-Dist: mkdocs>=1.6.1; extra == 'dev'
Requires-Dist: pygments>=2.20.0; extra == 'dev'
Requires-Dist: pyinstaller>=6.16.0; extra == 'dev'
Requires-Dist: pymdown-extensions>=10.21.2; extra == 'dev'
Requires-Dist: pytest-xdist>=3.8.0; extra == 'dev'
Requires-Dist: pytest>=9.0.2; extra == 'dev'
Requires-Dist: ruff>=0.15.6; extra == 'dev'
Requires-Dist: ty>=0.0.1a19; extra == 'dev'
Provides-Extra: examples
Requires-Dist: torch>=2.10.0; extra == 'examples'
Provides-Extra: mcp
Requires-Dist: mcp>=1.27.0; extra == 'mcp'
Description-Content-Type: text/markdown

# TorchShapeFlow

[![CI](https://github.com/Davidxswang/torchshapeflow/actions/workflows/ci.yml/badge.svg)](https://github.com/Davidxswang/torchshapeflow/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/torchshapeflow?logo=pypi)](https://pypi.org/project/torchshapeflow/)
[![Python](https://img.shields.io/pypi/pyversions/torchshapeflow?logo=python)](https://pypi.org/project/torchshapeflow/)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

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.

```python
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)
```

```bash
$ 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):

```text
/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):

```bash
pip install torchshapeflow
```

## Documentation

Full docs at **[davidxswang.github.io/torchshapeflow](https://davidxswang.github.io/torchshapeflow)**

- [Quickstart](https://davidxswang.github.io/torchshapeflow/quickstart/) — install and run your first check
- [Annotation syntax](https://davidxswang.github.io/torchshapeflow/syntax/) — how to annotate your tensors
- [Supported operators](https://davidxswang.github.io/torchshapeflow/operators/) — what is analyzed and what shapes are inferred
- [Limitations](https://davidxswang.github.io/torchshapeflow/limitations/) — what the analyzer does not handle
- [For AI coding agents](https://davidxswang.github.io/torchshapeflow/agents/) — how Claude Code / Cursor / Copilot / Aider should invoke the CLI and interpret output

## Contributing

```bash
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:

```bash
uv sync --extra dev --extra examples
```

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