ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw::cker::reference Namespace Reference

Functions

void BatchMatMul (const BatchMatMulParams &params, const float *lhs_data, const float *rhs_data, float *output_data)
 
template<typename T >
void BinaryArithmeticOp (const BinaryArithmeticOpParam &params, const Shape &input1_shape, const T *input1_data, const Shape &input2_shape, const T *input2_data, const Shape &output_shape, T *output_data, const std::function< T(const T &, const T &)> &fn)
 
template<>
void BinaryArithmeticOp (const BinaryArithmeticOpParam &params, const Shape &input1_shape, const float *input1_data, const Shape &input2_shape, const float *input2_data, const Shape &output_shape, float *output_data, const std::function< float(const float &, const float &)> &fn)
 
template<>
void BinaryArithmeticOp (const BinaryArithmeticOpParam &, const Shape &input1_shape, const bool *input1_data, const Shape &input2_shape, const bool *input2_data, const Shape &output_shape, bool *output_data, const std::function< bool(const bool &, const bool &)> &fn)
 
template<>
void BinaryArithmeticOp (const BinaryArithmeticOpParam &params, const Shape &input1_shape, const int64_t *input1_data, const Shape &input2_shape, const int64_t *input2_data, const Shape &output_shape, int64_t *output_data, const std::function< int64_t(const int64_t &, const int64_t &)> &fn)
 
template<typename T >
std::enable_if_t< is_quant8< T >::value > BroadcastBinaryArithmeticOpSlow (const BinaryArithmeticOpParam &params, const Shape &input1_shape, const T *input1_data, const Shape &input2_shape, const T *input2_data, const Shape &output_shape, T *output_data, const std::function< T(const BinaryArithmeticOpParam &params, const T &, const T &)> &fn)
 
template<typename T >
void BroadcastBinaryArithmeticOpSlow (const BinaryArithmeticOpParam &params, const Shape &input1_shape, const T *input1_data, const Shape &input2_shape, const T *input2_data, const Shape &output_shape, T *output_data, const std::function< T(const T &, const T &)> &fn)
 
template<>
void BroadcastBinaryArithmeticOpSlow (const BinaryArithmeticOpParam &params, const Shape &input1_shape, const float *input1_data, const Shape &input2_shape, const float *input2_data, const Shape &output_shape, float *output_data, const std::function< float(const float &, const float &)> &fn)
 
template<>
void BroadcastBinaryArithmeticOpSlow (const BinaryArithmeticOpParam &, const Shape &input1_shape, const bool *input1_data, const Shape &input2_shape, const bool *input2_data, const Shape &output_shape, bool *output_data, const std::function< bool(const bool &, const bool &)> &fn)
 
template<>
void BroadcastBinaryArithmeticOpSlow (const BinaryArithmeticOpParam &params, const Shape &input1_shape, const int64_t *input1_data, const Shape &input2_shape, const int64_t *input2_data, const Shape &output_shape, int64_t *output_data, const std::function< int64_t(const int64_t &, const int64_t &)> &fn)
 
void Conv (const ConvParams &params, const Shape &input_shape, const float *input_data, const Shape &filter_shape, const float *filter_data, const Shape &bias_shape, const float *bias_data, const Shape &output_shape, float *output_data)
 
void Conv (const ConvParams &params, const Shape &input_shape, const uint8_t *input_data, const Shape &filter_shape, const uint8_t *filter_data, const Shape &bias_shape, const int32_t *bias_data, const Shape &output_shape, uint8_t *output_data)
 
template<typename T , bool is_asymmetric>
void Conv (const ConvParams &params, const int32_t *output_multiplier, const int32_t *output_shift, const Shape &input_shape, const T *input_data, const Shape &filter_shape, const T *filter_data, const int32_t *filter_zeropoint, const Shape &bias_shape, const int32_t *bias_data, const Shape &output_shape, T *output_data)
 
void HybridConvPerChannel (const ConvParams &params, float *scaling_factors_ptr, const Shape &input_shape, const int8_t *input_data, const Shape &filter_shape, const int8_t *filter_data, const Shape &bias_shape, const float *bias_data, const Shape &output_shape, float *output_data, const float *per_channel_scale, const int32_t *input_offset)
 
