diff --git a/mindspore/lite/src/runtime/kernel/arm/base/prelu_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/prelu_base.cc new file mode 100644 index 0000000000000000000000000000000000000000..f08cb56950cef70f13f07f610bdb43146d1b68af --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/base/prelu_base.cc @@ -0,0 +1,58 @@ +/** + * 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/base/prelu_base.h" +#include +#include "src/runtime/kernel/arm/int8/prelu_int8.h" +#include "schema/model_generated.h" +#include "src/kernel_factory.h" +#include "include/errorcode.h" +#include "include/context.h" + +using mindspore::lite::KernelRegistrar; +using mindspore::lite::RET_ERROR; +using mindspore::lite::RET_OK; +using mindspore::schema::PrimitiveType_Prelu; + +namespace mindspore::kernel { +int PreluBaseCPUKernel::Init() {return RET_OK;} + +kernel::LiteKernel *CpuPreluInt8KernelCreator(const std::vector &inputs, + const std::vector &outputs, + OpParameter *opParameter, const Context *ctx, + const kernel::KernelKey &desc) { + if (opParameter == nullptr) { + MS_LOG(ERROR) << "Input opParameter is nullptr!"; + return nullptr; + } + MS_ASSERT(desc.type == schema::PrimitiveType_Prelu); + auto *kernel = new(std::nothrow) PreluInt8CPUKernel(opParameter, inputs, outputs, ctx); + if (kernel == nullptr) { + MS_LOG(ERROR) << "new PreluCPUKernel fail!"; + return nullptr; + } + auto ret = kernel->Init(); + if (ret != RET_OK) { + delete kernel; + MS_LOG(ERROR) << "Init kernel failed, name: " << opParameter->name_ << ", type: " + << schema::EnumNamePrimitiveType(static_cast(opParameter->type_)); + return nullptr; + } + return kernel; +} + +REG_KERNEL(kCPU, kNumberTypeInt8, PrimitiveType_Prelu, CpuPreluInt8KernelCreator) +} // namespace mindspore::kernel + diff --git a/mindspore/lite/src/runtime/kernel/arm/base/prelu_base.h b/mindspore/lite/src/runtime/kernel/arm/base/prelu_base.h new file mode 100644 index 0000000000000000000000000000000000000000..3b100236818b6f246d8503c8732bc662c176242d --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/base/prelu_base.h @@ -0,0 +1,52 @@ +/** + * 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_BASE_Prelu_BASE_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_Prelu_BASE_H_ + +#include +#include "src/lite_kernel.h" +#include "src/runtime/kernel/arm/nnacl/prelu_parameter.h" +#include "src/runtime/kernel/arm/base/layout_transform.h" + +using mindspore::lite::Context; + +namespace mindspore::kernel { +class PreluBaseCPUKernel : public LiteKernel { + public: + PreluBaseCPUKernel(OpParameter *parameter, const std::vector &inputs, + const std::vector &outputs, const Context *ctx) + : LiteKernel(parameter, inputs, outputs), ctx_(ctx), thread_count_(ctx->thread_num_) { + opParameter->thread_num_ = ctx->thread_num_; + prelu_param_ = reinterpret_cast(opParameter); + } + + ~PreluBaseCPUKernel() = default; + + int Init() override; + + int ReSize() override { return 0; } + + int Run() override { return 0; } + + protected: + int thread_count_; + const Context *ctx_; + PreluParameter *prelu_param_; +}; +} // namespace mindspore::kernel + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_Prelu_BASE_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/base/squeeze_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/squeeze_base.cc new file mode 100644 index 0000000000000000000000000000000000000000..ebc5107a6d3ac60c0ed82230b0adf190a4b88f72 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/base/squeeze_base.cc @@ -0,0 +1,57 @@ +/** + * 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/base/squeeze_base.h" +#include +#include "src/runtime/kernel/arm/int8/squeeze_int8.h" +#include "schema/model_generated.h" +#include "src/kernel_factory.h" +#include "include/errorcode.h" +#include "include/context.h" + +using mindspore::lite::KernelRegistrar; +using mindspore::lite::RET_ERROR; +using mindspore::lite::RET_OK; +using mindspore::schema::PrimitiveType_Squeeze; + +namespace mindspore::kernel { +int SqueezeBaseCPUKernel::Init() { return RET_OK; } + +kernel::LiteKernel *CpuSqueezeInt8KernelCreator(const std::vector &inputs, + const std::vector &outputs, + OpParameter *opParameter, const Context *ctx, + const kernel::KernelKey &desc) { + if (opParameter == nullptr) { + MS_LOG(ERROR) << "Input opParameter is nullptr!"; + return nullptr; + } + MS_ASSERT(desc.type == schema::PrimitiveType_Squeeze); + auto *kernel = new (std::nothrow) SqueezeInt8CPUKernel(opParameter, inputs, outputs, ctx); + if (kernel == nullptr) { + MS_LOG(ERROR) << "new SqueezeCPUKernel fail!"; + return nullptr; + } + auto ret = kernel->Init(); + if (ret != RET_OK) { + delete kernel; + MS_LOG(ERROR) << "Init kernel failed, name: " << opParameter->name_ << ", type: " + << schema::EnumNamePrimitiveType(static_cast(opParameter->type_)); + return nullptr; + } + return kernel; +} + +REG_KERNEL(kCPU, kNumberTypeInt8, PrimitiveType_Squeeze, CpuSqueezeInt8KernelCreator) +} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/base/squeeze_base.h b/mindspore/lite/src/runtime/kernel/arm/base/squeeze_base.h new file mode 100644 index 0000000000000000000000000000000000000000..66f10f936e7bf755f4c5c13f52c0436fb1a76f63 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/base/squeeze_base.h @@ -0,0 +1,51 @@ +/** + * 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_BASE_SQUEEZE_BASE_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_SQUEEZE_BASE_H_ + +#include +#include "src/lite_kernel.h" +#include "src/runtime/kernel/arm/nnacl/squeeze_parameter.h" +#include "src/runtime/kernel/arm/base/layout_transform.h" + +using mindspore::lite::Context; + +namespace mindspore::kernel { +class SqueezeBaseCPUKernel : public LiteKernel { + public: + SqueezeBaseCPUKernel(OpParameter *parameter, const std::vector &inputs, + const std::vector &outputs, const Context *ctx) + : LiteKernel(parameter, inputs, outputs), ctx_(ctx), thread_count_(ctx->thread_num_) { + opParameter->thread_num_ = ctx->thread_num_; + } + + virtual ~SqueezeBaseCPUKernel() = default; + + int Init() override; + + int ReSize() override { return 0; } + + int Run() override { return 0; } + + protected: + int thread_count_; + int *axis_; + const Context *ctx_; +}; +} // namespace mindspore::kernel + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_SQUEEZE_BASE_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/zeroslike.h b/mindspore/lite/src/runtime/kernel/arm/fp32/zeroslike.h index cd6aad26679b462978e19e7c15c15c14d8b3a423..1c55aa1f35d3ada83557ddf46fdb27a498a6660b 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/zeroslike.h +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/zeroslike.h @@ -19,7 +19,6 @@ #include #include "src/lite_kernel.h" - namespace mindspore::kernel { class ZerosLikeCPUKernel : public LiteKernel { public: diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/activation.cc b/mindspore/lite/src/runtime/kernel/arm/int8/activation.cc index 32d933d5e8fed4804ea9528665bc0ac2422bda27..daaecd272cef1aac48371aa80d6e913845ed3b15 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/activation.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/activation.cc @@ -17,6 +17,7 @@ #include "src/runtime/kernel/arm/fp32/activation.h" #include "src/runtime/kernel/arm/int8/relu_int8.h" #include "src/runtime/kernel/arm/int8/hswish_int8.h" +#include "src/runtime/kernel/arm/int8/sigmoid_int8.h" #include "schema/model_generated.h" #include "src/kernel_registry.h" #include "src/runtime/runtime_api.h" @@ -47,6 +48,9 @@ kernel::LiteKernel *CpuActivationInt8KernelCreator(const std::vector +#include "src/runtime/kernel/arm/nnacl/int8/prelu_int8.h" +#include "src/runtime/runtime_api.h" +#include "src/kernel_registry.h" +#include "include/errorcode.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_Prelu; + +namespace mindspore::kernel { +int PreluInt8CPUKernel::Init() { + PreluBaseCPUKernel::Init(); + auto *input_tensor = inputs_.at(kInputIndex); + auto in_quant_args = input_tensor->GetQuantParams(); + quant_prelu_parm_->quant_arg.in_args_.scale_ = in_quant_args.front().scale; + quant_prelu_parm_->quant_arg.in_args_.zp_ = in_quant_args.front().zeroPoint; + auto input_dim = input_tensor->shape().size(); + MS_ASSERT(input_dim <= CROP_OFFSET_MAX_SIZE); + quant_prelu_parm_->input_dim_ = input_dim; + quant_prelu_parm_->element_num = inputs_[0]->Size(); + auto *out_tensor = outputs_.at(kOutputIndex); + auto out_quant_args = out_tensor->GetQuantParams(); + quant_prelu_parm_->quant_arg.out_args_.scale_ = out_quant_args.front().scale; + quant_prelu_parm_->quant_arg.out_args_.zp_ = out_quant_args.front().zeroPoint; + quant_prelu_parm_->in_shape_ = input_tensor->shape().data(); + quant_prelu_parm_->out_shape_ = out_tensor->shape().data(); + quant_prelu_parm_->quant_arg.output_activation_max_ = std::numeric_limits::max(); + quant_prelu_parm_->quant_arg.output_activation_min_ = std::numeric_limits::min(); + return RET_OK; +} + +int PreluInt8CPUKernel::ReSize() { return 0; } + +int PreluInt8CPUKernel::Run() { + auto ret = LiteBackendParallelLaunch(PreluInt8Run, this, thread_count_); + if (ret != RET_OK) { + MS_LOG(ERROR) << "RunPreluParam failed. errorcode: "; + } + return RET_OK; +} +int PreluInt8Run(int task_id, LiteParallelGroupEnv *penv, void *cdata) { + auto prelu = reinterpret_cast(cdata); + prelu->DoExecute(task_id); + return RET_OK; +} + +int PreluInt8CPUKernel::DoExecute(int task_id) { + auto input_tensor = inputs_.at(kInputIndex); + auto out_tensor = outputs_.at(kOutputIndex); + int8_t *input_data = reinterpret_cast(input_tensor->Data()); + int8_t *output_data = reinterpret_cast(out_tensor->Data()); + prelu(input_data, output_data, quant_prelu_parm_, task_id); + return RET_OK; +} + +} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/prelu_int8.h b/mindspore/lite/src/runtime/kernel/arm/int8/prelu_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..c93fb1256b19c8da613d60767a5371677d852d6d --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/int8/prelu_int8.h @@ -0,0 +1,49 @@ +/** + * 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_INT8_PRELU_INT8_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_PRELU_INT8_H_ + +#include +#include "src/lite_kernel.h" +#include "include/context.h" +#include "src/runtime/kernel/arm/base/prelu_base.h" +#include "src/runtime/runtime_api.h" + +using mindspore::lite::Context; + +namespace mindspore::kernel { +class PreluInt8CPUKernel : public PreluBaseCPUKernel { + public: + PreluInt8CPUKernel(OpParameter *parameter, const std::vector &inputs, + const std::vector &outputs, const Context *ctx) + : PreluBaseCPUKernel(parameter, inputs, outputs, ctx) { + quant_prelu_parm_ = reinterpret_cast(opParameter); + } + ~PreluInt8CPUKernel() override {} + + int Init() override; + int ReSize() override; + int Run() override; + int DoExecute(int task_id); + + private: + PreluParameter *quant_prelu_parm_; +}; +int PreluInt8Run(int task_id, LiteParallelGroupEnv *penv, void *cdata); +} // namespace mindspore::kernel + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_PRELU_INT8_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.cc new file mode 100644 index 0000000000000000000000000000000000000000..9adfa5165d806f3cb502247cbf13f6bb3aef5158 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.cc @@ -0,0 +1,99 @@ +/** + * 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/int8/sigmoid_int8.h" +#include +#include "src/runtime/kernel/arm/nnacl/int8/sigmoid_int8.h" +#include "schema/model_generated.h" +#include "src/kernel_registry.h" +#include "src/runtime/runtime_api.h" +#include "include/errorcode.h" + +using mindspore::kernel::KERNEL_ARCH::kCPU; +using mindspore::lite::KernelRegistrar; +using mindspore::lite::RET_ERROR; +using mindspore::lite::RET_OK; +using mindspore::schema::ActivationType_SIGMOID; + +namespace mindspore::kernel { +int SigmoidInt8CPUKernel::Init() { + lite::tensor::Tensor *input = inputs_.at(0); + lite::tensor::Tensor *output = outputs_.at(0); + MS_ASSERT(input); + MS_ASSERT(output); + + quant_arg_.input_scale = input->GetQuantParams().front().scale; + quant_arg_.input_zp = input->GetQuantParams().front().zeroPoint; + quant_arg_.output_scale = output->GetQuantParams().front().scale; + quant_arg_.output_zp = output->GetQuantParams().front().zeroPoint; + + const float output_multiplier = (1.0f / 128.0f) * quant_arg_.input_scale / quant_arg_.output_scale; + + int32_t output_multiplier_fixedpoint; + QuantizeMultiplier(output_multiplier, &output_multiplier_fixedpoint, &quant_arg_.output_multiplier_exponent); + MS_ASSERT(quant_arg_.output_multiplier_exponent <= 0); + MultiplierInt32ToInt16(output_multiplier_fixedpoint, &quant_arg_.output_multiplier_fixedpoint_int16); + + const float relu6_multiplier = (1.0f / 128.0f) * quant_arg_.input_scale / (3.0f / 32768.0f); + int32_t relu6_multiplier_fixedpoint; + QuantizeMultiplier(relu6_multiplier, &relu6_multiplier_fixedpoint, &quant_arg_.relu6_multiplier_exponent); + MultiplierInt32ToInt16(relu6_multiplier_fixedpoint, &quant_arg_.relu6_multiplier_fixedpoint_int16); + + return RET_OK; +} + +void SigmoidInt8CPUKernel::MultiplierInt32ToInt16(int32_t input, int16_t *output) { + MS_ASSERT(input >= 0); + if (input >= std::numeric_limits::max() - (1 << 15)) { + *output = std::numeric_limits::max(); + return; + } + *output = (input + (1 << 15)) >> 16; +} + +int SigmoidInt8CPUKernel::ReSize() { return RET_OK; } + +int SigmoidInt8CPUKernel::DoActivation(int task_id) { + auto input_addr = reinterpret_cast(inputs_.at(0)->Data()); + auto output_addr = reinterpret_cast(outputs_.at(0)->Data()); + auto length = inputs_.at(0)->ElementsNum(); + + int stride = UP_DIV(length, thread_count_); + int count = MSMIN(stride, length - stride * task_id); + + SigmoidInt8(input_addr + stride * task_id, count, output_addr + stride * task_id, &quant_arg_); + return RET_OK; +} + +int SigmoidInt8Run(int task_id, LiteParallelGroupEnv *penv, void *cdata) { + auto activation_kernel = reinterpret_cast(cdata); + auto error_code = activation_kernel->DoActivation(task_id); + if (error_code != RET_OK) { + MS_LOG(ERROR) << "SigmoidInt8Run error task_id[" << task_id << "] error_code[" << error_code << "]"; + return RET_ERROR; + } + return RET_OK; +} + +int SigmoidInt8CPUKernel::Run() { + int error_code = LiteBackendParallelLaunch(SigmoidInt8Run, this, thread_count_); + if (error_code != RET_OK) { + MS_LOG(ERROR) << "SigmoidInt8Run function error error_code[" << error_code << "]"; + return RET_ERROR; + } + return RET_OK; +} +} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.h b/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..da0aa408f58dd0b24b0a1d2df6bf02f8b3588e0f --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.h @@ -0,0 +1,44 @@ +/** + * 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_BACKEND_ARM_INT8_SIGMOID_INT8_H_ +#define MINDSPORE_LITE_SRC_BACKEND_ARM_INT8_SIGMOID_INT8_H_ + +#include +#include "src/lite_kernel.h" +#include "src/runtime/kernel/arm/nnacl/int8/sigmoid_int8.h" + +namespace mindspore::kernel { +class SigmoidInt8CPUKernel : public LiteKernel { + public: + SigmoidInt8CPUKernel(OpParameter *parameter, const std::vector &inputs, + const std::vector &outputs, const lite::Context *ctx) + : LiteKernel(parameter, inputs, outputs), thread_count_(ctx->thread_num_) {} + ~SigmoidInt8CPUKernel() override = default; + + int Init() override; + int ReSize() override; + int Run() override; + int DoActivation(int task_id); + + private: + int thread_count_; + SigmoidQuantArg quant_arg_; + void MultiplierInt32ToInt16(int32_t input, int16_t *output); +}; +} // namespace mindspore::kernel + +#endif // MINDSPORE_LITE_SRC_BACKEND_ARM_INT8_SIGMOID_INT8_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/squeeze_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/squeeze_int8.cc new file mode 100644 index 0000000000000000000000000000000000000000..7cd464902266fbb606dddd945708556e47e1480b --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/int8/squeeze_int8.cc @@ -0,0 +1,167 @@ +/** + * 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/nnacl/int8/squeeze_int8.h" +#include "src/runtime/kernel/arm/int8/squeeze_int8.h" +#include "src/runtime/kernel/arm/nnacl/squeeze_parameter.h" + +#include "schema/model_generated.h" +#include "src/runtime/runtime_api.h" +#include "include/errorcode.h" + +using mindspore::kernel::KERNEL_ARCH::kCPU; +using mindspore::lite::RET_ERROR; +using mindspore::lite::RET_OK; + +namespace mindspore::kernel { + +int SqueezeInt8CPUKernel::Init() { + SqueezeBaseCPUKernel::Init(); + quant_Squeeze_parm_ = new (std::nothrow) SqueezeQuantArg; + auto input_num = inputs_.size(); + quant_Squeeze_parm_->input_num_ = input_num; + quant_Squeeze_parm_->input_sizes_ = reinterpret_cast(malloc(sizeof(int) * input_num)); + if (quant_Squeeze_parm_->input_sizes_ == nullptr) { + MS_LOG(ERROR) << "Null pointer reference: quant_Squeeze_parm_->input_sizes_."; + return RET_ERROR; + } + + for (size_t i = 0; i < input_num; i++) { + quant_Squeeze_parm_->input_sizes_[i] = 1; + } + quant_Squeeze_parm_->input_shapes_ = reinterpret_cast(malloc(sizeof(int *) * input_num)); + if (quant_Squeeze_parm_->input_shapes_ == nullptr) { + MS_LOG(ERROR) << "Null pointer reference: quant_Squeeze_parm_->input_shapes_."; + return RET_ERROR; + } + + for (size_t i = 0; i < input_num; i++) { + auto *input_tensor = inputs_.at(i); + MS_ASSERT(input_tensor != nullptr); + auto input_size = input_tensor->shape().size(); + MS_ASSERT(input_size != NULL); + quant_Squeeze_parm_->input_shapes_[i] = reinterpret_cast(malloc(sizeof(int) * input_size)); + if (quant_Squeeze_parm_->input_shapes_[i] == nullptr) { + MS_LOG(ERROR) << "Null pointer reference: quant_Squeeze_parm_->input_shapes_[" << i << "]."; + return RET_ERROR; + } + + ::memcpy(quant_Squeeze_parm_->input_shapes_[i], input_tensor->shape().data(), sizeof(int) * input_size); + for (size_t j = 0; j < input_size; j++) { + auto *input_tensor_tmp = inputs_.at(i); + auto input_shape = input_tensor_tmp->shape()[j]; + quant_Squeeze_parm_->input_sizes_[i] *= input_shape; + } + } + quant_Squeeze_parm_->axis_ = 0; + quant_Squeeze_parm_->in_quant_args_ = reinterpret_cast(malloc(sizeof(QuantArg) * input_num)); + if (quant_Squeeze_parm_->in_quant_args_ == nullptr) { + MS_LOG(ERROR) << "Null pointer reference: quant_Squeeze_parm_->in_quant_args_."; + return RET_ERROR; + } + + for (size_t i = 0; i < input_num; i++) { + auto *input_tensor = inputs_.at(i); + auto quant_args = input_tensor->GetQuantParams(); + MS_ASSERT(quant_args.size() == 1); + quant_Squeeze_parm_->in_quant_args_[i].scale_ = quant_args.front().scale; + quant_Squeeze_parm_->in_quant_args_[i].zp_ = quant_args.front().zeroPoint; + } + + MS_ASSERT(outputs_.size() == 1); + auto output_tensor = outputs_.at(0); + MS_ASSERT(output_tensor != nullptr); + auto output_shape = output_tensor->shape(); + MS_ASSERT(output_shape != NULL); + auto output_dim = output_shape.size(); + quant_Squeeze_parm_->output_dim_ = output_dim; + int output_size = 1; + for (size_t i = 0; i < output_dim; i++) { + output_size *= output_shape[i]; + } + quant_Squeeze_parm_->output_size_ = output_size; + + quant_Squeeze_parm_->output_shape_ = new int[output_size]; + ::memcpy(quant_Squeeze_parm_->output_shape_, output_shape.data(), sizeof(int) * output_size); + + auto quant_args = output_tensor->GetQuantParams(); + MS_ASSERT(quant_args.size() == 1); + quant_Squeeze_parm_->out_quant_args_.scale_ = quant_args.front().scale; + quant_Squeeze_parm_->out_quant_args_.zp_ = quant_args.front().zeroPoint; + + return RET_OK; +} + +int SqueezeInt8CPUKernel::ReSize() { return 0; } + +int SqueezeInt8CPUKernel::Run() { + auto input_dim = quant_Squeeze_parm_->input_num_; + int8_t **inputs_array = reinterpret_cast(malloc(sizeof(int8_t *) * input_dim)); + for (size_t i = 0; i < input_dim; i++) { + auto input_size = quant_Squeeze_parm_->input_sizes_[i]; + inputs_array[i] = reinterpret_cast(malloc(sizeof(int8_t) * input_size)); + auto input_type = inputs_[i]->data_type(); + if (input_type == kNumberTypeUInt8) { + uint8_t *input_tmp = reinterpret_cast(inputs_[i]->Data()); + for (size_t j = 0; j < input_size; j++) { + inputs_array[i][j] = (int8_t)(input_tmp[j] - 128); + } + for (size_t j = 0; j < input_dim; j++) { + quant_Squeeze_parm_->in_quant_args_[j].zp_ -= 128; + } + quant_Squeeze_parm_->out_quant_args_.zp_ -= 128; + } else { + ::memcpy(inputs_array[i], inputs_.at(i)->Data(), sizeof(int8_t) * input_size); + } + } + int8_t *output_addr = reinterpret_cast(outputs_.at(0)->Data()); + auto output_type = outputs_[0]->data_type(); + if (output_type == kNumberTypeUInt8) { + auto output_size = quant_Squeeze_parm_->output_size_; + for (size_t i = 0; i < output_size; i++) { + output_addr[i] = (uint8_t)(output_addr[i] + 128); + } + } + + for (int i = 0; i < input_dim; i++) { + free(*(inputs_array + i)); + } + + auto ret = LiteBackendParallelLaunch(SqueezeInt8Run, this, thread_count_); + if (ret != RET_OK) { + MS_LOG(ERROR) << "RunSqueezeParam failed. errorcode: "; + } + return RET_OK; +} + +int SqueezeInt8Run(int task_id, LiteParallelGroupEnv *penv, void *cdata) { + auto Squeeze = reinterpret_cast(cdata); + Squeeze->DoExecute(task_id); + return RET_OK; +} + +int SqueezeInt8CPUKernel::DoExecute(int task_id) { + auto input_tensor = inputs_.at(kInputIndex); + auto out_tensor = outputs_.at(kOutputIndex); + int8_t *input_data = reinterpret_cast(input_tensor->Data()); + int8_t *output_data = reinterpret_cast(out_tensor->Data()); + + size_t data_size = inputs_.front()->Size(); + Squeeze(&input_data, output_data, task_id, quant_Squeeze_parm_, para_, data_size); + return RET_OK; +} + +} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/squeeze_int8.h b/mindspore/lite/src/runtime/kernel/arm/int8/squeeze_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..9ff467ffc781251c7867a7c778ea673a15acc013 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/int8/squeeze_int8.h @@ -0,0 +1,50 @@ +/** + * 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_INT8_SQUEEZE_INT8_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_SQUEEZE_INT8_H_ + +#include +#include "src/lite_kernel.h" +#include "include/context.h" +#include "src/runtime/runtime_api.h" +#include "src/runtime/kernel/arm/base/squeeze_base.h" + +using mindspore::lite::Context; + +namespace mindspore::kernel { +class SqueezeInt8CPUKernel : public SqueezeBaseCPUKernel { + public: + SqueezeInt8CPUKernel(OpParameter *parameter, const std::vector &inputs, + const std::vector &outputs, const Context *ctx) + : SqueezeBaseCPUKernel(parameter, inputs, outputs, ctx) {} + ~SqueezeInt8CPUKernel() override { delete quant_Squeeze_parm_; } + + int Init() override; + int ReSize() override; + int Run() override; + int DoExecute(int tId); + + private: + SqueezeParameter *para_; + SqueezeQuantArg *quant_Squeeze_parm_; +}; + +int SqueezeInt8Run(int task_id, LiteParallelGroupEnv *penv, void *cdata); + +} // namespace mindspore::kernel + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_SQUEEZE_INT8_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/unsqueeze_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/unsqueeze_int8.cc new file mode 100644 index 0000000000000000000000000000000000000000..42936af7131eae38105e44dc2810fa54aa444d54 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/int8/unsqueeze_int8.cc @@ -0,0 +1,115 @@ +/** + * 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/nnacl/int8/unsqueeze_int8.h" +#include "src/runtime/kernel/arm/int8/unsqueeze_int8.h" +#include "schema/model_generated.h" +#include "src/kernel_registry.h" +#include "include/errorcode.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_Unsqueeze; + +namespace mindspore::kernel { +int Unsqueezeint8CPUKernel::Init() { + auto *input_tensor = inputs_.at(0); + auto quant_args = input_tensor->GetQuantParams(); + MS_ASSERT(quant_args.size() == 1); + Unsq_para_->quant_arg.in_quant_args_.scale_ = quant_args.front().scale; + Unsq_para_->quant_arg.in_quant_args_.zp_ = quant_args.front().zeroPoint; + + auto out_quant_args = input_tensor->GetQuantParams(); + Unsq_para_->quant_arg.out_quant_args_.scale_ = out_quant_args.front().scale; + Unsq_para_->quant_arg.out_quant_args_.zp_ = out_quant_args.front().zeroPoint; + Unsq_para_->thread_count_ = thread_count_; + + int ret = ReSize(); + return ret; +} + +int Unsqueezeint8CPUKernel::ReSize() { + data_size_ = inputs_.at(0)->ElementsNum(); + thread_sz_count_ = MSMIN(thread_count_, data_size_); + thread_sz_stride_ = UP_DIV(data_size_, thread_sz_count_); + return RET_OK; +} + +int Unsqueezeint8CPUKernel::DoUnsqueeze(int task_id) { + size_t size = MSMIN(thread_sz_stride_, data_size_ - task_id * thread_sz_stride_); + if (size == 0) { + return RET_OK; + } + + auto input_ptr = reinterpret_cast(inputs_.front()->Data()); + auto output_ptr = reinterpret_cast(outputs_.front()->Data()); + size_t data_size = outputs_.front()->Size(); + + int ret = Unsqueeze(input_ptr, output_ptr, Unsq_para_, data_size, task_id); + if (ret != RET_OK) { + MS_LOG(ERROR) << "UnsqueezeRun error task_id[" << task_id << "] error_code[" << ret << "]"; + return ret; + } + return RET_OK; +} + +int UnsqueezeIn8Run(int task_id, LiteParallelGroupEnv *penv, void *cdata) { + auto g_kernel = reinterpret_cast(cdata); + auto ret = g_kernel->DoUnsqueeze(task_id); + if (ret != RET_OK) { + MS_LOG(ERROR) << "UnsqueezeRun error task_id[" << task_id << "] error_code[" << ret << "]"; + return ret; + } + return RET_OK; +} + +int Unsqueezeint8CPUKernel::Run() { + in_ptr_ = reinterpret_cast(inputs_.at(0)->Data()); + out_ptr_ = reinterpret_cast(outputs_.at(0)->Data()); + int ret = LiteBackendParallelLaunch(UnsqueezeIn8Run, this, thread_sz_count_); + if (ret != RET_OK) { + MS_LOG(ERROR) << "UnsqueezeRun error error_code[" << ret << "]"; + return ret; + } + return RET_OK; +} + +kernel::LiteKernel *CpuUnsqueezeInt8KernelCreator(const std::vector &inputs, + const std::vector &outputs, + OpParameter *opParameter, const lite::Context *ctx, + const kernel::KernelKey &desc) { + MS_ASSERT(opParameter != nullptr); + MS_ASSERT(desc.type == schema::PrimitiveType_Unsqueeze); + auto *kernel = new (std::nothrow) Unsqueezeint8CPUKernel(opParameter, inputs, outputs, ctx); + if (kernel == nullptr) { + MS_LOG(ERROR) << "new UnsqueezeCPUKernel 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, kNumberTypeInt8, PrimitiveType_Unsqueeze, CpuUnsqueezeInt8KernelCreator) +} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/unsqueeze_int8.h b/mindspore/lite/src/runtime/kernel/arm/int8/unsqueeze_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..070cf1fdd1850695a81169aa1ffe792476ce0377 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/int8/unsqueeze_int8.h @@ -0,0 +1,56 @@ +/** + * 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_FP32_UNSQUEEZE_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_UNSQUEEZE_H_ + +#include +#include "src/lite_kernel.h" +#include "include/context.h" +#include "src/runtime/kernel/arm/nnacl/int8/unsqueeze_int8.h" +#include "src/runtime/kernel/arm/base/layout_transform.h" + +using mindspore::lite::Context; + +namespace mindspore::kernel { +class Unsqueezeint8CPUKernel : public LiteKernel { + public: + Unsqueezeint8CPUKernel(OpParameter *parameter, const std::vector &inputs, + const std::vector &outputs, const Context *ctx) + : LiteKernel(parameter, inputs, outputs), ctx_(ctx), thread_count_(ctx->thread_num_) { + Unsq_para_ = reinterpret_cast(opParameter); + Unsq_para_->thread_count_ = opParameter->thread_num_; + } + ~Unsqueezeint8CPUKernel() = default; + + int Init() override; + int ReSize() override; + int Run() override; + int DoUnsqueeze(int task_id); + + private: + UnSqueezeQuantArg *quant_Unsqueeze_parm_; + UnSqueezeParameter *Unsq_para_; + int thread_count_; + int thread_sz_count_; + int thread_sz_stride_; + int data_size_; + float *in_ptr_; + float *out_ptr_; + const Context *ctx_; +}; +} // namespace mindspore::kernel + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_UNSQUEEZE_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/prelu_int8.cc b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/prelu_int8.cc new file mode 100644 index 0000000000000000000000000000000000000000..496379ec37fd5a791cb50bfab2b94831f1d37008 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/prelu_int8.cc @@ -0,0 +1,57 @@ +/** + * 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/nnacl/prelu_parameter.h" +#include "src/runtime/kernel/arm/nnacl/int8/prelu_int8.h" + +void prelu(int8_t *inputs, int8_t *output_ptr, PreluParameter *quant_prelu_parm, int task_id) { + float output_scale = quant_prelu_parm->quant_arg.out_args_.scale_; + int output_zp = quant_prelu_parm->quant_arg.out_args_.zp_; + float output_inverse_scale = 1.f / output_scale; + int output_dim = quant_prelu_parm->input_dim_; + + QuantArg *input_quant = new QuantArg; + for (int i = 0; i < output_dim; i++) { + input_quant[i].scale_ = quant_prelu_parm->quant_arg.in_args_.scale_; + input_quant[i].zp_ = quant_prelu_parm->quant_arg.in_args_.zp_; + } + + for (int i = 0; i < output_dim; i++) { + float scale = input_quant[i].scale_ * output_inverse_scale; + float bias = -input_quant[i].zp_ * scale; + for (int j = task_id; j < quant_prelu_parm->element_num; j += quant_prelu_parm->op_parameter_.thread_num_) { + if (inputs[j] <= 0) { + int32_t output_tmp = round(inputs[j] * quant_prelu_parm->alpha_ * scale + bias) + output_zp; + if (output_tmp > 127) { + output_ptr[j] = 127; + } else if (output_tmp < -128) { + output_ptr[j] = -128; + } else { + output_ptr[j] = (int8_t)output_tmp; + } + } else { + int32_t output_tmp = round(inputs[j] * scale + bias) + output_zp; + if (output_tmp > 127) { + output_ptr[j] = 127; + } else if (output_tmp < -128) { + output_ptr[j] = -128; + } else { + output_ptr[j] = (int8_t)output_tmp; + } + } + } + } +} diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/prelu_int8.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/prelu_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..187540c20db748e4c25f9fe5de163d62feab5828 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/prelu_int8.h @@ -0,0 +1,25 @@ +/** + * 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_OPCLIB_INT8_PRELU_INT8_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_PRELU_INT8_H_ + +#include "src/runtime/kernel/arm/nnacl/op_base.h" +#include "src/runtime/kernel/arm/nnacl/prelu_parameter.h" + +void prelu(int8_t *inputs, int8_t *output_ptr, PreluParameter *quant_Prelu_parm, int task_id); + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_PRELU_INT8_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/sigmoid_int8.cc b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/sigmoid_int8.cc new file mode 100644 index 0000000000000000000000000000000000000000..af0cf889bc1354ac638171f82a9e2cf5fd5e60dd --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/sigmoid_int8.cc @@ -0,0 +1,30 @@ +/** + * 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/nnacl/int8/sigmoid_int8.h" + +int SigmoidInt8(const int8_t *src, int length, int8_t *dst, SigmoidQuantArg *arg) { + for (int i = 0; i < length; i++) { + const int16_t input_value = src[i] - arg->input_zp; + int16_t output; + output = round(1 / arg->output_scale * (1 / (1 + exp(-arg->input_scale * input_value)))); + output += arg->output_zp; + output = MSMIN(output, 127); + output = MSMAX(output, -128); + dst[i] = (int8_t)output; + } + return 0; +} diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/sigmoid_int8.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/sigmoid_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..5be0108d3cfc5a187adb2085828da5da4c75e7c0 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/sigmoid_int8.h @@ -0,0 +1,37 @@ +/** + * 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_OPCLIB_INT8_Sigmoid_INT8_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_Sigmoid_INT8_H_ + +#include +#include "src/runtime/kernel/arm/nnacl/op_base.h" +#include "src/runtime/kernel/arm/nnacl/errorcode.h" +#include "src/runtime/kernel/arm/nnacl/quantization/fixed_point.h" + +struct SigmoidQuantArg { + double input_scale; + int32_t input_zp; + double output_scale; + int32_t output_zp; + int16_t relu6_multiplier_fixedpoint_int16; + int32_t relu6_multiplier_exponent; + int16_t output_multiplier_fixedpoint_int16; + int32_t output_multiplier_exponent; +}; + +int SigmoidInt8(const int8_t *src, int length, int8_t *dst, SigmoidQuantArg *arg); + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_Sigmoid_INT8_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/squeeze_int8.cc b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/squeeze_int8.cc new file mode 100644 index 0000000000000000000000000000000000000000..013f4fe89abca5734d83050b651399ea04691240 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/squeeze_int8.cc @@ -0,0 +1,41 @@ +/** + * 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/nnacl/squeeze_parameter.h" +#include "src/runtime/kernel/arm/nnacl/int8/squeeze_int8.h" +#include + +void Squeeze(int8_t **inputs, int8_t *output_ptr, int task_id, SqueezeQuantArg *quant_Squeeze_parm, + SqueezeParameter *para_, size_t osize) { + float output_scale = quant_Squeeze_parm->out_quant_args_.scale_; + float output_inverse_scale = 1.f / output_scale; + QuantArg *input_quant = quant_Squeeze_parm->in_quant_args_; + int output_zp = quant_Squeeze_parm->out_quant_args_.zp_; + + int i = 0; + int8_t *input_ptr = inputs[0]; + for (int j = task_id; j < osize; j += para_->op_parameter_.thread_num_) { + float scale = input_quant[i].scale_ * output_inverse_scale; + float bias = -input_quant[i].zp_ * scale; + int32_t output_tmp = round(input_ptr[j] * scale + bias) + output_zp; + if (output_tmp > 127) { + output_ptr[j] = 127; + } else if (output_tmp < -128) { + output_ptr[j] = -128; + } else { + output_ptr[j] = (int8_t)output_tmp; + } + } +} diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/squeeze_int8.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/squeeze_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..6f9de3d515e40e4723a21333d0419e631e5b801c --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/squeeze_int8.h @@ -0,0 +1,28 @@ +/** + * 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_OPCLIB_INT8_SQUEEZE_INT8_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_SQUEEZE_INT8_H_ + +#include "src/runtime/kernel/arm/nnacl/op_base.h" +#include "src/runtime/kernel/arm/nnacl/int8/squeeze_int8.h" +#include "src/runtime/kernel/arm/int8/squeeze_int8.h" + + +void Squeeze(int8_t **inputs, int8_t *output_ptr, int task_id, SqueezeQuantArg *quant_Squeeze_parm, + SqueezeParameter *para_, size_t osize); + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_SQUEEZE_INT8_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/unsqueeze_int8.cc b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/unsqueeze_int8.cc new file mode 100644 index 0000000000000000000000000000000000000000..f996ad4efb7b01107f005355eeb0a168a6adb6ba --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/unsqueeze_int8.cc @@ -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. + */ + +#include "src/runtime/kernel/arm/nnacl/unsqueeze_parameter.h" +#include "src/runtime/kernel/arm/nnacl/int8/unsqueeze_int8.h" +#include +#include "src/runtime/kernel/arm/nnacl/errorcode.h" + +int Unsqueeze(int8_t *input_ptr, int8_t *output_ptr, UnSqueezeParameter *para_, size_t data_size, int task_id) { + float output_scale = para_->quant_arg.out_quant_args_.scale_; + int8_t output_zp = para_->quant_arg.out_quant_args_.zp_; + float input_scale = para_->quant_arg.in_quant_args_.scale_; + int8_t input_zp = para_->quant_arg.in_quant_args_.zp_; + + for (int i = task_id; i < data_size; i += para_->thread_count_) { + output_ptr[i] = output_zp + round(1 / output_scale * input_scale * (input_ptr[i] - input_zp)); + } + return 0; +} diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/unsqueeze_int8.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/unsqueeze_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..18639ef369e5dec9e9b712d549003828ce371020 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/unsqueeze_int8.h @@ -0,0 +1,24 @@ +/** + * 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_OPCLIB_INT8_UNSQUEEZE_INT8_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_UNSQUEEZE_INT8_H_ + +#include "src/runtime/kernel/arm/nnacl/op_base.h" +#include "src/runtime/kernel/arm/nnacl/unsqueeze_parameter.h" + +int Unsqueeze(int8_t *input_ptr, int8_t *output_ptr, UnSqueezeParameter *para_, size_t data_size, int task_id); +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_UNSQUEEZE_INT8_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/prelu_parameter.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/prelu_parameter.h new file mode 100644 index 0000000000000000000000000000000000000000..b7db1db9495b2c0c8059e9233a61d1a2c806be54 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/prelu_parameter.h @@ -0,0 +1,37 @@ +/** + * 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_OPCLIB_PRELU_PARAMETER_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_PRELU_PARAMETER_H_ + +#include "src/runtime/kernel/arm/nnacl/op_base.h" +#define PRELU_OFFSET_MAX_SIZE 4 + +struct PreluParameter { + OpParameter op_parameter_; + PreluQuantArg quant_arg; + double alpha_; + int thread_count_; + int64_t offset_[PRELU_OFFSET_MAX_SIZE]; + int64_t in_offset_[PRELU_OFFSET_MAX_SIZE]; + int64_t axis_; + const int *in_shape_; + const int *out_shape_; + int input_dim_; + int element_num; +}; + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_PRELU_PARAMETER_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/quantization/quantize.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/quantization/quantize.h index 421d582c5e2eaf87288dc931f47723985b69760c..eed29fa470baa70862f5e249126f1f137bf42d1b 100644 --- a/mindspore/lite/src/runtime/kernel/arm/nnacl/quantization/quantize.h +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/quantization/quantize.h @@ -40,16 +40,76 @@ struct ConvQuantArg { }; struct ConcatQuantArg { + int *input_sizes_; + int output_size_; + int **input_shapes_; + int *output_shape_; + float alpha; + size_t input_num_; + size_t output_dim_; + QuantArg *in_quant_args_; + QuantArg out_quant_args_; +}; + +struct SqueezeQuantArg { + int *input_sizes_; + int output_size_; + int **input_shapes_; + int *output_shape_; + float alpha; + int axis_; + size_t input_num_; + size_t output_dim_; + QuantArg *in_quant_args_; + QuantArg out_quant_args_; +}; + +struct UnSqueezeQuantArg { + int *input_sizes_; + int output_size_; + int **input_shapes_; + int *output_shape_; + float alpha; + int axis_; + size_t input_num_; + size_t output_dim_; + QuantArg in_quant_args_; + QuantArg out_quant_args_; +}; + + +struct PreluQuantArg { int *input_sizes_; int output_size_; int **input_shapes_; int *output_shape_; size_t input_num_; size_t output_dim_; + float alpha_; + QuantArg in_args_; + QuantArg out_args_; + int output_activation_min_; + int output_activation_max_; QuantArg *in_quant_args_; QuantArg out_quant_args_; }; +/*struct SigmoidQuantArg { + int *input_sizes_; + int output_size_; + int **input_shapes_; + int *output_shape_; + size_t input_num_; + size_t output_dim_; + float alpha_; + QuantArg in_args_; + QuantArg out_args_; + int output_activation_min_; + int output_activation_max_; + QuantArg *in_quant_args_; + QuantArg out_quant_args_; +};*/ + struct MatmulQuantArg { QuantArg input; QuantArg weight; diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/sigmoid_parameter.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/sigmoid_parameter.h new file mode 100644 index 0000000000000000000000000000000000000000..dc51784bfb000944bc24a3853ab340d332728b46 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/sigmoid_parameter.h @@ -0,0 +1,37 @@ +/** + * 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_OPCLIB_SIGMOID_PARAMETER_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_SIGMOID_PARAMETER_H_ + +#include "src/runtime/kernel/arm/nnacl/op_base.h" +#define SIGMOID_OFFSET_MAX_SIZE 4 + +struct SigmoidParameter { + OpParameter op_parameter_; + SigmoidQuantArg quant_arg; + double alpha_; + int thread_count_; + int64_t offset_[PRELU_OFFSET_MAX_SIZE]; + int64_t in_offset_[PRELU_OFFSET_MAX_SIZE]; + int64_t axis_; + const int *in_shape_; + const int *out_shape_; + int input_dim_; + int element_num; +}; + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_SIGMOID_PARAMETER_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/squeeze_parameter.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/squeeze_parameter.h new file mode 100644 index 0000000000000000000000000000000000000000..cb8eb0b248ef7711511de64c93e83b680c255c43 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/squeeze_parameter.h @@ -0,0 +1,37 @@ +/** + * 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_OPCLIB_SQUEEZE_PARAMETER_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_SQUEEZE_PARAMETER_H_ +#include "src/runtime/kernel/arm/nnacl/op_base.h" + +#define SQUEEZE_OFFSET_MAX_SIZE 4 + +struct SqueezeParameter { + OpParameter op_parameter_; + SqueezeQuantArg quant_arg; + int thread_count_; + int thread_id_; + int offset_size_; + int64_t offset_[SQUEEZE_OFFSET_MAX_SIZE]; + int64_t in_offset_[SQUEEZE_OFFSET_MAX_SIZE]; + int64_t axis_; + const int *in_shape_; + const int *out_shape_; + int input_dim_; +}; + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_SQUEEZE_PARAMETER_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/unsqueeze_parameter.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/unsqueeze_parameter.h new file mode 100644 index 0000000000000000000000000000000000000000..1a1e64a395b44b597c6eb4add6b132bf1883ea5a --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/unsqueeze_parameter.h @@ -0,0 +1,37 @@ +/** + * 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_OPCLIB_UNSQUEEZE_PARAMETER_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_UNSQUEEZE_PARAMETER_H_ +#include "src/runtime/kernel/arm/nnacl/op_base.h" + +#define UNSQUEEZE_OFFSET_MAX_SIZE 4 + +struct UnSqueezeParameter { + OpParameter op_parameter_; + UnSqueezeQuantArg quant_arg; + int thread_count_; + int thread_id_; + int offset_size_; + int64_t offset_[UNSQUEEZE_OFFSET_MAX_SIZE]; + int64_t in_offset_[UNSQUEEZE_OFFSET_MAX_SIZE]; + int64_t axis_; + const int *in_shape_; + const int *out_shape_; + int input_dim_; +}; + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_SQUEEZE_PARAMETER_H_ diff --git a/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/prelu_int8_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/prelu_int8_tests.cc new file mode 100644 index 0000000000000000000000000000000000000000..d3260227611d8c212e3b625d819a466ef75c755c --- /dev/null +++ b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/prelu_int8_tests.cc @@ -0,0 +1,95 @@ +/** + * 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 +#include "utils/log_adapter.h" +#include "common/common_test.h" +#include "mindspore/lite/src/runtime/kernel/arm/nnacl/prelu_parameter.h" +#include "mindspore/lite/src/kernel_registry.h" +#include "mindspore/lite/src/lite_kernel.h" +#include "mindspore/lite/src/ir/tensor.h" + +namespace mindspore { + +class TestPreluInt8 : public mindspore::Common { + public: + TestPreluInt8() {} +}; + +TEST_F(TestPreluInt8, prelu_1) { + std::vector input1 = {1, -2, 3, 4, 5, 6, 7, -8}; + std::vector shape1 = {8}; + std::vector input(1, nullptr); + input[0] = input1.data(); + + const int output_size = 8; + int8_t output[8]; + std::vector output_shape = {8}; + lite::tensor::QuantArg input_quant_arg; + input_quant_arg.scale = 1.0; + input_quant_arg.zeroPoint = 0; + lite::tensor::QuantArg output_quant_arg; + output_quant_arg.scale = 1.0; + output_quant_arg.zeroPoint = 0; + + lite::tensor::Tensor *input_tensor1 = new lite::tensor::Tensor; + TypeId tid_int8 = kNumberTypeInt8; + input_tensor1->SetData(input1.data()); + input_tensor1->set_shape(shape1); + input_tensor1->AddQuantParam(input_quant_arg); + input_tensor1->set_data_type(tid_int8); + + std::vector inputs_tensor(1); + inputs_tensor[0] = input_tensor1; + + std::vector outputs_tensor(1); + lite::tensor::Tensor *output0_tensor = new lite::tensor::Tensor; + output0_tensor->SetData(output); + output0_tensor->set_shape(output_shape); + output0_tensor->AddQuantParam(output_quant_arg); + output0_tensor->set_data_type(tid_int8); + outputs_tensor[0] = output0_tensor; + + PreluParameter op_param; + op_param.op_parameter_.type_ = schema::PrimitiveType_Prelu; + + op_param.alpha_ = 0.25; + + lite::Context *ctx = new lite::Context; + ctx->thread_num_ = 2; + op_param.axis_ = 0.25; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Prelu}; + auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc); + ASSERT_NE(creator, nullptr); + kernel::LiteKernel *kernel = + creator(inputs_tensor, outputs_tensor, reinterpret_cast(&op_param), ctx, desc); + ASSERT_NE(kernel, nullptr); + auto output_tensor_shape = output0_tensor->shape(); + ASSERT_EQ(output_tensor_shape, output_shape); + kernel->Run(); + + std::vector except_result = {1, -1, 3, 4, 5, 6, 7, -2}; + PrintData("output data", output, output_size); + PrintData("output data shape", output_tensor_shape.data(), output_tensor_shape.size()); + CompareOutputData(output, except_result.data(), output_size, 0.000001); + + input_tensor1->SetData(nullptr); + output0_tensor->SetData(nullptr); + delete input_tensor1; + delete ctx; +} + +} // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/sigmoid_int8_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/sigmoid_int8_tests.cc new file mode 100644 index 0000000000000000000000000000000000000000..86ceb4c294ac0f2f65c5667cab1ba7664443464c --- /dev/null +++ b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/sigmoid_int8_tests.cc @@ -0,0 +1,70 @@ +/** + * 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 +#include "common/common_test.h" +#include "mindspore/lite/src/runtime/kernel/arm/nnacl/fp32/activation.h" +#include "mindspore/lite/src/kernel_registry.h" +#include "mindspore/lite/include/context.h" + +namespace mindspore { +class TestSigmoidInt8 : public mindspore::Common { + public: + TestSigmoidInt8() {} +}; + +TEST_F(TestSigmoidInt8, Sigmoid) { + lite::tensor::Tensor in_tensor(kNumberTypeInt8, {4, 4}); + lite::tensor::Tensor out_tensor(kNumberTypeInt8, {4, 4}); + + int8_t input_data[] = {0, 0, 0, 0, 1, 1, 1, 1}; // -3.5f, -3.0f, -2.5f, 0.f, 2.5f, 3.0f, 3.5f, 6.0f + int8_t output_data[8] = {0}; + in_tensor.SetData(input_data); + out_tensor.SetData(output_data); + + const lite::tensor::QuantArg quant_in = {1.0, 0}; // -4.0 -- 7.0 + const lite::tensor::QuantArg quant_out = {1.0, 0}; // -3.0 -- 7.0 + in_tensor.AddQuantParam(quant_in); + out_tensor.AddQuantParam(quant_out); + + std::vector inputs = {&in_tensor}; + std::vector outputs = {&out_tensor}; + + ActivationParameter parameter = {0}; + parameter.op_parameter_.type_ = schema::PrimitiveType_Activation; + parameter.type_ = schema::ActivationType_SIGMOID; + + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Activation}; + + auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc); + ASSERT_NE(creator, nullptr); + + auto ctx = std::make_shared(); + auto kernel = creator(inputs, outputs, reinterpret_cast(¶meter), ctx.get(), desc); + ASSERT_NE(kernel, nullptr); + + auto ret = kernel->Run(); + EXPECT_EQ(0, ret); + + int8_t expect[8] = {1, 1, 1, 1, 1, 1, 1, 1}; // 0, 0, -0.208333, 0, 2.29167, 3, 3.5, 6 + for (int i = 0; i < 8; ++i) { + EXPECT_EQ(output_data[i], expect[i]); + } + + in_tensor.SetData(nullptr); + out_tensor.SetData(nullptr); +} +} // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/squeeze_int8_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/squeeze_int8_tests.cc new file mode 100644 index 0000000000000000000000000000000000000000..d77bc2eb795feccacde89ff0302c8fccff05dcbb --- /dev/null +++ b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/squeeze_int8_tests.cc @@ -0,0 +1,95 @@ +/** + * 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 +#include "utils/log_adapter.h" +#include "common/common_test.h" +#include "mindspore/lite/src/runtime/kernel/arm/nnacl/squeeze_parameter.h" +#include "mindspore/lite/src/kernel_registry.h" +#include "mindspore/lite/src/lite_kernel.h" +#include "mindspore/lite/src/ir/tensor.h" + +namespace mindspore { + +class TestSqueezeInt8 : public mindspore::Common { + public: + TestSqueezeInt8() {} +}; + +TEST_F(TestSqueezeInt8, Squeeze_1d_axis0_offset0_quant0_thread2) { + std::vector input1 = {1, 2, 3, 4, 5, 6, 7, 8}; + std::vector shape1 = {8, 1}; + std::vector input(1, nullptr); + input[0] = input1.data(); + + const int output_size = 8; + int8_t output[8]; + std::vector output_shape = {8}; + lite::tensor::QuantArg input_quant_arg; + input_quant_arg.scale = 1.0; + input_quant_arg.zeroPoint = 0; + lite::tensor::QuantArg output_quant_arg; + output_quant_arg.scale = 1.0; + output_quant_arg.zeroPoint = 0; + + lite::tensor::Tensor *input_tensor1 = new lite::tensor::Tensor; + TypeId tid_int8 = kNumberTypeInt8; + input_tensor1->SetData(input1.data()); + input_tensor1->set_shape(shape1); + input_tensor1->AddQuantParam(input_quant_arg); + input_tensor1->set_data_type(tid_int8); + + std::vector inputs_tensor(1); + inputs_tensor[0] = input_tensor1; + + std::vector outputs_tensor(1); + lite::tensor::Tensor *output0_tensor = new lite::tensor::Tensor; + output0_tensor->SetData(output); + output0_tensor->set_shape(output_shape); + output0_tensor->AddQuantParam(output_quant_arg); + output0_tensor->set_data_type(tid_int8); + outputs_tensor[0] = output0_tensor; + + SqueezeParameter op_param; + op_param.op_parameter_.type_ = schema::PrimitiveType_Squeeze; + lite::Context *ctx = new lite::Context; + ctx->thread_num_ = 2; + op_param.axis_ = 0; + op_param.offset_[0] = 1; + op_param.offset_size_ = 1; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Squeeze}; + auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc); + ASSERT_NE(creator, nullptr); + kernel::LiteKernel *kernel = + creator(inputs_tensor, outputs_tensor, reinterpret_cast(&op_param), ctx, desc); + ASSERT_NE(kernel, nullptr); + auto output_tensor_shape = output0_tensor->shape(); + ASSERT_EQ(output_tensor_shape, output_shape); + kernel->Run(); + + std::vector except_result = {1, 2, 3, 4, 5, 6, 7, 8}; + PrintData("output data", output, output_size); + PrintData("output data shape", output_tensor_shape.data(), output_tensor_shape.size()); + CompareOutputData(output, except_result.data(), output_size, 0.000001); + + input_tensor1->SetData(nullptr); + output0_tensor->SetData(nullptr); + delete input_tensor1; + delete output0_tensor; + delete ctx; +} + +} // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/unsqueeze_int8_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/unsqueeze_int8_tests.cc new file mode 100644 index 0000000000000000000000000000000000000000..59edaffa5b773f3261b60228c81c74947282e7c4 --- /dev/null +++ b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/unsqueeze_int8_tests.cc @@ -0,0 +1,95 @@ +/** + * 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 +#include "utils/log_adapter.h" +#include "common/common_test.h" +#include "mindspore/lite/src/runtime/kernel/arm/nnacl/unsqueeze_parameter.h" +#include "mindspore/lite/src/kernel_registry.h" +#include "mindspore/lite/src/lite_kernel.h" +#include "mindspore/lite/src/ir/tensor.h" + +namespace mindspore { + +class TestUnsqueezeInt8 : public mindspore::Common { + public: + TestUnsqueezeInt8() {} +}; + +TEST_F(TestUnsqueezeInt8, Unsqueeze_1) { + std::vector input1 = {1, 2, 3, 4, 5, 6, 7, 8}; + std::vector shape1 = {8}; + std::vector input(1, nullptr); + input[0] = input1.data(); + + const int output_size = 8; + int8_t output[8]; + std::vector output_shape = {8, 1}; + lite::tensor::QuantArg input_quant_arg; + input_quant_arg.scale = 1.0; + input_quant_arg.zeroPoint = 0; + lite::tensor::QuantArg output_quant_arg; + output_quant_arg.scale = 1.0; + output_quant_arg.zeroPoint = 0; + + lite::tensor::Tensor *input_tensor1 = new lite::tensor::Tensor; + TypeId tid_int8 = kNumberTypeInt8; + input_tensor1->SetData(input1.data()); + input_tensor1->set_shape(shape1); + input_tensor1->AddQuantParam(input_quant_arg); + input_tensor1->set_data_type(tid_int8); + + std::vector inputs_tensor(1); + inputs_tensor[0] = input_tensor1; + + std::vector outputs_tensor(1); + lite::tensor::Tensor *output0_tensor = new lite::tensor::Tensor; + output0_tensor->SetData(output); + output0_tensor->set_shape(output_shape); + output0_tensor->AddQuantParam(output_quant_arg); + output0_tensor->set_data_type(tid_int8); + outputs_tensor[0] = output0_tensor; + + UnSqueezeParameter op_param; + op_param.op_parameter_.type_ = schema::PrimitiveType_Unsqueeze; + lite::Context *ctx = new lite::Context; + ctx->thread_num_ = 2; + op_param.axis_ = 0; + op_param.offset_[0] = 1; + op_param.offset_size_ = 1; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Unsqueeze}; + auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc); + ASSERT_NE(creator, nullptr); + kernel::LiteKernel *kernel = + creator(inputs_tensor, outputs_tensor, reinterpret_cast(&op_param), ctx, desc); + ASSERT_NE(kernel, nullptr); + auto output_tensor_shape = output0_tensor->shape(); + ASSERT_EQ(output_tensor_shape, output_shape); + kernel->Run(); + + std::vector except_result = {1, 2, 3, 4, 5, 6, 7, 8}; + PrintData("output data", output, output_size); + PrintData("output data shape", output_tensor_shape.data(), output_tensor_shape.size()); + CompareOutputData(output, except_result.data(), output_size, 0.000001); + + input_tensor1->SetData(nullptr); + output0_tensor->SetData(nullptr); + delete input_tensor1; + delete output0_tensor; + delete ctx; +} + +} // namespace mindspore