From 172e910a892476ab757ce9a593e6fe25de605464 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fran=C3=A7ois=20Fleuret?= Date: Wed, 24 May 2023 15:59:03 +0200 Subject: [PATCH] Update. --- eingather.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 eingather.py diff --git a/eingather.py b/eingather.py new file mode 100755 index 0000000..2310c55 --- /dev/null +++ b/eingather.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +# Any copyright is dedicated to the Public Domain. +# https://creativecommons.org/publicdomain/zero/1.0/ + +# Written by Francois Fleuret + +import re, torch + +##################### + + +def eingather(op, src, index): + s_src, s_dst = re.search("^([^ ]*) *-> *(.*)", op).groups() + s_index = re.search("\(([^)]*)\)", s_src).group(1) + s_src = re.sub("\([^)]*\)", "_", s_src) + + shape = tuple(src.size(s_src.index(v)) for v in s_dst) + + idx = [] + + for i in range(index.dim()): + v = s_index[i] + j = s_dst.index(v) + a = ( + torch.arange(index.size(i)) + .reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1)) + .expand(shape) + ) + idx.append(a) + + index = index[idx] + + idx = [] + + for i in range(src.dim()): + v = s_src[i] + if v == "_": + idx.append(index) + else: + j = s_dst.index(v) + a = ( + torch.arange(src.size(i)) + .reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1)) + .expand(shape) + ) + idx.append(a) + + return src[idx] + + +####################### + +src = torch.rand(3, 5, 7, 11) +index = torch.randint(src.size(2), (src.size(3), src.size(1), src.size(3))) + +# I want result[a,c,e]=src[c,a,index[e,a,e],e] + +result = eingather("ca(eae)e -> ace", src, index) + +# Check + +error = 0 + +for a in range(result.size(0)): + for c in range(result.size(1)): + for e in range(result.size(2)): + error += (result[a, c, e] - src[c, a, index[e, a, e], e]).abs() + +print(error.item()) -- 2.20.1