convolution.hpp 3.24 KB
Newer Older
1
2
3
4
#ifndef MIGRAPHX_GUARD_OPERATORS_CONVOLUTION_HPP
#define MIGRAPHX_GUARD_OPERATORS_CONVOLUTION_HPP

#include <array>
5
#include <migraphx/op/common.hpp>
6
7
8
9
10
11
#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>
kahmed10's avatar
kahmed10 committed
12
13
#include <migraphx/value.hpp>
#include <migraphx/op/normalize_attribute.hpp>
14
15
16
17
18
19
20
21
22
#include <cmath>
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

struct convolution
{
23
24
25
    std::vector<std::size_t> padding  = {0, 0};
    std::vector<std::size_t> stride   = {1, 1};
    std::vector<std::size_t> dilation = {1, 1};
26
27

    int group                   = 1;
28
    padding_mode_t padding_mode = default_;
29
30
31
32
33
34
35

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.padding, "padding"),
                    f(self.stride, "stride"),
                    f(self.dilation, "dilation"),
36
37
                    f(self.group, "group"),
                    f(self.padding_mode, "padding_mode"));
38
39
40
    }

    std::string name() const { return "convolution"; }
kahmed10's avatar
kahmed10 committed
41
42

    void check_attribute_size() const
43
    {
kahmed10's avatar
kahmed10 committed
44
45
        if(not((padding.size() == stride.size() or (padding.size() / 2) == stride.size()) and
               stride.size() == dilation.size()))
46
        {
Shucai Xiao's avatar
Shucai Xiao committed
47
            MIGRAPHX_THROW("CONVOLUTION: inconsistent attribute sizes");
48
        }
kahmed10's avatar
kahmed10 committed
49
50
    }

kahmed10's avatar
kahmed10 committed
51
52
53
    value attributes() const { return {{"normalize_padding", "padding"}}; }

    shape normalize_compute_shape(std::vector<shape> inputs) const
kahmed10's avatar
kahmed10 committed
54
55
56
    {
        check_shapes{inputs, *this}.has(2).same_type().same_ndims().min_ndims(3);
        check_attribute_size();
Shucai Xiao's avatar
Shucai Xiao committed
57
        // dim num of input and attribute should match
kahmed10's avatar
kahmed10 committed
58
59
60
        auto input_size   = inputs[0].lens().size();
        auto padding_size = padding.size();
        if(not(input_size == padding_size / 2 + 2 or input_size == padding_size + 2))
Shucai Xiao's avatar
Shucai Xiao committed
61
62
63
        {
            MIGRAPHX_THROW("CONVOLUTION: input and attribute size mismatch!");
        }
64
65
66

        const shape& input   = inputs.at(0);
        const shape& weights = inputs.at(1);
kahmed10's avatar
kahmed10 committed
67
        size_t kdims         = input_size - 2;
kahmed10's avatar
kahmed10 committed
68
69
70
71
        if(kdims != this->kdims())
        {
            MIGRAPHX_THROW("convolution: input k-dims does not match attribute size");
        }
Khalique's avatar
Khalique committed
72

73
74
75
        if(input.lens().at(1) != (weights.lens().at(1) * group))
            MIGRAPHX_THROW("CONVOLUTION: Mismatch channel numbers");

76
77
78
79
        std::vector<size_t> output_lens{input.lens()[0], weights.lens()[0]};

        for(size_t i = 0; i < kdims; i++)
        {
kahmed10's avatar
kahmed10 committed
80
81
82
            auto padding_factor = 2 * padding[i];
            if(padding_size == 2 * kdims)
                padding_factor = padding[i] + padding[i + kdims];
83
84
85
            output_lens.push_back(std::size_t(std::max<std::ptrdiff_t>(
                1,
                (input.lens()[i + 2] - (1 + dilation[i] * (weights.lens()[i + 2] - 1)) +
kahmed10's avatar
kahmed10 committed
86
                 padding_factor) /
87
88
89
90
                        stride[i] +
                    1)));
        }

91
        return inputs[0].with_lens(output_lens);
92
    }
kahmed10's avatar
kahmed10 committed
93
94
95
96

    size_t kdims() const
    {
        check_attribute_size();
kahmed10's avatar
kahmed10 committed
97
        return stride.size();
kahmed10's avatar
kahmed10 committed
98
    }
99
100
101
102
103
104
105
};

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

#endif