aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/ops/image.cc
blob: 891261b83c0cb539f8275b150d0c6cf116a3af7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

// Copyright (c) 2020-2022, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#include "image.h"
#include "arith_util.h"

#include <type_traits>

using namespace TosaReference;
using namespace Eigen;
using namespace tosa;

template <DType InDtype, DType OutDtype, typename resize_t>
OpResize<InDtype, OutDtype, resize_t>::OpResize(SubgraphTraverser* sgt_,
                                                TosaAttributeBase* attribute_,
                                                uint64_t id_)
    : GraphNode(sgt_, Op_RESIZE, id_)
{
    setRequiredOperands(1, 1);
    setRequiredRank(4, 4);

    INIT_ATTRIBUTE(Resize);
}

template <DType InDtype, DType OutDtype, typename resize_t>
OpResize<InDtype, OutDtype, resize_t>::~OpResize()
{
    if (attribute)
        delete attribute;
}

template <DType InDtype, DType OutDtype, typename resize_t>
int OpResize<InDtype, OutDtype, resize_t>::checkTensorAttributes()
{
    if (validateRequiredOperands())
        return 1;

    if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0]))
        return 1;

    if (this->attribute->scale().size() != 4)
    {
        printNodeValidationError("OpResize: illegal size for attribute scale");
        return 1;
    }

    scale  = this->attribute->scale();
    offset = this->attribute->offset();
    border = this->attribute->border();
    mode   = this->attribute->mode();

    if (this->mode == ResizeMode_BILINEAR)
    {
        if (OutDtype != DType_INT32 && OutDtype != DType_INT48 && OutDtype != DType_FLOAT && OutDtype != DType_FP16)
        {
            printNodeValidationError("OpResize: invalid data type for BILINEAR");
            return 1;
        }
    }
    else
    {
        if (OutDtype != DType_INT8 && OutDtype != DType_INT16 && OutDtype != DType_FLOAT && OutDtype != DType_FP16)
        {
            printNodeValidationError("OpResize: invalid data type for NEAREST");
            return 1;
        }
    }

    in  = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
    out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);

    ASSERT_MEM(in && out);

    return 0;
}