void Softmax (const SoftmaxParams &params, const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
 
template<typename T >
void TransposeImpl (const TransposeParams &params, const Shape &unextended_input_shape, const T *input_data, const Shape &unextended_output_shape, T *output_data)
 
template<typename T >
void Transpose (const TransposeParams &params, const Shape &unextended_input_shape, const T *input_data, const Shape &unextended_output_shape, T *output_data)
 

Function Documentation

◆ BatchMatMul()

void nnfw::cker::reference::BatchMatMul ( const BatchMatMulParams params,
const float *  lhs_data,
const float *  rhs_data,
float *  output_data 
)
inline

Definition at line 32 of file BatchMatMul.h.

34{
35 for (int b0 = 0; b0 < params.batch_dim0; ++b0)
36 {
37 const float *lhs_ptr0 = lhs_data + (b0 * params.lhs_ext0);
38 const float *rhs_ptr0 = rhs_data + (b0 * params.rhs_ext0);
39 for (int b1 = 0; b1 < params.batch_dim1; ++b1)
40 {
41 const float *lhs_ptr1 = lhs_ptr0 + b1 * params.lhs_ext1;
42 const float *rhs_ptr1 = rhs_ptr0 + b1 * params.rhs_ext1;
43 for (int b2 = 0; b2 < params.batch_dim2; ++b2)
44 {
45 const float *lhs_ptr2 = lhs_ptr1 + b2 * params.lhs_ext2;
46 const float *rhs_ptr2 = rhs_ptr1 + b2 * params.rhs_ext2;
47 float *out_ptr = output_data + ((b0 * params.batch_dim1 * params.batch_dim2) +
48 b1 * params.batch_dim2 + b2) *
49 params.lhs_rows * params.rhs_cols;
50 for (int j = 0; j < params.rhs_cols; ++j)
51 {
52 for (int i = 0; i < params.lhs_rows; ++i)
53 {
54 float total = 0.f;
55 for (int k = 0; k < params.accum_depth; ++k)
56 {
57 total += lhs_ptr2[params.accum_depth * i + k] * rhs_ptr2[j * params.accum_depth + k];
58 }
59 int idx = params.lhs_rows * j + i;
60 out_ptr[idx] = total;
61 }
62 }
63 }
64 }
65 }
66}

References nnfw::cker::BatchMatMulParams::accum_depth, nnfw::cker::BatchMatMulParams::batch_dim0, nnfw::cker::BatchMatMulParams::batch_dim1, nnfw::cker::BatchMatMulParams::batch_dim2, nnfw::cker::BatchMatMulParams::lhs_ext0, nnfw::cker::BatchMatMulParams::lhs_ext1, nnfw::cker::BatchMatMulParams::lhs_ext2, nnfw::cker::BatchMatMulParams::lhs_rows, nnfw::cker::BatchMatMulParams::rhs_cols, nnfw::cker::BatchMatMulParams::rhs_ext0, nnfw::cker::BatchMatMulParams::rhs_ext1, and nnfw::cker::BatchMatMulParams::rhs_ext2.

Referenced by nnfw::cker::BatchMatMul::operator()().

◆ BinaryArithmeticOp() [1/4]

template<>
void nnfw::cker::reference::BinaryArithmeticOp ( const BinaryArithmeticOpParam ,
const Shape input1_shape,
const bool *  input1_data,
const Shape input2_shape,
const bool *  input2_data,
const Shape output_shape,
bool *  output_data,
const std::function< bool(const bool &, const bool &)> &  fn 
)
inline

Definition at line 65 of file BinaryArithmeticOps.h.

70{
71 const int size = MatchingElementsSize(input1_shape, input2_shape, output_shape);
72 for (int i = 0; i < size; i++)
73 {
74 output_data[i] = fn(input1_data[i], input2_data[i]);
75 }
76}
const luci_interpreter::RuntimeShape output_shape
int MatchingElementsSize(const Shape &shape, const Shape &check_shape_0, const Shape &check_shape_1)
Definition Shape.h:333
int32_t size[5]
Definition Slice.cpp:35

References nnfw::cker::MatchingElementsSize(), output_shape, and size.

◆ BinaryArithmeticOp() [2/4]

template<>
void nnfw::cker::reference::BinaryArithmeticOp ( const BinaryArithmeticOpParam params,
const Shape input1_shape,
const float *  input1_data,
const Shape input2_shape,
const float *  input2_data,
const Shape output_shape,
float *  output_data,
const std::function< float(const float &, const float &)> &  fn 
)
inline

Definition at line 50 of file BinaryArithmeticOps.h.

55{
56 const int size = MatchingElementsSize(input1_shape, input2_shape, output_shape);
57 for (int i = 0; i < size; i++)
58 {
59 output_data[i] = ActivationFunctionWithMinMax(
60 fn(input1_data[i], input2_data[i]), params.float_activation_min, params.float_activation_max);
61 }
62}
T ActivationFunctionWithMinMax(T x, T output_activation_min, T output_activation_max)
Definition Utils.h:43

References nnfw::cker::ActivationFunctionWithMinMax(), nnfw::cker::BinaryArithmeticOpParam::float_activation_max, nnfw::cker::BinaryArithmeticOpParam::float_activation_min, nnfw::cker::MatchingElementsSize(), output_shape, and size.

◆ BinaryArithmeticOp() [3/4]

template<>
void nnfw::cker::reference::BinaryArithmeticOp ( const BinaryArithmeticOpParam params,
const Shape input1_shape,
const int64_t *  input1_data,
const Shape input2_shape,
const int64_t *  input2_data,
const Shape output_shape,
int64_t *  output_data,
const std::function< int64_t(const int64_t &, const int64_t &)> &  fn 
)
inline

Definition at line 79 of file BinaryArithmeticOps.h.

84{
85 const int flat_size = MatchingElementsSize(input1_shape, input2_shape, output_shape);
86 for (int i = 0; i < flat_size; ++i)
87 {
88 output_data[i] = ActivationFunctionWithMinMax(
89 fn(input1_data[i], input2_data[i]), params.int64_activation_min, params.int64_activation_max);
90 }
91}

References nnfw::cker::ActivationFunctionWithMinMax(), nnfw::cker::BinaryArithmeticOpParam::int64_activation_max, nnfw::cker::BinaryArithmeticOpParam::int64_activation_min, nnfw::cker::MatchingElementsSize(), and output_shape.

◆ BinaryArithmeticOp() [4/4]

template<typename T >
void nnfw::cker::reference::BinaryArithmeticOp ( const BinaryArithmeticOpParam params,
const Shape input1_shape,
const T *  input1_data,
const Shape input2_shape,
const T *  input2_data,
const Shape output_shape,
T *  output_data,
const std::function< T(const T &, const T &)> &  fn 
)
inline

Definition at line 35 of file BinaryArithmeticOps.h.

39{
40 const int32_t flat_size = MatchingElementsSize(input1_shape, input2_shape, output_shape);
41 for (int i = 0; i < flat_size; ++i)
42 {
43 output_data[i] = ActivationFunctionWithMinMax(fn(input1_data[i], input2_data[i]),
46 }
47}

References nnfw::cker::ActivationFunctionWithMinMax(), nnfw::cker::MatchingElementsSize(), output_shape, nnfw::cker::BinaryArithmeticOpParam::quantized_activation_max, and nnfw::cker::BinaryArithmeticOpParam::quantized_activation_min.

Referenced by nnfw::cker::BinaryArithmeticOp(), and nnfw::cker::optimized::Div().

◆ BroadcastBinaryArithmeticOpSlow() [1/5]

template<>
void nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow ( const BinaryArithmeticOpParam ,
const Shape input1_shape,
const bool *  input1_data,
const Shape input2_shape,
const bool *  input2_data,
const Shape output_shape,
bool *  output_data,
const std::function< bool(const bool &, const bool &)> &  fn 
)
inline

Definition at line 207 of file BinaryArithmeticOps.h.

211{
214 NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, &desc2);
215 const Shape extended_output_shape = Shape::ExtendedShape(4, output_shape);
216
217 for (int b = 0; b < extended_output_shape.Dims(0); ++b)
218 {
219 for (int y = 0; y < extended_output_shape.Dims(1); ++y)
220 {
221 for (int x = 0; x < extended_output_shape.Dims(2); ++x)
222 {
223 for (int c = 0; c < extended_output_shape.Dims(3); ++c)
224 {
225 output_data[Offset(extended_output_shape, b, y, x, c)] =
226 fn(input1_data[SubscriptToIndex(desc1, b, y, x, c)],
227 input2_data[SubscriptToIndex(desc2, b, y, x, c)]);
228 }
229 }
230 }
231 }
232}
void NdArrayDescsForElementwiseBroadcast(const Dims< N > &input0_dims, const Dims< N > &input1_dims, NdArrayDesc< N > *desc0_out, NdArrayDesc< N > *desc1_out)
Definition NDArray.h:89
int SubscriptToIndex(const NdArrayDesc< 4 > &desc, int i0, int i1, int i2, int i3)
Definition NDArray.h:54
int Offset(const Dims< 4 > &dims, int i0, int i1, int i2, int i3)
Definition Dims.h:64
int32_t Dims(int i) const
Definition Shape.h:92
NdArrayDesc< 4 > desc1
NdArrayDesc< 4 > desc2

References desc1, desc2, nnfw::cker::Shape::Dims(), nnfw::cker::NdArrayDescsForElementwiseBroadcast(), nnfw::cker::Offset(), output_shape, and nnfw::cker::SubscriptToIndex().

◆ BroadcastBinaryArithmeticOpSlow() [2/5]

template<>
void nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow ( const BinaryArithmeticOpParam params,
const Shape input1_shape,
const float *  input1_data,
const Shape input2_shape,
const float *  input2_data,
const Shape output_shape,
float *  output_data,
const std::function< float(const float &, const float &)> &  fn 
)
inline

Definition at line 178 of file BinaryArithmeticOps.h.

182{
185 NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, &desc2);
186 const Shape extended_output_shape = Shape::ExtendedShape(4, output_shape);
187
188 for (int b = 0; b < extended_output_shape.Dims(0); ++b)
189 {
190 for (int y = 0; y < extended_output_shape.Dims(1); ++y)
191 {
192 for (int x = 0; x < extended_output_shape.Dims(2); ++x)
193 {
194 for (int c = 0; c < extended_output_shape.Dims(3); ++c)
195 {
196 output_data[Offset(extended_output_shape, b, y, x, c)] =
197 ActivationFunctionWithMinMax(fn(input1_data[SubscriptToIndex(desc1, b, y, x, c)],
198 input2_data[SubscriptToIndex(desc2, b, y, x, c)]),
200 }
201 }
202 }
203 }
204}

References nnfw::cker::ActivationFunctionWithMinMax(), desc1, desc2, nnfw::cker::Shape::Dims(), nnfw::cker::BinaryArithmeticOpParam::float_activation_max, nnfw::cker::BinaryArithmeticOpParam::float_activation_min, nnfw::cker::NdArrayDescsForElementwiseBroadcast(), nnfw::cker::Offset(), output_shape, and nnfw::cker::SubscriptToIndex().

◆ BroadcastBinaryArithmeticOpSlow() [3/5]

template<>
void nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow ( const BinaryArithmeticOpParam params,
const Shape input1_shape,
const int64_t *  input1_data,
const Shape input2_shape,
const int64_t *  input2_data,
const Shape output_shape,
int64_t *  output_data,
const std::function< int64_t(const int64_t &, const int64_t &)> &  fn 
)
inline

Definition at line 235 of file BinaryArithmeticOps.h.

239{
242 NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, &desc2);
243 const Shape extended_output_shape = Shape::ExtendedShape(4, output_shape);
244
245 for (int b = 0; b < extended_output_shape.Dims(0); ++b)
246 {
247 for (int y = 0; y < extended_output_shape.Dims(1); ++y)
248 {
249 for (int x = 0; x < extended_output_shape.Dims(2); ++x)
250 {
251 for (int c = 0; c < extended_output_shape.Dims(3); ++c)
252 {
253 output_data[Offset(extended_output_shape, b, y, x, c)] =
254 ActivationFunctionWithMinMax(fn(input1_data[SubscriptToIndex(desc1, b, y, x, c)],
255 input2_data[SubscriptToIndex(desc2, b, y, x, c)]),
257 }
258 }
259 }
260 }
261}

References nnfw::cker::ActivationFunctionWithMinMax(), desc1, desc2, nnfw::cker::Shape::Dims(), nnfw::cker::BinaryArithmeticOpParam::int64_activation_max, nnfw::cker::BinaryArithmeticOpParam::int64_activation_min, nnfw::cker::NdArrayDescsForElementwiseBroadcast(), nnfw::cker::Offset(), output_shape, and nnfw::cker::SubscriptToIndex().

◆ BroadcastBinaryArithmeticOpSlow() [4/5]

template<typename T >
std::enable_if_t< is_quant8< T >::value > nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow ( const BinaryArithmeticOpParam params,
const Shape input1_shape,
const T *  input1_data,
const Shape input2_shape,
const T *  input2_data,
const Shape output_shape,
T *  output_data,
const std::function< T(const BinaryArithmeticOpParam &params, const T &, const T &)> &  fn 
)
inline

Definition at line 94 of file BinaryArithmeticOps.h.

98{
101 NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, &desc2);
102 const Shape extended_output_shape = Shape::ExtendedShape(4, output_shape);
103
104 // Comment from tensorflow lite:
105 //
106 // In Tensorflow, the dimensions are canonically named (batch_number, row,
107 // col, channel), with extents (batches, height, width, depth), with the
108 // trailing dimension changing most rapidly (channels has the smallest stride,
109 // typically 1 element).
110 //
111 // In generated C code, we store arrays with the dimensions reversed. The
112 // first dimension has smallest stride.
113 //
114 // We name our variables by their Tensorflow convention, but generate C code
115 // nesting loops such that the innermost loop has the smallest stride for the
116 // best cache behavior.
117 for (int b = 0; b < extended_output_shape.Dims(0); ++b)
118 {
119 for (int y = 0; y < extended_output_shape.Dims(1); ++y)
120 {
121 for (int x = 0; x < extended_output_shape.Dims(2); ++x)
122 {
123 for (int c = 0; c < extended_output_shape.Dims(3); ++c)
124 {
125 output_data[Offset(extended_output_shape, b, y, x, c)] = ActivationFunctionWithMinMax<T>(
126 fn(params, input1_data[SubscriptToIndex(desc1, b, y, x, c)],
127 input2_data[SubscriptToIndex(desc2, b, y, x, c)]),
129 }
130 }
131 }
132 }
133}

References desc1, desc2, nnfw::cker::Shape::Dims(), nnfw::cker::NdArrayDescsForElementwiseBroadcast(), nnfw::cker::Offset(), output_shape, nnfw::cker::BinaryArithmeticOpParam::quantized_activation_max, nnfw::cker::BinaryArithmeticOpParam::quantized_activation_min, and nnfw::cker::SubscriptToIndex().

Referenced by nnfw::cker::optimized::BroadcastAddDispatch(), nnfw::cker::optimized::BroadcastAddDispatch(), nnfw::cker::BroadcastBinaryArithmeticOp(), nnfw::cker::optimized::BroadcastDivDispatch(), nnfw::cker::optimized::BroadcastMulDispatch(), nnfw::cker::optimized::BroadcastMulDispatch(), and nnfw::cker::optimized::BroadcastSubDispatch().

◆ BroadcastBinaryArithmeticOpSlow() [5/5]

template<typename T >
void nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow ( const BinaryArithmeticOpParam params,
const Shape input1_shape,
const T *  input1_data,
const Shape input2_shape,
const T *  input2_data,
const Shape output_shape,
T *  output_data,
const std::function< T(const T &, const T &)> &  fn 
)
inline

Definition at line 135 of file BinaryArithmeticOps.h.

140{
143 NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, &desc2);
144 const Shape extended_output_shape = Shape::ExtendedShape(4, output_shape);
145
146 // Comment from tensorflow lite:
147 //
148 // In Tensorflow, the dimensions are canonically named (batch_number, row,
149 // col, channel), with extents (batches, height, width, depth), with the
150 // trailing dimension changing most rapidly (channels has the smallest stride,
151 // typically 1 element).
152 //
153 // In generated C code, we store arrays with the dimensions reversed. The
154 // first dimension has smallest stride.
155 //
156 // We name our variables by their Tensorflow convention, but generate C code
157 // nesting loops such that the innermost loop has the smallest stride for the
158 // best cache behavior.
159 for (int b = 0; b < extended_output_shape.Dims(0); ++b)
160 {
161 for (int y = 0; y < extended_output_shape.Dims(1); ++y)
162 {
163 for (int x = 0; x < extended_output_shape.Dims(2); ++x)
164 {
165 for (int c = 0; c < extended_output_shape.Dims(3); ++c)
166 {
167 output_data[Offset(extended_output_shape, b, y, x, c)] = ActivationFunctionWithMinMax<T>(
168 fn(input1_data[SubscriptToIndex(desc1, b, y, x, c)],
169 input2_data[SubscriptToIndex(desc2, b, y, x, c)]),
171 }
172 }
173 }
174 }
175}

References desc1, desc2, nnfw::cker::Shape::Dims(), nnfw::cker::NdArrayDescsForElementwiseBroadcast(), nnfw::cker::Offset(), output_shape, nnfw::cker::BinaryArithmeticOpParam::quantized_activation_max, nnfw::cker::BinaryArithmeticOpParam::quantized_activation_min, and nnfw::cker::SubscriptToIndex().

◆ Conv() [1/3]

template<typename T , bool is_asymmetric>
void nnfw::cker::reference::Conv ( const ConvParams params,
const int32_t *  output_multiplier,
const int32_t *  output_shift,
const Shape input_shape,
const T *  input_data,
const Shape filter_shape,
const T *  filter_data,
const int32_t *  filter_zeropoint,
const Shape bias_shape,
const int32_t *  bias_data,
const Shape output_shape,
T *  output_data 
)
inline

Definition at line 195 of file Conv.h.

200{
201 // Get parameters.
202 const int32_t input_offset = params.input_offset; // r = s(q - Z)
203 const int stride_width = params.stride_width;
204 const int stride_height = params.stride_height;
205 const int dilation_width_factor = params.dilation_width_factor;
206 const int dilation_height_factor = params.dilation_height_factor;
207 const int pad_width = params.padding_values.width;
208 const int pad_height = params.padding_values.height;
209 const int32_t output_offset = params.output_offset;
210
211 // Set min and max value of the output.
212 const int32_t output_activation_min = params.quantized_activation_min;
213 const int32_t output_activation_max = params.quantized_activation_max;
214
215 // Consistency check.
216 assert(output_activation_min < output_activation_max);
217 assert(input_shape.DimensionsCount() == 4);
218 assert(filter_shape.DimensionsCount() == 4);
219 assert(output_shape.DimensionsCount() == 4);
220 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
221 const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
222 const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
223 if (bias_data)
224 {
225 assert(bias_shape.FlatSize() == output_depth);
226 }
227
228 // Check dimensions of the tensors.
229 const int input_height = input_shape.Dims(1);
230 const int input_width = input_shape.Dims(2);
231 const int filter_height = filter_shape.Dims(1);
232 const int filter_width = filter_shape.Dims(2);
233 const int output_height = output_shape.Dims(1);
234 const int output_width = output_shape.Dims(2);
235 for (int batch = 0; batch < batches; ++batch)
236 {
237 for (int out_y = 0; out_y < output_height; ++out_y)
238 {
239 const int in_y_origin = (out_y * stride_height) - pad_height;
240 for (int out_x = 0; out_x < output_width; ++out_x)
241 {
242 const int in_x_origin = (out_x * stride_width) - pad_width;
243 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
244 {
245 int32_t acc = 0;
246 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
247 {
248 const int in_y = in_y_origin + dilation_height_factor * filter_y;
249 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
250 {
251 const int in_x = in_x_origin + dilation_width_factor * filter_x;
252
253 // Zero padding by omitting the areas outside the image.
254 const bool is_point_inside_image =
255 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
256
257 if (!is_point_inside_image)
258 {
259 continue;
260 }
261
262 for (int in_channel = 0; in_channel < input_depth; ++in_channel)
263 {
264 const T input_val = input_data[Offset(input_shape, batch, in_y, in_x, in_channel)];
265 const T filter_val =
266 filter_data[Offset(filter_shape, out_channel, filter_y, filter_x, in_channel)];
267 if (is_asymmetric)
268 {
269 const int32_t filter_offset = -filter_zeropoint[out_channel];
270 acc += (filter_val + filter_offset) * (input_val + input_offset);
271 }
272 else
273 {
274 // Accumulate with 32 bits accumulator.
275 // In the nudging process during model quantization, we force
276 // real value of 0.0 be represented by a quantized value. This
277 // guarantees that the input_offset is a int8_t, even though
278 // it is represented using int32_t. int32_t += int8_t *
279 // (int8_t - int8_t) so the highest value we can get from each
280 // accumulation is [-127, 127] * ([-128, 127] -
281 // [-128, 127]), which is [-32512, 32512]. log2(32512)
282 // = 14.98, which means we can accumulate at least 2^16
283 // multiplications without overflow. The accumulator is
284 // applied to a filter so the accumulation logic will hold as
285 // long as the filter size (filter_y * filter_x * in_channel)
286 // does not exceed 2^16, which is the case in all the models
287 // we have seen so far.
288 // TODO(jianlijianli): Add a check to make sure the
289 // accumulator depth is smaller than 2^16.
290 acc += filter_val * (input_val + input_offset);
291 }
292 }
293 }
294 }
295
296 if (bias_data)
297 {
298 acc += bias_data[out_channel];
299 }
300 acc = MultiplyByQuantizedMultiplier(acc, output_multiplier[out_channel],
301 output_shift[out_channel]);
302 acc += output_offset;
303 acc = std::max(acc, output_activation_min);
304 acc = std::min(acc, output_activation_max);
305 output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = static_cast<T>(acc);
306 }
307 }
308 }
309 }
310}
int32_t DimensionsCount() const
Definition Shape.h:91
int FlatSize() const
Definition Shape.h:181
int MatchingDim(const Shape &shape1, int index1, const Shape &shape2, int index2)
Definition Shape.h:220
int32_t MultiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
Definition Utils.h:96
int16_t stride_height
Definition Types.h:146
PaddingValues padding_values
Definition Types.h:143
int32_t output_offset
Definition Types.h:153
int16_t dilation_width_factor
Definition Types.h:147
int32_t quantized_activation_max
Definition Types.h:158
int16_t dilation_height_factor
Definition Types.h:148
int32_t quantized_activation_min
Definition Types.h:157

References nnfw::cker::ConvParams::dilation_height_factor, nnfw::cker::ConvParams::dilation_width_factor, nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), nnfw::cker::PaddingValues::height, nnfw::cker::ConvParams::input_offset, nnfw::cker::MatchingDim(), nnfw::cker::MultiplyByQuantizedMultiplier(), nnfw::cker::Offset(), nnfw::cker::ConvParams::output_offset, output_shape, nnfw::cker::ConvParams::padding_values, nnfw::cker::ConvParams::quantized_activation_max, nnfw::cker::ConvParams::quantized_activation_min, nnfw::cker::ConvParams::stride_height, nnfw::cker::ConvParams::stride_width, and nnfw::cker::PaddingValues::width.

