gather.hpp 3.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#ifndef MIGRAPHX_GUARD_OPERATORS_GATHER_HPP
#define MIGRAPHX_GUARD_OPERATORS_GATHER_HPP

#include <array>
#include <migraphx/check_shapes.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/streamutils.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/shape_for_each.hpp>
#include <migraphx/config.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
11
12
#include <migraphx/value.hpp>
#include <migraphx/op/normalize_attribute.hpp>
13
14
15
16
17
18
19
20
21
#include <cmath>
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

struct gather
{
Shucai Xiao's avatar
Shucai Xiao committed
22
    int64_t axis = 0;
23
24
25
26
27
28
29

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.axis, "axis"));
    }

Shucai Xiao's avatar
Shucai Xiao committed
30
31
32
33
34
35
36
    value attributes() const
    {
        value normalize;
        normalize["axis"] = value::array{normalize_attribute::include_min};
        return {{"normalize_axes", normalize}};
    }

37
38
    std::string name() const { return "gather"; }

Shucai Xiao's avatar
Shucai Xiao committed
39
    shape normalize_compute_shape(std::vector<shape> inputs) const
40
    {
41
        check_shapes{inputs, *this}.has(2);
42
43
        auto lens = inputs[0].lens();
        auto type = inputs[0].type();
Shucai Xiao's avatar
Shucai Xiao committed
44
        lens.erase(lens.begin() + axis);
45
46
47
        if(!inputs[1].scalar())
        {
            auto ind_lens = inputs[1].lens();
Shucai Xiao's avatar
Shucai Xiao committed
48
            lens.insert(lens.begin() + axis, ind_lens.begin(), ind_lens.end());
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        }

        // for scalar output
        if(lens.empty())
        {
            return {type};
        }

        return {type, lens};
    }

    argument compute(const shape& output_shape, std::vector<argument> args) const
    {
        argument result{output_shape};
        // negative axis means counting dimensions from back
Shucai Xiao's avatar
Shucai Xiao committed
64
65
        auto lens                 = args[0].get_shape().lens();
        std::size_t axis_dim_size = lens[axis];
66
67
68
69
70
        // max dimension in axis
        visit_all(result, args[0])([&](auto output, auto data) {
            args[1].visit([&](auto indices) {
                if(output_shape.scalar())
                {
Shucai Xiao's avatar
Shucai Xiao committed
71
72
                    auto in_index = indices.front();
                    in_index      = (in_index < 0) ? in_index + axis_dim_size : in_index;
73
                    output[0]     = data[in_index];
74
75
76
                }
                else
                {
Shucai Xiao's avatar
Shucai Xiao committed
77
78
                    auto out_lens  = data.get_shape().lens();
                    out_lens[axis] = indices.get_shape().elements();
79
80
                    migraphx::shape out_comp_shape{data.get_shape().type(), out_lens};
                    shape_for_each(out_comp_shape, [&](const auto& out_idx) {
Shucai Xiao's avatar
Shucai Xiao committed
81
82
83
84
                        auto data_idx  = out_idx;
                        auto in_index  = indices[data_idx[axis]];
                        in_index       = (in_index < 0) ? in_index + axis_dim_size : in_index;
                        data_idx[axis] = in_index;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
                        output[out_comp_shape.index(out_idx.begin(), out_idx.end())] =
                            data(data_idx.begin(), data_idx.end());
                    });
                }
            });
        });

        return result;
    }
};

} // namespace op
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif