ef27992c3eee8ca1b7ba84cc318a44ba36a91f2e
[platform/core/ml/nnfw.git] / runtime / onert / core / src / exec / feature / nhwc / Reader.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __ONERT_EXEC_FEATURE_NHWC_READER_H__
18 #define __ONERT_EXEC_FEATURE_NHWC_READER_H__
19
20 #include "../Reader.h"
21
22 #include <cassert>
23
24 #include "backend/ITensor.h"
25 #include "ir/Shape.h"
26 #include "util/Utils.h"
27
28 namespace onert
29 {
30 namespace exec
31 {
32 namespace feature
33 {
34 namespace nhwc
35 {
36
37 template <typename T> class Reader final : public feature::Reader<T>
38 {
39 public:
40   // Construct for buffer of model inputs
41   Reader(const ir::FeatureShape &shape, const T *ptr, size_t len)
42       : _shape{shape}, _ptr{reinterpret_cast<const uint8_t *>(ptr)}, _len{len}
43   {
44     UNUSED_RELEASE(len); // Workaround for unused variable in release mode
45     assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len);
46
47     // No padding
48     _strides.C = sizeof(T);
49     _strides.W = shape.C * sizeof(T);
50     _strides.H = shape.C * shape.W * sizeof(T);
51     _strides.N = shape.C * shape.W * shape.H * sizeof(T);
52   }
53
54   // Construct for backend tensor
55   Reader(const backend::ITensor *tensor)
56       : _ptr{tensor->buffer() + tensor->calcOffset({0, 0, 0, 0})}, _len{tensor->total_size()}
57   {
58     assert(tensor->layout() == ir::Layout::NHWC);
59
60     const auto start_offset = tensor->calcOffset({0, 0, 0, 0});
61     _strides.C = tensor->dimension(3) == 1 ? 0 : tensor->calcOffset({0, 0, 0, 1}) - start_offset;
62     _strides.W = tensor->dimension(2) == 1 ? 0 : tensor->calcOffset({0, 0, 1, 0}) - start_offset;
63     _strides.H = tensor->dimension(1) == 1 ? 0 : tensor->calcOffset({0, 1, 0, 0}) - start_offset;
64     _strides.N = tensor->dimension(0) == 1 ? 0 : tensor->calcOffset({1, 0, 0, 0}) - start_offset;
65
66     _shape.C = tensor->dimension(3);
67     _shape.W = tensor->dimension(2);
68     _shape.H = tensor->dimension(1);
69     _shape.N = tensor->dimension(0);
70   }
71
72 public:
73   T at(uint32_t row, uint32_t col, uint32_t ch) const override
74   {
75     const auto offset = feature_index_to_byte_offset(0, row, col, ch);
76
77     const T *ptr = reinterpret_cast<const T *>(_ptr + offset);
78
79     return *ptr;
80   }
81   T at(uint32_t batch, uint32_t row, uint32_t col, uint32_t ch) const override
82   {
83     const auto offset = feature_index_to_byte_offset(batch, row, col, ch);
84
85     const T *ptr = reinterpret_cast<const T *>(_ptr + offset);
86
87     return *ptr;
88   }
89
90 private:
91   size_t feature_index_to_byte_offset(uint32_t batch, uint32_t row, uint32_t col, uint32_t ch) const
92   {
93     assert(1u * _shape.N > batch); // shape.N > batch
94     assert(1u * _shape.H > row);   // shape.H > row
95     assert(1u * _shape.W > col);   // shape.W > col
96     assert(1u * _shape.C > ch);    // shape.C > ch
97
98     uint32_t res = 0;
99     res += batch * _strides.N;
100     res += row * _strides.H;
101     res += col * _strides.W;
102     res += ch * _strides.C;
103
104     return res;
105   }
106
107 private:
108   // TODO Remove _shape
109   ir::FeatureShape _shape;
110   using Strides = ir::FeatureShape;
111   Strides _strides;
112   const uint8_t *_ptr;
113   size_t _len;
114 };
115
116 } // namespace nhwc
117 } // namespace feature
118 } // namespace exec
119 } // namespace onert
120
121 #endif // __ONERT_EXEC_FEATURE_NHWC_READER_H__