Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmgray committed Oct 16, 2023
1 parent 2195e97 commit 824a553
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![autoray-header](https://github.com/jcmgray/autoray/assets/8982598/c5cb89bf-cc16-4345-8796-e0bd98dc2a15)

[![tests](https://github.com/jcmgray/autoray/actions/workflows/tests.yml/badge.svg)](https://github.com/jcmgray/autoray/actions/workflows/tests.yml) [![codecov](https://codecov.io/gh/jcmgray/autoray/branch/main/graph/badge.svg?token=Q5evNiuT9S)](https://codecov.io/gh/jcmgray/autoray) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/ba896d74c4954dd58da01df30c7bf326)](https://www.codacy.com/gh/jcmgray/autoray/dashboard?utm_source=github.com&utm_medium=referral&utm_content=jcmgray/autoray&utm_campaign=Badge_Grade) [![PyPI](https://img.shields.io/pypi/v/autoray?color=teal)](https://pypi.org/project/autoray/) [![Anaconda-Server Badge](https://anaconda.org/conda-forge/autoray/badges/version.svg)](https://anaconda.org/conda-forge/autoray)
[![tests](https://github.com/jcmgray/autoray/actions/workflows/tests.yml/badge.svg)](https://github.com/jcmgray/autoray/actions/workflows/tests.yml) [![codecov](https://codecov.io/gh/jcmgray/autoray/branch/main/graph/badge.svg?token=Q5evNiuT9S)](https://codecov.io/gh/jcmgray/autoray) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/ba896d74c4954dd58da01df30c7bf326)](https://app.codacy.com/gh/jcmgray/autoray/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) [![PyPI](https://img.shields.io/pypi/v/autoray?color=teal)](https://pypi.org/project/autoray/) [![Anaconda-Server Badge](https://anaconda.org/conda-forge/autoray/badges/version.svg)](https://anaconda.org/conda-forge/autoray)

[`autoray`](https://autoray.readthedocs.io/en/latest) is a lightweight python AUTOmatic-arRAY library for
abstracting your tensor operations. Primarily it provides an
Expand Down
2 changes: 1 addition & 1 deletion autoray/lazy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ def tensordot(a, b, axes=2):
def _basic_einsum_parse_input(operands):
# handle the basic, fully specified equation format
eq, *arrays = operands
lhs, rhs = eq.split('->')
lhs, rhs = eq.split("->")
return lhs, rhs, arrays


Expand Down
77 changes: 43 additions & 34 deletions tests/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def test_reshape_chain(backend):
assert l2.deps == (lx,)
assert_allclose(
to_numpy(lx.compute()).flatten(),
to_numpy(l2.compute()), atol=1e-6,
to_numpy(l2.compute()),
atol=1e-6,
)


Expand Down Expand Up @@ -494,23 +495,23 @@ def test_where():

def test_lazy_function_pytree_input_and_output():
inputs = {
'a': lazy.Variable(shape=(2, 3), backend="numpy"),
'b': lazy.Variable(shape=(3, 4), backend="numpy"),
"a": lazy.Variable(shape=(2, 3), backend="numpy"),
"b": lazy.Variable(shape=(3, 4), backend="numpy"),
}
outputs = {
'outa': do("tanh", inputs['a'] @ inputs['b']),
'outb': [inputs['a'] - 1, inputs['b'] - 1],
"outa": do("tanh", inputs["a"] @ inputs["b"]),
"outb": [inputs["a"] - 1, inputs["b"] - 1],
}
f = lazy.Function(inputs, outputs)

a = do("random.uniform", size=(2, 3), like="numpy")
b = do("random.uniform", size=(3, 4), like="numpy")

outs = f({'a': a, 'b': b})
outs = f({"a": a, "b": b})

assert_allclose(outs['outa'], do("tanh", a @ b))
assert_allclose(outs['outb'][0], a - 1)
assert_allclose(outs['outb'][1], b - 1)
assert_allclose(outs["outa"], do("tanh", a @ b))
assert_allclose(outs["outb"][0], a - 1)
assert_allclose(outs["outb"][1], b - 1)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -576,9 +577,9 @@ def test_getitem(indices, shape):


def random_indexer(ndim_min=0, ndim_max=10, d_min=1, d_max=5, seed=None):
"""Generate a random shape and valid indexing object into that shape.
"""
"""Generate a random shape and valid indexing object into that shape."""
import numpy as np

rng = np.random.default_rng(seed=seed)

ndim = rng.integers(ndim_min, ndim_max + 1)
Expand All @@ -595,43 +596,43 @@ def rand_adv_ix_broadcastable_shape():

shape = []
indexer = []
choices = ['index', 'slice', 'ellipsis', 'array', 'list', 'newaxis']
choices = ["index", "slice", "ellipsis", "array", "list", "newaxis"]

i = 0
while i < ndim:
kind = rng.choice(choices)

if kind == 'newaxis':
if kind == "newaxis":
indexer.append(None)
continue

d = rng.integers(d_min, d_max + 1)
shape.append(d)

if kind == 'index':
if kind == "index":
ix = rng.integers(-d, d)
if rng.random() > 0.5:
# randomly supply integers and numpy ints
ix = int(ix)

elif kind == 'ellipsis':
elif kind == "ellipsis":
# only one ellipsis allowed
ix = ...
choices.remove('ellipsis')
choices.remove("ellipsis")
# how many dims ellipsis should expand to
i += rng.integers(0, 4)

elif kind == 'slice':
elif kind == "slice":
start = rng.integers(-d - 2, d + 2)
stop = rng.integers(-d - 2, d - 2)
step = rng.choice([-3, -2, -1, 1, 2, 3])
ix = slice(start, stop, step)

elif kind == 'array':
elif kind == "array":
ai_shape = rand_adv_ix_broadcastable_shape()
ix = rng.integers(-d, d, size=ai_shape)

elif kind == 'list':
elif kind == "list":
ai_shape = rand_adv_ix_broadcastable_shape()
ix = rng.integers(-d, d, size=ai_shape).tolist()

Expand All @@ -640,7 +641,7 @@ def rand_adv_ix_broadcastable_shape():

if (len(indexer) == 1) and (rng.random() > 0.5):
# return the raw object
indexer, = indexer
(indexer,) = indexer
else:
indexer = tuple(indexer)

Expand All @@ -664,7 +665,7 @@ def test_lazy_getitem_random(seed):
((3,), (3,)),
((3,), (3, 2)),
((6, 5, 4, 3), (3,)),
((7, 6, 5, 4), (7, 6, 4, 3))
((7, 6, 5, 4), (7, 6, 4, 3)),
],
)
def test_matmul_shape(shape1, shape2):
Expand All @@ -680,12 +681,21 @@ def test_matmul_shape(shape1, shape2):

@pytest.mark.parametrize(
"shape1, shape2",
[((3,), (1,)),
((3,), (4, 3)),
((3,), (3, 2, 1)),
((2, 2, 3, 4), (1, 2, 4, 5,)),
((6, 5, 4), (6, 3, 3))
]
[
((3,), (1,)),
((3,), (4, 3)),
((3,), (3, 2, 1)),
(
(2, 2, 3, 4),
(
1,
2,
4,
5,
),
),
((6, 5, 4), (6, 3, 3)),
],
)
def test_matmul_shape_error(shape1, shape2):
a = lazy.Variable(shape=shape1)
Expand All @@ -701,10 +711,10 @@ def test_pytree_compute():
x = do("random.uniform", size=(5, 6), like="numpy")
lx = lazy.array(x)
lu, ls, lv = do("linalg.svd", lx)
lresults = {'u': lu,'s': ls,'v': lv}
lresults = {"u": lu, "s": ls, "v": lv}
results = lazy.compute(lresults)
assert isinstance(results, dict)
assert infer_backend(results['s']) == infer_backend(x)
assert infer_backend(results["s"]) == infer_backend(x)


def test_kron():
Expand All @@ -728,7 +738,7 @@ def test_kron():
assert lxy.shape == xy.shape
assert_allclose(lxy.compute(), xy)

x = do("random.uniform", size=(3 ,4, 5), like="numpy")
x = do("random.uniform", size=(3, 4, 5), like="numpy")
y = do("random.uniform", size=(3,), like="numpy")
xy = do("kron", x, y)

Expand All @@ -740,9 +750,9 @@ def test_kron():


def test_concatenate():
x = do("random.uniform", size=(3 ,4, 5), like="numpy")
y = do("random.uniform", size=(3 ,1, 5), like="numpy")
z = do("random.uniform", size=(3 ,7, 5), like="numpy")
x = do("random.uniform", size=(3, 4, 5), like="numpy")
y = do("random.uniform", size=(3, 1, 5), like="numpy")
z = do("random.uniform", size=(3, 7, 5), like="numpy")
xyz = do("concatenate", (x, y, z), axis=1)

lx = lazy.array(x)
Expand All @@ -752,4 +762,3 @@ def test_concatenate():

assert lxyz.shape == xyz.shape
assert_allclose(lxyz.compute(), xyz)

0 comments on commit 824a553

Please sign in to comment.