broadcast.hpp 5.17 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
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
25
26
27
#ifndef MIGRAPHX_GUARD_OPERATORS_BROADCAST_HPP
#define MIGRAPHX_GUARD_OPERATORS_BROADCAST_HPP

#include <migraphx/check_shapes.hpp>
28
#include <migraphx/argument.hpp>
29
#include <migraphx/config.hpp>
charlie's avatar
charlie committed
30
#include <migraphx/common.hpp>
31
32
33
34
35

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

charlie's avatar
initial  
charlie committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * 1 input version:
 * Broadcasts a tensor from the original shape to the broadcast_lens by setting the stride of
 * broadcasted dimensions to zero. `axis` attribute for a 1D input shape is the output dimension
 * that stays the same. ex: broadcasting shape [1024] -> [4, 1024, 3] has axis = 1 For higher rank
 * input shapes, axis is an offset parameter for the broadcasting. Such that this operator would
 * work in the opposite direction of NumPy broadcasting. ex: broadcasting shape [2, 2] -> [2, 2, 3]
 * with axis = 0
 *
 * 2 input version:
 * Broadcast the first input 1D shape into the second input shape based on the axis parameter.
 * Handles broadcasting a 1D fixed shape into a higher rank dynamic shape.
 * broadcast_lens is not used
 */
50
51
struct broadcast
{
charlie's avatar
initial  
charlie committed
52
53
    uint64_t axis                           = 0;
    std::vector<std::size_t> broadcast_lens = {};
54
55
56
57

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
58
        return pack(f(self.axis, "axis"), f(self.broadcast_lens, "out_lens"));
59
60
61
62
63
    }

    std::string name() const { return "broadcast"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
charlie's avatar
initial  
charlie committed
64
        check_shapes{inputs, *this, true}.has(1, 2);
charlie's avatar
charlie committed
65
        auto s0 = inputs.at(0);
charlie's avatar
charlie committed
66
        auto t  = s0.type();
charlie's avatar
initial  
charlie committed
67
        if(inputs.size() == 1)
charlie's avatar
charlie committed
68
        {
charlie's avatar
initial  
charlie committed
69
70
71
72
73
74
75
76
            // the ONNX broadcast op is deprecated now, so not handling the negative
            // value of axis anymore
            if(axis >= broadcast_lens.size())
                MIGRAPHX_THROW("BROADCAST : axis is out of range");
            if(broadcast_lens.size() - axis < s0.lens().size())
                MIGRAPHX_THROW("BROADCAST: (broadcast ndims - axis) is less than s0 ndims");
            if(not std::equal(s0.lens().begin(), s0.lens().end(), broadcast_lens.begin() + axis))
                MIGRAPHX_THROW("BROADCAST: when broadcasting, succeeding sizes must match");
77

charlie's avatar
initial  
charlie committed
78
79
80
81
82
83
            std::vector<size_t> bcast_strides(broadcast_lens.size(), 0);
            std::copy(s0.strides().begin(), s0.strides().end(), bcast_strides.begin() + axis);
            shape output{t, broadcast_lens, std::move(bcast_strides)};
            if(output.elements() < s0.elements())
                MIGRAPHX_THROW("BROADCAST: output size must be greater than or equal to s0 size");
            return output;
84
        }
charlie's avatar
initial  
charlie committed
85
        else
charlie's avatar
charlie committed
86
        {
charlie's avatar
initial  
charlie committed
87
88
89
90
91
92
93
94
95
96
97
98
            // two inputs
            auto s1 = inputs.at(1);
            if(s0.dynamic())
                MIGRAPHX_THROW("BROADCAST_2in: s0 is a static shape, does not handle broadcasting "
                               "a static shape");
            if(s0.ndim() != 1)
                MIGRAPHX_THROW("BROADCAST_2in: s0 has ndim " + migraphx::to_string(s0.ndim()) +
                               ", only handle ndim = 1");
            if(axis > s1.ndim())
                MIGRAPHX_THROW("BROADCAST_2in: axis is out of range");
            if(s1.ndim() - axis < s0.ndim())
                MIGRAPHX_THROW("BROADCAST_2in: (s1_ndim - axis) is less than s0 ndim");
99

charlie's avatar
initial  
charlie committed
100
101
102
103
104
105
106
107
108
109
110
            if(s1.dynamic())
                return s1;

            std::vector<size_t> bcast_strides(s1.ndim(), 0);
            std::copy(s0.strides().begin(), s0.strides().end(), bcast_strides.begin() + axis);
            shape output{t, s1.lens(), std::move(bcast_strides)};
            if(output.elements() < s0.elements())
                MIGRAPHX_THROW(
                    "BROADCAST_2in: output size must be greater than or equal to s0 size");
            return output;
        }
111
    }
charlie's avatar
charlie committed
112

113
114
    argument compute(shape output_shape, std::vector<argument> args) const
    {
Paul Fultz II's avatar
Paul Fultz II committed
115
        return args[0].reshape(output_shape);
116
    }
charlie's avatar
charlie committed
117

Paul's avatar
Paul committed
118
    std::ptrdiff_t output_alias(const std::vector<shape>&) const { return 0; }
119
120
121
122
123
124
125
};

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

#endif