ONE - On-device Neural Engine
Loading...
Searching...
No Matches
TensorUtils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#ifndef __NNFW_CKER_TENSOR_UTILS_H__
19#define __NNFW_CKER_TENSOR_UTILS_H__
20
21#include "cker/Types.h"
25
26#include <cstring>
27#include <cmath>
28
29namespace nnfw
30{
31namespace cker
32{
33
34inline void CwiseClipping(float *vector, const int v_size, const float clipping_value)
35{
36 NEON_OR_PORTABLE(CwiseClipping, vector, v_size, clipping_value);
37}
38
39inline void VectorBatchVectorAdd(const float *vector, int v_size, int n_batch, float *batch_vector)
40{
41 PortableVectorBatchVectorAdd(vector, v_size, n_batch, batch_vector);
42}
43
44inline void VectorBatchVectorAssign(const float *vector, int v_size, int n_batch,
45 float *batch_vector)
46{
47 PortableVectorBatchVectorAssign(vector, v_size, n_batch, batch_vector);
48}
49
50// Cwise product of two vectors.
51template <typename T>
52inline void VectorVectorCwiseProduct(const T *__restrict__ vector1, const T *__restrict__ vector2,
53 int v_size, T *__restrict__ result)
54{
55 for (int v = 0; v < v_size; v++)
56 {
57 *result++ = *vector1++ * *vector2++;
58 }
59}
60
61// Cwise product and accumulate of two vectors. Since it's a MAC operation, the
62// assumption here is that result array is initialized to valid values.
63template <typename T>
64inline void VectorVectorCwiseProductAccumulate(const T *__restrict__ vector1,
65 const T *__restrict__ vector2, int v_size,
66 T *__restrict__ result)
67{
68 for (int v = 0; v < v_size; v++)
69 {
70 *result++ += *vector1++ * *vector2++;
71 }
72}
73
74// Cwise product of a vector and a batch-vector.
75template <typename T>
76inline void VectorBatchVectorCwiseProduct(const T *vector, int v_size, const T *batch_vector,
77 int n_batch, T *result)
78{
79 for (int b = 0; b < n_batch; b++)
80 {
81 VectorVectorCwiseProduct(vector, batch_vector, v_size, result);
82 // Update the pointers.
83 result += v_size;
84 batch_vector += v_size;
85 }
86}
87
88// Cwise product and accumulate of a vector and a batch-vector. Since it's a MAC
89// operation, the assumption here is that result array is initialized to valid
90// values.
91template <typename T>
92inline void VectorBatchVectorCwiseProductAccumulate(const T *vector, int v_size,
93 const T *batch_vector, int n_batch, T *result)
94{
95 for (int b = 0; b < n_batch; b++)
96 {
97 VectorVectorCwiseProductAccumulate(vector, batch_vector, v_size, result);
98 // Update the pointers.
99 result += v_size;
100 batch_vector += v_size;
101 }
102}
103
104inline bool IsZeroVector(const float *vector, int v_size)
105{
106 return NEON_OR_PORTABLE(IsZeroVector, vector, v_size);
107}
108
109inline void ApplyActivationToVector(const float *vector, int v_size,
110 FusedActivationFunctionType activation, float *result)
111{
112 PortableApplyActivationToVector(vector, v_size, activation, result);
113}
114
115inline void Sub1Vector(const float *vector, int v_size, float *result)
116{
117 NEON_OR_PORTABLE(Sub1Vector, vector, v_size, result);
118}
119
120inline void SymmetricQuantizeFloats(const float *values, const int size, int8_t *quantized_values,
121 float *min, float *max, float *scaling_factor)
122{
123 return NEON_OR_PORTABLE(SymmetricQuantizeFloats, values, size, quantized_values, min, max,
124 scaling_factor);
125}
126
127inline void MatrixBatchVectorMultiplyAccumulate(const int8_t *matrix, const int m_rows,
128 const int m_cols, const int8_t *vector,
129 const float *scaling_factors, int n_batch,
130 float *result, int result_stride)
131{
132 NEON_OR_PORTABLE(MatrixBatchVectorMultiplyAccumulate, matrix, m_rows, m_cols, vector,
133 scaling_factors, n_batch, result, result_stride);
134}
135
136inline void MatrixBatchVectorMultiplyAccumulate(const float *matrix, int m_rows, int m_cols,
137 const float *vector, int n_batch, float *result,
138 int result_stride)
139{
140 NEON_OR_PORTABLE(MatrixBatchVectorMultiplyAccumulate, matrix, m_rows, m_cols, vector, n_batch,
141 result, result_stride);
142}
143
144inline void MatrixBatchVectorMultiplyAccumulate(const int8_t *matrix, const int m_rows,
145 const int m_cols, const int8_t *vectors,
146 const float *scaling_factors, int n_batch,
147 int32_t *scratch, float *result, int result_stride,
148 ruy::Context *ruy_context)
149{
150 NEON_OR_PORTABLE(MatrixBatchVectorMultiplyAccumulate, matrix, m_rows, m_cols, vectors,
151 scaling_factors, n_batch, scratch, result, result_stride, ruy_context);
152}
153
154inline void MeanStddevNormalization(const float *input_vector, float *output_vector, int v_size,
155 int n_batch)
156{
157 PortableMeanStddevNormalization(input_vector, output_vector, v_size, n_batch);
158}
159
160inline void ZeroVector(float *vector, int v_size) { PortableZeroVector(vector, v_size); }
161
162} // namespace cker
163} // namespace nnfw
164
165#endif // __NNFW_CKER_TENSOR_UTILS_H__
#define NEON_OR_PORTABLE(funcname,...)
Definition neon_check.h:47
void ZeroVector(float *vector, int v_size)
void VectorVectorCwiseProduct(const T *__restrict__ vector1, const T *__restrict__ vector2, int v_size, T *__restrict__ result)
Definition TensorUtils.h:52
void PortableMeanStddevNormalization(const float *input_vector, float *output_vector, int v_size, int n_batch)
void PortableZeroVector(float *vector, int v_size)
void MeanStddevNormalization(const float *input_vector, float *output_vector, int v_size, int n_batch)
void VectorBatchVectorCwiseProduct(const T *vector, int v_size, const T *batch_vector, int n_batch, T *result)
Definition TensorUtils.h:76
void Sub1Vector(const float *vector, int v_size, float *result)
void MatrixBatchVectorMultiplyAccumulate(const int8_t *matrix, const int m_rows, const int m_cols, const int8_t *vector, const float *scaling_factors, int n_batch, float *result, int result_stride)
void PortableApplyActivationToVector(const float *vector, int v_size, FusedActivationFunctionType activation, float *result)
void ApplyActivationToVector(const float *vector, int v_size, FusedActivationFunctionType activation, float *result)
void SymmetricQuantizeFloats(const float *values, const int size, int8_t *quantized_values, float *min, float *max, float *scaling_factor)
void VectorBatchVectorAssign(const float *vector, int v_size, int n_batch, float *batch_vector)
Definition TensorUtils.h:44
void PortableVectorBatchVectorAssign(const float *vector, int v_size, int n_batch, float *batch_vector)
void PortableVectorBatchVectorAdd(const float *vector, int v_size, int n_batch, float *batch_vector)
void CwiseClipping(float *vector, const int v_size, const float clipping_value)
Definition TensorUtils.h:34
void VectorVectorCwiseProductAccumulate(const T *__restrict__ vector1, const T *__restrict__ vector2, int v_size, T *__restrict__ result)
Definition TensorUtils.h:64
void VectorBatchVectorAdd(const float *vector, int v_size, int n_batch, float *batch_vector)
Definition TensorUtils.h:39
FusedActivationFunctionType
Definition Types.h:32
bool IsZeroVector(const float *vector, int v_size)
void VectorBatchVectorCwiseProductAccumulate(const T *vector, int v_size, const T *batch_vector, int n_batch, T *result)
Definition TensorUtils.h:92
Definition topk_v2.h:30
int32_t size[5]
Definition Slice.cpp:35