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 "Tile.h"
20
21 #include <algorithm>
22 #include <utility>
23
24 #include "Tracing.h"
25
26 namespace android {
27 namespace nn {
28 namespace tile {
29
30 namespace {
31
32 template <typename T>
CopyMultipleTimes(const T * in_data,int32_t in_size,int32_t multiplier,T * out_data)33 void CopyMultipleTimes(const T* in_data, int32_t in_size, int32_t multiplier, T* out_data) {
34 for (int i = 0; i < multiplier; ++i) {
35 const T* in_end = in_data + in_size;
36 T* new_out_data = std::copy(in_data, in_end, out_data);
37 in_data = out_data;
38 out_data = new_out_data;
39 }
40 }
41
42 template <typename T, typename M>
TileOneDimension(const Shape & input_shape,const T * in_data,const M * multipliers,T * out_data,int dimension)43 std::pair<int, int> TileOneDimension(const Shape& input_shape, const T* in_data,
44 const M* multipliers, T* out_data, int dimension) {
45 const int dimension_size = input_shape.dimensions[dimension];
46 if (static_cast<size_t>(dimension) == input_shape.dimensions.size() - 1) {
47 CopyMultipleTimes(in_data, dimension_size, multipliers[dimension], out_data);
48 return std::make_pair(dimension_size,
49 dimension_size * static_cast<int>(multipliers[dimension]));
50 }
51 int total_stride_size = 0, total_tiled_stride_size = 0;
52 const T* copy_from_data = in_data;
53 T* copy_to_data = out_data;
54 for (int i = 0; i < dimension_size; ++i) {
55 int stride_size = 0, tiled_stride_size = 0;
56 std::tie(stride_size, tiled_stride_size) = TileOneDimension(
57 input_shape, copy_from_data, multipliers, copy_to_data, dimension + 1);
58 copy_from_data += stride_size;
59 copy_to_data += tiled_stride_size;
60 total_stride_size += stride_size;
61 total_tiled_stride_size += tiled_stride_size;
62 }
63 CopyMultipleTimes(out_data, total_tiled_stride_size, multipliers[dimension] - 1,
64 out_data + total_tiled_stride_size);
65 return std::make_pair(total_stride_size, total_tiled_stride_size * multipliers[dimension]);
66 }
67
68 template <typename T>
tileImpl(const T * inputData,const Shape & inputShape,const int32_t * multiples,T * outputData,const Shape &)69 void tileImpl(const T* inputData, const Shape& inputShape, const int32_t* multiples, T* outputData,
70 const Shape& /*outputShape*/) {
71 TileOneDimension(inputShape, inputData, multiples, outputData, 0);
72 }
73
74 } // namespace
75
prepare(const Shape & input,const int32_t * multiples,const Shape &,Shape * output)76 bool prepare(const Shape& input, const int32_t* multiples, const Shape& /*multiplesShape*/,
77 Shape* output) {
78 output->type = input.type;
79 output->offset = input.offset;
80 output->scale = input.scale;
81
82 output->dimensions.assign(input.dimensions.begin(), input.dimensions.end());
83 for (size_t i = 0; i < output->dimensions.size(); ++i) {
84 output->dimensions[i] *= multiples[i];
85 }
86
87 return true;
88 }
89
eval(const uint8_t * inputData,const Shape & inputShape,const int32_t * multiples,uint8_t * outputData,const Shape & outputShape)90 bool eval(const uint8_t* inputData, const Shape& inputShape, const int32_t* multiples,
91 uint8_t* outputData, const Shape& outputShape) {
92 NNTRACE_TRANS("tile::eval");
93 #define ANDROID_NN_IMPL_TILE(operandType, dataType) \
94 case operandType: { \
95 NNTRACE_COMP_SWITCH("tileImpl::" #dataType); \
96 tileImpl(reinterpret_cast<const dataType*>(inputData), inputShape, multiples, \
97 reinterpret_cast<dataType*>(outputData), outputShape); \
98 return true; \
99 }
100
101 switch (inputShape.type) {
102 ANDROID_NN_IMPL_TILE(OperandType::TENSOR_FLOAT16, _Float16);
103 ANDROID_NN_IMPL_TILE(OperandType::TENSOR_FLOAT32, float);
104 ANDROID_NN_IMPL_TILE(OperandType::TENSOR_INT32, int32_t);
105 ANDROID_NN_IMPL_TILE(OperandType::TENSOR_QUANT8_ASYMM, uint8_t);
106 ANDROID_NN_IMPL_TILE(OperandType::TENSOR_QUANT8_ASYMM_SIGNED, int8_t);
107 default:
108 LOG(ERROR) << "Unsupported data type";
109 return false;
110 }
111 #undef ANDROID_NN_IMPL_TILE
112 }
113
114 } // namespace tile
115 } // namespace nn
116 } // namespace android
117