broadcast.hpp 2.76 KB
Newer Older
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
#ifndef MIGRAPHX_GUARD_OPERATORS_BROADCAST_HPP
#define MIGRAPHX_GUARD_OPERATORS_BROADCAST_HPP

#include <array>
#include <migraphx/operation.hpp>
#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>
#include <cmath>
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

/// The broadcast operator performs the numpy-style broadcasting of an axis of a given tensor. This
/// is achieved primarily by setting the stride of the broadcasted axis to zero. Linear indicies are
/// computed from multi-indicies by computing the inner product on the multi-index with the strides.
/// For example, if we have a tensor A(2,3) it has lengths of (2,3) and strides of (3,1). If we want
/// to compute the linear offset that corresponds to the element on the 2nd row (i = 1) and 3rd
/// column (j = 2), we compute the following inner product (1,2) dot (3, 1) = 1*3 + 2*1 = 5. It is
/// obvious from there that we can negate the effects of a given axis by setting the stride of that
/// axis to zero.
struct broadcast
{
    uint64_t axis = 0;
30
    std::vector<std::size_t> broadcast_lens;
31
32
33
34

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Shucai Xiao's avatar
Shucai Xiao committed
35
        return pack(f(self.axis, "axis"), f(self.broadcast_lens, "dims"));
36
37
38
39
40
41
42
43
    }

    std::string name() const { return "broadcast"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
        auto t     = inputs.at(0).type();
        auto input = inputs.at(0);

44
        std::vector<size_t> bcast_strides(broadcast_lens.size(), 0);
45

Shucai Xiao's avatar
Shucai Xiao committed
46
47
        if(std::all_of(
               broadcast_lens.cbegin(), broadcast_lens.cend(), [&](auto x) { return x == 1; }))
48
49
        {
            if(axis != 0)
50
                MIGRAPHX_THROW("BROADCAST: when broadcasting tensor of size 1, axis should be 0");
51
            return {t, broadcast_lens, std::move(bcast_strides)};
52
53
54
        }
        else
        {
55
            assert(broadcast_lens.size() - axis >= input.lens().size());
Shucai Xiao's avatar
Shucai Xiao committed
56
            if(!std::equal(input.lens().begin(), input.lens().end(), broadcast_lens.begin() + axis))
57
                MIGRAPHX_THROW("BROADCAST: when broadcasting success sizes must match");
58
            std::copy(input.strides().begin(), input.strides().end(), bcast_strides.begin() + axis);
59
            return {t, broadcast_lens, std::move(bcast_strides)};
60
61
62
63
64
65
        }
    }
    argument compute(shape output_shape, std::vector<argument> args) const
    {
        return {std::move(output_shape), std::move(args.at(0).data)};
    }
Paul's avatar
Paul committed
66
    std::ptrdiff_t output_alias(const std::vector<shape>&) const { return 0; }
67
68
69
70
71
72
73
};

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

#endif