Runtime shape validation for size-annotated Python code
Project description
Runtime Shape Validation for Size-Annotated Python Code
The sizecheck package provides a decorator that automatically adds runtime shape checking to Python functions based on size-annotated variable names using AST transformation.
Overview
When writing PyTorch or NumPy code, it's common to use naming conventions that indicate tensor shapes, as in this Medium post. For example, if a tensor weights has shape N × K, you might name the variable weights_NK. This library automatically validates that tensors match their annotated shapes at runtime by analyzing and modifying your function's Abstract Syntax Tree (AST).
Key Features
- AST-based transformation: Automatically injects shape checks into function arguments and variable assignments
- Intuitive naming convention: Use underscores to indicate tensor shapes
- Framework agnostic: Works with PyTorch, NumPy, Jax, and any other libraries that use
.shapeto indicate tensor shapes. - Comprehensive checking: Validates both function parameters and intermediate assignments
- Clear error messages: Provides detailed information when shapes don't match
Quick Start
import torch
from sizecheck import sizecheck
@sizecheck
def matrix_multiply(a_NK, b_KM):
"""Matrix multiplication with automatic shape checking."""
result_NM = torch.matmul(a_NK, b_KM)
return result_NM
# This works fine
a_NK = torch.randn(3, 4) # N=3, K=4
b_KM = torch.randn(4, 5) # K=4, M=5
result = matrix_multiply(a_NK, b_KM) # Shape: (3, 5)
# This raises an AssertionError
a_NK = torch.randn(3, 4)
b_KM = torch.randn(5, 6) # Wrong! K dimensions don't match
result = matrix_multiply(a_NK, b_KM) # AssertionError!
Shape Annotation Format
Each character in the dimensions suffix represents one dimension:
tensor_NK: 2D tensor with dimensions N × Kdata_BCHW: 4D tensor with dimensions B × C × H × W
Only single capital letters are supported: NK, BCHW, IJ
What Gets Checked
The decorator automatically adds shape validation for:
- Function arguments with underscores in their names
- Variable assignments to names containing underscores
- Augmented assignments (+=, -=, *=, etc.)
- Annotated assignments (PEP 526 style)
Dimension Scope
The dimensions are scoped to the function they are defined in. For example, if you define a function foo with a parameter x_NK, the dimension N is only valid within the scope of foo. If you define another function bar with a parameter y_NL, this dimension N can differ from the one in foo, but it is only valid within the scope of bar.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sizecheck-0.1.0.tar.gz.
File metadata
- Download URL: sizecheck-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39ab7184b0c90c91eb13bd7946d5758904d1965e576e80e3a4293f4aec70b099
|
|
| MD5 |
72703b41bac9ff7a69a015ce53e96bbf
|
|
| BLAKE2b-256 |
76f9c0bbada5f51e822c64117c25d322a8d9cdef6a5d513b4561b9d5a9f69e8a
|
File details
Details for the file sizecheck-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sizecheck-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46b7a4a87a1d71688e4c4f0d8ba276fd721dadcf3ac9367e693db1568bdbe52e
|
|
| MD5 |
ae69282a7f5ad3d10fa01c270b6eca7a
|
|
| BLAKE2b-256 |
b0b9067ab07727463ef478410e7117062086ea7800d5ec247e37b264ab295f9a
|