◆ Conv() [2/3]

void nnfw::cker::reference::Conv ( const ConvParams params,
const Shape input_shape,
const float *  input_data,
const Shape filter_shape,
const float *  filter_data,
const Shape bias_shape,
const float *  bias_data,
const Shape output_shape,
float *  output_data 
)
inline

Definition at line 33 of file Conv.h.

37{
38 const int stride_width = params.stride_width;
39 const int stride_height = params.stride_height;
40 const int dilation_width_factor = params.dilation_width_factor;
41 const int dilation_height_factor = params.dilation_height_factor;
42 const int pad_width = params.padding_values.width;
43 const int pad_height = params.padding_values.height;
44 const float output_activation_min = params.float_activation_min;
45 const float output_activation_max = params.float_activation_max;
46 assert(input_shape.DimensionsCount() == 4);
47 assert(filter_shape.DimensionsCount() == 4);
48 assert(output_shape.DimensionsCount() == 4);
49
50 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
51 const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
52 const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
53 if (bias_data)
54 {
55 assert(bias_shape.FlatSize() == output_depth);
56 }
57 const int input_height = input_shape.Dims(1);
58 const int input_width = input_shape.Dims(2);
59 const int filter_height = filter_shape.Dims(1);
60 const int filter_width = filter_shape.Dims(2);
61 const int output_height = output_shape.Dims(1);
62 const int output_width = output_shape.Dims(2);
63 for (int batch = 0; batch < batches; ++batch)
64 {
65 for (int out_y = 0; out_y < output_height; ++out_y)
66 {
67 for (int out_x = 0; out_x < output_width; ++out_x)
68 {
69 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
70 {
71 const int in_x_origin = (out_x * stride_width) - pad_width;
72 const int in_y_origin = (out_y * stride_height) - pad_height;
73 float total = 0.f;
74 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
75 {
76 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
77 {
78 const int in_x = in_x_origin + dilation_width_factor * filter_x;
79 const int in_y = in_y_origin + dilation_height_factor * filter_y;
80 // If the location is outside the bounds of the input image,
81 // use zero as a default value.
82 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
83 {
84 const int in_offset = Offset(input_shape, batch, in_y, in_x, 0);
85 const int filter_offset = Offset(filter_shape, out_channel, filter_y, filter_x, 0);
86 for (int in_channel = 0; in_channel < input_depth; ++in_channel)
87 {
88 float input_value = input_data[in_offset + in_channel];
89 float filter_value = filter_data[filter_offset + in_channel];
90 total += (input_value * filter_value);
91 }
92 }
93 }
94 }
95 float bias_value = 0.0f;
96 if (bias_data)
97 {
98 bias_value = bias_data[out_channel];
99 }
100 output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
101 ActivationFunctionWithMinMax(total + bias_value, output_activation_min,
102 output_activation_max);
103 }
104 }
105 }
106 }
107}
float float_activation_max
Definition Types.h:161
float float_activation_min
Definition Types.h:160

