1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
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 #include "src/runtime/kernel/arm/base/tile_base.h"
18 #include "src/kernel_registry.h"
19 #include "nnacl/nnacl_common.h"
20 #include "include/errorcode.h"
21 #include "nnacl/errorcode.h"
22
23 using mindspore::lite::KernelRegistrar;
24 using mindspore::lite::RET_ERROR;
25 using mindspore::lite::RET_OK;
26 using mindspore::schema::PrimitiveType_TileFusion;
27
28 namespace mindspore::kernel {
29 namespace {
30 constexpr size_t kDoubleInputsSize = 2;
31 }
Init()32 int TileCPUKernel::Init() {
33 CHECK_LESS_RETURN(in_tensors_.size(), 1);
34 CHECK_LESS_RETURN(out_tensors_.size(), 1);
35 if (!InferShapeDone()) {
36 return RET_OK;
37 }
38 return ReSize();
39 }
40
ReSize()41 int TileCPUKernel::ReSize() {
42 tile_parameter_ = reinterpret_cast<TileParameter *>(op_parameter_);
43 CHECK_NULL_RETURN(tile_parameter_);
44 if (in_tensors_.size() == kDoubleInputsSize) {
45 if (in_tensors_[1]->ElementsNum() > static_cast<int>(in_tensors_[0]->shape().size())) {
46 MS_LOG(ERROR) << "tile's input1 data_num cannot be larger than input0's shape_size.";
47 return RET_ERROR;
48 }
49 if (in_tensors_[1]->data_type() != kNumberTypeInt && in_tensors_[1]->data_type() != kNumberTypeInt32) {
50 MS_LOG(ERROR) << "in_tensors_[1]->data_type():" << in_tensors_[1]->data_type()
51 << " must be kNumberTypeInt32 or kNumberTypeInt!";
52 return RET_ERROR;
53 }
54 auto input1_addr = reinterpret_cast<int *>(in_tensors_[1]->data());
55 for (int i = 0; i < in_tensors_[1]->ElementsNum(); ++i) {
56 tile_parameter_->dims_[i] = i;
57 tile_parameter_->multiples_[i] = input1_addr[i];
58 }
59 }
60 tile_parameter_->in_dim_ = in_tensors_.at(0)->shape().size();
61 CHECK_LESS_RETURN(tile_parameter_->in_dim_, 1);
62 for (int i = 0; i < tile_parameter_->in_dim_; ++i) {
63 tile_parameter_->in_shape_[i] = in_tensors_.at(0)->shape().at(i);
64 tile_parameter_->out_shape_[i] = out_tensors_.at(0)->shape().at(i);
65 }
66 ComputeStrides(tile_parameter_->in_shape_, tile_parameter_->in_strides_, tile_parameter_->in_dim_);
67 ComputeStrides(tile_parameter_->out_shape_, tile_parameter_->out_strides_, tile_parameter_->in_dim_);
68
69 auto data_type = in_tensors_.at(0)->data_type();
70 if (data_type == kNumberTypeFloat32 || data_type == kNumberTypeInt32) {
71 tile_parameter_->data_size_ = sizeof(float);
72 } else if (data_type == kNumberTypeFloat16) {
73 tile_parameter_->data_size_ = sizeof(float) / 2;
74 } else {
75 MS_LOG(ERROR) << "tile not support data type: " << data_type;
76 return RET_ERROR;
77 }
78
79 return FillOneDimTileParam();
80 }
81
SimpleTile(void * cdata,int task_id,float lhs_scale,float rhs_scale)82 int SimpleTile(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
83 CHECK_NULL_RETURN(cdata);
84 auto kernel = reinterpret_cast<TileCPUKernel *>(cdata);
85 auto ret = kernel->SimpleTileImpl(task_id);
86 if (ret != RET_OK) {
87 MS_LOG(ERROR) << "SimpleTile error task_id[" << task_id << "] error_code[" << ret << "]";
88 return ret;
89 }
90 return RET_OK;
91 }
92
FillOneDimTileParam()93 int TileCPUKernel::FillOneDimTileParam() {
94 // check if tile exact one dim
95 int large_one_multiple_count = 0;
96 int multiple = 0;
97 int mul_index = 0;
98 CHECK_LESS_RETURN(MAX_TILE_DIM_SIZE - 1, tile_parameter_->in_dim_);
99 for (auto i = 0; i < tile_parameter_->in_dim_; ++i) {
100 if (tile_parameter_->multiples_[i] > 1) {
101 large_one_multiple_count++;
102 multiple = tile_parameter_->multiples_[i];
103 mul_index = i;
104 }
105 }
106 one_dim_tile_ = large_one_multiple_count == 1;
107 if (one_dim_tile_) {
108 tile_parameter_->fast_multiple_ = static_cast<size_t>(multiple);
109 MS_CHECK_FALSE(INT_MUL_OVERFLOW(tile_parameter_->in_shape_[mul_index], tile_parameter_->in_strides_[mul_index]),
110 mindspore::lite::RET_ERROR);
111 tile_parameter_->fast_stride_ =
112 static_cast<size_t>(tile_parameter_->in_shape_[mul_index] * tile_parameter_->in_strides_[mul_index]);
113 CHECK_LESS_RETURN(tile_parameter_->fast_stride_, 1);
114 tile_parameter_->fast_outer_size_ =
115 static_cast<size_t>(in_tensors_.at(0)->ElementsNum()) / tile_parameter_->fast_stride_;
116 }
117 return RET_OK;
118 }
119
SimpleTileImpl(int task_id)120 int TileCPUKernel::SimpleTileImpl(int task_id) {
121 CHECK_LESS_RETURN(static_cast<size_t>(op_parameter_->thread_num_), 1);
122 size_t unit = UP_DIV(tile_parameter_->fast_outer_size_, static_cast<size_t>(op_parameter_->thread_num_));
123 if (unit == 0 && task_id > 0) {
124 return RET_OK;
125 }
126 MS_CHECK_FALSE(INT_MUL_OVERFLOW(unit, static_cast<size_t>(task_id)), RET_ERROR);
127 size_t begin = unit * static_cast<size_t>(task_id);
128 size_t end = MSMIN(begin + unit, tile_parameter_->fast_outer_size_);
129 TileSimple(input_addr_, output_addr_, begin, end, tile_parameter_);
130 return RET_OK;
131 }
132
RunSimpleTile()133 int TileCPUKernel::RunSimpleTile() {
134 auto ret = ParallelLaunch(this->ms_context_, SimpleTile, this, op_parameter_->thread_num_);
135 if (ret != RET_OK) {
136 MS_LOG(ERROR) << "RunSimpleTile error code[" << ret << "]";
137 return ret;
138 }
139 return RET_OK;
140 }
141
Run()142 int TileCPUKernel::Run() {
143 auto data_type = in_tensors_.at(0)->data_type();
144 tile_parameter_->data_size_ = lite::DataTypeSize(data_type);
145 input_addr_ = reinterpret_cast<uint8_t *>(in_tensors_.at(0)->data());
146 output_addr_ = reinterpret_cast<uint8_t *>(out_tensors_.at(0)->data());
147 CHECK_NULL_RETURN(input_addr_);
148 CHECK_NULL_RETURN(output_addr_);
149 if (one_dim_tile_) {
150 return RunSimpleTile();
151 }
152 Tile(input_addr_, output_addr_, reinterpret_cast<TileParameter *>(op_parameter_));
153 return RET_OK;
154 }
155
156 REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_TileFusion, LiteKernelCreator<TileCPUKernel>)
157 REG_KERNEL(kCPU, kNumberTypeInt32, PrimitiveType_TileFusion, LiteKernelCreator<TileCPUKernel>)
158 REG_KERNEL(kCPU, kNumberTypeFloat16, PrimitiveType_TileFusion, LiteKernelCreator<TileCPUKernel>)
159 } // namespace mindspore::kernel
160