diff --git a/mindspore/lite/nnacl/fp16/pad_fp16.c b/mindspore/lite/nnacl/fp16/pad_fp16.c new file mode 100644 index 0000000000..2ff775a867 --- /dev/null +++ b/mindspore/lite/nnacl/fp16/pad_fp16.c @@ -0,0 +1,35 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nnacl/fp16/pad_fp16.h" +#include "nnacl/common_func.h" + +void PadFp16(const float16_t *input_data, float16_t *output_data, const int *input_shape, const int *output_shape, + const int *paddings, const int tid, const int thread_num) { + int in[4], out[4]; + for (in[0] = 0; in[0] < input_shape[0]; in[0]++) { + out[0] = in[0] + paddings[0]; + for (in[1] = tid; in[1] < input_shape[1]; in[1] += thread_num) { + out[1] = in[1] + paddings[2]; + for (in[2] = 0; in[2] < input_shape[2]; in[2]++) { + out[2] = in[2] + paddings[4]; + float16_t *dst = output_data + offset(output_shape, out[0], out[1], out[2], paddings[6]); + const float16_t *src = input_data + offset(input_shape, in[0], in[1], in[2], 0); + memcpy(dst, src, input_shape[3] * sizeof(float16_t)); + } + } + } +} diff --git a/mindspore/lite/nnacl/fp16/pad_fp16.h b/mindspore/lite/nnacl/fp16/pad_fp16.h new file mode 100644 index 0000000000..e6c783829f --- /dev/null +++ b/mindspore/lite/nnacl/fp16/pad_fp16.h @@ -0,0 +1,32 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MINDSPORE_LITE_NNACL_FP16_PAD_FP16_H_ +#define MINDSPORE_LITE_NNACL_FP16_PAD_FP16_H_ + +#ifdef ENABLE_NEON +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif +void PadFp16(const float16_t *input_data, float16_t *output_data, const int *input_shape, const int *output_shape, + const int *paddings, const int tid, const int thread_num); +#ifdef __cplusplus +} +#endif + +#endif // MINDSPORE_LITE_NNACL_FP16_PAD_FP16_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc new file mode 100644 index 0000000000..2b8d1572fc --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc @@ -0,0 +1,101 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "src/runtime/kernel/arm/fp16/pad_fp16.h" +#include "src/runtime/kernel/arm/fp16/common_fp16.h" +#include "nnacl/fp16/cast_fp16.h" +#include "src/kernel_registry.h" +#include "src/runtime/runtime_api.h" + +using mindspore::kernel::KERNEL_ARCH::kCPU; +using mindspore::lite::KernelRegistrar; +using mindspore::lite::RET_ERROR; +using mindspore::lite::RET_OK; +using mindspore::schema::PrimitiveType_Pad; + +namespace mindspore::kernel { +int PadFp16CPUKernel::RunImpl(int task_id) { + auto input_data = reinterpret_cast(in_tensors_.at(0)->MutableData()); + auto output_data = reinterpret_cast(out_tensors_.at(0)->MutableData()); + + PadFp16(input_data, output_data, in_, out_, pad_param_->paddings_, task_id, context_->thread_num_); + return RET_OK; +} + +int PadFp16CPUKernel::Run() { + auto ret = Prepare(); + if (ret != RET_OK) { + MS_LOG(ERROR) << "Prepare fail!ret: " << ret; + return ret; + } + auto input_tensor = in_tensors_.at(0); + auto output_tensor = out_tensors_.at(0); + is_input_fp32_ = input_tensor->data_type() == kNumberTypeFloat32; + is_output_fp32_ = output_tensor->data_type() == kNumberTypeFloat32; + + input_ = ConvertInputFp32toFp16(input_tensor, context_); + output_ = MallocOutputFp16(output_tensor, context_); + if (input_ == nullptr || output_ == nullptr) { + FreeInputAndOutput(); + MS_LOG(ERROR) << "input or output is nullptr"; + return RET_ERROR; + } + + memset(output_, 0, output_tensor->ElementsNum() * sizeof(float16_t)); + ret = ParallelLaunch(THREAD_POOL_DEFAULT, PadImpl, this, op_parameter_->thread_num_); + if (ret != RET_OK) { + MS_LOG(ERROR) << "BatchnormRun error error_code[" << ret << "]"; + } + if (is_output_fp32_) { + Float16ToFloat32(output_, reinterpret_cast(output_tensor->MutableData()), output_tensor->ElementsNum()); + } + FreeInputAndOutput(); + return ret; +} + +void PadFp16CPUKernel::FreeInputAndOutput() { + if (is_input_fp32_) { + context_->allocator->Free(input_); + input_ = nullptr; + } + if (is_output_fp32_) { + context_->allocator->Free(output_); + output_ = nullptr; + } +} + +kernel::LiteKernel *CpuPadFp16KernelCreator(const std::vector &inputs, + const std::vector &outputs, + OpParameter *opParameter, const lite::Context *ctx, + const kernel::KernelKey &desc, + const mindspore::lite::PrimitiveC *primitive) { + auto *kernel = new (std::nothrow) PadFp16CPUKernel(opParameter, inputs, outputs, ctx, primitive); + if (kernel == nullptr) { + MS_LOG(ERROR) << "new PadFp16CPUKernel fail!"; + return nullptr; + } + auto ret = kernel->Init(); + if (ret != RET_OK) { + MS_LOG(ERROR) << "Init kernel failed, name: " << opParameter->name_ << ", type: " + << schema::EnumNamePrimitiveType(static_cast(opParameter->type_)); + delete kernel; + return nullptr; + } + return kernel; +} + +REG_KERNEL(kCPU, kNumberTypeFloat16, PrimitiveType_Pad, CpuPadFp16KernelCreator) +} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.h b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.h new file mode 100644 index 0000000000..caedf186b9 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.h @@ -0,0 +1,45 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP16_PAD_FP16_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP16_PAD_FP16_H_ + +#include +#include "src/runtime/kernel/arm/fp32/pad.h" +#include "nnacl/fp16/pad_fp16.h" + +namespace mindspore::kernel { +class PadFp16CPUKernel : public PadCPUKernel { + public: + PadFp16CPUKernel(OpParameter *parameter, const std::vector &inputs, + const std::vector &outputs, const lite::Context *ctx, + const mindspore::lite::PrimitiveC *primitive) + : PadCPUKernel(parameter, inputs, outputs, ctx, primitive) {} + + ~PadFp16CPUKernel() {} + + int Run() override; + int RunImpl(int task_id) override; + + private: + void FreeInputAndOutput(); + bool is_input_fp32_ = false; + bool is_output_fp32_ = false; + float16_t *input_ = nullptr; + float16_t *output_ = nullptr; +}; +} // namespace mindspore::kernel + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP16_PAD_FP16_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/pad.h b/mindspore/lite/src/runtime/kernel/arm/fp32/pad.h index 8818cf5be1..6ca5d10626 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/pad.h +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/pad.h @@ -28,7 +28,7 @@ class PadCPUKernel : public LiteKernel { PadCPUKernel(OpParameter *parameter, const std::vector &inputs, const std::vector &outputs, const lite::Context *ctx, const mindspore::lite::PrimitiveC *primitive) - : LiteKernel(parameter, inputs, outputs, ctx, primitive), context_(ctx) { + : LiteKernel(parameter, inputs, outputs, ctx, primitive) { pad_param_ = reinterpret_cast(parameter); } @@ -37,14 +37,15 @@ class PadCPUKernel : public LiteKernel { int Init() override; int ReSize() override; int Run() override; - int RunImpl(int task_id); + virtual int RunImpl(int task_id); - private: - const lite::Context *context_; + protected: const PadParameter *pad_param_; int in_[4] = {1, 1, 1, 1}; int out_[4] = {1, 1, 1, 1}; }; + +int PadImpl(void *cdata, int task_id); } // namespace mindspore::kernel #endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_PAD_H_