References nnfw::cker::ActivationFunctionWithMinMax(), nnfw::cker::ConvParams::dilation_height_factor, nnfw::cker::ConvParams::dilation_width_factor, nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), nnfw::cker::ConvParams::float_activation_max, nnfw::cker::ConvParams::float_activation_min, nnfw::cker::PaddingValues::height, nnfw::cker::MatchingDim(), nnfw::cker::Offset(), output_shape, nnfw::cker::ConvParams::padding_values, nnfw::cker::ConvParams::stride_height, nnfw::cker::ConvParams::stride_width, and nnfw::cker::PaddingValues::width.

Referenced by nnfw::cker::Conv::operator()().

◆ Conv() [3/3]

void nnfw::cker::reference::Conv ( const ConvParams params,
const Shape input_shape,
const uint8_t *  input_data,
const Shape filter_shape,
const uint8_t *  filter_data,
const Shape bias_shape,
const int32_t *  bias_data,
const Shape output_shape,
uint8_t *  output_data 
)
inline

Definition at line 109 of file Conv.h.

113{
114 const int stride_width = params.stride_width;
115 const int stride_height = params.stride_height;
116 const int dilation_width_factor = params.dilation_width_factor;
117 const int dilation_height_factor = params.dilation_height_factor;
118 const int pad_width = params.padding_values.width;
119 const int pad_height = params.padding_values.height;
120 const int32_t input_offset = params.input_offset;
121 const int32_t filter_offset = params.weights_offset;
122 const int32_t output_offset = params.output_offset;
123 const int32_t output_multiplier = params.output_multiplier;
124 const int output_shift = params.output_shift;
125 const int32_t output_activation_min = params.quantized_activation_min;
126 const int32_t output_activation_max = params.quantized_activation_max;
127 assert(output_activation_min <= output_activation_max);
128
129 assert(input_shape.DimensionsCount() == 4);
130 assert(filter_shape.DimensionsCount() == 4);
131 assert(output_shape.DimensionsCount() == 4);
132 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
133 const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
134 const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
135 if (bias_data)
136 {
137 assert(bias_shape.FlatSize() == output_depth);
138 }
139 const int input_height = input_shape.Dims(1);
140 const int input_width = input_shape.Dims(2);
141 const int filter_height = filter_shape.Dims(1);
142 const int filter_width = filter_shape.Dims(2);
143 const int output_height = output_shape.Dims(1);
144 const int output_width = output_shape.Dims(2);
145 for (int batch = 0; batch < batches; ++batch)
146 {
147 for (int out_y = 0; out_y < output_height; ++out_y)
148 {
149 for (int out_x = 0; out_x < output_width; ++out_x)
150 {
151 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
152 {
153 const int in_x_origin = (out_x * stride_width) - pad_width;
154 const int in_y_origin = (out_y * stride_height) - pad_height;
155 int32_t acc = 0;
156 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
157 {
158 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
159 {
160 const int in_x = in_x_origin + dilation_width_factor * filter_x;
161 const int in_y = in_y_origin + dilation_height_factor * filter_y;
162 // If the location is outside the bounds of the input image,
163 // use zero as a default value.
164 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
165 {
166 const int in_base = Offset(input_shape, batch, in_y, in_x, 0);
167 const int filter_base = Offset(filter_shape, out_channel, filter_y, filter_x, 0);
168 for (int in_channel = 0; in_channel < input_depth; in_channel++)
169 {
170 int32_t input_val = input_data[in_channel + in_base];
171 int32_t filter_val = filter_data[in_channel + filter_base];
172 acc += (filter_val + filter_offset) * (input_val + input_offset);
173 }
174 }
175 }
176 }
177 if (bias_data)
178 {
179 acc += bias_data[out_channel];
180 }
181 acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift);
182 acc += output_offset;
183 acc = std::max(acc, output_activation_min);
184 acc = std::min(acc, output_activation_max);
185 output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
186 static_cast<uint8_t>(acc);
187 }
188 }
189 }
190 }
191}
int32_t output_multiplier
Definition Types.h:154
int32_t weights_offset
Definition Types.h:152

