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