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, index):
14 s_src, s_dst = re.search("^([^ ]*) *-> *(.*)", op).groups()
15 s_index = re.search("\(([^)]*)\)", s_src).group(1)
16 s_src = re.sub("\([^)]*\)", "_", s_src)
18 shape = tuple(src.size(s_src.index(v)) for v in s_dst)
22 for i in range(index.dim()):
26 torch.arange(index.size(i))
27 .reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1))
36 for i in range(src.dim()):
43 torch.arange(src.size(i))
44 .reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1))
52 #######################
54 src = torch.rand(3, 5, 7, 11)
55 index = torch.randint(src.size(2), (src.size(3), src.size(1), src.size(3)))
57 # I want result[a,c,e]=src[c,a,index[e,a,e],e]
59 result = eingather("ca(eae)e -> ace", src, index)
65 for a in range(result.size(0)):
66 for c in range(result.size(1)):
67 for e in range(result.size(2)):
68 error += (result[a, c, e] - src[c, a, index[e, a, e], e]).abs()