File: test_cotengrust.py

package info (click to toggle)
python-cotengrust 0.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 252 kB
  • sloc: python: 195; makefile: 17
file content (235 lines) | stat: -rw-r--r-- 5,940 bytes parent folder | download
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
import pytest

try:
    import cotengra as ctg

    ctg_missing = False
except ImportError:
    ctg_missing = True
    ctg = None

import cotengrust as ctgr


requires_cotengra = pytest.mark.skipif(ctg_missing, reason="requires cotengra")


@pytest.mark.parametrize("which", ["greedy", "optimal"])
def test_basic_call(which):
    inputs = [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]
    output = ('b', 'd')
    size_dict = {'a': 2, 'b': 3, 'c': 4, 'd': 5}
    path = {
        "greedy": ctgr.optimize_greedy,
        "optimal": ctgr.optimize_optimal,
    }[
        which
    ](inputs, output, size_dict)
    assert all(len(con) <= 2 for con in path)


def find_output_str(lhs):
    tmp_lhs = lhs.replace(",", "")
    return "".join(s for s in sorted(set(tmp_lhs)) if tmp_lhs.count(s) == 1)


def eq_to_inputs_output(eq):
    if "->" not in eq:
        eq += "->" + find_output_str(eq)
    inputs, output = eq.split("->")
    inputs = inputs.split(",")
    inputs = [list(s) for s in inputs]
    output = list(output)
    return inputs, output


def get_rand_size_dict(inputs, d_min=2, d_max=3):
    import random

    size_dict = {}
    for term in inputs:
        for ix in term:
            if ix not in size_dict:
                size_dict[ix] = random.randint(d_min, d_max)
    return size_dict


# these are taken from opt_einsum
test_case_eqs = [
    # Test single-term equations
    "->",
    "a->a",
    "ab->ab",
    "ab->ba",
    "abc->bca",
    "abc->b",
    "baa->ba",
    "aba->b",
    # Test scalar-like operations
    "a,->a",
    "ab,->ab",
    ",ab,->ab",
    ",,->",
    # Test hadamard-like products
    "a,ab,abc->abc",
    "a,b,ab->ab",
    # Test index-transformations
    "ea,fb,gc,hd,abcd->efgh",
    "ea,fb,abcd,gc,hd->efgh",
    "abcd,ea,fb,gc,hd->efgh",
    # Test complex contractions
    "acdf,jbje,gihb,hfac,gfac,gifabc,hfac",
    "cd,bdhe,aidb,hgca,gc,hgibcd,hgac",
    "abhe,hidj,jgba,hiab,gab",
    "bde,cdh,agdb,hica,ibd,hgicd,hiac",
    "chd,bde,agbc,hiad,hgc,hgi,hiad",
    "chd,bde,agbc,hiad,bdi,cgh,agdb",
    "bdhe,acad,hiab,agac,hibd",
    # Test collapse
    "ab,ab,c->",
    "ab,ab,c->c",
    "ab,ab,cd,cd->",
    "ab,ab,cd,cd->ac",
    "ab,ab,cd,cd->cd",
    "ab,ab,cd,cd,ef,ef->",
    # Test outer prodcuts
    "ab,cd,ef->abcdef",
    "ab,cd,ef->acdf",
    "ab,cd,de->abcde",
    "ab,cd,de->be",
    "ab,bcd,cd->abcd",
    "ab,bcd,cd->abd",
    # Random test cases that have previously failed
    "eb,cb,fb->cef",
    "dd,fb,be,cdb->cef",
    "bca,cdb,dbf,afc->",
    "dcc,fce,ea,dbf->ab",
    "fdf,cdd,ccd,afe->ae",
    "abcd,ad",
    "ed,fcd,ff,bcf->be",
    "baa,dcf,af,cde->be",
    "bd,db,eac->ace",
    "fff,fae,bef,def->abd",
    "efc,dbc,acf,fd->abe",
    # Inner products
    "ab,ab",
    "ab,ba",
    "abc,abc",
    "abc,bac",
    "abc,cba",
    # GEMM test cases
    "ab,bc",
    "ab,cb",
    "ba,bc",
    "ba,cb",
    "abcd,cd",
    "abcd,ab",
    "abcd,cdef",
    "abcd,cdef->feba",
    "abcd,efdc",
    # Inner than dot
    "aab,bc->ac",
    "ab,bcc->ac",
    "aab,bcc->ac",
    "baa,bcc->ac",
    "aab,ccb->ac",
    # Randomly built test caes
    "aab,fa,df,ecc->bde",
    "ecb,fef,bad,ed->ac",
    "bcf,bbb,fbf,fc->",
    "bb,ff,be->e",
    "bcb,bb,fc,fff->",
    "fbb,dfd,fc,fc->",
    "afd,ba,cc,dc->bf",
    "adb,bc,fa,cfc->d",
    "bbd,bda,fc,db->acf",
    "dba,ead,cad->bce",
    "aef,fbc,dca->bde",
]


