1 /*
2 * Copyright (C) 2018 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 #define LOG_TAG "Operations"
18
19 #include <vector>
20
21 #include "Operations.h"
22 #include "OperationsUtils.h"
23
24 #include "Tracing.h"
25
26 namespace android {
27 namespace nn {
28
29 template <typename Scalar>
splitGeneric(const Scalar * inputData,const Shape & inputShape,int32_t axis,const std::vector<Scalar * > * outputDataPtrs,const std::vector<Shape> & outputShapes)30 bool splitGeneric(const Scalar* inputData, const Shape& inputShape, int32_t axis,
31 const std::vector<Scalar*>* outputDataPtrs,
32 const std::vector<Shape>& outputShapes) {
33 NN_CHECK(handleNegativeAxis(inputShape, &axis));
34 int outerSize = 1;
35 for (int i = 0; i < axis; ++i) {
36 outerSize *= inputShape.dimensions[i];
37 }
38 int baseInnerSize = 1;
39 int concatDimensions = getNumberOfDimensions(inputShape);
40 for (int i = axis + 1; i < concatDimensions; ++i) {
41 baseInnerSize *= inputShape.dimensions[i];
42 }
43
44 const Scalar* inputPtr = inputData;
45 for (int k = 0; k < outerSize; k++) {
46 for (int i = 0; i < outputDataPtrs->size(); ++i) {
47 const int copySize = outputShapes[i].dimensions[axis] * baseInnerSize;
48 memcpy(outputDataPtrs->at(i) + k * copySize, inputPtr, copySize * sizeof(Scalar));
49 inputPtr += copySize;
50 }
51 }
52
53 return true;
54 }
55
splitFloat16(const _Float16 * inputData,const Shape & inputShape,int32_t axis,const std::vector<_Float16 * > * outputDataPtrs,const std::vector<Shape> & outputShapes)56 bool splitFloat16(const _Float16* inputData, const Shape& inputShape, int32_t axis,
57 const std::vector<_Float16*>* outputDataPtrs,
58 const std::vector<Shape>& outputShapes) {
59 NNTRACE_COMP("splitFloat16");
60 return splitGeneric<_Float16>(inputData, inputShape, axis, outputDataPtrs, outputShapes);
61 }
62
splitFloat32(const float * inputData,const Shape & inputShape,int32_t axis,const std::vector<float * > * outputDataPtrs,const std::vector<Shape> & outputShapes)63 bool splitFloat32(const float* inputData, const Shape& inputShape, int32_t axis,
64 const std::vector<float*>* outputDataPtrs,
65 const std::vector<Shape>& outputShapes) {
66 NNTRACE_COMP("splitFloat32");
67 return splitGeneric<float>(inputData, inputShape, axis, outputDataPtrs, outputShapes);
68 }
69
splitQuant8(const uint8_t * inputData,const Shape & inputShape,int32_t axis,const std::vector<uint8_t * > * outputDataPtrs,const std::vector<Shape> & outputShapes)70 bool splitQuant8(const uint8_t* inputData, const Shape& inputShape, int32_t axis,
71 const std::vector<uint8_t*>* outputDataPtrs,
72 const std::vector<Shape>& outputShapes) {
73 NNTRACE_COMP("splitQuant8");
74 return splitGeneric<uint8_t>(inputData, inputShape, axis, outputDataPtrs, outputShapes);
75 }
76
splitQuant8Signed(const int8_t * inputData,const Shape & inputShape,int32_t axis,const std::vector<int8_t * > * outputDataPtrs,const std::vector<Shape> & outputShapes)77 bool splitQuant8Signed(const int8_t* inputData, const Shape& inputShape, int32_t axis,
78 const std::vector<int8_t*>* outputDataPtrs,
79 const std::vector<Shape>& outputShapes) {
80 NNTRACE_COMP("splitQuant8Signed");
81 return splitGeneric<int8_t>(inputData, inputShape, axis, outputDataPtrs, outputShapes);
82 }
83
splitInt32(const int32_t * inputData,const Shape & inputShape,int32_t axis,const std::vector<int32_t * > * outputDataPtrs,const std::vector<Shape> & outputShapes)84 bool splitInt32(const int32_t* inputData, const Shape& inputShape, int32_t axis,
85 const std::vector<int32_t*>* outputDataPtrs,
86 const std::vector<Shape>& outputShapes) {
87 NNTRACE_COMP("splitInt32");
88 return splitGeneric<int32_t>(inputData, inputShape, axis, outputDataPtrs, outputShapes);
89 }
90
91 } // namespace nn
92 } // namespace android
93