1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
import itertools
import weakref
from collections import Counter
from typing import Any
import pytest
from opt_einsum import contract, contract_expression, contract_path, get_symbol, shared_intermediates
from opt_einsum.backends import to_cupy, to_torch
from opt_einsum.contract import _einsum
from opt_einsum.parser import parse_einsum_input
from opt_einsum.sharing import count_cached_ops, currently_sharing, get_sharing_cache
from opt_einsum.testing import build_views
from opt_einsum.typing import BackendType
pytest.importorskip("numpy")
try:
import numpy as np # type: ignore
numpy_if_found = "numpy"
except ImportError:
numpy_if_found = pytest.param("numpy", marks=[pytest.mark.skip(reason="NumPy not installed.")]) # type: ignore
try:
import cupy # noqa
cupy_if_found = "cupy"
except ImportError:
cupy_if_found = pytest.param("cupy", marks=[pytest.mark.skip(reason="CuPy not installed.")]) # type: ignore
try:
import torch # type: ignore # noqa
torch_if_found = "torch"
except ImportError:
torch_if_found = pytest.param("torch", marks=[pytest.mark.skip(reason="PyTorch not installed.")]) # type: ignore
backends = [numpy_if_found, torch_if_found, cupy_if_found]
equations = [
"ab,bc->ca",
"abc,bcd,dea",
"abc,def->fedcba",
"abc,bcd,df->fa",
# test 'prefer einsum' ops
"ijk,ikj",
"i,j->ij",
"ijk,k->ij",
"AB,BC->CA",
]
to_backend = {
"numpy": lambda x: x,
"torch": to_torch,
"cupy": to_cupy,
}
@pytest.mark.parametrize("eq", equations)
@pytest.mark.parametrize("backend", backends)
def test_sharing_value(eq: str, backend: BackendType) -> None:
views = build_views(eq)
shapes = [v.shape for v in views]
expr = contract_expression(eq, *shapes)
expected = expr(*views, backend=backend)
with shared_intermediates():
actual = expr(*views, backend=backend)
assert (actual == expected).all()
@pytest.mark.parametrize("backend", backends)
def test_complete_sharing(backend: BackendType) -> None:
eq = "ab,bc,cd->"
views = build_views(eq)
expr = contract_expression(eq, *(v.shape for v in views))
print("-" * 40)
print("Without sharing:")
with shared_intermediates() as cache:
expr(*views, backend=backend)
expected = count_cached_ops(cache)
print("-" * 40)
print("With sharing:")
with shared_intermediates() as cache:
expr(*views, backend=backend)
expr(*views, backend=backend)
actual = count_cached_ops(cache)
print("-" * 40)
print(f"Without sharing: {expected} expressions")
print(f"With sharing: {actual} expressions")
assert actual == expected
@pytest.mark.parametrize("backend", backends)
def test_sharing_reused_cache(backend: BackendType) -> None:
eq = "ab,bc,cd->"
views = build_views(eq)
expr = contract_expression(eq, *(v.shape for v in views))
print("-" * 40)
print("Without sharing:")
with shared_intermediates() as cache:
expr(*views, backend=backend)
expected = count_cached_ops(cache)
print("-" * 40)
print("With sharing:")
with shared_intermediates() as cache:
expr(*views, backend=backend)
with shared_intermediates(cache):
expr(*views, backend=backend)
actual = count_cached_ops(cache)
print("-" * 40)
print(f"Without sharing: {expected} expressions")
print(f"With sharing: {actual} expressions")
assert actual == expected
@pytest.mark.parametrize("backend", backends)
def test_no_sharing_separate_cache(backend: BackendType) -> None:
eq = "ab,bc,cd->"
views = build_views(eq)
expr = contract_expression(eq, *(v.shape for v in views))
print("-" * 40)
print("Without sharing:")
with shared_intermediates() as cache:
expr(*views, backend=backend)
expected = count_cached_ops(cache)
expected.update(count_cached_ops(cache)) # we expect double
print("-" * 40)
print("With sharing:")
with shared_intermediates() as cache1:
expr(*views, backend=backend)
actual = count_cached_ops(cache1)
with shared_intermediates() as cache2:
expr(*views, backend=backend)
actual.update(count_cached_ops(cache2))
print("-" * 40)
print(f"Without sharing: {expected} expressions")
print(f"With sharing: {actual} expressions")
assert actual == expected
@pytest.mark.parametrize("backend", backends)
def test_sharing_nesting(backend: BackendType) -> None:
eqs = ["ab,bc,cd->a", "ab,bc,cd->b", "ab,bc,cd->c", "ab,bc,cd->c"]
views = build_views(eqs[0])
shapes = [v.shape for v in views]
refs: Any = weakref.WeakValueDictionary()
def method1(views):
with shared_intermediates():
w = contract_expression(eqs[0], *shapes)(*views, backend=backend)
x = contract_expression(eqs[2], *shapes)(*views, backend=backend)
result = contract_expression("a,b->", w.shape, x.shape)(w, x, backend=backend)
refs["w"] = w
refs["x"] = x
del w, x
assert "w" in refs
assert "x" in refs
assert "w" not in refs, "cache leakage"
assert "x" not in refs, "cache leakage"
return result
def method2(views):
with shared_intermediates():
y = contract_expression(eqs[2], *shapes)(*views, backend=backend)
z = contract_expression(eqs[3], *shapes)(*views, backend=backend)
refs["y"] = y
refs["z"] = z
result = contract_expression("c,d->", y.shape, z.shape)(y, z, backend=backend)
result = result + method1(views) # nest method1 in method2
del y, z
assert "y" in refs
assert "z" in refs
assert "y" not in refs
assert "z" not in refs
method1(views)
method2(views)
@pytest.mark.parametrize("eq", equations)
@pytest.mark.parametrize("backend", backends)
def test_sharing_modulo_commutativity(eq: str, backend: BackendType) -> None:
ops = tuple(to_backend[backend](x) for x in build_views(eq))
inputs, output, _ = parse_einsum_input([eq] + list(ops))
inputs_list = inputs.split(",")
print("-" * 40)
print("Without sharing:")
with shared_intermediates() as cache:
_einsum(eq, *ops, backend=backend)
expected = count_cached_ops(cache)
print("-" * 40)
print("With sharing:")
with shared_intermediates() as cache:
for permuted in itertools.permutations(zip(inputs_list, ops)):
permuted_inputs = [p[0] for p in permuted]
permuted_ops = [p[1] for p in permuted]
permuted_eq = "{}->{}".format(",".join(permuted_inputs), output)
_einsum(permuted_eq, *permuted_ops, backend=backend)
actual = count_cached_ops(cache)
print("-" * 40)
print(f"Without sharing: {expected} expressions")
print(f"With sharing: {actual} expressions")
assert actual == expected
@pytest.mark.parametrize("backend", backends)
def test_partial_sharing(backend: BackendType) -> None:
eq = "ab,bc,de->"
x, y, z1 = build_views(eq) # type: ignore
z2 = 2.0 * z1 - 1.0
expr = contract_expression(eq, x.shape, y.shape, z1.shape)
print("-" * 40)
print("Without sharing:")
num_exprs_nosharing: Any = Counter()
with shared_intermediates() as cache:
expr(x, y, z1, backend=backend)
num_exprs_nosharing.update(count_cached_ops(cache))
with shared_intermediates() as cache:
expr(x, y, z2, backend=backend)
num_exprs_nosharing.update(count_cached_ops(cache))
print("-" * 40)
print("With sharing:")
with shared_intermediates() as cache:
expr(x, y, z1, backend=backend)
expr(x, y, z2, backend=backend)
num_exprs_sharing = count_cached_ops(cache)
print("-" * 40)
print(f"Without sharing: {num_exprs_nosharing} expressions")
print(f"With sharing: {num_exprs_sharing} expressions")
assert num_exprs_nosharing["einsum"] > num_exprs_sharing["einsum"]
@pytest.mark.parametrize("backend", backends)
def test_sharing_with_constants(backend: BackendType) -> None:
inputs = "ij,jk,kl"
outputs = "ijkl"
equations = [f"{inputs}->{output}" for output in outputs]
shapes = (2, 3), (3, 4), (4, 5)
constants = {0, 2}
ops = [np.random.rand(*shp) if i in constants else shp for i, shp in enumerate(shapes)]
var = np.random.rand(*shapes[1])
expected = [contract_expression(eq, *shapes)(ops[0], var, ops[2]) for eq in equations]
with shared_intermediates():
actual = [contract_expression(eq, *ops, constants=constants)(var) for eq in equations]
for dim, expected_dim, actual_dim in zip(outputs, expected, actual):
assert np.allclose(expected_dim, actual_dim), f"error at {dim}"
@pytest.mark.parametrize("size", [3, 4, 5])
@pytest.mark.parametrize("backend", backends)
def test_chain(size: int, backend: BackendType) -> None:
xs = [np.random.rand(2, 2) for _ in range(size)]
shapes = [x.shape for x in xs]
alphabet = "".join(get_symbol(i) for i in range(size + 1))
names = [alphabet[i : i + 2] for i in range(size)]
inputs = ",".join(names)
with shared_intermediates():
print(inputs)
for i in range(size + 1):
target = alphabet[i]
eq = f"{inputs}->{target}"
path_info = contract_path(eq, *xs)
print(path_info[1])
expr = contract_expression(eq, *shapes)
expr(*xs, backend=backend)
print("-" * 40)
@pytest.mark.parametrize("size", [3, 4, 5, 10])
@pytest.mark.parametrize("backend", backends)
def test_chain_2(size: int, backend: BackendType) -> None:
xs = [np.random.rand(2, 2) for _ in range(size)]
shapes = [x.shape for x in xs]
alphabet = "".join(get_symbol(i) for i in range(size + 1))
names = [alphabet[i : i + 2] for i in range(size)]
inputs = ",".join(names)
with shared_intermediates():
print(inputs)
for i in range(size):
target = alphabet[i : i + 2]
eq = f"{inputs}->{target}"
path_info = contract_path(eq, *xs)
print(path_info[1])
expr = contract_expression(eq, *shapes)
expr(*xs, backend=backend)
print("-" * 40)
def _compute_cost(cache):
counts = count_cached_ops(cache)
return counts["einsum"] + counts["tensordot"]
@pytest.mark.parametrize("backend", backends)
def test_chain_2_growth(backend: BackendType) -> None:
sizes = list(range(1, 21))
costs = []
for size in sizes:
xs = [np.random.rand(2, 2) for _ in range(size)]
alphabet = "".join(get_symbol(i) for i in range(size + 1))
names = [alphabet[i : i + 2] for i in range(size)]
inputs = ",".join(names)
with shared_intermediates() as cache:
for i in range(size):
target = alphabet[i : i + 2]
eq = f"{inputs}->{target}"
expr = contract_expression(eq, *(x.shape for x in xs))
expr(*xs, backend=backend)
costs.append(_compute_cost(cache))
print(f"sizes = {repr(sizes)}")
print(f"costs = {repr(costs)}")
for size, cost in zip(sizes, costs):
print(f"{size}\t{cost}")
@pytest.mark.parametrize("size", [3, 4, 5])
@pytest.mark.parametrize("backend", backends)
def test_chain_sharing(size: int, backend: BackendType) -> None:
xs = [np.random.rand(2, 2) for _ in range(size)]
alphabet = "".join(get_symbol(i) for i in range(size + 1))
names = [alphabet[i : i + 2] for i in range(size)]
inputs = ",".join(names)
num_exprs_nosharing = 0
for i in range(size + 1):
with shared_intermediates() as cache:
target = alphabet[i]
eq = f"{inputs}->{target}"
expr = contract_expression(eq, *tuple(x.shape for x in xs))
expr(*xs, backend=backend)
num_exprs_nosharing += _compute_cost(cache)
with shared_intermediates() as cache:
print(inputs)
for i in range(size + 1):
target = alphabet[i]
eq = f"{inputs}->{target}"
path_info = contract_path(eq, *xs)
print(path_info[1])
expr = contract_expression(eq, *[x.shape for x in xs])
expr(*xs, backend=backend)
num_exprs_sharing = _compute_cost(cache)
print("-" * 40)
print(f"Without sharing: {num_exprs_nosharing} expressions")
print(f"With sharing: {num_exprs_sharing} expressions")
assert num_exprs_nosharing > num_exprs_sharing
def test_multithreaded_sharing() -> None:
from multiprocessing.pool import ThreadPool
def fn():
x, y, z = build_views("ab,bc,cd")
with shared_intermediates():
contract("ab,bc,cd->a", x, y, z)
contract("ab,bc,cd->b", x, y, z)
return len(get_sharing_cache())
expected = fn()
pool = ThreadPool(8)
fs = [pool.apply_async(fn) for _ in range(16)]
assert not currently_sharing()
assert [f.get() for f in fs] == [expected] * 16
pool.close()
|