tsanley
Tsanley is a shape analyzer for tensor programs, using popular tensor libraries: tensorflow, pytorch, numpy. Plugs into your existing code seamlessly, with minimal changes.
Builds upon the library tsalib for specifying, annotating and transforming tensor shapes using named dimensions.
Quick Start
tsanley discovers shape errors at runtime by checking the runtime tensor shapes against the user-specified shape annotations. Tensor shape annotations are specified in the tsalib shape shorthand notation, e.g., x: 'btd'.
More details on the shorthand format here.
Example
Suppose we have the following functions foo and test_foo in our existing code. To setup tsanley analyzer for shape checking in foo, we add a function setup_named_dims before calling test_foo, label tensor variables by their expected shorthand shapes (e.g., b,d) and then execute the code normally.
def foo(x):
x: 'b,t,d' #shape check: ok! [line 36]
y: 'b,d' = x.mean(dim=0) # error! [line 37]
z: 'b,d' = x.mean(dim=1) #shape check: ok! [line 38]
def test_foo():
import torch
x = torch.Tensor(10, 100, 1024)
foo(x)
def setup_named_dims():
from tsalib import dim_vars
#declare the named dimension variables using the tsalib api
#e.g., 'b' stands for 'Batch' dimension with size 10
dim_vars('Batch(b):10 Length(t):100 Hidden(d):1024')
# initialize tsanley's dynamic shape analyzer
from tsanley.dynamic import init_analyzer
init_analyzer(trace_func_names=['foo'], show_updates=True) #check_tsa=True, debug=False
if __name__ == '__main__':
setup_named_dims()
test_foo()
On executing the above program, tsanley tracks shapes of tensor variables (x, y, z) in function foo and reports following shape check results.
Output
> Analyzing function foo
Update at line 36: actual shape of x = b,t,d
>> shape check succeeded at line 36
Update at line 37: actual shape of y = t,d
>> FAILED shape check at line 37
expected: (b:10, d:1024), actual: (100, 1024)
Update at line 38: actual shape of z = b,d
>> shape check succeeded at line 38
saving shapes to /tmp/shape_log.json ..
What does setup_named_dims do?
- Declare the named dimension variables (using
dim_vars) -- using them we can specify the expected shape of tensor variables in the code. For example, here we declare 3 dimension variables,Batch,LengthandHidden, and refer to them via shorthand namesb,t,d. - We use shorthand names to label tensor variables and check their shapes in one or more functions, e.g.,
foohere. - Initialize the
tsanleyanalyzer by callinginit_analyzer: parametertrace_func_namestakes a list of function names as Unix shell-style wildcards (using thefnmatchlibrary). We can specify names with wildcards, e.g.,Resnet.*to track all functions in theResnetclass.
See examples in models directory.
Installation
pip install tsanley
Annotation
tsanley can also annotate tensor variables in existing executable code with shape labels. This is useful when trying to understand external open-source code or labeling one's own code.
Suppose, we have some un-annotated code residing in file model.py.
- First, generate shape logs by adding
setup_named_dimsto themodel.py. - Execute
model.py. The logs are stored in/tmp/shape_log.json. - Use the logs to annotate
test.py.
Example
Let's revisit the earlier example, without our manual annotations. Suppose it resides in model.py.
def foo(x):
y = x.mean(dim=0)
z = x.mean(dim=1)
def test_foo():
import torch
x = torch.Tensor(10, 100, 1024)
foo(x)
We add setup_named_dims to the code, and execute it.
def setup_named_dims():
from tsalib import dim_vars
#declare the named dimension variables using the tsalib api
#e.g., 'b' stands for 'Batch' dimension with size 10
dim_vars('Batch(b):10 Length(t):100 Hidden(d):1024')
# initialize tsanley's dynamic shape analyzer
from tsanley.dynamic import init_analyzer
init_analyzer(trace_func_names=['foo'], show_updates=True, check_tsa=False) # debug=False
if __name__ == '__main__':
setup_named_dims()
test_foo()
This generates the shape logs in /tmp/shape_log.json. Flag check_tsa=False ensures no shape checks are performed by tsanley.
Now, annotate foo with the command:
tsa annotate -f model.py
The output is a file tsa_model.py with foo updated as follows:
def foo(x):
y: 't,d' = x.mean(dim=0)
z: 'b,d' = x.mean(dim=1)
tsanley makes smart guesses to map runtime shape values (100) to the shorthand names (t). If we do not declare the dimension names using dim_vars in setup_named_dims, we get the following annotation:
def foo(x):
y: '100,1024' = x.mean(dim=0)
z: '10,1024' = x.mean(dim=1)
Status: Experimental
tsanley performs a best-effort shape tracking during program execution. Here are a few tricky scenarios:
- calling same function multiple times -- shape values from only the last call are cached.
- recursive calls -- not handled.
Tested with pytorch examples. tensorflow and numpy programs should also work (tsalib supported backends), but remain to be tested.
Metadata
Release files for tsanley 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tsanley-0.2.0.tar.gz | 12.3 kB | Without provenance | Details for tsanley-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tsanley-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for tsanley-0.2.0-py3-none-any.whl |
Total release size: 28.5 kB
Release files / tsanley-0.2.0.tar.gz
| Download URL | tsanley-0.2.0.tar.gz |
|---|---|
| Size | 12.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
491f47029fa27651766be6e5ca044d001242b9b4d42fe01d82b3ae931fa2bf80
|
|
BLAKE2b-256 checksum How to use checksums |
38a946cd2e8615a74ff87fcc34a319e1f48bb2c1ab2968a41040caa579ecfc96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.35.0 CPython/3.6.7
|
Release files / tsanley-0.2.0-py3-none-any.whl
| Download URL | tsanley-0.2.0-py3-none-any.whl |
|---|---|
| Size | 16.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
10e140cafa42b10af2b659ad65cebca113edea48731cf627c6d3ef982d36b5cc
|
|
BLAKE2b-256 checksum How to use checksums |
638fad71ef5e215fa8304e7bdc20c48a332f26c1384965c6b7b4aaf4d5811ef5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.35.0 CPython/3.6.7
|
Security for tsanley 0.2.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.