scatter.hpp 3.54 KB
Newer Older
Shucai Xiao's avatar
Shucai Xiao committed
1
2
3
4
5
6
7
8
9
10
#ifndef MIGRAPHX_GUARD_OPERATORS_SCATTER_HPP
#define MIGRAPHX_GUARD_OPERATORS_SCATTER_HPP

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

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

20
21
22
23
24
25
26
27
28
29
30
// The scatter operator fetches a subset of data given by an index array and then performs a
// reduction operation (add, multiply, or just set the data) on each element returned.  We implement
// it as a separate derived struct for each of the three reduction methods.  The related operator
// scatterND is a generalization that works on a set of 3 tensors of different ranks.  The
// complementary operations are gather/gatherND.
//
// This is a template for deriving child structs from.  Each child needs to define
// only a reduction() method.  Names are automatically handled by the op_name template.

template <class Derived>
struct scatter : op_name<Derived>
Shucai Xiao's avatar
Shucai Xiao committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{
    int64_t axis = 0;

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

    value attributes() const
    {
        value normalize;
        normalize["axis"] = value::array{normalize_attribute::include_min};
        return {{"normalize_axes", normalize}};
    }

    shape normalize_compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(3).standard();
50
51
        // If non-packed, this converts to a packed output while preserving permutation of tensor
        return inputs.front().with_lens(inputs.front().lens());
Shucai Xiao's avatar
Shucai Xiao committed
52
53
54
55
56
    }

    argument compute(const shape& output_shape, std::vector<argument> args) const
    {
        argument result{output_shape};
57
58
59
        auto& self = static_cast<const Derived&>(*this);

        // max dimension in each axis
Shucai Xiao's avatar
Shucai Xiao committed
60
        auto axis_dim_size = output_shape.lens()[axis];
61
        // cast all arguments as correct type
Shucai Xiao's avatar
Shucai Xiao committed
62
        visit_all(result, args[0], args[2])([&](auto output, auto data, auto update) {
63
            // copy all of data to output
Shucai Xiao's avatar
Shucai Xiao committed
64
65
66
            std::copy(data.begin(), data.end(), output.begin());
            args[1].visit([&](auto indices) {
                auto ind_s = indices.get_shape();
67
                // iterate through items in shape
Shucai Xiao's avatar
Shucai Xiao committed
68
                shape_for_each(ind_s, [&](const auto& idx) {
69
70
71
72
73
74
75
76
77
                    auto out_idx = idx;

                    // Overloaded tensor_view::() invokes indexing logic of
                    // std::size_t shape::index(std::size_t i) const
                    // which handles nonstandard shapes correctly
                    auto index = indices(idx.begin(), idx.end());

                    // normalize negative indexes (may be redundant after using
                    // normalize_compute_shape())
Shucai Xiao's avatar
Shucai Xiao committed
78
                    index         = (index < 0) ? index + axis_dim_size : index;
Shucai Xiao's avatar
Shucai Xiao committed
79
                    out_idx[axis] = index;
80
81
82
83
84

                    // look up the appropriate locations in output, using idx and out_idx.
                    // call reduction() method of derived struct to copy and reduce that element
                    self.reduction()(output(out_idx.begin(), out_idx.end()),
                                     update(idx.begin(), idx.end()));
Shucai Xiao's avatar
Shucai Xiao committed
85
86
87
88
89
90
91
92
93
94
95
96
97
                });
            });
        });

        return result;
    }
};

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

#endif