Add new hms ops of floor, round and ceil with type of int8

This commit is contained in:
liuwenhao4 2020-08-01 15:36:37 +08:00
parent 201bcdd9af
commit a367b3c286
10 changed files with 746 additions and 6 deletions

View File

@ -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"

View File

@ -20,6 +20,7 @@
#include <vector>
#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"

View File

@ -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 <limits>
#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<int8_t>::max();
arithmeticSelfParameter_->quant_arg_.output_activation_min_ = std::numeric_limits<int8_t>::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<ArithmeticSelfInt8CPUKernel *>(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<int8_t *>(input_tensor->Data());
out_ptr_ = reinterpret_cast<int8_t *>(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<lite::tensor::Tensor *> &inputs,
const std::vector<lite::tensor::Tensor *> &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<schema::PrimitiveType>(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

View File

@ -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 <vector>
#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<lite::tensor::Tensor *> &inputs,
const std::vector<lite::tensor::Tensor *> &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<ArithmeticSelfParameter *>(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_

View File

@ -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_

View File

@ -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);

View File

@ -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 <math.h>
#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<int8_t>(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<int8_t>(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<int8_t>(output_tmp);
}
}
}
return OPCLIB_OK;
}

View File

@ -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 <arm_neon.h>
#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_

View File

@ -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,

View File

@ -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 <iostream>
#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<int8_t> input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
std::vector<int> shape1 = {2, 3, 2};
std::vector<int8_t *> input(1, nullptr);
input[0] = input1.data();
const int output_size = 12;
int8_t output[12];
std::vector<int> 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<lite::tensor::Tensor *> 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<lite::tensor::Tensor *> 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<OpParameter *>(&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<int8_t> 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<int8_t> input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
std::vector<int> shape1 = {2, 3, 2};
std::vector<int8_t *> input(1, nullptr);
input[0] = input1.data();
const int output_size = 12;
int8_t output[12];
std::vector<int> 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<lite::tensor::Tensor *> 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<lite::tensor::Tensor *> 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<OpParameter *>(&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<int8_t> 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<int8_t> input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
std::vector<int> shape1 = {2, 3, 2};
std::vector<int8_t *> input(1, nullptr);
input[0] = input1.data();
const int output_size = 12;
int8_t output[12];
std::vector<int> 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<lite::tensor::Tensor *> 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<lite::tensor::Tensor *> 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<OpParameter *>(&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<int8_t> 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<int8_t> input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
std::vector<int> shape1 = {2, 3, 2};
std::vector<int8_t *> input(1, nullptr);
input[0] = input1.data();
const int output_size = 12;
int8_t output[12];
std::vector<int> 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<lite::tensor::Tensor *> 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<lite::tensor::Tensor *> 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<OpParameter *>(&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<int8_t> 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<int8_t> input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
std::vector<int> shape1 = {2, 3, 2};
std::vector<int8_t *> input(1, nullptr);
input[0] = input1.data();
const int output_size = 12;
int8_t output[12];
std::vector<int> 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<lite::tensor::Tensor *> 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<lite::tensor::Tensor *> 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<OpParameter *>(&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<int8_t> 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<int8_t> input1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
std::vector<int> shape1 = {2, 3, 2};
std::vector<int8_t *> input(1, nullptr);
input[0] = input1.data();
const int output_size = 12;
int8_t output[12];
std::vector<int> 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<lite::tensor::Tensor *> 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<lite::tensor::Tensor *> 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<OpParameter *>(&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<int8_t> 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