operators.hpp 5.14 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 <cmath>
Paul's avatar
Paul committed
7

Paul's avatar
Paul committed
8
9
namespace rtg {

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

Paul's avatar
Paul committed
15
struct convolution
Paul's avatar
Paul committed
16
{
Paul's avatar
Paul committed
17
18
19
    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
20
21
    std::string name() const
    {
Paul's avatar
Paul committed
22
23
        return "convolution[padding={" + to_string(padding) + "}, stride={" + to_string(stride) +
               "}, dilation={" + to_string(dilation) + "}]";
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
60
61
62
63
64

    friend std::ostream & operator<<(std::ostream & os, const convolution & op)
    {
        os << op.name();
        return os;
    }
Paul's avatar
Paul committed
65
66
};

Paul's avatar
Paul committed
67
struct pooling
Paul's avatar
Paul committed
68
69
{
    std::string mode;
Paul's avatar
Paul committed
70
71
72
    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
73
74
    std::string name() const
    {
Paul's avatar
Paul committed
75
76
        return "pooling:" + mode + "[padding={" + to_string(padding) + "}, stride={" +
               to_string(stride) + "}, lengths={" + to_string(lengths) + "}]";
Paul's avatar
Paul committed
77
78
79
    }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
80
        if(inputs.empty())
Paul's avatar
Paul committed
81
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
82
83
        const shape& input = inputs.at(0);
        if(input.lens().size() != 4)
Paul's avatar
Paul committed
84
            RTG_THROW("Only 4d pooling supported");
Paul's avatar
Paul committed
85
86

        auto t = input.type();
Paul's avatar
Paul committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
        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
102
    }
Paul's avatar
Paul committed
103

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

    friend std::ostream & operator<<(std::ostream & os, const pooling & op)
    {
        os << op.name();
        return os;
    }
Paul's avatar
Paul committed
111
112
};

Paul's avatar
Paul committed
113
struct activation
Paul's avatar
Paul committed
114
115
{
    std::string mode;
Paul's avatar
Paul committed
116
    std::string name() const { return "activation:" + mode; }
Paul's avatar
Paul committed
117
118
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
119
        if(inputs.empty())
Paul's avatar
Paul committed
120
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
121
122
        return inputs.front();
    }
Paul's avatar
Paul committed
123

Paul's avatar
Paul committed
124
    argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
125
126
127
128
129
    friend std::ostream & operator<<(std::ostream & os, const activation & op)
    {
        os << op.name();
        return os;
    }
Paul's avatar
Paul committed
130
131
};

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

Paul's avatar
Paul committed
155
    argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
156
157
158
159
160
161

    friend std::ostream & operator<<(std::ostream & os, const reshape & op)
    {
        os << op.name();
        return os;
    }
Paul's avatar
Paul committed
162
163
};

Paul's avatar
Paul committed
164
165
166
} // namespace rtg

#endif