• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 The Android Open Source Project
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 FRAMEWORKS_ML_NN_COMMON_OPERATIONS_INTERNAL_TENSOR_UTILS_H_
18 #define FRAMEWORKS_ML_NN_COMMON_OPERATIONS_INTERNAL_TENSOR_UTILS_H_
19 
20 #include "ActivationFunctor.h"
21 
22 namespace android {
23 namespace nn {
24 namespace tensor_utils {
25 
26 // Limit a float input f betweeen +abs_limit and -abs_limit.
27 float Clip(float f, float abs_limit);
28 
29 // Multiply a matrix by a batch vector, and store results in a batch-size
30 // vector using a stride value provided in result_stride. 'result_stride' shows
31 // how the number of elements between consecutive result values. For example
32 // result_stride = 1, will cause the output to look like this:
33 // [O_1, 0_2, ... O_rows] in memory, but result_stride = 3, will cause it to be
34 // arranged like this in memory: [O_1, x, x, 0_2, x, x, ..., O_rows]
35 void MatrixBatchVectorMultiplyAccumulate(const float* matrix, int m_rows,
36                                          int m_cols, const float* vector,
37                                          int n_batch, float* result,
38                                          int result_stride);
39 
40 // Cwise product of two vectors.
41 void VectorVectorCwiseProduct(const float* vector1, const float* vector2,
42                               int v_size, float* result);
43 
44 // Cwise product and accumulate of two vectors. Since it's a MAC opertation, the
45 // assumption here is that result array is initialized to valid values.
46 void VectorVectorCwiseProductAccumulate(const float* vector1,
47                                         const float* vector2, int v_size,
48                                         float* result);
49 
50 // Dot product of two vectors.
51 float VectorVectorDotProduct(const float* vector1, const float* vector2,
52                              int v_size);
53 
54 // Dot product of two batch vectors of size n_batch * v_size:
55 // vector1 = [x_1_1, x_1_2, ..., x_1_vsize,
56 //            x_2_1, x_2_2, ..., x_2_vsize,
57 //            ...
58 //            x_nbatch_1,..., x_nbatch_vsize]
59 // vector2 = [y_1_1, y_1_2, ..., y_1_vsize,
60 //            y_2_1, y_2_2, ..., y_2_vsize,
61 //            ...
62 //            y_nbatch_1,..., y_nbatch_vsize]
63 // Then result will be a vector of n_batch size which will be saved with a
64 // stride of result_stride in memory starting from 'result':
65 // [x_1_1 * y_1_1 + x_1_2 * y_1_2 + ... + x_1_vsize * y_1_vsize,
66 //  x_2_1 * y_2_1 + x_2_2 * y_2_2 + ... + x_2_vsize * y_2_vsize,
67 //  ...
68 //  x_nbatch_1 * y_nbatch_1 + ... + x_nbatch_vsize * y_nbatch_vsize]
69 void BatchVectorBatchVectorDotProduct(const float* vector1,
70                                       const float* vector2, int v_size,
71                                       int n_batch, float* result,
72                                       int result_stride);
73 
74 // Cwise product and accumulate of a vector and a batch-vector. Since it's a MAC
75 // operation, the assumption here is that result array is initialized to valid
76 // values.
77 void VectorBatchVectorCwiseProductAccumulate(const float* vector, int v_size,
78                                              const float* batch_vector,
79                                              int n_batch, float* result);
80 
81 // Batch vector initialization with another vector.
82 void VectorBatchVectorAssign(const float* vector, int v_size, int n_batch,
83                              float* batch_vector);
84 
85 // Apply sigmoid to elements of a vector.
86 void ApplySigmoidToVector(const float* vector, int v_size, float* result);
87 
88 // Apply activation function to elements of a vector.
89 void ApplyActivationToVector(const float* vector, int v_size,
90                              ActivationFn activation, float* result);
91 
92 // Copy vector to another vector.
93 void CopyVector(const float* vector, int v_size, float* result);
94 
95 // Compute "1.0f - elements of vector" (used in CIFG).
96 void Sub1Vector(const float* vector, int v_size, float* result);
97 
98 // Fill vector with 0.f.
99 void ZeroVector(float* vector, int v_size);
100 
101 // Clip elements of a vector using a abs_limit value.
102 void ClipVector(const float* vector, int v_size, float abs_limit,
103                 float* result);
104 
105 // Shift left a vector in place with v_size size.
106 void VectorShiftLeft(float* vector, int v_size, float shift_value);
107 
108 // Reduce-sum on a float input vector:
109 // input_vector: float pointer to input vector.
110 // input_stride: input vector stride.
111 // output_vector: float pointer to vector.
112 // output_size: output vector size.
113 // reduction_size: number of consecutive elements from input vector which are
114 // added to get one element of output.
115 void ReductionSumVector(const float* input_vector, int input_stride,
116                         float* output_vector, int output_size,
117                         int reduction_size);
118 }  // namespace tensor_utils
119 }  // namespace nn
120 }  // namespace android
121 
122 #endif  // FRAMEWORKS_ML_NN_COMMON_OPERATIONS_INTERNAL_TENSOR_UTILS_H_
123