broadcast.hpp 6.1 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/dyn_output.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
/**
 * 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.
charlie's avatar
charlie committed
47
 * Handles broadcasting a 1D static shape into a higher rank dynamic shape.
charlie's avatar
initial  
charlie committed
48
49
 * 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
            // the ONNX broadcast op is deprecated now, so not handling the negative
            // value of axis anymore
            if(axis >= broadcast_lens.size())
charlie's avatar
charlie committed
72
            {
charlie's avatar
charlie committed
73
74
                MIGRAPHX_THROW("BROADCAST : axis " + migraphx::to_string(axis) +
                               " is out of range");
charlie's avatar
charlie committed
75
            }
charlie's avatar
initial  
charlie committed
76
            if(broadcast_lens.size() - axis < s0.lens().size())
charlie's avatar
charlie committed
77
            {
charlie's avatar
initial  
charlie committed
78
                MIGRAPHX_THROW("BROADCAST: (broadcast ndims - axis) is less than s0 ndims");
charlie's avatar
charlie committed
79
            }
charlie's avatar
initial  
charlie committed
80
            if(not std::equal(s0.lens().begin(), s0.lens().end(), broadcast_lens.begin() + axis))
charlie's avatar
charlie committed
81
            {
charlie's avatar
initial  
charlie committed
82
                MIGRAPHX_THROW("BROADCAST: when broadcasting, succeeding sizes must match");
charlie's avatar
charlie committed
83
            }
84

charlie's avatar
initial  
charlie committed
85
86
87
88
            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())
charlie's avatar
charlie committed
89
            {
90
                // don't think this can occur?
charlie's avatar
initial  
charlie committed
91
                MIGRAPHX_THROW("BROADCAST: output size must be greater than or equal to s0 size");
charlie's avatar
charlie committed
92
            }
charlie's avatar
initial  
charlie committed
93
            return output;
94
        }
charlie's avatar
initial  
charlie committed
95
        else
charlie's avatar
charlie committed
96
        {
charlie's avatar
initial  
charlie committed
97
98
99
            // two inputs
            auto s1 = inputs.at(1);
            if(s0.dynamic())
charlie's avatar
charlie committed
100
101
102
103
            {
                MIGRAPHX_THROW("BROADCAST_2in: s0 is a dynamic shape, does not handle broadcasting "
                               "a dynamic shape");
            }
charlie's avatar
initial  
charlie committed
104
            if(s0.ndim() != 1)
charlie's avatar
charlie committed
105
            {
charlie's avatar
initial  
charlie committed
106
107
                MIGRAPHX_THROW("BROADCAST_2in: s0 has ndim " + migraphx::to_string(s0.ndim()) +
                               ", only handle ndim = 1");
charlie's avatar
charlie committed
108
109
110
            }
            if(axis >= s1.ndim())
            {
charlie's avatar
charlie committed
111
112
                MIGRAPHX_THROW("BROADCAST_2in: axis " + migraphx::to_string(axis) +
                               " is out of range");
charlie's avatar
charlie committed
113
            }
charlie's avatar
initial  
charlie committed
114
            if(s1.dynamic())
charlie's avatar
charlie committed
115
116
117
            {
                s0 = s0.to_dynamic();
                if(s0.dyn_dims()[0] != s1.dyn_dims()[axis])
charlie's avatar
charlie committed
118
                {
charlie's avatar
charlie committed
119
                    MIGRAPHX_THROW("BROADCAST_2in: s0 length doesn't match with dynamic s1 axis "
charlie's avatar
charlie committed
120
121
122
123
                                   "dimension length (" +
                                   migraphx::to_string(s0.dyn_dims()[0]) +
                                   " != " + migraphx::to_string(s1.dyn_dims()[axis]) + ")");
                }
charlie's avatar
initial  
charlie committed
124
                return s1;
charlie's avatar
charlie committed
125
            }
charlie's avatar
initial  
charlie committed
126

charlie's avatar
charlie committed
127
128
            if(s0.lens()[0] != s1.lens()[axis])
            {
charlie's avatar
charlie committed
129
130
                MIGRAPHX_THROW("BROADCAST_2in: s0 length doesn't match with static s1 axis "
                               "dimension length (" +
charlie's avatar
charlie committed
131
132
                               migraphx::to_string(s0.lens()[0]) +
                               " != " + migraphx::to_string(s1.lens()[axis]) + ")");
charlie's avatar
charlie committed
133
            }
charlie's avatar
initial  
charlie committed
134
135
136
137
138
            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)};
            return output;
        }
139
    }
charlie's avatar
charlie committed
140

charlie's avatar
charlie committed
141
    argument compute(const dyn_output& dyn_out, std::vector<argument> args) const
142
    {
charlie's avatar
charlie committed
143
        return args[0].reshape(dyn_out.computed_shape);
144
    }
charlie's avatar
charlie committed
145

Paul's avatar
Paul committed
146
    std::ptrdiff_t output_alias(const std::vector<shape>&) const { return 0; }
147
148
149
150
151
152
153
};

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

#endif