References nnfw::cker::ConvParams::dilation_height_factor, nnfw::cker::ConvParams::dilation_width_factor, nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), nnfw::cker::PaddingValues::height, nnfw::cker::ConvParams::input_offset, nnfw::cker::MatchingDim(), nnfw::cker::MultiplyByQuantizedMultiplier(), nnfw::cker::Offset(), nnfw::cker::ConvParams::output_multiplier, nnfw::cker::ConvParams::output_offset, output_shape, nnfw::cker::ConvParams::output_shift, nnfw::cker::ConvParams::padding_values, nnfw::cker::ConvParams::quantized_activation_max, nnfw::cker::ConvParams::quantized_activation_min, nnfw::cker::ConvParams::stride_height, nnfw::cker::ConvParams::stride_width, nnfw::cker::ConvParams::weights_offset, and nnfw::cker::PaddingValues::width.

◆ HybridConvPerChannel()

void nnfw::cker::reference::HybridConvPerChannel ( const ConvParams params,
float *  scaling_factors_ptr,
const Shape input_shape,
const int8_t *  input_data,
const Shape filter_shape,
const int8_t *  filter_data,
const Shape bias_shape,
const float *  bias_data,
const Shape output_shape,
float *  output_data,
const float *  per_channel_scale,
const int32_t *  input_offset 
)
inline

