├── .gitignore
├── LICENSE
├── README.md
├── Tensor Puzzlers.ipynb
├── Tensor Puzzlers_files
├── Tensor Puzzlers_10_0.svg
├── Tensor Puzzlers_11_0.svg
├── Tensor Puzzlers_12_0.png
├── Tensor Puzzlers_12_0.svg
├── Tensor Puzzlers_13_0.svg
├── Tensor Puzzlers_14_0.svg
├── Tensor Puzzlers_15_0.png
├── Tensor Puzzlers_15_0.svg
├── Tensor Puzzlers_16_0.svg
├── Tensor Puzzlers_17_0.svg
├── Tensor Puzzlers_18_0.png
├── Tensor Puzzlers_18_0.svg
├── Tensor Puzzlers_19_0.svg
├── Tensor Puzzlers_20_0.svg
├── Tensor Puzzlers_21_0.png
├── Tensor Puzzlers_21_0.svg
├── Tensor Puzzlers_22_0.svg
├── Tensor Puzzlers_23_0.svg
├── Tensor Puzzlers_24_0.png
├── Tensor Puzzlers_24_0.svg
├── Tensor Puzzlers_25_0.svg
├── Tensor Puzzlers_26_0.svg
├── Tensor Puzzlers_27_0.png
├── Tensor Puzzlers_27_0.svg
├── Tensor Puzzlers_27_1.png
├── Tensor Puzzlers_28_0.svg
├── Tensor Puzzlers_29_0.svg
├── Tensor Puzzlers_2_0.png
├── Tensor Puzzlers_30_0.png
├── Tensor Puzzlers_30_0.svg
├── Tensor Puzzlers_30_1.png
├── Tensor Puzzlers_31_0.svg
├── Tensor Puzzlers_32_0.svg
├── Tensor Puzzlers_33_0.png
├── Tensor Puzzlers_33_0.svg
├── Tensor Puzzlers_33_1.png
├── Tensor Puzzlers_34_0.svg
├── Tensor Puzzlers_35_0.svg
├── Tensor Puzzlers_36_0.png
├── Tensor Puzzlers_36_0.svg
├── Tensor Puzzlers_37_0.svg
├── Tensor Puzzlers_38_0.svg
├── Tensor Puzzlers_39_0.png
├── Tensor Puzzlers_39_0.svg
├── Tensor Puzzlers_3_0.png
├── Tensor Puzzlers_40_0.svg
├── Tensor Puzzlers_41_0.svg
├── Tensor Puzzlers_42_0.png
├── Tensor Puzzlers_42_0.svg
├── Tensor Puzzlers_43_0.svg
├── Tensor Puzzlers_44_0.svg
├── Tensor Puzzlers_45_0.png
├── Tensor Puzzlers_45_0.svg
├── Tensor Puzzlers_45_1.png
├── Tensor Puzzlers_46_0.svg
├── Tensor Puzzlers_47_0.svg
├── Tensor Puzzlers_48_0.png
├── Tensor Puzzlers_48_0.svg
├── Tensor Puzzlers_49_0.svg
├── Tensor Puzzlers_4_0.svg
├── Tensor Puzzlers_50_0.svg
├── Tensor Puzzlers_51_0.png
├── Tensor Puzzlers_51_0.svg
├── Tensor Puzzlers_52_0.svg
├── Tensor Puzzlers_53_0.svg
├── Tensor Puzzlers_54_0.png
├── Tensor Puzzlers_54_0.svg
├── Tensor Puzzlers_55_0.svg
├── Tensor Puzzlers_56_0.svg
├── Tensor Puzzlers_57_0.png
├── Tensor Puzzlers_57_0.svg
├── Tensor Puzzlers_57_1.png
├── Tensor Puzzlers_58_0.svg
├── Tensor Puzzlers_59_0.svg
├── Tensor Puzzlers_5_0.png
├── Tensor Puzzlers_5_0.svg
├── Tensor Puzzlers_5_1.png
├── Tensor Puzzlers_60_0.svg
├── Tensor Puzzlers_61_0.svg
├── Tensor Puzzlers_62_0.svg
├── Tensor Puzzlers_63_0.svg
├── Tensor Puzzlers_64_0.svg
├── Tensor Puzzlers_65_0.svg
├── Tensor Puzzlers_66_0.svg
├── Tensor Puzzlers_67_0.svg
├── Tensor Puzzlers_68_0.svg
├── Tensor Puzzlers_69_0.svg
├── Tensor Puzzlers_6_0.png
├── Tensor Puzzlers_6_1.png
├── Tensor Puzzlers_70_0.svg
├── Tensor Puzzlers_7_0.svg
├── Tensor Puzzlers_8_0.svg
├── Tensor Puzzlers_9_0.png
├── Tensor Puzzlers_9_0.svg
└── Tensor Puzzlers_9_1.png
├── chess.jpeg
├── lib.py
└── requirements.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Sasha Rush
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_10_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_11_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_12_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_12_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_12_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_13_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_14_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_15_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_15_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_18_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_18_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_21_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_21_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_24_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_24_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_27_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_27_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_27_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_27_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_27_1.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_29_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_2_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_2_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_30_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_30_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_30_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_30_1.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_32_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_33_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_33_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_33_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_33_1.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_36_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_36_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_36_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_38_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_39_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_39_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_3_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_3_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_42_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_42_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_45_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_45_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_45_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_45_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_45_1.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_47_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_48_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_48_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_4_0.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_51_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_51_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_51_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_53_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_54_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_54_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_57_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_57_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_57_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_57_1.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_5_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_5_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_5_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_5_1.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_60_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_62_0.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_6_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_6_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_6_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_6_1.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_7_0.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_9_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_9_0.png
--------------------------------------------------------------------------------
/Tensor Puzzlers_files/Tensor Puzzlers_9_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/Tensor Puzzlers_files/Tensor Puzzlers_9_1.png
--------------------------------------------------------------------------------
/chess.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srush/Tensor-Puzzles/ad5069f7ef48523f733d445a7f94e93427d64f2d/chess.jpeg
--------------------------------------------------------------------------------
/lib.py:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | from hypothesis.extra.numpy import arrays
5 | from hypothesis.strategies import integers, lists, composite, floats
6 | from hypothesis import given
7 | import numpy as np
8 | import random
9 | import sys
10 | import typing
11 | import matplotlib.pyplot as plt
12 |
13 | import urllib
14 | import torch
15 | import time
16 | from chalk import *
17 | import chalk
18 | from colour import Color
19 | from IPython.display import display, SVG
20 |
21 | color = [Color("red")] * 50
22 |
23 | def color(v):
24 | d = rectangle(1, 1)
25 | if v == 0:
26 | return d
27 | elif v > 0:
28 | return d.fill_color(Color("orange")).fill_opacity(0.4 + 0.6 *( v / 10))
29 | elif v < 0:
30 | return d.fill_color(Color("blue")).fill_opacity(0.4 + 0.6 * ( abs(v) / 10))
31 |
32 | def draw_matrix(mat):
33 | return vcat((hcat((color(v)
34 | for j, v in enumerate(inner)))
35 | for i, inner in enumerate(mat)))
36 |
37 | def grid(diagrams):
38 | mhs = [0] * 100
39 | mws = [0] * 100
40 | for i, row in enumerate(diagrams):
41 | mh = 0
42 | for j, col in enumerate(row):
43 | env = col.get_envelope()
44 | mhs[i] = max(env.height, mhs[i])
45 | mws[j] = max(mws[j], env.width)
46 | return vcat([hcat([col.center_xy().with_envelope(rectangle(mws[j], mhs[i]))
47 | for j, col in enumerate(row)], 1.0) for i, row in enumerate(diagrams)], 1.0)
48 |
49 | def draw_example(data):
50 |
51 |
52 | name = data["name"]
53 | keys = list(data["vals"][0].keys())
54 | # cols = [[vstrut(0)] + [vstrut(0.5) / text(f"Ex. {i}", 0.5).fill_color(Color("black")).line_width(0.0) / vstrut(0.5) for i in range(len(data["vals"]))]]
55 | cols = []
56 | for k in keys:
57 | mat = [(vstrut(0.5) / text(k, 0.5).fill_color(Color("black")).line_width(0.0) / vstrut(0.5))]
58 | for ex in data["vals"]:
59 | v2 = ex[k]
60 | mat.append(draw_matrix(v2))
61 | cols.append(mat)
62 |
63 | full = grid(cols)
64 |
65 | full = (
66 | vstrut(1)
67 | / text(name, 0.75).fill_color(Color("black")).line_width(0)
68 | / vstrut(1)
69 | / full.center_xy()
70 | )
71 | full = full.pad(1.2).center_xy()
72 | env = full.get_envelope()
73 | set_svg_height(50 * env.height)
74 | height = 50 * env.height
75 | chalk.set_svg_height(300)
76 | return rectangle(env.width, env.height).fill_color(Color("white")) + full
77 |
78 | def draw_examples(name, examples):
79 | data = {"name":name,
80 | "vals" :[{k: [v.tolist()] if len(v.shape) == 1 else v.tolist()
81 | for k, v in example.items()}
82 | for example in examples ] }
83 | return draw_example(data)
84 |
85 |
86 | tensor = torch.tensor
87 |
88 | numpy_to_torch_dtype_dict = {
89 | bool: torch.bool,
90 | np.uint8: torch.uint8,
91 | np.int8: torch.int8,
92 | np.int16: torch.int16,
93 | np.int32: torch.int32,
94 | np.int64: torch.int64,
95 | np.float16: torch.float16,
96 | np.float32: torch.float32,
97 | np.float64: torch.float64,
98 | }
99 | torch_to_numpy_dtype_dict = {v: k for k, v in numpy_to_torch_dtype_dict.items()}
100 |
101 |
102 | @composite
103 | def spec(draw, x, min_size=1):
104 | # Get the type hints.
105 | if sys.version_info >= (3, 9):
106 | gth = typing.get_type_hints(x, include_extras=True)
107 | else:
108 | gth = typing.get_type_hints(x)
109 |
110 | # Collect all the dimension names.
111 | names = set()
112 | for k in gth:
113 | if not hasattr(gth[k], "__metadata__"):
114 | continue
115 | dims = gth[k].__metadata__[0]["details"][0].dims
116 | names.update([d.name for d in dims if isinstance(d.name, str)])
117 | names = list(names)
118 |
119 | # draw sizes for each dim.
120 | size = integers(min_value=min_size, max_value=5)
121 | arr = draw(arrays(shape=(len(names),), unique=True, elements=size, dtype=np.int32)).tolist()
122 | sizes = dict(zip(names, arr))
123 | for n in list(sizes.keys()):
124 | if '*' in n or '+' in n or '-' in n or '//' in n:
125 | i, op, j = n.split()
126 | i_val = i if i.isdigit() else sizes[i]
127 | j_val = j if j.isdigit() else sizes[j]
128 | sizes[n] = eval('{}{}{}'.format(i_val, op,j_val))
129 |
130 | # Create tensors for each size.
131 | ret = {}
132 | for k in gth:
133 | if not hasattr(gth[k], "__metadata__"):
134 | continue
135 | shape = tuple(
136 | [
137 | sizes[d.name] if isinstance(d.name, str) else d.size
138 | for d in gth[k].__metadata__[0]["details"][0].dims
139 | ]
140 | )
141 | dtype = (torch_to_numpy_dtype_dict[
142 | gth[k].__metadata__[0]["details"][1].dtype
143 | ]
144 | if len(gth[k].__metadata__[0]["details"]) >= 2
145 | else int)
146 | ret[k] = draw(
147 | arrays(
148 | shape=shape,
149 | dtype=dtype,
150 | elements=integers(min_value=-5, max_value=5) if
151 | dtype == int else None,
152 | unique=False
153 | )
154 | )
155 | ret[k] = np.nan_to_num(ret[k], nan=0, neginf=0, posinf=0)
156 |
157 | ret["return"][:] = 0
158 | return ret, sizes
159 |
160 |
161 | def make_test(name, problem, problem_spec, add_sizes=[], constraint=lambda d: d):
162 | examples = []
163 | for i in range(3):
164 | example, sizes = spec(problem, 3).example()
165 | example = constraint(example)
166 | out = example["return"].tolist()
167 | del example["return"]
168 | problem_spec(*example.values(), out)
169 |
170 | for size in add_sizes:
171 | example[size] = sizes[size]
172 |
173 | yours = None
174 | try:
175 | yours = problem(*map(tensor, example.values()))
176 |
177 | except NotImplementedError:
178 | pass
179 | for size in add_sizes:
180 | del example[size]
181 | example["target"] = tensor(out)
182 | if yours is not None:
183 | example["yours"] = yours
184 | examples.append(example)
185 |
186 | diagram = draw_examples(name, examples)
187 | display(SVG(diagram._repr_svg_()))
188 |
189 | @given(spec(problem))
190 | def test_problem(d):
191 | d, sizes = d
192 | d = constraint(d)
193 | out = d["return"].tolist()
194 | del d["return"]
195 | problem_spec(*d.values(), out)
196 | for size in add_sizes:
197 | d[size] = sizes[size]
198 |
199 | out2 = problem(*map(tensor, d.values()))
200 | out = tensor(out)
201 | out2 = torch.broadcast_to(out2, out.shape)
202 | assert torch.allclose(
203 | out, out2
204 | ), "Two tensors are not equal\n Spec: \n\t%s \n\t%s" % (out, out2)
205 |
206 | return test_problem
207 |
208 |
209 | def run_test(fn):
210 | fn()
211 | # Generate a random puppy video if you are correct.
212 | print("Correct!")
213 | from IPython.display import HTML
214 | pups = [
215 | "2m78jPG",
216 | "pn1e9TO",
217 | "MQCIwzT",
218 | "udLK6FS",
219 | "ZNem5o3",
220 | "DS2IZ6K",
221 | "aydRUz8",
222 | "MVUdQYK",
223 | "kLvno0p",
224 | "wScLiVz",
225 | "Z0TII8i",
226 | "F1SChho",
227 | "9hRi2jN",
228 | "lvzRF3W",
229 | "fqHxOGI",
230 | "1xeUYme",
231 | "6tVqKyM",
232 | "CCxZ6Wr",
233 | "lMW0OPQ",
234 | "wHVpHVG",
235 | "Wj2PGRl",
236 | "HlaTE8H",
237 | "k5jALH0",
238 | "3V37Hqr",
239 | "Eq2uMTA",
240 | "Vy9JShx",
241 | "g9I2ZmK",
242 | "Nu4RH7f",
243 | "sWp0Dqd",
244 | "bRKfspn",
245 | "qawCMl5",
246 | "2F6j2B4",
247 | "fiJxCVA",
248 | "pCAIlxD",
249 | "zJx2skh",
250 | "2Gdl1u7",
251 | "aJJAY4c",
252 | "ros6RLC",
253 | "DKLBJh7",
254 | "eyxH0Wc",
255 | "rJEkEw4"]
256 | return HTML("""
257 |
260 | """%(random.sample(pups, 1)[0]))
261 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | jupytext
2 | torchtyping
3 | hypothesis
4 | torch==1.10
5 | numpy
6 | pytest
7 | notebookjs
8 | jupyter
9 |
--------------------------------------------------------------------------------