2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #ifndef __NNFW_CKER_PORTABLE_TENSOR_UTILS_H__
19 #define __NNFW_CKER_PORTABLE_TENSOR_UTILS_H__
21 #include "cker/Types.h"
22 #include "cker/neon/neon_check.h"
32 class ActivationFunctor
35 explicit ActivationFunctor(FusedActivationFunctionType act) : act_(act) {}
37 float operator()(float a) const
41 case FusedActivationFunctionType::kNone:
43 case FusedActivationFunctionType::kRelu:
44 return a < 0.f ? 0.f : a;
45 case FusedActivationFunctionType::kRelu6:
46 return std::max(0.f, std::min(a, 6.f));
48 // TODO(aselle): More informative fatal error!
54 FusedActivationFunctionType act_;
57 void PortableVectorBatchVectorAssign(const float *vector, int v_size, int n_batch,
60 for (int b = 0; b < n_batch; b++)
62 memcpy(batch_vector + b * v_size, vector, v_size * sizeof(float));
66 bool PortableIsZeroVector(const float *vector, int v_size)
68 for (int i = 0; i < v_size; ++i)
70 if (*vector++ != 0.0f)
76 void PortableApplyActivationToVector(const float *vector, int v_size,
77 FusedActivationFunctionType activation, float *result)
79 auto activation_func = ActivationFunctor(activation);
80 for (int v = 0; v < v_size; v++)
82 *result++ = (activation_func)(*vector++);
86 void PortableSymmetricQuantizeFloats(const float *values, const int size, int8_t *quantized_values,
87 float *min_value, float *max_value, float *scaling_factor)
89 auto minmax = std::minmax_element(values, values + size);
90 *min_value = *minmax.first;
91 *max_value = *minmax.second;
92 const int kScale = 127;
93 const float range = std::max(std::abs(*min_value), std::abs(*max_value));
96 memset(quantized_values, 0, size * sizeof(int8_t));
100 *scaling_factor = range / kScale;
101 const float scaling_factor_inv = kScale / range;
102 for (int i = 0; i < size; ++i)
104 const int32_t quantized_value =
105 static_cast<int32_t>(std::round(values[i] * scaling_factor_inv));
106 // Clamp: just in case some odd numeric offset.
107 quantized_values[i] = std::min(kScale, std::max(-kScale, quantized_value));
111 void PortableMatrixBatchVectorMultiplyAccumulate(const int8_t *__restrict__ matrix,
112 const int m_rows, const int m_cols,
113 const int8_t *__restrict__ vectors,
114 const float *scaling_factors, int n_batch,
115 float *__restrict__ result, int result_stride)
118 for (batch = 0; batch < n_batch; ++batch, vectors += m_cols)
120 const float batch_scaling_factor = scaling_factors[batch];
121 // Get the address of the first row.
122 const int8_t *row_ptr = matrix;
123 for (row = 0; row < m_rows; ++row, result += result_stride)
125 // Initialize the dot product sum for the row to 0.
127 #if defined(__GNUC__)
128 // Prefetch the row to cache.
129 __builtin_prefetch(row_ptr, 0 /* prefetch for read */, 3 /* temporal locality */);
131 for (col = 0; col < m_cols; ++col, ++row_ptr)
133 dotprod += (*row_ptr) * (vectors[col]);
135 *result += (dotprod * batch_scaling_factor);
140 void PortableMatrixBatchVectorMultiplyAccumulate(const int8_t *__restrict__ matrix,
141 const int m_rows, const int m_cols,
142 const int8_t *__restrict__ vector,
143 const float *scaling_factors, int n_batch,
144 int32_t *, float *__restrict__ result,
147 PortableMatrixBatchVectorMultiplyAccumulate(matrix, m_rows, m_cols, vector, scaling_factors,
148 n_batch, result, result_stride);
151 void PortableMatrixBatchVectorMultiplyAccumulate(const float *matrix, int m_rows, int m_cols,
152 const float *vector, int n_batch, float *result,
155 float *result_in_batch = result;
156 for (int b = 0; b < n_batch; b++)
158 const float *matrix_ptr = matrix;
159 for (int r = 0; r < m_rows; r++)
161 float dot_prod = 0.0f;
162 const float *vector_in_batch = vector + b * m_cols;
163 for (int c = 0; c < m_cols; c++)
165 dot_prod += *matrix_ptr++ * *vector_in_batch++;
167 *result_in_batch += dot_prod;
168 result_in_batch += result_stride;
173 void PortableZeroVector(float *vector, int v_size) { std::fill_n(vector, v_size, 0); }
178 #endif // __NNFW_CKER_PORTABLE_TENSOR_UTILS_H__