Definition at line 314 of file Conv.h.

321{
322 const int stride_width = params.stride_width;
323 const int stride_height = params.stride_height;
324 const int dilation_width_factor = params.dilation_width_factor;
325 const int dilation_height_factor = params.dilation_height_factor;
326 const int pad_width = params.padding_values.width;
327 const int pad_height = params.padding_values.height;
328 const float output_activation_min = params.float_activation_min;
329 const float output_activation_max = params.float_activation_max;
330 assert(input_shape.DimensionsCount() == 4);
331 assert(filter_shape.DimensionsCount() == 4);
332 assert(output_shape.DimensionsCount() == 4);
333 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
334 const int input_depth = input_shape.Dims(3);
335 const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
336 if (bias_data)
337 {
338 assert(bias_shape.FlatSize() == output_depth);
339 }
340 const int input_height = input_shape.Dims(1);
341 const int input_width = input_shape.Dims(2);
342 const int filter_height = filter_shape.Dims(1);
343 const int filter_width = filter_shape.Dims(2);
344 const int filter_input_depth = filter_shape.Dims(3);
345 const int groups = input_depth / filter_input_depth;
346 assert(input_depth % filter_input_depth == 0);
347 const int filters_per_group = output_depth / groups;
348 const int output_height = output_shape.Dims(1);
349 const int output_width = output_shape.Dims(2);
350 for (int batch = 0; batch < batches; ++batch)
351 {
352 for (int out_y = 0; out_y < output_height; ++out_y)
353 {
354 for (int out_x = 0; out_x < output_width; ++out_x)
355 {
356 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
357 {
358 auto group = out_channel / filters_per_group;
359 const int in_x_origin = (out_x * stride_width) - pad_width;
360 const int in_y_origin = (out_y * stride_height) - pad_height;
361 int32_t acc = 0;
362 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
363 {
364 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
365 {
366 for (int in_channel = 0; in_channel < filter_input_depth; ++in_channel)
367 {
368 const int in_x = in_x_origin + dilation_width_factor * filter_x;
369 const int in_y = in_y_origin + dilation_height_factor * filter_y;
370 // If the location is outside the bounds of the input image,
371 // use zero as a default value.
372 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
373 {
374 int32_t input_val = input_data[Offset(input_shape, batch, in_y, in_x,
375 in_channel + group * filter_input_depth)];
376 int32_t filter_val =
377 filter_data[Offset(filter_shape, out_channel, filter_y, filter_x, in_channel)];
378 acc += filter_val * (input_val - input_offset[batch]);
379 }
380 }
381 }
382 }
383 float acc_float = acc * per_channel_scale[out_channel] * scaling_factors_ptr[batch];
384 if (bias_data)
385 {
386 acc_float += bias_data[out_channel];
387 }
388 output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
389 ActivationFunctionWithMinMax(acc_float, output_activation_min, output_activation_max);
390 }
391 }
392 }
393 }
394}

