operators.hpp 5.37 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#ifndef RTG_GUARD_OPERATORS_HPP
#define RTG_GUARD_OPERATORS_HPP

Paul's avatar
Paul committed
4
#include <rtg/operation.hpp>
Paul's avatar
Paul committed
5
#include <rtg/stringutils.hpp>
Paul's avatar
Paul committed
6
#include <rtg/streamutils.hpp>
Paul's avatar
Paul committed
7
#include <cmath>
Paul's avatar
Paul committed
8

Paul's avatar
Paul committed
9
10
namespace rtg {

Paul's avatar
Paul committed
11
12
struct not_computable
{
Paul's avatar
Paul committed
13
    argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
14
15
};

Paul's avatar
Paul committed
16
struct convolution
Paul's avatar
Paul committed
17
{
Paul's avatar
Paul committed
18
19
20
    std::array<std::size_t, 2> padding  = {{0, 0}};
    std::array<std::size_t, 2> stride   = {{1, 1}};
    std::array<std::size_t, 2> dilation = {{1, 1}};
Paul's avatar
Paul committed
21
22
    std::string name() const
    {
Paul's avatar
Paul committed
23
        return "convolution";
Paul's avatar
Paul committed
24
25
26
    }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
27
        if(inputs.size() != 2)
Paul's avatar
Paul committed
28
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
29
        const shape& input   = inputs.at(0);
Paul's avatar
Paul committed
30
        const shape& weights = inputs.at(1);
Paul's avatar
Paul committed
31
        if(input.type() != weights.type())
Paul's avatar
Paul committed
32
            RTG_THROW("Type doesn't match");
Paul's avatar
Paul committed
33
        if(input.lens().size() != weights.lens().size())
Paul's avatar
Paul committed
34
            RTG_THROW("Dimensions don't match");
Paul's avatar
Paul committed
35
        if(input.lens().size() != 4)
Paul's avatar
Paul committed
36
            RTG_THROW("Only 4d convolution supported");
Paul's avatar
Paul committed
37
38

        auto t = input.type();
Paul's avatar
Paul committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        return {t,
                {
                    input.lens()[0],
                    weights.lens()[0],
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
                        (input.lens()[2] - (1 + dilation[0] * (weights.lens()[2] - 1)) +
                         2 * padding[0]) /
                                stride[0] +
                            1)),
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
                        (input.lens()[3] - (1 + dilation[1] * (weights.lens()[3] - 1)) +
                         2 * padding[1]) /
                                stride[1] +
                            1)),
                }};
Paul's avatar
Paul committed
56
    }
Paul's avatar
Paul committed
57

Paul's avatar
Paul committed
58
    argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
59

Paul's avatar
Paul committed
60
    friend std::ostream& operator<<(std::ostream& os, const convolution& op)
Paul's avatar
Paul committed
61
    {
Paul's avatar
Paul committed
62
63
64
65
66
        os << op.name() << "[";
        os << "padding={" << stream_range(op.padding) << "}, ";
        os << "stride={" << stream_range(op.stride) << "}, ";
        os << "dilation={" << stream_range(op.dilation) << "}";
        os << "]";
Paul's avatar
Paul committed
67
68
        return os;
    }
Paul's avatar
Paul committed
69
70
};

Paul's avatar
Paul committed
71
struct pooling
Paul's avatar
Paul committed
72
73
{
    std::string mode;
Paul's avatar
Paul committed
74
75
76
    std::array<std::size_t, 2> padding = {{0, 0}};
    std::array<std::size_t, 2> stride  = {{1, 1}};
    std::array<std::size_t, 2> lengths = {{1, 1}};
Paul's avatar
Paul committed
77
78
    std::string name() const
    {
Paul's avatar
Paul committed
79
        return "pooling";
Paul's avatar
Paul committed
80
81
82
    }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
83
        if(inputs.empty())
Paul's avatar
Paul committed
84
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
85
86
        const shape& input = inputs.at(0);
        if(input.lens().size() != 4)
Paul's avatar
Paul committed
87
            RTG_THROW("Only 4d pooling supported");
Paul's avatar
Paul committed
88
89

        auto t = input.type();
Paul's avatar
Paul committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
        return {t,
                {
                    input.lens()[0],
                    input.lens()[1],
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
                        std::ceil((input.lens()[3] + 2 * padding[0] - lengths[0]) /
                                  static_cast<float>(stride[0])) +
                            1)),
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
                        std::ceil((input.lens()[4] + 2 * padding[1] - lengths[1]) /
                                  static_cast<float>(stride[1])) +
                            1)),
                }};
Paul's avatar
Paul committed
105
    }
Paul's avatar
Paul committed
106

Paul's avatar
Paul committed
107
    argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
108

Paul's avatar
Paul committed
109
    friend std::ostream& operator<<(std::ostream& os, const pooling& op)
Paul's avatar
Paul committed
110
    {
Paul's avatar
Paul committed
111
112
113
114
115
        os << op.name() << "[";
        os << "padding={" << stream_range(op.padding) << "}, ";
        os << "stride={" << stream_range(op.stride) << "}, ";
        os << "lengths={" << stream_range(op.lengths) << "}";
        os << "]";
Paul's avatar
Paul committed
116
117
        return os;
    }
Paul's avatar
Paul committed
118
119
};

Paul's avatar
Paul committed
120
struct activation
Paul's avatar
Paul committed
121
122
{
    std::string mode;
Paul's avatar
Paul committed
123
    std::string name() const { return "activation"; }
Paul's avatar
Paul committed
124
125
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
126
        if(inputs.empty())
Paul's avatar
Paul committed
127
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
128
129
        return inputs.front();
    }
Paul's avatar
Paul committed
130

Paul's avatar
Paul committed
131
    argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
132
    friend std::ostream& operator<<(std::ostream& os, const activation& op)
Paul's avatar
Paul committed
133
    {
Paul's avatar
Paul committed
134
        os << op.name() << ":" << op.mode;
Paul's avatar
Paul committed
135
136
        return os;
    }
Paul's avatar
Paul committed
137
138
};

Paul's avatar
Paul committed
139
140
141
struct reshape
{
    std::vector<int64_t> dims;
Paul's avatar
Paul committed
142
    std::string name() const { return "reshape"; }
Paul's avatar
Paul committed
143
144
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
145
        if(inputs.empty())
Paul's avatar
Paul committed
146
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
147
148
        auto&& idims = inputs.front().lens();
        std::vector<std::size_t> rdims(dims.begin(), dims.end());
Paul's avatar
Paul committed
149
        for(std::size_t i = 0; i < dims.size(); i++)
Paul's avatar
Paul committed
150
151
152
153
154
155
156
        {
            if(dims[i] == 0)
                rdims[i] = idims[i];
        }
        if(dims.back() == -1)
        {
            rdims.pop_back();
Paul's avatar
Paul committed
157
            std::copy(idims.begin() + rdims.size(), idims.end(), std::back_inserter(rdims));
Paul's avatar
Paul committed
158
159
160
161
        }
        return {inputs.front().type(), rdims};
    }

Paul's avatar
Paul committed
162
    argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
163

Paul's avatar
Paul committed
164
    friend std::ostream& operator<<(std::ostream& os, const reshape& op)
Paul's avatar
Paul committed
165
    {
Paul's avatar
Paul committed
166
167
168
        os << op.name() << "[";
        os << "dims={" << stream_range(op.dims) << "}, ";
        os << "]";
Paul's avatar
Paul committed
169
170
        return os;
    }
Paul's avatar
Paul committed
171
172
};

Paul's avatar
Paul committed
173
174
175
} // namespace rtg

#endif