Tsanley: Understanding Tensor Programs
Project description
tsanley
A shape analyzer for tensor programs, using popular tensor libraries: tensorflow, pytorch, numpy, ...
Builds upon a 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 with 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, we add the following code before test_foo is called (in the test function):
- Declare the named dimension variables (using
dim_vars) -- these help specify the expected shape of tensor variables used in the code. For example, here we declare 3 dimension variables, referred to via shorthand namesb,t,d. We use these shorthand names to label 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).
def foo(x):
x: 'b,t,d' #shape check: ok! [line 36]
y: 'b,d' = x.mean(dim=0) #error: dim should be 1 [line 37]
# ^
# | tsanley detects shape violation
z: 'b,d' = x.mean(dim=1) #shape check: ok! [line 38]
def test_foo():
import torch
from tsalib import get_dim_vars
# get the declared dimension sizes: 10, 100, 1024
B, L, D = get_dim_vars('b t d')
x = torch.Tensor(B, L, D)
foo(x)
def test():
#declare the named dimension variables using the tsalib api
from tsalib import dim_vars
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
test_foo()
if __name__ == '__main__': test()
On executing the above program, tsanley tracks shapes of tensor variables (x, y, z) in function foo and reports shape check successes and failures.
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: (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 ..
See examples in models directory.
Installation
pip install tsanley
Status: Experimental
tsanley performs a best-effort shape tracking when the program runs. 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).
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 tsanley-0.1.1.tar.gz.
File metadata
- Download URL: tsanley-0.1.1.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3bbeb1d1e68ef2d2f1af53f038ae78c1012e50aafcb330b70ab96701de3ae6b
|
|
| MD5 |
bcefc124fd8ce72217bfb68d43961610
|
|
| BLAKE2b-256 |
4f52be7bd42d094b141667f8eb6e406cb57f428bb4b70c094fdf676832b5e4d1
|
File details
Details for the file tsanley-0.1.1-py3-none-any.whl.
File metadata
- Download URL: tsanley-0.1.1-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f88d2cfcebb0a469a1fa8ecfa78bbc53ad168f649335072f72b02621980048d
|
|
| MD5 |
e8d7af8f96872adb2a48a453f0077121
|
|
| BLAKE2b-256 |
8bcfe12857674f84459d033ec90583df29a1d5e33e2b1eeaa9bea06d5440bf37
|