Skip to main content

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.

For previous versions of the documentation, substitute latest in the documentation URL above for the desired version.

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 .shape to 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 × K
  • data_BCHW: 4D tensor with dimensions B × C × H × W

Dimension annotations can contain:

  • Variable dimensions (uppercase letters): N, K, M - stored in variables of the same name
  • Constant dimensions (single digits): 3, 4, 2 - checked for exact size

What Gets Checked

The decorator automatically adds shape validation for:

  • Function arguments with underscores in their names
  • Variable assignments to names containing underscores

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.

Julia version

If you're looking for the Julia version of this library, check out SizeCheck.jl.

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

sizecheck-0.2.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

sizecheck-0.2.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file sizecheck-0.2.0.tar.gz.

File metadata

  • Download URL: sizecheck-0.2.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for sizecheck-0.2.0.tar.gz
Algorithm Hash digest
SHA256 47eb22836c3eaedd8ccf987283f397774b48194d7152d928b46c06cad7561437
MD5 08c563adc51565fb7baf43850205621f
BLAKE2b-256 8c8a040b9b33cd8ddca249134743d24732fd2fefee7c4b2eb5af5a85c030cc6e

See more details on using hashes here.

File details

Details for the file sizecheck-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sizecheck-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for sizecheck-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc13bff51cb6c9b45a57a4489aaf6037314dfecfa1b5b7e31d3b62ed4a6e7c12
MD5 cb3c9aa965ec41c7c709f70076f1ec30
BLAKE2b-256 d73f7f4b7e40993bfac319fd4a36421a9a39847d17befb2a598b2bba5e41ce00

See more details on using hashes here.

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