operators.hpp 4.6 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
5
#include <rtg/operand.hpp>
#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
    std::array<std::size_t, 2> padding  = {0, 0};
    std::array<std::size_t, 2> stride   = {1, 1};
Paul's avatar
Paul committed
19
20
21
    std::array<std::size_t, 2> dilation = {1, 1};
    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
};

Paul's avatar
Paul committed
61
struct pooling
Paul's avatar
Paul committed
62
63
64
{
    std::string mode;
    std::array<std::size_t, 2> padding = {0, 0};
Paul's avatar
Paul committed
65
    std::array<std::size_t, 2> stride  = {1, 1};
Paul's avatar
Paul committed
66
67
68
    std::array<std::size_t, 2> lengths = {1, 1};
    std::string name() const
    {
Paul's avatar
Paul committed
69
70
        return "pooling:" + mode + "[padding={" + to_string(padding) + "}, stride={" +
               to_string(stride) + "}, lengths={" + to_string(lengths) + "}]";
Paul's avatar
Paul committed
71
72
73
    }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
74
        if(inputs.empty())
Paul's avatar
Paul committed
75
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
76
77
        const shape& input = inputs.at(0);
        if(input.lens().size() != 4)
Paul's avatar
Paul committed
78
            RTG_THROW("Only 4d pooling supported");
Paul's avatar
Paul committed
79
80

        auto t = input.type();
Paul's avatar
Paul committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        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
96
    }
Paul's avatar
Paul committed
97

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

Paul's avatar
Paul committed
101
struct activation
Paul's avatar
Paul committed
102
103
{
    std::string mode;
Paul's avatar
Paul committed
104
    std::string name() const { return "activation:" + mode; }
Paul's avatar
Paul committed
105
106
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
107
        if(inputs.empty())
Paul's avatar
Paul committed
108
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
109
110
        return inputs.front();
    }
Paul's avatar
Paul committed
111

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

Paul's avatar
Paul committed
115
116
117
struct reshape
{
    std::vector<int64_t> dims;
Paul's avatar
Paul committed
118
    std::string name() const { return "reshape[dims={" + to_string(dims) + "}]"; }
Paul's avatar
Paul committed
119
120
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
121
        if(inputs.empty())
Paul's avatar
Paul committed
122
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
123
124
        auto&& idims = inputs.front().lens();
        std::vector<std::size_t> rdims(dims.begin(), dims.end());
Paul's avatar
Paul committed
125
        for(std::size_t i = 0; i < dims.size(); i++)
Paul's avatar
Paul committed
126
127
128
129
130
131
132
        {
            if(dims[i] == 0)
                rdims[i] = idims[i];
        }
        if(dims.back() == -1)
        {
            rdims.pop_back();
Paul's avatar
Paul committed
133
            std::copy(idims.begin() + rdims.size(), idims.end(), std::back_inserter(rdims));
Paul's avatar
Paul committed
134
135
136
137
        }
        return {inputs.front().type(), rdims};
    }

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

Paul's avatar
Paul committed
141
142
143
} // namespace rtg

#endif