"backends/v3/src/client/sharded_client.rs" did not exist on "ac8c0f6fe4c0c061e0f858242ee61dfa090c32a6"
operators.hpp 5.38 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(shape, 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
    std::string name() const { return "convolution"; }
Paul's avatar
Paul committed
22
23
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
24
        if(inputs.size() != 2)
Paul's avatar
Paul committed
25
            RTG_THROW("Wrong number of arguments");
Paul's avatar
Paul committed
26
        const shape& input   = inputs.at(0);
Paul's avatar
Paul committed
27
        const shape& weights = inputs.at(1);
Paul's avatar
Paul committed
28
        if(input.type() != weights.type())
Paul's avatar
Paul committed
29
            RTG_THROW("Type doesn't match");
Paul's avatar
Paul committed
30
        if(input.lens().size() != weights.lens().size())
Paul's avatar
Paul committed
31
            RTG_THROW("Dimensions don't match");
Paul's avatar
Paul committed
32
        if(input.lens().size() != 4)
Paul's avatar
Paul committed
33
            RTG_THROW("Only 4d convolution supported");
Paul's avatar
Paul committed
34
35

        auto t = input.type();
Paul's avatar
Paul committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        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
53
    }
Paul's avatar
Paul committed
54

Paul's avatar
Paul committed
55
    argument compute(shape, std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
56

Paul's avatar
Paul committed
57
    friend std::ostream& operator<<(std::ostream& os, const convolution& op)
Paul's avatar
Paul committed
58
    {
Paul's avatar
Paul committed
59
60
61
62
63
        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
64
65
        return os;
    }
Paul's avatar
Paul committed
66
67
};

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

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

Paul's avatar
Paul committed
101
    argument compute(shape, std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
102

Paul's avatar
Paul committed
103
    friend std::ostream& operator<<(std::ostream& os, const pooling& op)
Paul's avatar
Paul committed
104
    {
Paul's avatar
Paul committed
105
106
107
108
109
        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
110
111
        return os;
    }
Paul's avatar
Paul committed
112
113
};

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

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

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

Paul's avatar
Paul committed
156
    argument compute(shape, std::vector<argument>) const { RTG_THROW("not computable"); }
Paul's avatar
Paul committed
157

Paul's avatar
Paul committed
158
    friend std::ostream& operator<<(std::ostream& os, const reshape& op)
Paul's avatar
Paul committed
159
    {
Paul's avatar
Paul committed
160
161
162
        os << op.name() << "[";
        os << "dims={" << stream_range(op.dims) << "}, ";
        os << "]";
Paul's avatar
Paul committed
163
164
        return os;
    }
Paul's avatar
Paul committed
165
166
};

Paul's avatar
Paul committed
167
168
169
} // namespace rtg

#endif