3 # Any copyright is dedicated to the Public Domain.
4 # https://creativecommons.org/publicdomain/zero/1.0/
6 # Written by Francois Fleuret <francois@fleuret.org>
13 def eingather(op, src, *indexes):
14 s_src, s_dst = re.search("^([^ ]*) *-> *(.*)", op).groups()
15 s_indexes = re.findall("\(([^)]*)\)", s_src)
16 s_src = re.sub("\([^)]*\)", "_", s_src)
18 shape = tuple(src.size(s_src.index(v)) for v in s_dst)
23 for i in range(src.dim()):
26 index, s_index = indexes[n_index], s_indexes[n_index]
31 for i in range(index.dim()):
35 torch.arange(index.size(i))
36 .reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1))
41 index = index[sub_idx]
46 torch.arange(src.size(i))
47 .reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1))
55 #######################
57 src = torch.rand(3, 5, 7, 11, 13)
58 index1 = torch.randint(src.size(2), (src.size(4), src.size(1), src.size(4)))
59 index2 = torch.randint(src.size(3), (src.size(1),))
61 # I want result[a, c, e] = src[c, a, index1[e, a, e], index2[a], e]
63 result = eingather("ca(eae)(a)e -> ace", src, index1, index2)
69 for a in range(result.size(0)):
70 for c in range(result.size(1)):
71 for e in range(result.size(2)):
72 error += (result[a, c, e] - src[c, a, index1[e, a, e], index2[a], e]).abs()