References nnfw::cker::ActivationFunctionWithMinMax(), nnfw::cker::ConvParams::dilation_height_factor, nnfw::cker::ConvParams::dilation_width_factor, nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), nnfw::cker::ConvParams::float_activation_max, nnfw::cker::ConvParams::float_activation_min, nnfw::cker::PaddingValues::height, nnfw::cker::MatchingDim(), nnfw::cker::Offset(), output_shape, nnfw::cker::ConvParams::padding_values, nnfw::cker::ConvParams::stride_height, nnfw::cker::ConvParams::stride_width, and nnfw::cker::PaddingValues::width.

◆ Softmax()

void nnfw::cker::reference::Softmax ( const SoftmaxParams params,
const Shape input_shape,
const float *  input_data,
const Shape output_shape,
float *  output_data 
)
inline

Definition at line 43 of file SoftMax.h.

45{
46 const int trailing_dim = input_shape.DimensionsCount() - 1;
47 const int outer_size = MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
48 const int depth = MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
49
50 for (int i = 0; i < outer_size; ++i)
51 {
52 // Find max element value which we'll use to ensure numerical stability
53 // taking advantage of the following equality:
54 // exp(x[i])/sum(exp(x[i])) == exp(x[i]+C)/sum(exp(x[i]+C))
55 float max = std::numeric_limits<float>::lowest();
56 for (int c = 0; c < depth; ++c)
57 {
58 max = std::max(max, input_data[i * depth + c]);
59 }
60
61 // Compute sum.
62 float sum = 0.f;
63 for (int c = 0; c < depth; ++c)
64 {
65 sum += std::exp((input_data[i * depth + c] - max) * static_cast<float>(params.beta));
66 }
67
68 // Compute result.
69 for (int c = 0; c < depth; ++c)
70 {
71 output_data[i * depth + c] =
72 std::exp((input_data[i * depth + c] - max) * static_cast<float>(params.beta)) / sum;
73 }
74 }
75}
int MatchingFlatSizeSkipDim(const Shape &shape, int skip_dim, const Shape &check_shape_0)
Definition Shape.h:304

