File size: 1,039 Bytes
a3f2e4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
from bitblas import tvm
from tvm import te
from tvm.tir import IndexMap
from tvm.contrib.dlpack import to_pytorch_func
import torch


def apply_transform_on_input(input: torch.Tensor, index_map: IndexMap) -> torch.Tensor:
    dtype = str(input.dtype).split(".")[1]
    inp = te.placeholder(input.shape, name="inp", dtype=dtype)
    args = [inp]
    arg = args[-1]

    def fcompute(*args):
        warp_i, warp_j = args[-2:]
        spatial_args = args[:-2]
        permutate_i, permutate_j = index_map.map_indices([warp_i, warp_j])
        new_index = (*spatial_args, permutate_i, permutate_j)
        return arg[new_index]

    out = te.compute(
        input.shape,
        fcompute,
        name="permutate",
    )
    args.append(out)
    func = te.create_prim_func(args)
    rt_mod = tvm.build(func, target="llvm", name="permutate")
    output = torch.zeros_like(input)
    torch_func = to_pytorch_func(rt_mod)
    torch_func(input, output)

    return output