template <DType InDtype, DType OutDtype, typename resize_t>
int OpResize<InDtype, OutDtype, resize_t>::eval()
{
    int in_batch    = in->getShape()[0];
    int in_height   = in->getShape()[1];
    int in_width    = in->getShape()[2];
    int in_channels = in->getShape()[3];

    int out_batch    = out->getShape()[0];
    int out_height   = out->getShape()[1];
    int out_width    = out->getShape()[2];
    int out_channels = out->getShape()[3];

    int16_t scale_y_n = scale[0];
    int16_t scale_y_d = scale[1];
    int16_t scale_x_n = scale[2];
    int16_t scale_x_d = scale[3];

    int16_t offset_y = offset[0];
    int16_t offset_x = offset[1];

    int16_t border_y = border[0];
    int16_t border_x = border[1];

    ERROR_IF(std::max<int>({ in_height, in_width, out_height, out_width }) >= 16384,
             "OpResize: exceeds maximum dimension");
    ERROR_IF(in_batch != out_batch, "OpResize: output tensor batch mismatch");
    ERROR_IF(in_channels != out_channels, "OpResize: output tensor channel mismatch");
    ERROR_IF(scale_y_n <= 0 || scale_y_d <= 0 || scale_x_n <= 0 || scale_x_d <= 0,
             "OpResize: attribute scale must not be negative");
    // If data type is int8_t then ensure that an int32_t accumulator can be used.
    ERROR_IF(scale_y_n > (1 << 11) || scale_x_n > (1 << 11), "OpResize: invalid attribute scale");
    // Set a consistent lower limit of 1/16 downscale to simplify implementations
    ERROR_IF((scale_y_d >= 16 * scale_y_n) || (scale_x_d >= 16 * scale_x_n), "OpResize: invalid attribute scale");
    ERROR_IF((offset_y < -scale_y_n) || (offset_y >= 16 * scale_y_n),
             "OpResize: invalid attribute offset height dimension");
    ERROR_IF((offset_x < -scale_x_n) || (offset_x >= 16 * scale_x_n),
             "OpResize: invalid attribute offset width dimension");
    ERROR_IF((border_y < -16 * scale_y_n || border_y >= scale_y_n),
             "OpResize: invalid attribute border height dimension");
    ERROR_IF((border_x < -16 * scale_x_n || border_x >= scale_x_n),
             "OpResize: invalid attribute border width dimension");

    int32_t res_height = 0;
    int32_t res_width = 0;

    if (idiv_check((in_height - 1) * scale_y_n - offset_y + border_y, scale_y_d, res_height))
       return 1;

    if (idiv_check((in_width - 1) * scale_x_n - offset_x + border_x, scale_x_d, res_width))
       return 1;

    ERROR_IF(out_height != res_height + 1,
             "OpResize: mismatch between output height dimension provided and expected shape");
    ERROR_IF(out_width != res_width + 1,
             "OpResize: mismatch between output width dimension provided and expected shape");

    for (int b = 0; b < out_batch; b++)
        for (int c = 0; c < out_channels; c++)
            for (int oy = 0; oy < out_height; oy++)
                for (int ox = 0; ox < out_width; ox++)
                {
                    int32_t y = oy * scale_y_d + offset_y;
                    int32_t x = ox * scale_x_d + offset_x;

                    float fy = static_cast<float>(y) / static_cast<float>(scale_y_n);
                    float fx = static_cast<float>(x) / static_cast<float>(scale_x_n);

                    int32_t iy = floor(fy);
                    int32_t ix = floor(fx);

                    resize_t dy;
                    resize_t dx;
                    if (std::is_floating_point<resize_t>::value)
                    {
                        dy = fy - iy;
                        dx = fx - ix;
                    }
                    else
                    {
                        dy = y - (iy * scale_y_n);
                        dx = x - (ix * scale_x_n);
                    }

                    int32_t iy0 = MAX(iy, 0);
                    int32_t iy1 = MIN(iy + 1, in_height - 1);
                    int32_t ix0 = MAX(ix, 0);
                    int32_t ix1 = MIN(ix + 1, in_width - 1);

                    OutEigenType acc;
                    if (mode == ResizeMode_BILINEAR)
                    {
                        InEigenType v00 = in->getTensor()(b, iy0, ix0, c);
                        InEigenType v01 = in->getTensor()(b, iy0, ix1, c);
                        InEigenType v10 = in->getTensor()(b, iy1, ix0, c);
                        InEigenType v11 = in->getTensor()(b, iy1, ix1, c);

                        if (std::is_floating_point<resize_t>::value)
                        {
                            acc = (OutEigenType)v00 * (1.0 - dy) * (1.0 - dx);
                            acc += (OutEigenType)v01 * (1.0 - dy) * dx;
                            acc += (OutEigenType)v10 * dy * (1.0 - dx);
                            acc += (OutEigenType)v11 * dy * dx;
                        }
                        else
                        {
                            acc = (OutEigenType)v00 * (scale_y_n - dy) * (scale_x_n - dx);
                            acc += (OutEigenType)v01 * (scale_y_n - dy) * dx;
                            acc += (OutEigenType)v10 * dy * (scale_x_n - dx);
                            acc += (OutEigenType)v11 * dy * dx;
                        }
                    }
                    else
                    {
                        ASSERT_MSG(mode == ResizeMode_NEAREST, "OpResize: invalid mode");
                        if (std::is_floating_point<resize_t>::value)
                        {
                            iy = (dy >= 0.5) ? iy1 : iy0;
                            ix = (dx >= 0.5) ? ix1 : ix0;
                        }
                        else
                        {
                            iy = (2 * dy >= scale_y_n) ? iy1 : iy0;
                            ix = (2 * dx >= scale_x_n) ? ix1 : ix0;
                        }
                        acc = in->getTensor()(b, iy, ix, c);
                    }
                    out->getTensor()(b, oy, ox, c) = acc;
                }

    return GraphNode::eval();
}

// template explicit instantiation
DEF_INSTANTIATE_THREE_TYPE(OpResize, INT8, INT32, int16_t);
DEF_INSTANTIATE_THREE_TYPE(OpResize, INT8, INT8, int16_t);
DEF_INSTANTIATE_THREE_TYPE(OpResize, INT16, INT48, int16_t);
DEF_INSTANTIATE_THREE_TYPE(OpResize, INT16, INT16, int16_t);
DEF_INSTANTIATE_THREE_TYPE(OpResize, FP16, FP16, float);
DEF_INSTANTIATE_THREE_TYPE(OpResize, FLOAT, FLOAT, float);