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