diff --git a/mindspore/lite/src/populate_parameter.cc b/mindspore/lite/src/populate_parameter.cc index e792fb9b2a4f144a6423fa156d8dcdb1cbf083ca..afbd2ec0a0b726949a84a0ad4b6923fd426067e4 100644 --- a/mindspore/lite/src/populate_parameter.cc +++ b/mindspore/lite/src/populate_parameter.cc @@ -43,7 +43,7 @@ #include "src/runtime/kernel/arm/opclib/fp32/range.h" #include "src/runtime/kernel/arm/opclib/fp32/local_response_norm.h" #include "src/runtime/kernel/arm/opclib/fp32/expandDims.h" -#include "src/runtime/kernel/arm/opclib/fp32/arithmetic_self.h" +#include "src/runtime/kernel/arm/opclib/arithmetic_self_parameter.h" #include "src/runtime/kernel/arm/opclib/pad_parameter.h" #include "src/runtime/kernel/arm/opclib/fp32/fill.h" #include "src/runtime/kernel/arm/opclib/transpose.h" diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_self.h b/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_self.h index c6be54dd678cafd2695850abf286c3b2293a568b..d92c4deda218dd8609659c753d40bdaee6613f65 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_self.h +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_self.h @@ -20,6 +20,7 @@ #include #include "src/lite_kernel.h" #include "src/runtime/kernel/arm/opclib/fp32/arithmetic_self.h" +#include "src/runtime/kernel/arm/opclib/arithmetic_self_parameter.h" #include "schema/model_generated.h" #include "include/context.h" diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/arithmetic_self_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/arithmetic_self_int8.cc new file mode 100644 index 0000000000000000000000000000000000000000..aa821fd4e2f4e230e31ca0d9087aec87177479b1 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/int8/arithmetic_self_int8.cc @@ -0,0 +1,120 @@ +/** + * 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/arithmetic_self_int8.h" +#include +#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; + +namespace mindspore::kernel { +int ArithmeticSelfInt8CPUKernel::Init() { + int ret = ReSize(); + auto *input_tensor = inputs_.at(kInputIndex); + auto in_quant_args = input_tensor->GetQuantParams(); + arithmeticSelfParameter_->quant_arg_.in_args_.scale_ = in_quant_args.front().scale; + arithmeticSelfParameter_->quant_arg_.in_args_.zp_ = in_quant_args.front().zeroPoint; + + auto *out_tensor = outputs_.at(kOutputIndex); + auto out_quant_args = out_tensor->GetQuantParams(); + arithmeticSelfParameter_->quant_arg_.out_args_.scale_ = out_quant_args.front().scale; + arithmeticSelfParameter_->quant_arg_.out_args_.zp_ = out_quant_args.front().zeroPoint; + + arithmeticSelfParameter_->quant_arg_.output_activation_max_ = std::numeric_limits::max(); + arithmeticSelfParameter_->quant_arg_.output_activation_min_ = std::numeric_limits::min(); + return ret; +} + +int ArithmeticSelfInt8CPUKernel::ReSize() { + data_size_ = inputs_[0]->ElementsNum(); + thread_sz_count_ = MSMIN(thread_count_, data_size_); + thread_sz_stride_ = UP_DIV(data_size_, thread_sz_count_); + return RET_OK; +} + +int ArithmeticSelfInt8Runs(int task_id, LiteParallelGroupEnv *penv, void *cdata) { + auto g_kernel = reinterpret_cast(cdata); + auto ret = g_kernel->DoArithmeticSelf(task_id); + if (ret != RET_OK) { + MS_LOG(ERROR) << "ArithmeticSelfRuns error task_id[" << task_id << "] error_code[" << ret << "]"; + return ret; + } + return RET_OK; +} + +int ArithmeticSelfInt8CPUKernel::DoArithmeticSelf(int task_id) { + int size = MSMIN(thread_sz_stride_, data_size_ - task_id * thread_sz_stride_); + if (size <= 0) { + return RET_OK; + } + int offset = task_id * thread_sz_stride_; + if (arithmeticSelf_run_) { + auto ret = arithmeticSelf_run_(in_ptr_ + offset, out_ptr_ + offset, size, arithmeticSelfParameter_->quant_arg_); + if (ret != RET_OK) { + MS_LOG(ERROR) << "Run failed, illegal input! "; + return ret; + } + } else { + MS_LOG(ERROR) << "Run function is null! "; + return RET_ERROR; + } + return RET_OK; +} + +int ArithmeticSelfInt8CPUKernel::Run() { + auto input_tensor = inputs_.at(0); + auto out_tensor = outputs_.at(0); + in_ptr_ = reinterpret_cast(input_tensor->Data()); + out_ptr_ = reinterpret_cast(out_tensor->Data()); + int ret = LiteBackendParallelLaunch(ArithmeticSelfInt8Runs, this, thread_sz_count_); + if (ret != RET_OK) { + MS_LOG(ERROR) << "ArithmeticSelfRun error error_code[" << ret << "]"; + return ret; + } + return RET_OK; +} + +kernel::LiteKernel *CpuArithmeticSelfInt8KernelCreator(const std::vector &inputs, + const std::vector &outputs, + OpParameter *opParameter, const lite::Context *ctx, + const kernel::KernelKey &desc) { + MS_ASSERT(opParameter != nullptr); + if (opParameter == nullptr) { + MS_LOG(ERROR) << "Creator failed, opParameter is nullptr!"; + return nullptr; + } + auto *kernel = new (std::nothrow) ArithmeticSelfInt8CPUKernel(opParameter, inputs, outputs, ctx); + MS_ASSERT(kernel != 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_Round, CpuArithmeticSelfInt8KernelCreator) +REG_KERNEL(kCPU, kNumberTypeInt8, PrimitiveType_Floor, CpuArithmeticSelfInt8KernelCreator) +REG_KERNEL(kCPU, kNumberTypeInt8, PrimitiveType_Ceil, CpuArithmeticSelfInt8KernelCreator) +} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/arithmetic_self_int8.h b/mindspore/lite/src/runtime/kernel/arm/int8/arithmetic_self_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..69ffd2ec7c7371a2c9e6b6b361f56277b7830dc0 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/int8/arithmetic_self_int8.h @@ -0,0 +1,77 @@ +/** + * 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_ARITHMETIC_SELF_INT8_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_ARITHMETIC_SELF_INT8_H_ + +#include +#include "src/lite_kernel.h" +#include "src/runtime/kernel/arm/opclib/arithmetic_self_parameter.h" +#include "src/runtime/kernel/arm/opclib/int8/arithmetic_self_int8.h" +#include "schema/model_generated.h" +#include "include/context.h" + + +using mindspore::lite::Context; +using mindspore::schema::PrimitiveType_Round; +using mindspore::schema::PrimitiveType_Floor; +using mindspore::schema::PrimitiveType_Ceil; + +namespace mindspore::kernel { +class ArithmeticSelfInt8CPUKernel : public LiteKernel { + typedef int (*ArithmeticSelfInt8Run)(int8_t *input, int8_t *output, int element_size, ArithSelfQuantArg para); + + public: + explicit ArithmeticSelfInt8CPUKernel(OpParameter *parameter, const std::vector &inputs, + const std::vector &outputs, const Context *ctx) + : LiteKernel(parameter, inputs, outputs), ctx_(ctx), thread_count_(ctx->threadNum) { + switch (parameter->type_) { + case PrimitiveType_Round: + arithmeticSelf_run_ = ElementRound; + break; + case PrimitiveType_Floor: + arithmeticSelf_run_ = ElementFloor; + break; + case PrimitiveType_Ceil: + arithmeticSelf_run_ = ElementCeil; + break; + default: + break; + } + arithmeticSelfParameter_ = reinterpret_cast(parameter); + } + ~ArithmeticSelfInt8CPUKernel() override = default; + + int Init() override; + int ReSize() override; + int Run() override; + int DoArithmeticSelf(int task_id); + + private: + int thread_count_; + int thread_sz_count_; + int thread_sz_stride_; + size_t data_size_; + ArithmeticSelfParameter *arithmeticSelfParameter_; + ArithmeticSelfInt8Run arithmeticSelf_run_; + const Context *ctx_; + int8_t *in_ptr_; + int8_t *out_ptr_; +}; +} // namespace mindspore::kernel + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_ARITHMETIC_SELF_INT8_H_ + diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/arithmetic_self_parameter.h b/mindspore/lite/src/runtime/kernel/arm/opclib/arithmetic_self_parameter.h new file mode 100644 index 0000000000000000000000000000000000000000..98fbe698a7eaf4346a998ae9bf7ea91846612929 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/opclib/arithmetic_self_parameter.h @@ -0,0 +1,29 @@ +/** + * 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_ARITHMETIC_SELF_PARAMETER_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_ARITHMETIC_SELF_PARAMETER_H_ + +#include "src/runtime/kernel/arm/opclib/op_base.h" +#include "src/runtime/kernel/arm/opclib/errorcode.h" + +// For Abs, Cos, Exp, Log, Square, Sqrt, Rsqrt ops. +struct ArithmeticSelfParameter { + OpParameter op_parameter_; + ArithSelfQuantArg quant_arg_; +}; + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_ARITHMETIC_SELF_PARAMETER_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/arithmetic_self.h b/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/arithmetic_self.h index 44489d628bb03270eef98902ced7d14ab932e68d..9e2d65dcdd371b50f8b431c6c76bd7925562958e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/arithmetic_self.h +++ b/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/arithmetic_self.h @@ -23,11 +23,6 @@ #include "src/runtime/kernel/arm/opclib/op_base.h" #include "src/runtime/kernel/arm/opclib/errorcode.h" -// For Abs, Cos, Exp, Log, Square, Sqrt, Rsqrt ops. -struct ArithmeticSelfParameter { - OpParameter op_parameter_; -}; - int ElementAbs(float *input, float *output, int element_size); int ElementCos(float *input, float *output, int element_size); diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/int8/arithmetic_self_int8.cc b/mindspore/lite/src/runtime/kernel/arm/opclib/int8/arithmetic_self_int8.cc new file mode 100644 index 0000000000000000000000000000000000000000..2994936865871550afe2e9f2a6ee30b1b13714b0 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/opclib/int8/arithmetic_self_int8.cc @@ -0,0 +1,93 @@ +/** + * 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 "src/runtime/kernel/arm/opclib/int8/arithmetic_self_int8.h" + +int ElementFloor(int8_t *input, int8_t *output, int element_size, ArithSelfQuantArg para) { + if (para.in_args_.scale_ == para.out_args_.scale_ && para.in_args_.zp_ == para.out_args_.zp_) { + for (int i = 0; i < element_size; i++) { + output[i] = floorf(input[i]); + } + } else { + float in_scale = para.in_args_.scale_; + int32_t in_zp = para.in_args_.zp_; + float out_scale = para.out_args_.scale_; + int32_t out_zp = para.out_args_.zp_; + float bias = -in_zp * in_scale; + for (int i = 0; i < element_size; i++) { + int32_t output_tmp = round(floorf(input[i] * in_scale + bias) / out_scale) + out_zp; + if (output_tmp > para.output_activation_max_) { + output[i] = para.output_activation_max_; + } else if (output_tmp < para.output_activation_min_) { + output[i] = para.output_activation_min_; + } else { + output[i] = static_cast(output_tmp); + } + } + } + return OPCLIB_OK; +} + +int ElementRound(int8_t *input, int8_t *output, int element_size, ArithSelfQuantArg para) { + if (para.in_args_.scale_ == para.out_args_.scale_ && para.in_args_.zp_ == para.out_args_.zp_) { + for (int i = 0; i < element_size; i++) { + output[i] = round(input[i]); + } + } else { + float in_scale = para.in_args_.scale_; + int32_t in_zp = para.in_args_.zp_; + float out_scale = para.out_args_.scale_; + int32_t out_zp = para.out_args_.zp_; + float bias = -in_zp * in_scale; + for (int i = 0; i < element_size; i++) { + int32_t output_tmp = round(round(input[i] * in_scale + bias) / out_scale) + out_zp; + if (output_tmp > para.output_activation_max_) { + output[i] = para.output_activation_max_; + } else if (output_tmp < para.output_activation_min_) { + output[i] = para.output_activation_min_; + } else { + output[i] = static_cast(output_tmp); + } + } + } + return OPCLIB_OK; +} + +int ElementCeil(int8_t *input, int8_t *output, int element_size, ArithSelfQuantArg para) { + if (para.in_args_.scale_ == para.out_args_.scale_ && para.in_args_.zp_ == para.out_args_.zp_) { + for (int i = 0; i < element_size; i++) { + output[i] = ceil(input[i]); + } + } else { + float in_scale = para.in_args_.scale_; + int32_t in_zp = para.in_args_.zp_; + float out_scale = para.out_args_.scale_; + int32_t out_zp = para.out_args_.zp_; + float bias = -in_zp * in_scale; + for (int i = 0; i < element_size; i++) { + int32_t output_tmp = round(ceil(input[i] * in_scale + bias) / out_scale) + out_zp; + if (output_tmp > para.output_activation_max_) { + output[i] = para.output_activation_max_; + } else if (output_tmp < para.output_activation_min_) { + output[i] = para.output_activation_min_; + } else { + output[i] = static_cast(output_tmp); + } + } + } + return OPCLIB_OK; +} diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/int8/arithmetic_self_int8.h b/mindspore/lite/src/runtime/kernel/arm/opclib/int8/arithmetic_self_int8.h new file mode 100644 index 0000000000000000000000000000000000000000..bd75f83d076427e11ea3b4b390c8819fee8759e1 --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/opclib/int8/arithmetic_self_int8.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_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_ARITHMETIC_SELF_INT8_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_ARITHMETIC_SELF_INT8_H_ + +#ifdef ENABLE_NEON +#include +#endif +#include "src/runtime/kernel/arm/opclib/op_base.h" +#include "src/runtime/kernel/arm/opclib/errorcode.h" + +int ElementRound(int8_t *input, int8_t *output, int element_size, ArithSelfQuantArg para); + +int ElementFloor(int8_t *input, int8_t *output, int element_size, ArithSelfQuantArg para); + +int ElementCeil(int8_t *input, int8_t *output, int number, ArithSelfQuantArg para); + +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_ARITHMETIC_SELF_INT8_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/quantization/quantize.h b/mindspore/lite/src/runtime/kernel/arm/opclib/quantization/quantize.h index b0bf995535e69990962d0189916bd1ce9e708251..f1ccf947c4f7a4e66875625246b895f568299ce5 100644 --- a/mindspore/lite/src/runtime/kernel/arm/opclib/quantization/quantize.h +++ b/mindspore/lite/src/runtime/kernel/arm/opclib/quantization/quantize.h @@ -83,6 +83,13 @@ struct CropQuantArg { int output_activation_max_; }; +struct ArithSelfQuantArg { + QuantArg in_args_; + QuantArg out_args_; + int output_activation_min_; + int output_activation_max_; +}; + void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift); inline void QuantizeMultiplierSmallerThanOne(double double_multiplier, int32_t *quantized_multiplier, diff --git a/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/arithmetic_self_int8_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/arithmetic_self_int8_tests.cc new file mode 100644 index 0000000000000000000000000000000000000000..81b3b645a322f681075bbb6bea1d23e7d582b68b --- /dev/null +++ b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/arithmetic_self_int8_tests.cc @@ -0,0 +1,386 @@ +/** + * 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/opclib/arithmetic_self_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 TestArithmeticSelfInt8 : public mindspore::Common { + public: + TestArithmeticSelfInt8() {} +}; + +TEST_F(TestArithmeticSelfInt8, floor_quant0_thread2) { + std::vector input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + std::vector shape1 = {2, 3, 2}; + std::vector input(1, nullptr); + input[0] = input1.data(); + + const int output_size = 12; + int8_t output[12]; + std::vector output_shape = {2, 3, 2}; + 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; + + TypeId tid_int8 = kNumberTypeInt8; + lite::tensor::Tensor *input_tensor1 = new lite::tensor::Tensor; + 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; + + 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); + std::vector outputs_tensor(1); + outputs_tensor[0] = output0_tensor; + + ArithmeticSelfParameter op_param; + op_param.op_parameter_.type_ = schema::PrimitiveType_Floor; + lite::Context *ctx = new lite::Context; + ctx->threadNum = 2; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Floor}; + 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, 9, 10, 11, 12}; + 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; +} + +TEST_F(TestArithmeticSelfInt8, floor_quant1_thread2) { + std::vector input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + std::vector shape1 = {2, 3, 2}; + std::vector input(1, nullptr); + input[0] = input1.data(); + + const int output_size = 12; + int8_t output[12]; + std::vector output_shape = {2, 3, 2}; + lite::tensor::QuantArg input_quant_arg; + input_quant_arg.scale = 0.8; + input_quant_arg.zeroPoint = 0; + lite::tensor::QuantArg output_quant_arg; + output_quant_arg.scale = 1.5; + output_quant_arg.zeroPoint = 0; + + TypeId tid_int8 = kNumberTypeInt8; + lite::tensor::Tensor *input_tensor1 = new lite::tensor::Tensor; + 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; + + 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); + std::vector outputs_tensor(1); + outputs_tensor[0] = output0_tensor; + + ArithmeticSelfParameter op_param; + op_param.op_parameter_.type_ = schema::PrimitiveType_Floor; + lite::Context *ctx = new lite::Context; + ctx->threadNum = 2; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Floor}; + 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 = {0, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6}; + 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; +} + +TEST_F(TestArithmeticSelfInt8, round_quant0_thread2) { + std::vector input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + std::vector shape1 = {2, 3, 2}; + std::vector input(1, nullptr); + input[0] = input1.data(); + + const int output_size = 12; + int8_t output[12]; + std::vector output_shape = {2, 3, 2}; + 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; + + TypeId tid_int8 = kNumberTypeInt8; + lite::tensor::Tensor *input_tensor1 = new lite::tensor::Tensor; + 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; + + 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); + std::vector outputs_tensor(1); + outputs_tensor[0] = output0_tensor; + + ArithmeticSelfParameter op_param; + op_param.op_parameter_.type_ = schema::PrimitiveType_Round; + lite::Context *ctx = new lite::Context; + ctx->threadNum = 2; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Floor}; + 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, 9, 10, 11, 12}; + 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; +} + +TEST_F(TestArithmeticSelfInt8, round_quant1_thread2) { + std::vector input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + std::vector shape1 = {2, 3, 2}; + std::vector input(1, nullptr); + input[0] = input1.data(); + + const int output_size = 12; + int8_t output[12]; + std::vector output_shape = {2, 3, 2}; + lite::tensor::QuantArg input_quant_arg; + input_quant_arg.scale = 0.8; + input_quant_arg.zeroPoint = 0; + lite::tensor::QuantArg output_quant_arg; + output_quant_arg.scale = 1.5; + output_quant_arg.zeroPoint = 0; + + TypeId tid_int8 = kNumberTypeInt8; + lite::tensor::Tensor *input_tensor1 = new lite::tensor::Tensor; + 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; + + 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); + std::vector outputs_tensor(1); + outputs_tensor[0] = output0_tensor; + + ArithmeticSelfParameter op_param; + op_param.op_parameter_.type_ = schema::PrimitiveType_Round; + lite::Context *ctx = new lite::Context; + ctx->threadNum = 2; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Floor}; + 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, 1, 2, 3, 3, 4, 4, 5, 5, 6, 7}; + 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; +} + +TEST_F(TestArithmeticSelfInt8, ceil_quant0_thread2) { + std::vector input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + std::vector shape1 = {2, 3, 2}; + std::vector input(1, nullptr); + input[0] = input1.data(); + + const int output_size = 12; + int8_t output[12]; + std::vector output_shape = {2, 3, 2}; + 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; + + TypeId tid_int8 = kNumberTypeInt8; + lite::tensor::Tensor *input_tensor1 = new lite::tensor::Tensor; + 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; + + 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); + std::vector outputs_tensor(1); + outputs_tensor[0] = output0_tensor; + + ArithmeticSelfParameter op_param; + op_param.op_parameter_.type_ = schema::PrimitiveType_Ceil; + lite::Context *ctx = new lite::Context; + ctx->threadNum = 2; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Floor}; + 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, 9, 10, 11, 12}; + 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; +} + +TEST_F(TestArithmeticSelfInt8, ceil_quant1_thread2) { + std::vector input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + std::vector shape1 = {2, 3, 2}; + std::vector input(1, nullptr); + input[0] = input1.data(); + + const int output_size = 12; + int8_t output[12]; + std::vector output_shape = {2, 3, 2}; + lite::tensor::QuantArg input_quant_arg; + input_quant_arg.scale = 0.8; + input_quant_arg.zeroPoint = 0; + lite::tensor::QuantArg output_quant_arg; + output_quant_arg.scale = 1.5; + output_quant_arg.zeroPoint = 0; + + TypeId tid_int8 = kNumberTypeInt8; + lite::tensor::Tensor *input_tensor1 = new lite::tensor::Tensor; + 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; + + 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); + std::vector outputs_tensor(1); + outputs_tensor[0] = output0_tensor; + + ArithmeticSelfParameter op_param; + op_param.op_parameter_.type_ = schema::PrimitiveType_Ceil; + lite::Context *ctx = new lite::Context; + ctx->threadNum = 2; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_Floor}; + 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, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7}; + 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