@requires_cotengra
@pytest.mark.parametrize("eq", test_case_eqs)
@pytest.mark.parametrize("which", ["greedy", "optimal"])
def test_manual_cases(eq, which):
    inputs, output = eq_to_inputs_output(eq)
    size_dict = get_rand_size_dict(inputs)
    path = {
        "greedy": ctgr.optimize_greedy,
        "optimal": ctgr.optimize_optimal,
    }[
        which
    ](inputs, output, size_dict)
    assert all(len(con) <= 2 for con in path)
    tree = ctg.ContractionTree.from_path(
        inputs, output, size_dict, path=path, check=True
    )
    assert tree.is_complete()


@requires_cotengra
@pytest.mark.parametrize("seed", range(10))
@pytest.mark.parametrize("which", ["greedy", "optimal"])
def test_basic_rand(seed, which):
    inputs, output, shapes, size_dict = ctg.utils.rand_equation(
        n=10,
        reg=4,
        n_out=2,
        n_hyper_in=1,
        n_hyper_out=1,
        d_min=2,
        d_max=3,
        seed=seed,
    )
    path = {
        "greedy": ctgr.optimize_greedy,
        "optimal": ctgr.optimize_optimal,
    }[
        which
    ](inputs, output, size_dict)
    assert all(len(con) <= 2 for con in path)
    tree = ctg.ContractionTree.from_path(
        inputs, output, size_dict, path=path, check=True
    )
    assert tree.is_complete()


@requires_cotengra
def test_optimal_lattice_eq():
    inputs, output, _, size_dict = ctg.utils.lattice_equation(
        [4, 5], d_max=2, seed=42
    )

    path = ctgr.optimize_optimal(inputs, output, size_dict, minimize='flops')
    tree = ctg.ContractionTree.from_path(
        inputs, output, size_dict, path=path
    )
    assert tree.is_complete()
    assert tree.contraction_cost() == 964

    path = ctgr.optimize_optimal(inputs, output, size_dict, minimize='size')
    assert all(len(con) <= 2 for con in path)
    tree = ctg.ContractionTree.from_path(
        inputs, output, size_dict, path=path
    )
    assert tree.contraction_width() == pytest.approx(5)


@requires_cotengra
def test_optimize_random_greedy_log_flops():
    inputs, output, _, size_dict = ctg.utils.lattice_equation(
        [10, 10], d_max=3, seed=42
    )

    path, cost1 = ctgr.optimize_random_greedy_track_flops(
        inputs, output, size_dict, ntrials=4, seed=42
    )
    _, cost2 = ctgr.optimize_random_greedy_track_flops(
        inputs, output, size_dict, ntrials=4, seed=42
    )
    assert cost1 == cost2
    tree = ctg.ContractionTree.from_path(
        inputs, output, size_dict, path=path
    )
    assert tree.is_complete()
    assert tree.contraction_cost(log=10) == pytest.approx(cost1)