References nnfw::cker::SoftmaxParams::beta, nnfw::cker::Shape::DimensionsCount(), nnfw::cker::MatchingDim(), nnfw::cker::MatchingFlatSizeSkipDim(), and output_shape.

Referenced by onert::backend::cpu::ops::SoftMaxLayer::softmaxFloat32().

◆ Transpose()

template<typename T >
void nnfw::cker::reference::Transpose ( const TransposeParams params,
const Shape unextended_input_shape,
const T *  input_data,
const Shape unextended_output_shape,
T *  output_data 
)

Definition at line 88 of file Transpose.h.

90{
91 // Transpose kernel only does rearranging values not numeric evaluations on
92 // each cell. It's safe to implement per size of scalar type and this trick
93 // keeps the total code size in a reasonable range.
94 switch (sizeof(T))
95 {
96 case 1:
97 TransposeImpl<int8_t>(params, unextended_input_shape,
98 reinterpret_cast<const int8_t *>(input_data), unextended_output_shape,
99 reinterpret_cast<int8_t *>(output_data));
100 break;
101 case 2:
102 TransposeImpl<int16_t>(params, unextended_input_shape,
103 reinterpret_cast<const int16_t *>(input_data), unextended_output_shape,
104 reinterpret_cast<int16_t *>(output_data));
105 break;
106
107 case 4:
108 TransposeImpl<int32_t>(params, unextended_input_shape,
109 reinterpret_cast<const int32_t *>(input_data), unextended_output_shape,
110 reinterpret_cast<int32_t *>(output_data));
111 break;
112 case 8:
113 TransposeImpl<int64_t>(params, unextended_input_shape,
114 reinterpret_cast<const int64_t *>(input_data), unextended_output_shape,
115 reinterpret_cast<int64_t *>(output_data));
116 break;
117 }
118}

Referenced by nnfw::cker::TransposeImpl().

◆ TransposeImpl()

template<typename T >
void nnfw::cker::reference::TransposeImpl ( const TransposeParams params,
const Shape unextended_input_shape,
const T *  input_data,
const Shape unextended_output_shape,
T *  output_data 
)

Definition at line 33 of file Transpose.h.

35{
36 const int unextended_output_size = unextended_output_shape.DimensionsCount();
37 assert(unextended_input_shape.DimensionsCount() <= 4);
38 assert(unextended_output_size <= 4);
39 assert(unextended_output_size == params.perm_count);
40 const Shape input_shape = Shape::ExtendedShape(4, unextended_input_shape);
41 const Shape output_shape = Shape::ExtendedShape(4, unextended_output_shape);
42 const int input_ext_size = 4 - unextended_input_shape.DimensionsCount();
43 const int output_ext_size = 4 - unextended_output_size;
44
45 // The perm data is extended to match the output, each index incremented by
46 // the amount of front padding of the input shape.
47 int extended_perm[4];
48 for (int i = 0; i < output_ext_size; ++i)
49 {
50 extended_perm[i] = i;
51 }
52 for (int i = 0; i < unextended_output_size; ++i)
53 {
54 extended_perm[i + output_ext_size] = params.perm[i] + input_ext_size;
55 }
56
57 int out_sizes[4];
58 // Compute the inverse permutation array so we can do an output centered
59 // transpose. Also, check to make sure output_dims is matching input_dims.
60 for (int k = 0; k < 4; k++)
61 {
62 out_sizes[k] = MatchingDim(input_shape, extended_perm[k], output_shape, k);
63 }
64
65 // Naive transpose loop (iterate on output index and compute input index).
66 int o[4]; // loop index (on output).
67 int i[4];
68 for (o[3] = 0; o[3] < out_sizes[3]; o[3]++)
69 {
70 i[extended_perm[3]] = o[3];
71 for (o[2] = 0; o[2] < out_sizes[2]; o[2]++)
72 {
73 i[extended_perm[2]] = o[2];
74 for (o[1] = 0; o[1] < out_sizes[1]; o[1]++)
75 {
76 i[extended_perm[1]] = o[1];
77 for (o[0] = 0; o[0] < out_sizes[0]; o[0]++)
78 {
79 i[extended_perm[0]] = o[0];
80 output_data[Offset(output_shape, o)] = input_data[Offset(input_shape, i)];
81 }
82 }
83 }
84 }
85}

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::MatchingDim(), nnfw::cker::Offset(), output_shape, nnfw::cker::TransposeParams::perm, and nnfw::cker::TransposeParams::perm_count.