diff --git a/mindspore/lite/src/ops/ops.cc b/mindspore/lite/src/ops/ops.cc index f411e2e1b5e3e73554e1ca0fbdf35ab3ed240c2b..a90f1a75d906789b99ccaf4db21891312b3d6b8a 100644 --- a/mindspore/lite/src/ops/ops.cc +++ b/mindspore/lite/src/ops/ops.cc @@ -137,10 +137,8 @@ Primitive *Primitive::CreatePrimitive(schema::Primitive *primitive) { return new lite::SpaceToDepth(const_cast(primitive)); case schema::PrimitiveType_SpaceToBatch: return new lite::SpaceToBatch(const_cast(primitive)); - case schema::PrimitiveType_OnnxInt8Dequantize: - return new lite::Dequantize(const_cast(primitive)); - case schema::PrimitiveType_OnnxInt8Quantize: - return new lite::Quantize(const_cast(primitive)); + case schema::PrimitiveType_QuantDTypeCast: + return new lite::QuantDTypeCast(const_cast(primitive)); default: break; } diff --git a/mindspore/lite/src/ops/ops.h b/mindspore/lite/src/ops/ops.h index 5fb261bf8cdbdcb447c2b92134d9faae9622660d..7ed3f78c401f32d8be8870e3dec56061953c8f71 100644 --- a/mindspore/lite/src/ops/ops.h +++ b/mindspore/lite/src/ops/ops.h @@ -691,17 +691,10 @@ class SpaceToDepth : public Primitive { int InferShape(std::vector inputs, std::vector outputs) override; }; -class Dequantize : public Primitive { +class QuantDTypeCast : public Primitive { public: - explicit Dequantize(schema::Primitive *primitive) : Primitive(primitive) {} - const schema::OnnxInt8Dequantize *GetAttribute() const { return this->primitive->value_as_OnnxInt8Dequantize(); } - int InferShape(std::vector inputs, std::vector outputs) override; -}; - -class Quantize : public Primitive { - public: - explicit Quantize(schema::Primitive *primitive) : Primitive(primitive) {} - const schema::OnnxInt8Quantize *GetAttribute() const { return this->primitive->value_as_OnnxInt8Quantize(); } + explicit QuantDTypeCast(schema::Primitive *primitive) : Primitive(primitive) {} + const schema::QuantDTypeCast *GetAttribute() const { return this->primitive->value_as_QuantDTypeCast(); } int InferShape(std::vector inputs, std::vector outputs) override; }; } // namespace lite diff --git a/mindspore/lite/src/ops/dequantize.cc b/mindspore/lite/src/ops/quant_dtype_cast.cc similarity index 78% rename from mindspore/lite/src/ops/dequantize.cc rename to mindspore/lite/src/ops/quant_dtype_cast.cc index e8496d24b2ebd6bce32b5dafd2df3f1e93978f94..93855a89c462259b501e57ad199a8665815ee245 100644 --- a/mindspore/lite/src/ops/dequantize.cc +++ b/mindspore/lite/src/ops/quant_dtype_cast.cc @@ -20,15 +20,16 @@ #include "src/ir/tensor.h" namespace mindspore::lite { -int Dequantize::InferShape(std::vector inputs_, std::vector outputs_) { +int QuantDTypeCast::InferShape(std::vector inputs_, std::vector outputs_) { MS_ASSERT(this->primitive != nullptr); auto input = inputs_.front(); MS_ASSERT(input != nullptr); auto output = outputs_.front(); MS_ASSERT(output != nullptr); output->set_shape(input->shape()); - output->set_data_type(kNumberTypeFloat32); + auto param = primitive->value_as_QuantDTypeCast(); + MS_ASSERT(input->data_type() == param->srcT); + output->set_data_type(static_cast(param->dstT())); return RET_OK; } } // namespace mindspore::lite - diff --git a/mindspore/lite/src/ops/quantize.cc b/mindspore/lite/src/ops/quantize.cc deleted file mode 100644 index 0db68e1ffad49acb27d5928aca65f3e595ab5c7c..0000000000000000000000000000000000000000 --- a/mindspore/lite/src/ops/quantize.cc +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2019-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/ops/ops.h" -#include "include/errorcode.h" -#include "utils/log_adapter.h" -#include "src/ir/tensor.h" - -namespace mindspore::lite { -int Quantize::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_shape(input->shape()); - output->set_data_type(kNumberTypeInt8); - return RET_OK; -} -} // namespace mindspore::lite - diff --git a/mindspore/lite/src/populate_parameter.cc b/mindspore/lite/src/populate_parameter.cc index 46a6dcd0453400d76509470275277d9b107738b0..95fa48b109647ca290455edb4989c3d2fa4ebb34 100644 --- a/mindspore/lite/src/populate_parameter.cc +++ b/mindspore/lite/src/populate_parameter.cc @@ -65,8 +65,7 @@ #include "src/runtime/kernel/arm/base/prior_box.h" #include "src/runtime/kernel/arm/opclib/fp32/space_to_depth.h" #include "src/runtime/kernel/arm/opclib/fp32/space_to_batch.h" -#include "src/runtime/kernel/arm/opclib/int8/dequantize.h" -#include "src/runtime/kernel/arm/opclib/fp32/quantize.h" +#include "src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.h" namespace mindspore::kernel { OpParameter *PopulateFillParameter(const lite::Primitive *primitive) { @@ -1032,24 +1031,17 @@ OpParameter *PopulateFlattenParameter(const lite::Primitive *primitive) { return reinterpret_cast(flatten_param); } -OpParameter *PopulateDequantizeParameter(const lite::Primitive *primitive) { - DequantizeParameter *dequantize_parameter = new (std::nothrow) DequantizeParameter(); - if (dequantize_parameter == nullptr) { - MS_LOG(ERROR) << "new DequantizeParameter fail!"; - return nullptr; - } - dequantize_parameter->op_parameter_.type_ = primitive->Type(); - return reinterpret_cast(dequantize_parameter); -} - -OpParameter *PopulateQuantizeParameter(const lite::Primitive *primitive) { - QuantizeParameter *quantize_parameter = new (std::nothrow) QuantizeParameter(); - if (quantize_parameter == nullptr) { - MS_LOG(ERROR) << "new QuantizeParameter fail!"; +OpParameter *PopulateQuantDTypeCastParameter(const lite::Primitive *primitive) { + QuantDTypeCastParameter *parameter = new (std::nothrow) QuantDTypeCastParameter(); + if (parameter == nullptr) { + MS_LOG(ERROR) << "new QuantDTypeCastParameter fail!"; return nullptr; } - quantize_parameter->op_parameter_.type_ = primitive->Type(); - return reinterpret_cast(quantize_parameter); + parameter->op_parameter_.type_ = primitive->Type(); + auto quant_dtype_cast_param = primitive->Value()->value_as_QuantDTypeCast(); + parameter->srcT = quant_dtype_cast_param->srcT(); + parameter->dstT = quant_dtype_cast_param->dstT(); + return reinterpret_cast(parameter); } OpParameter *PopulateStridedSliceParameter(const lite::Primitive *primitive) { @@ -1209,8 +1201,7 @@ PopulateParameterRegistry::PopulateParameterRegistry() { populate_parameter_funcs_[schema::PrimitiveType_Square] = PopulateSqueezeParameter; populate_parameter_funcs_[schema::PrimitiveType_Split] = PopulateSplitParameter; populate_parameter_funcs_[schema::PrimitiveType_PriorBox] = PopulatePriorBoxParameter; - populate_parameter_funcs_[schema::PrimitiveType_OnnxInt8Dequantize] = PopulateDequantizeParameter; - populate_parameter_funcs_[schema::PrimitiveType_OnnxInt8Quantize] = PopulateQuantizeParameter; + populate_parameter_funcs_[schema::PrimitiveType_QuantDTypeCast] = PopulateQuantDTypeCastParameter; } PopulateParameterRegistry *PopulateParameterRegistry::GetInstance() { diff --git a/mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.cc b/mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.cc new file mode 100644 index 0000000000000000000000000000000000000000..9fb087cd8b56fa09607a7dbd97273b33c4627a3e --- /dev/null +++ b/mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.cc @@ -0,0 +1,148 @@ +/** + * 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/quant_dtype_cast.h" +#include +#include "src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.h" +#include "src/runtime/runtime_api.h" +#include "src/kernel_registry.h" +#include "schema/model_generated.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_QuantDTypeCast; + +namespace mindspore::kernel { +namespace { +constexpr int kQuantDTypeCastInputNum = 1; +constexpr int kQuantDTypeCastOutputNum = 1; +} // namespace + +int QuantDTypeCastCPUKernel::Init() { + if (inputs_.size() != 1) { + MS_LOG(ERROR) << "inputs number should be 1, but " << inputs_.size() << " is given."; + return RET_ERROR; + } + if (outputs_.size() != 1) { + MS_LOG(ERROR) << "outputs number should be 1, but " << inputs_.size() << " is given."; + return RET_ERROR; + } + auto in_tensor = inputs_.front(); + auto out_tensor = outputs_.front(); + auto param = reinterpret_cast(opParameter); + if (param->srcT == kNumberTypeFloat32 && param->dstT == kNumberTypeInt8) { + if (in_tensor->data_type() != kNumberTypeFloat32 || out_tensor->data_type() != kNumberTypeInt8) { + MS_LOG(ERROR) << "param data type and tensor data type do not match."; + return RET_ERROR; + } + inverse_ = false; + } else if (param->srcT == kNumberTypeInt8 && param->dstT == kNumberTypeFloat32) { + if (in_tensor->data_type() != kNumberTypeInt8 || out_tensor->data_type() != kNumberTypeFloat32) { + MS_LOG(ERROR) << "param data type and tensor data type do not match."; + return RET_ERROR; + } + inverse_ = true; + } else { + MS_LOG(ERROR) << "param data type not supported."; + return RET_ERROR; + } + + num_unit_ = static_cast(in_tensor->DataSize()); + thread_n_num_ = MSMIN(thread_num_, num_unit_); + thread_n_stride_ = UP_DIV(num_unit_, thread_n_num_); + + return RET_OK; +} + +int QuantDTypeCastCPUKernel::ReSize() { return RET_OK; } + +int QuantDTypeCastCPUKernel::QuantDTypeCast(int task_id) { + int num_unit_thread = MSMIN(thread_n_stride_, num_unit_ - task_id * thread_n_stride_); + if (num_unit_thread <= 0) { + return RET_OK; + } + int thread_offset = task_id * thread_n_stride_; + auto quant_arg = inputs_.front()->GetQuantParams().front(); + int ret; + if (inverse_) { + ret = DequantizeInt8(int8_ptr_ + thread_offset, float32_ptr_ + thread_offset, quant_arg.scale, quant_arg.zeroPoint, + num_unit_thread); + } else { + ret = QuantizeToInt8(float32_ptr_ + thread_offset, int8_ptr_ + thread_offset, quant_arg.scale, + quant_arg.zeroPoint, num_unit_thread); + } + if (ret != RET_OK) { + MS_LOG(ERROR) << "QuantDTypeCast error task_id[" << task_id << "] error_code[" << ret << "]"; + return RET_ERROR; + } + return RET_OK; +} + +int QuantDTypeCastRun(int task_id, LiteParallelGroupEnv *penv, void *cdata) { + auto g_kernel = reinterpret_cast(cdata); + auto ret = g_kernel->QuantDTypeCast(task_id); + if (ret != RET_OK) { + MS_LOG(ERROR) << "QuantDTypeCastRun error task_id[" << task_id << "] error_code[" << ret << "]"; + return RET_ERROR; + } + return RET_OK; +} + +int QuantDTypeCastCPUKernel::Run() { + if (inverse_) { + int8_ptr_ = reinterpret_cast(inputs_[0]->Data()); + float32_ptr_ = reinterpret_cast(outputs_[0]->Data()); + } else { + float32_ptr_ = reinterpret_cast(inputs_[0]->Data()); + int8_ptr_ = reinterpret_cast(outputs_[0]->Data()); + } + + int ret = LiteBackendParallelLaunch(QuantDTypeCastRun, this, thread_n_num_); + if (ret != RET_OK) { + MS_LOG(ERROR) << "Scale error error_code[" << ret << "]"; + return RET_ERROR; + } + + return RET_OK; +} + +kernel::LiteKernel *CpuQuantDTypeCastFp32KernelCreator(const std::vector &inputs, + const std::vector &outputs, + OpParameter *opParameter, const lite::Context *ctx, + const kernel::KernelKey &desc) { + if (opParameter == nullptr) { + MS_LOG(ERROR) << "Input opParameter is nullptr!"; + return nullptr; + } + auto *kernel = new (std::nothrow) QuantDTypeCastCPUKernel(opParameter, inputs, outputs, ctx); + if (kernel == nullptr) { + MS_LOG(ERROR) << "new QuantDTypeCastCPUKernel 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_QuantDTypeCast, CpuQuantDTypeCastFp32KernelCreator) +} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/dequantize.h b/mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.h similarity index 68% rename from mindspore/lite/src/runtime/kernel/arm/int8/dequantize.h rename to mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.h index 0d913b54831163dce5784c1753b584854109d956..dd108d813e4286920e4e60eb6c97a545aebb591f 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/dequantize.h +++ b/mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.h @@ -14,33 +14,34 @@ * limitations under the License. */ -#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_DEQUANTIZE_H_ -#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_DEQUANTIZE_H_ +#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_QUANTDTYPECAST_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_QUANTDTYPECAST_H_ #include #include "src/lite_kernel.h" namespace mindspore::kernel { -class DequantizeCPUKernel : public LiteKernel { +class QuantDTypeCastCPUKernel : public LiteKernel { public: - DequantizeCPUKernel(OpParameter *parameter, const std::vector &inputs, + QuantDTypeCastCPUKernel(OpParameter *parameter, const std::vector &inputs, const std::vector &outputs, const lite::Context *ctx) : LiteKernel(parameter, inputs, outputs), thread_num_(ctx->threadNum) {} - ~DequantizeCPUKernel() = default; + ~QuantDTypeCastCPUKernel() = default; int Init() override; int ReSize() override; int Run() override; - int Dequantize(int task_id); + int QuantDTypeCast(int task_id); private: int thread_num_; int thread_n_num_; int thread_n_stride_; int num_unit_; - int8_t *input_ptr_; - float *output_ptr_; + int8_t *int8_ptr_; + float *float32_ptr_; + bool inverse_; }; } // namespace mindspore::kernel -#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_DEQUANTIZE_H_ +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_QUANTDTYPECAST_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/quantize.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/quantize.cc deleted file mode 100644 index f7057ba14fc593444747be5f7d11de52acab1f21..0000000000000000000000000000000000000000 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/quantize.cc +++ /dev/null @@ -1,118 +0,0 @@ -/** - * 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/fp32/quantize.h" -#include -#include "src/runtime/kernel/arm/opclib/fp32/quantize.h" -#include "src/kernel_registry.h" -#include "src/runtime/runtime_api.h" -#include "schema/model_generated.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_OnnxInt8Quantize; - -namespace mindspore::kernel { -namespace { -constexpr int kQuantizeInputNum = 1; -constexpr int kQuantizeOutputNum = 1; -} // namespace - -int QuantizeCPUKernel::Init() { - if (inputs_.size() != 1) { - MS_LOG(ERROR) << "inputs number should be 1, but " << inputs_.size() << " is given."; - return RET_ERROR; - } - if (outputs_.size() != 1) { - MS_LOG(ERROR) << "outputs number should be 1, but " << inputs_.size() << " is given."; - return RET_ERROR; - } - auto in_tensor = inputs_.front(); - num_unit_ = static_cast(in_tensor->DataSize()); - thread_n_num_ = MSMIN(thread_num_, num_unit_); - thread_n_stride_ = UP_DIV(num_unit_, thread_n_num_); - - return RET_OK; -} - -int QuantizeCPUKernel::ReSize() { return RET_OK; } - -int QuantizeCPUKernel::Quantize(int task_id) { - int num_unit_thread = MSMIN(thread_n_stride_, num_unit_ - task_id * thread_n_stride_); - if (num_unit_thread <= 0) { - return RET_OK; - } - int thread_offset = task_id * thread_n_stride_; - auto quant_arg = inputs_.front()->GetQuantParams().front(); - int ret = QuantizeToInt8(input_ptr_ + thread_offset, output_ptr_ + thread_offset, quant_arg.scale, - quant_arg.zeroPoint, num_unit_thread); - - if (ret != RET_OK) { - MS_LOG(ERROR) << "Quantize error task_id[" << task_id << "] error_code[" << ret << "]"; - return RET_ERROR; - } - return RET_OK; -} - -int QuantizeRun(int task_id, LiteParallelGroupEnv *penv, void *cdata) { - auto g_kernel = reinterpret_cast(cdata); - auto ret = g_kernel->Quantize(task_id); - if (ret != RET_OK) { - MS_LOG(ERROR) << "QuantizeRun error task_id[" << task_id << "] error_code[" << ret << "]"; - return RET_ERROR; - } - return RET_OK; -} - -int QuantizeCPUKernel::Run() { - input_ptr_ = reinterpret_cast(inputs_[0]->Data()); - output_ptr_ = reinterpret_cast(outputs_[0]->Data()); - int ret = LiteBackendParallelLaunch(QuantizeRun, this, thread_n_num_); - if (ret != RET_OK) { - MS_LOG(ERROR) << "Scale error error_code[" << ret << "]"; - return RET_ERROR; - } - - return RET_OK; -} - -kernel::LiteKernel *CpuQuantizeFp32KernelCreator(const std::vector &inputs, - const std::vector &outputs, - OpParameter *opParameter, const lite::Context *ctx, - const kernel::KernelKey &desc) { - if (opParameter == nullptr) { - MS_LOG(ERROR) << "Input opParameter is nullptr!"; - return nullptr; - } - auto *kernel = new (std::nothrow) QuantizeCPUKernel(opParameter, inputs, outputs, ctx); - if (kernel == nullptr) { - MS_LOG(ERROR) << "new QuantizeCPUKernel 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, kNumberTypeFloat32, PrimitiveType_OnnxInt8Quantize, CpuQuantizeFp32KernelCreator) -} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/quantize.h b/mindspore/lite/src/runtime/kernel/arm/fp32/quantize.h deleted file mode 100644 index 64540e647b27374ca29b16eee28f2187f57ee127..0000000000000000000000000000000000000000 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/quantize.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * 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_QUANTIZE_H_ -#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_QUANTIZE_H_ - -#include -#include "src/lite_kernel.h" - -namespace mindspore::kernel { -class QuantizeCPUKernel : public LiteKernel { - public: - QuantizeCPUKernel(OpParameter *parameter, const std::vector &inputs, - const std::vector &outputs, const lite::Context *ctx) - : LiteKernel(parameter, inputs, outputs), thread_num_(ctx->threadNum) {} - ~QuantizeCPUKernel() = default; - - int Init() override; - int ReSize() override; - int Run() override; - int Quantize(int task_id); - - private: - int thread_num_; - int thread_n_num_; - int thread_n_stride_; - int num_unit_; - float *input_ptr_; - int8_t *output_ptr_; -}; -} // namespace mindspore::kernel - -#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_QUANTIZE_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/space_to_depth.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/space_to_depth.cc index 8f26a731a17c9b7771326d1ebf015ec9ca75d752..6b0819798111e0e5e0d12707e44ca1fc604664e6 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/space_to_depth.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/space_to_depth.cc @@ -16,11 +16,11 @@ #include "src/runtime/kernel/arm/fp32/space_to_depth.h" #include -#include "schema/ops_generated.h" #include "schema/model_generated.h" #include "src/kernel_registry.h" #include "src/runtime/kernel/arm/opclib/fp32/space_to_depth.h" #include "include/errorcode.h" +#include "src/runtime/runtime_api.h" using mindspore::lite::KernelRegistrar; using mindspore::lite::RET_ERROR; @@ -41,21 +41,48 @@ int SpaceToDepthCPUKernel::Init() { MS_LOG(ERROR) << "Input block_size should > 0!"; return RET_PARAM_INVALID; } + + num_unit_ = static_cast(inputs_[0]->shape().at(kNHWC_H)); + thread_h_num_ = MSMIN(thread_num_, num_unit_); + thread_h_stride_ = UP_DIV(num_unit_, thread_h_num_); return RET_OK; } -int SpaceToDepthCPUKernel::Run() { - auto input = inputs_[0]; - auto output = outputs_[0]; - const float *input_data = static_cast(input->Data()); - float *output_data = static_cast(output->Data()); - auto in_shape = input->shape(); - auto out_shape = output->shape(); +int SpaceToDepthCPUKernel::SpaceToDepth(int task_id) { + int num_unit_thread = MSMIN(thread_h_stride_, num_unit_ - task_id * thread_h_stride_); + if (num_unit_thread <= 0) { + return RET_OK; + } + int thread_offset = task_id * thread_h_stride_; + auto in_shape = inputs_[0]->shape(); + auto out_shape = outputs_[0]->shape(); SpaceToDepthParameter *param = reinterpret_cast(opParameter); - if (input->GetFormat() == schema::Format_NHWC) { - auto ret = SpaceToDepthForNHWC(input_data, output_data, in_shape.data(), out_shape.data(), in_shape.size(), - param->block_size_); - return ret; + auto ret = SpaceToDepthForNHWC(input_ptr_, output_ptr_, in_shape.data(), out_shape.data(), in_shape.size(), + param->block_size_, thread_offset, thread_offset + num_unit_thread); + if (ret != RET_OK) { + MS_LOG(ERROR) << "SpaceToDepth error task_id[" << task_id << "] error_code[" << ret << "]"; + return RET_ERROR; + } + return RET_OK; +} + +int SpaceToDepthRun(int task_id, LiteParallelGroupEnv *penv, void *cdata) { + auto g_kernel = reinterpret_cast(cdata); + auto ret = g_kernel->SpaceToDepth(task_id); + if (ret != RET_OK) { + MS_LOG(ERROR) << "SpaceToDepthRun error task_id[" << task_id << "] error_code[" << ret << "]"; + return RET_ERROR; + } + return RET_OK; +} + +int SpaceToDepthCPUKernel::Run() { + if (inputs_[0]->GetFormat() == schema::Format_NHWC) { + int ret = LiteBackendParallelLaunch(SpaceToDepthRun, this, thread_h_num_); + if (ret != RET_OK) { + MS_LOG(ERROR) << "SpaceToDepth error error_code[" << ret << "]"; + return ret; + } } else { MS_LOG(ERROR) << "Only support NHWC now!"; return RET_ERROR; @@ -69,7 +96,7 @@ kernel::LiteKernel *CpuSpaceToDepthFp32KernelCreator(const std::vector &inputs, - const std::vector &outputs) - : LiteKernel(parameter, inputs, outputs) {} + const std::vector &outputs, const lite::Context *ctx) + : LiteKernel(parameter, inputs, outputs), thread_num_(ctx->threadNum) {} ~SpaceToDepthCPUKernel() = default; + int SpaceToDepth(int task_id); int Init() override; int ReSize() override { return 0; }; int Run() override; + + private: + int thread_num_; + int thread_h_stride_; + int thread_h_num_; + int num_unit_; + float *input_ptr_; + float *output_ptr_; }; } // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/dequantize.cc b/mindspore/lite/src/runtime/kernel/arm/int8/dequantize.cc deleted file mode 100644 index d9f6bfebd95f99dc07bc9faf49bd841daf37c396..0000000000000000000000000000000000000000 --- a/mindspore/lite/src/runtime/kernel/arm/int8/dequantize.cc +++ /dev/null @@ -1,118 +0,0 @@ -/** - * 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/dequantize.h" -#include -#include "src/runtime/kernel/arm/opclib/int8/dequantize.h" -#include "src/runtime/runtime_api.h" -#include "src/kernel_registry.h" -#include "schema/model_generated.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_OnnxInt8Dequantize; - -namespace mindspore::kernel { -namespace { -constexpr int kDequantizeInputNum = 1; -constexpr int kDequantizeOutputNum = 1; -} // namespace - -int DequantizeCPUKernel::Init() { - if (inputs_.size() != 1) { - MS_LOG(ERROR) << "inputs number should be 1, but " << inputs_.size() << " is given."; - return RET_ERROR; - } - if (outputs_.size() != 1) { - MS_LOG(ERROR) << "outputs number should be 1, but " << inputs_.size() << " is given."; - return RET_ERROR; - } - auto in_tensor = inputs_.front(); - num_unit_ = static_cast(in_tensor->DataSize()); - thread_n_num_ = MSMIN(thread_num_, num_unit_); - thread_n_stride_ = UP_DIV(num_unit_, thread_n_num_); - - return RET_OK; -} - -int DequantizeCPUKernel::ReSize() { return RET_OK; } - -int DequantizeCPUKernel::Dequantize(int task_id) { - int num_unit_thread = MSMIN(thread_n_stride_, num_unit_ - task_id * thread_n_stride_); - if (num_unit_thread <= 0) { - return RET_OK; - } - int thread_offset = task_id * thread_n_stride_; - auto quant_arg = inputs_.front()->GetQuantParams().front(); - int ret = DequantizeInt8(input_ptr_ + thread_offset, output_ptr_ + thread_offset, quant_arg.scale, - quant_arg.zeroPoint, num_unit_thread); - - if (ret != RET_OK) { - MS_LOG(ERROR) << "Dequantize error task_id[" << task_id << "] error_code[" << ret << "]"; - return RET_ERROR; - } - return RET_OK; -} - -int DequantizeRun(int task_id, LiteParallelGroupEnv *penv, void *cdata) { - auto g_kernel = reinterpret_cast(cdata); - auto ret = g_kernel->Dequantize(task_id); - if (ret != RET_OK) { - MS_LOG(ERROR) << "DequantizeRun error task_id[" << task_id << "] error_code[" << ret << "]"; - return RET_ERROR; - } - return RET_OK; -} - -int DequantizeCPUKernel::Run() { - input_ptr_ = reinterpret_cast(inputs_[0]->Data()); - output_ptr_ = reinterpret_cast(outputs_[0]->Data()); - int ret = LiteBackendParallelLaunch(DequantizeRun, this, thread_n_num_); - if (ret != RET_OK) { - MS_LOG(ERROR) << "Scale error error_code[" << ret << "]"; - return RET_ERROR; - } - - return RET_OK; -} - -kernel::LiteKernel *CpuDequantizeFp32KernelCreator(const std::vector &inputs, - const std::vector &outputs, - OpParameter *opParameter, const lite::Context *ctx, - const kernel::KernelKey &desc) { - if (opParameter == nullptr) { - MS_LOG(ERROR) << "Input opParameter is nullptr!"; - return nullptr; - } - auto *kernel = new (std::nothrow) DequantizeCPUKernel(opParameter, inputs, outputs, ctx); - if (kernel == nullptr) { - MS_LOG(ERROR) << "new DequantizeCPUKernel 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_OnnxInt8Dequantize, CpuDequantizeFp32KernelCreator) -} // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/quantize.h b/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/quantize.h deleted file mode 100644 index 5acf127bd18dfdd8111e609f8d58591aec539347..0000000000000000000000000000000000000000 --- a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/quantize.h +++ /dev/null @@ -1,28 +0,0 @@ -/** - * 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_FP32_QUANTIZE_H_ -#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_FP32_QUANTIZE_H_ - -#include "src/runtime/kernel/arm/opclib/op_base.h" - -struct QuantizeParameter { - OpParameter op_parameter_; -}; - -int QuantizeToInt8(float *real_values, int8_t *quant_values, float scale, int32_t zp, int size); - -#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_FP32_QUANTIZE_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/space_to_depth.cc b/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/space_to_depth.cc index 38313ac7a5fbe74234be6bb984b95e11a37995d7..c785e52eb253dab3a8cadf64580559a319d5c39e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/space_to_depth.cc +++ b/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/space_to_depth.cc @@ -19,13 +19,16 @@ #include "src/runtime/kernel/arm/opclib/op_base.h" int SpaceToDepthForNHWC(const float *input, float *output, int *in_shape, int *out_shape, int shape_size, - int block_size) { + int block_size, int h_start, int h_end) { if (input == nullptr || output == nullptr) { return OPCLIB_NULL_PTR; } if (shape_size != C4NUM) { return OPCLIB_PARAM_INVALID; } + if (h_start < 0 || h_start >= h_end || h_end > out_shape[1]) { + return OPCLIB_PARAM_INVALID; + } int in_strides[C4NUM]; ComputeStrides(in_shape, in_strides, shape_size); int out_strides[C4NUM]; @@ -33,7 +36,7 @@ int SpaceToDepthForNHWC(const float *input, float *output, int *in_shape, int *o for (int i = 0; i < out_shape[0]; ++i) { size_t in_offset_n = i * in_strides[0]; size_t out_offset_n = i * out_strides[0]; - for (int j = 0; j < out_shape[1]; ++j) { + for (int j = h_start; j < h_end; ++j) { size_t in_offset_h = in_offset_n + j * block_size * in_strides[1]; size_t out_offset_h = out_offset_n + j * out_strides[1]; for (int k = 0; k < out_shape[2]; ++k) { diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/space_to_depth.h b/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/space_to_depth.h index 752c9cc4d1086053f1fa9709b9ecf202cf6e5465..319c7d24e5dd6829daab0c2a64ff3f3b4e476db8 100644 --- a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/space_to_depth.h +++ b/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/space_to_depth.h @@ -23,5 +23,5 @@ struct SpaceToDepthParameter { }; int SpaceToDepthForNHWC(const float *input, float *output, int *in_shape, int *out_shape, int shape_size, - int block_size); + int block_size, int h_start, int h_end); #endif // MINDSPORE_LITE_SRC_BACKEND_ARM_OPCLIB_FP32_SPACE_TO_DEPTH_H_ diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/int8/dequantize.cc b/mindspore/lite/src/runtime/kernel/arm/opclib/int8/dequantize.cc deleted file mode 100644 index 5365f90729122277c14687272f6c297fcefd5400..0000000000000000000000000000000000000000 --- a/mindspore/lite/src/runtime/kernel/arm/opclib/int8/dequantize.cc +++ /dev/null @@ -1,29 +0,0 @@ -/** - * 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/opclib/int8/dequantize.h" -#include "src/runtime/kernel/arm/opclib/errorcode.h" - -int DequantizeInt8(int8_t *quant_values, float *real_values, float scale, int32_t zp, int size) { - if (quant_values == nullptr || real_values == nullptr) { - return OPCLIB_PARAM_INVALID; - } - - for (int i = 0; i < size; ++i) { - real_values[i] = (quant_values[i] + zp) * scale; - } - return OPCLIB_OK; -} diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/quantize.cc b/mindspore/lite/src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.cc similarity index 72% rename from mindspore/lite/src/runtime/kernel/arm/opclib/fp32/quantize.cc rename to mindspore/lite/src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.cc index 07913eb579827c08ec4d9d36781f5d3e25157785..91aefc4ee65ce5ea7db483655fa565c2817fc23d 100644 --- a/mindspore/lite/src/runtime/kernel/arm/opclib/fp32/quantize.cc +++ b/mindspore/lite/src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.cc @@ -14,9 +14,20 @@ * limitations under the License. */ -#include "src/runtime/kernel/arm/opclib/fp32/quantize.h" +#include "src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.h" #include "src/runtime/kernel/arm/opclib/errorcode.h" +int DequantizeInt8(int8_t *quant_values, float *real_values, float scale, int32_t zp, int size) { + if (quant_values == nullptr || real_values == nullptr) { + return OPCLIB_PARAM_INVALID; + } + + for (int i = 0; i < size; ++i) { + real_values[i] = (quant_values[i] + zp) * scale; + } + return OPCLIB_OK; +} + int QuantizeToInt8(float *real_values, int8_t *quant_values, float scale, int32_t zp, int size) { if (quant_values == nullptr || real_values == nullptr) { return OPCLIB_PARAM_INVALID; diff --git a/mindspore/lite/src/runtime/kernel/arm/opclib/int8/dequantize.h b/mindspore/lite/src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.h similarity index 66% rename from mindspore/lite/src/runtime/kernel/arm/opclib/int8/dequantize.h rename to mindspore/lite/src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.h index c35883601635e0aae9575618ee6b30c8475efed5..97f4ae67a17845fde48a18a932bd94d6914bc064 100644 --- a/mindspore/lite/src/runtime/kernel/arm/opclib/int8/dequantize.h +++ b/mindspore/lite/src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.h @@ -14,15 +14,18 @@ * limitations under the License. */ -#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_DEQUANTIZE_H_ -#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_DEQUANTIZE_H_ +#ifndef MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_QUANTDTYPECAST_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_QUANTDTYPECAST_H_ #include "src/runtime/kernel/arm/opclib/op_base.h" -struct DequantizeParameter { +struct QuantDTypeCastParameter { OpParameter op_parameter_; + int32_t srcT; + int32_t dstT; }; int DequantizeInt8(int8_t *quant_values, float *real_values, float scale, int32_t zp, int size); +int QuantizeToInt8(float *real_values, int8_t *quant_values, float scale, int32_t zp, int size); -#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_DEQUANTIZE_H_ +#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_QUANTDTYPECAST_H_ diff --git a/mindspore/lite/test/ut/src/runtime/kernel/arm/fp32/quantize_fp32_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/arm/fp32/quantize_fp32_tests.cc deleted file mode 100644 index 74f1687699a6bf92014621a3414566e5df0c5e87..0000000000000000000000000000000000000000 --- a/mindspore/lite/test/ut/src/runtime/kernel/arm/fp32/quantize_fp32_tests.cc +++ /dev/null @@ -1,77 +0,0 @@ -/** - * 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 -#include "utils/log_adapter.h" -#include "common/common_test.h" -#include "mindspore/lite/src/runtime/kernel/arm/fp32/quantize.h" -#include "mindspore/lite/src/runtime/kernel/arm/opclib/fp32/quantize.h" -#include "mindspore/lite/src/kernel_registry.h" -#include "mindspore/lite/src/lite_kernel.h" - -namespace mindspore { - -class QuantizeTestFp32 : public mindspore::Common { - public: - QuantizeTestFp32() {} -}; - -TEST_F(QuantizeTestFp32, QuantizeTest1) { - const lite::tensor::QuantArg quant_arg = {0.3515625, -57}; - QuantizeParameter param; - param.op_parameter_.type_ = schema::PrimitiveType_OnnxInt8Quantize; - - std::vector input = {1, 2, 5, 6, 10, -20, 3, 8, 18, 10, 3, 4, 11, 16, 15, 25}; - std::vector in_shape = {1, 4, 4, 1}; - lite::tensor::Tensor input_tensor; - input_tensor.SetData(input.data()); - input_tensor.set_shape(in_shape); - input_tensor.SetFormat(schema::Format_NHWC); - input_tensor.set_data_type(kNumberTypeFloat32); - input_tensor.AddQuantParam(quant_arg); - std::vector inputs_tensor; - inputs_tensor.emplace_back(&input_tensor); - - const int out_size = 16; - int8_t expect_out[16] = {-54, -51, -43, -40, -29, -114, -48, -34, -6, -29, -48, -46, -26, -11, -14, 14}; - std::vector output(16); - std::vector out_shape = {1, 4, 4, 1}; - lite::tensor::Tensor output_tensor; - output_tensor.SetData(output.data()); - output_tensor.set_shape(out_shape); - output_tensor.SetFormat(schema::Format_NHWC); - output_tensor.set_data_type(kNumberTypeInt8); - std::vector outputs_tensor; - outputs_tensor.emplace_back(&output_tensor); - - lite::Context ctx; - ctx.threadNum = 3; - kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, schema::PrimitiveType_OnnxInt8Quantize}; - auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc); - ASSERT_NE(creator, nullptr); - kernel::LiteKernel *kernel = - creator(inputs_tensor, outputs_tensor, reinterpret_cast(¶m), &ctx, desc); - ASSERT_NE(kernel, nullptr); - kernel->Run(); - - for (int i = 0; i < out_size; ++i) { - std::cout << output[i] << " "; - } - std::cout << "\n"; - CompareOutputData(output.data(), expect_out, out_size, 0.000001); -} - -} // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/arm/fp32/space_to_depth_fp32_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/arm/fp32/space_to_depth_fp32_tests.cc index e9822c59815bc221972984ddfbdd615d5fd7621c..ba48fa11c0c3babe13c5742023a84ba05541cb0c 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/arm/fp32/space_to_depth_fp32_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/arm/fp32/space_to_depth_fp32_tests.cc @@ -37,7 +37,9 @@ TEST_F(SpaceToDepthTestFp32, SpaceToDepthTest1) { float output[16]; int in_shape[4] = {1, 4, 4, 1}; int out_shape[4] = {1, 2, 2, 4}; - SpaceToDepthForNHWC((const float *)input, output, in_shape, out_shape, 4, 2); + int h_start = 0; + int h_end = 2; + SpaceToDepthForNHWC((const float *)input, output, in_shape, out_shape, 4, 2, h_start, h_end); for (int i = 0; i < out_size; ++i) { std::cout << output[i] << " "; } @@ -69,10 +71,11 @@ TEST_F(SpaceToDepthTestFp32, SpaceToDepthTest2) { outputs_tensor.emplace_back(&output_tensor); SpaceToDepthParameter op_param; - op_param.op_parameter_.type_ = schema::PrimitiveType_SpaceToBatch; + op_param.op_parameter_.type_ = schema::PrimitiveType_SpaceToDepth; op_param.block_size_ = 2; lite::Context ctx; + ctx.threadNum = 3; kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, schema::PrimitiveType_SpaceToDepth}; auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc); ASSERT_NE(creator, nullptr); diff --git a/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/dequantize_int8_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/quant_dtype_cast_tests.cc similarity index 51% rename from mindspore/lite/test/ut/src/runtime/kernel/arm/int8/dequantize_int8_tests.cc rename to mindspore/lite/test/ut/src/runtime/kernel/arm/int8/quant_dtype_cast_tests.cc index 480ae296987db913f72abf066b731c6cc91bbafe..795f92c3fb6c2f62b22d4b084cd6781a40e3ccf4 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/dequantize_int8_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/quant_dtype_cast_tests.cc @@ -17,28 +17,26 @@ #include #include "utils/log_adapter.h" #include "common/common_test.h" -#include "mindspore/lite/src/runtime/kernel/arm/int8/dequantize.h" -#include "mindspore/lite/src/runtime/kernel/arm/opclib/int8/dequantize.h" +#include "mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.h" +#include "mindspore/lite/src/runtime/kernel/arm/opclib/int8/quant_dtype_cast.h" #include "mindspore/lite/src/kernel_registry.h" #include "mindspore/lite/src/lite_kernel.h" namespace mindspore { -class DequantizeTestFp32 : public mindspore::Common { +class QuantDTypeCastTestFp32 : public mindspore::Common { public: - DequantizeTestFp32() {} + QuantDTypeCastTestFp32() {} }; -TEST_F(DequantizeTestFp32, DequantizeTest1) { +TEST_F(QuantDTypeCastTestFp32, QuantDTypeCastTest1) { const lite::tensor::QuantArg quant_arg{0.21176, 5}; - // quant_arg.scale = 100.0; - // quant_arg.zeroPoint = 20; - DequantizeParameter param; - param.op_parameter_.type_ = schema::PrimitiveType_OnnxInt8Dequantize; + QuantDTypeCastParameter param; + param.srcT = kNumberTypeInt8; + param.dstT = kNumberTypeFloat32; + param.op_parameter_.type_ = schema::PrimitiveType_QuantDTypeCast; std::vector input = {10, 14, 29, 33, 52, 99, 19, 43, 90, 52, 19, 24, 57, 127, 76, 123}; - // int8_t input0[] = {1, 2, 10}; - // int32_t a = input0[0] + 2; std::vector in_shape = {1, 4, 4, 1}; lite::tensor::Tensor input_tensor; input_tensor.SetData(input.data()); @@ -59,13 +57,13 @@ TEST_F(DequantizeTestFp32, DequantizeTest1) { output_tensor.SetData(output.data()); output_tensor.set_shape(out_shape); output_tensor.set_data_type(kNumberTypeFloat32); - output_tensor.SetFormat(schema::Format_NHWC); + // output_tensor.SetFormat(schema::Format_NHWC); std::vector outputs_tensor; outputs_tensor.emplace_back(&output_tensor); lite::Context ctx; ctx.threadNum = 3; - kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_OnnxInt8Dequantize}; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_QuantDTypeCast}; auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc); ASSERT_NE(creator, nullptr); kernel::LiteKernel *kernel = @@ -80,4 +78,49 @@ TEST_F(DequantizeTestFp32, DequantizeTest1) { CompareOutputData(output.data(), expect_out, out_size, 0.000001); } +TEST_F(QuantDTypeCastTestFp32, QuantDTypeCastTest2) { + const lite::tensor::QuantArg quant_arg = {0.3515625, -57}; + QuantDTypeCastParameter param; + param.op_parameter_.type_ = schema::PrimitiveType_QuantDTypeCast; + param.dstT = kNumberTypeInt8; + param.srcT = kNumberTypeFloat32; + std::vector input = {1, 2, 5, 6, 10, -20, 3, 8, 18, 10, 3, 4, 11, 16, 15, 25}; + std::vector in_shape = {1, 4, 4, 1}; + lite::tensor::Tensor input_tensor; + input_tensor.SetData(input.data()); + input_tensor.set_shape(in_shape); + // input_tensor.SetFormat(schema::Format_NHWC); + input_tensor.set_data_type(kNumberTypeFloat32); + input_tensor.AddQuantParam(quant_arg); + std::vector inputs_tensor; + inputs_tensor.emplace_back(&input_tensor); + + const int out_size = 16; + int8_t expect_out[16] = {-54, -51, -43, -40, -29, -114, -48, -34, -6, -29, -48, -46, -26, -11, -14, 14}; + std::vector output(16); + std::vector out_shape = {1, 4, 4, 1}; + lite::tensor::Tensor output_tensor; + output_tensor.SetData(output.data()); + output_tensor.set_shape(out_shape); + output_tensor.SetFormat(schema::Format_NHWC); + output_tensor.set_data_type(kNumberTypeInt8); + std::vector outputs_tensor; + outputs_tensor.emplace_back(&output_tensor); + + lite::Context ctx; + ctx.threadNum = 3; + kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeInt8, schema::PrimitiveType_QuantDTypeCast}; + auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc); + ASSERT_NE(creator, nullptr); + kernel::LiteKernel *kernel = + creator(inputs_tensor, outputs_tensor, reinterpret_cast(¶m), &ctx, desc); + ASSERT_NE(kernel, nullptr); + kernel->Run(); + + for (int i = 0; i < out_size; ++i) { + std::cout << output[i] << " "; + } + std::cout << "\n"; + CompareOutputData(output.data(), expect_out, out_size, 0.000001); +} } // namespace mindspore