From fbfefca639416070028c7b67e5afa076046e47ce Mon Sep 17 00:00:00 2001 From: linhai Date: Mon, 24 Nov 2025 16:21:06 +0800 Subject: [PATCH] remove cmsis --- .../ascend_ge/ge_graph_executor_v1.cc | 40 ++-- .../micro/cmake/cortex-m/CMakeLists.txt | 20 -- .../converter/micro/cmake/file_list.cmake | 3 - .../generator/component/cmake_component.cc | 3 - .../opcoders/cmsis-nn/int8/add_int8_coder.cc | 128 ------------ .../opcoders/cmsis-nn/int8/add_int8_coder.h | 54 ----- .../cmsis-nn/int8/conv2d_base_coder.cc | 60 ------ .../cmsis-nn/int8/conv2d_base_coder.h | 44 ---- .../cmsis-nn/int8/conv2d_int8_coder.cc | 188 ------------------ .../cmsis-nn/int8/conv2d_int8_coder.h | 70 ------- .../cmsis-nn/int8/dwconv_int8_coder.cc | 163 --------------- .../cmsis-nn/int8/dwconv_int8_coder.h | 78 -------- .../int8/fullconnection_int8_coder.cc | 80 -------- .../cmsis-nn/int8/fullconnection_int8_coder.h | 54 ----- .../opcoders/cmsis-nn/int8/mul_int8_coder.cc | 78 -------- .../opcoders/cmsis-nn/int8/mul_int8_coder.h | 48 ----- .../cmsis-nn/int8/pooling_int8_coder.cc | 109 ---------- .../cmsis-nn/int8/pooling_int8_coder.h | 60 ------ .../cmsis-nn/int8/reshape_int8_coder.cc | 49 ----- .../cmsis-nn/int8/reshape_int8_coder.h | 36 ---- .../cmsis-nn/int8/softmax_int8_coder.cc | 84 -------- .../cmsis-nn/int8/softmax_int8_coder.h | 46 ----- 22 files changed, 26 insertions(+), 1469 deletions(-) delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.h delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.cc delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.cc delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.h delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.cc delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc delete mode 100644 mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h diff --git a/mindspore-lite/src/extendrt/delegate/ascend_ge/ge_graph_executor_v1.cc b/mindspore-lite/src/extendrt/delegate/ascend_ge/ge_graph_executor_v1.cc index 3d2e6acb0..d87682594 100644 --- a/mindspore-lite/src/extendrt/delegate/ascend_ge/ge_graph_executor_v1.cc +++ b/mindspore-lite/src/extendrt/delegate/ascend_ge/ge_graph_executor_v1.cc @@ -200,19 +200,31 @@ bool GeGraphExecutorV1::InitMsTensor(const FuncGraphPtr &graph, uint32_t graph_i bool GeGraphExecutorV1::InitGeTensor(uint32_t graph_id) { // Delayed HBM memory allocation. auto create_func = [this, graph_id](const std::vector &ms_tensors, - std::vector>> *ge_tensors) { - auto summary = this->ge_session_info_.session_->GetCompiledGraphSummary(graph_id); - std::vector shapes; - auto res = summary->GetOutputShapes(shapes); - if (res != ge::GRAPH_SUCCESS) { - MS_LOG(ERROR) << "GetOutputShapes failed!"; - return false; + std::vector>> *ge_tensors, + bool graph_input) { + std::vector> shapes; + if (!graph_input) { + auto summary = this->ge_session_info_.session_->GetCompiledGraphSummary(graph_id); + std::vector ge_shapes; + auto res = summary->GetOutputShapes(ge_shapes); + if (res != ge::GRAPH_SUCCESS) { + MS_LOG(ERROR) << "GetOutputShapes failed!"; + return false; + } + (void)std::transform(ge_shapes.begin(), ge_shapes.end(), std::back_inserter(shapes), + [](const ge::Shape &ge_shape) { return ge_shape.GetDims(); }); + } else { + shapes.resize(ms_tensors.size()); + } + if (shapes.size() != ms_tensors.size()) { + MS_LOG(ERROR) << "The number of shape is different with that of tensor when initializing graph " + << (graph_input ? "input" : "output") << ", which is " << shapes.size() << " VS " + << ms_tensors.size(); } - MS_LOG(INFO) << "MODEL STATUS: " << res; ge_tensors->resize(ms_tensors.size()); for (size_t i = 0; i < ms_tensors.size(); ++i) { auto dtype = static_cast(ms_tensors[i].DataType()); - auto desc = device::ascend::TransformUtil::GetGeTensorDesc(shapes[i].GetDims(), dtype, kOpFormat_NCHW); + auto desc = device::ascend::TransformUtil::GetGeTensorDesc(shapes[i], dtype, kOpFormat_NCHW); if (desc == nullptr) { MS_LOG(ERROR) << "Failed to create Tensor Desc"; return false; @@ -228,12 +240,12 @@ bool GeGraphExecutorV1::InitGeTensor(uint32_t graph_id) { return true; }; ge_inputs_[graph_id] = {}; - if (!create_func(ms_inputs_[graph_id], &ge_inputs_[graph_id])) { + if (!create_func(ms_inputs_[graph_id], &ge_inputs_[graph_id], true)) { MS_LOG(ERROR) << "Create ge::Tensor for inputs failed."; return false; } ge_outputs_[graph_id] = {}; - if (!create_func(ms_outputs_[graph_id], &ge_outputs_[graph_id])) { + if (!create_func(ms_outputs_[graph_id], &ge_outputs_[graph_id], false)) { MS_LOG(ERROR) << "Create ge::Tensor for outputs failed."; return false; } @@ -254,6 +266,7 @@ bool GeGraphExecutorV1::RunGraph(uint32_t graph_id, const std::vector MS_LOG(ERROR) << " outputs param is nullptr."; return false; } + MS_LOG(INFO) << "Run ge graph [" << graph_id << "] with " << inputs.size() << " ms_tensor_inputs"; std::vector ge_inputs; if (!PrepareGeInputs(inputs, &ge_inputs, graph_id)) { MS_LOG(ERROR) << "Prepare ge inputs failed."; @@ -324,7 +337,6 @@ bool GeGraphExecutorV1::PrepareGeInputs(const std::vector &inputs, std auto mem_ret = memory_manager_->MemcpyHost2Device(device_addr, size, input.MutableData(), size); if (!mem_ret) { MS_LOG(ERROR) << "Failed to H2D, input " << i; - (void)memory_manager_->FreeDeviceMemory(device_addr); return false; } } else { @@ -424,7 +436,7 @@ bool GeGraphExecutorV1::PostProcessGeOutputs(std::vector *outputs, uin if (outputs->empty()) { for (size_t i = 0; i < ge_outputs_[graph_id].size(); ++i) { auto name = "output[" + std::to_string(i) + "]"; - auto ms_tensor = MSTensor(name, ms_inputs_[graph_id][i].DataType(), {}, nullptr, 0); + auto ms_tensor = MSTensor(name, ms_outputs_[graph_id][i].DataType(), {}, nullptr, 0); outputs->push_back(ms_tensor); } } @@ -436,7 +448,7 @@ bool GeGraphExecutorV1::PostProcessGeOutputs(std::vector *outputs, uin continue; } if (ms_tensor.Data() == nullptr) { - ms_tensor.SetDataType(ms_inputs_[graph_id][i].DataType()); + ms_tensor.SetDataType(ms_outputs_[graph_id][i].DataType()); ms_tensor.SetShape(it.first.GetTensorDesc().GetShape().GetDims()); } if (ms_tensor.DataSize() > it.second.second) { diff --git a/mindspore-lite/tools/converter/micro/cmake/cortex-m/CMakeLists.txt b/mindspore-lite/tools/converter/micro/cmake/cortex-m/CMakeLists.txt index d5b9fe304..c6f3f39f6 100644 --- a/mindspore-lite/tools/converter/micro/cmake/cortex-m/CMakeLists.txt +++ b/mindspore-lite/tools/converter/micro/cmake/cortex-m/CMakeLists.txt @@ -8,26 +8,6 @@ include_directories(${NNACL_DIR}/..) include(${TOP_DIR}/cmake/utils.cmake) -set(CMSIS_DIR ${CMAKE_BINARY_DIR}/cmsis) -message("build cmsis kernels") -include_directories(${CMSIS_DIR}/CMSIS/Core/Include) -include_directories(${CMSIS_DIR}/CMSIS/DSP/Include) -include_directories(${CMSIS_DIR}/CMSIS/NN/Include) - -file(GLOB CMSIS_OPS - ${CMSIS_DIR}/CMSIS/NN/Source/BasicMathFunctions/*.c - ${CMSIS_DIR}/CMSIS/NN/Source/ActivationFunctions/*.c - ${CMSIS_DIR}/CMSIS/NN/Source/ConcatenationFunctions/*.c - ${CMSIS_DIR}/CMSIS/NN/Source/ConvolutionFunctions/*.c - ${CMSIS_DIR}/CMSIS/NN/Source/FullyConnectedFunctions/*.c - ${CMSIS_DIR}/CMSIS/NN/Source/NNSupportFunctions/*.c - ${CMSIS_DIR}/CMSIS/NN/Source/PoolingFunctions/*.c - ${CMSIS_DIR}/CMSIS/NN/Source/ReshapeFunctions/*.c - ${CMSIS_DIR}/CMSIS/NN/Source/SoftmaxFunctions/*.c - ) - -add_library(cmsis_nn STATIC ${CMSIS_OPS}) - include_directories(${MICRO_DIR}/coder/) set(WRAPPER_DIR ${MICRO_DIR}/coder/wrapper/) file(GLOB WRAPPER_SRC diff --git a/mindspore-lite/tools/converter/micro/cmake/file_list.cmake b/mindspore-lite/tools/converter/micro/cmake/file_list.cmake index c4d7bd3b2..2395ff5a1 100644 --- a/mindspore-lite/tools/converter/micro/cmake/file_list.cmake +++ b/mindspore-lite/tools/converter/micro/cmake/file_list.cmake @@ -36,9 +36,6 @@ file(GLOB CODER_OPCODERS_SRC ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/*.cc #### base coder ${MICRO_DIR}/coder/opcoders/base/*.cc - # cmsis is not supported and currently disabled in the current version. - # #### cmsis int8 coder - # ${MICRO_DIR}/coder/opcoders/cmsis-nn/int8/*.cc #### nnacl fp16 coder ${MICRO_DIR}/coder/opcoders/nnacl/fp16/*.cc #### nnacl fp32 coder diff --git a/mindspore-lite/tools/converter/micro/coder/generator/component/cmake_component.cc b/mindspore-lite/tools/converter/micro/coder/generator/component/cmake_component.cc index 099afab7e..216c39b9c 100644 --- a/mindspore-lite/tools/converter/micro/coder/generator/component/cmake_component.cc +++ b/mindspore-lite/tools/converter/micro/coder/generator/component/cmake_component.cc @@ -22,9 +22,6 @@ namespace mindspore::lite::micro { void CodeCMakeNetLibrary(std::ofstream &ofs, const std::unique_ptr &ctx, const Configurator *config) { ofs << "include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include/)\n"; ofs << "include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../)\n"; - if (config->target() == kCortex_M) { - // cmsis is not supported and currently disabled in the current version. - } ofs << "set(OP_SRC\n"; for (const std::string &c_file : ctx->c_files()) { ofs << " " << c_file << ".o\n"; diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc deleted file mode 100644 index 2e92d877c..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc +++ /dev/null @@ -1,128 +0,0 @@ -/** - * Copyright 2022 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 "coder/opcoders/cmsis-nn/int8/add_int8_coder.h" -#include -#include -#include "coder/log.h" -#include "coder/opcoders/file_collector.h" -#include "coder/opcoders/nnacl/int8/add_int8_coder.h" -#include "coder/opcoders/serializers/serializer.h" -#include "coder/utils/common.h" -#include "mindspore/ops/op_def/array_ops.h" -#include "nnacl_c/arithmetic_parameter.h" -#include "nnacl_c/int8/quantize.h" - -using mindspore::schema::PrimitiveType_AddFusion; - -namespace mindspore::lite::micro::cmsis { -int AddInt8Coder::Prepare(CoderContext *const context) { - MS_CHECK_GE(input_tensors_.size(), 2, RET_ERROR); - input1_ = input_tensors_.at(0); - input2 = input_tensors_.at(1); - - MS_CHECK_PTR(input1_); - MS_CHECK_PTR(input2); - - MS_CHECK_TRUE(!input1_->quant_params().empty(), "input1_ quant_params is empty"); - MS_CHECK_TRUE(!input2->quant_params().empty(), "input2_ quant_params is empty"); - MS_CHECK_TRUE(!output_tensor_->quant_params().empty(), "output quant_params is empty"); - - input_1_offset_ = -input1_->quant_params().at(0).zeroPoint; - input_2_offset_ = -input2->quant_params().at(0).zeroPoint; - out_offset_ = output_tensor_->quant_params().at(0).zeroPoint; - const double input1_scale = input1_->quant_params().at(0).scale; - const double input2_scale = input2->quant_params().at(0).scale; - const double output_scale = output_tensor_->quant_params().at(0).scale; - - const double twice_max_input_scale = 2 * std::max(input1_scale, input2_scale); - MS_CHECK_TRUE(twice_max_input_scale > 0, "twice_max_input_scale should larger than 0."); - MS_CHECK_TRUE(output_scale > 0, "output_scale should larger than 0."); - const double real_input1_multiplier = static_cast(input1_scale) / twice_max_input_scale; - const double real_input2_multiplier = static_cast(input2_scale) / twice_max_input_scale; - const double real_output_multiplier = - twice_max_input_scale / ((1 << static_cast(kLeftShift)) * static_cast(output_scale)); - - MS_CHECK_TRUE((real_input1_multiplier >= 0) && (real_input1_multiplier <= 1), - "real_input1_multiplier should be in (0, 1)"); - QuantizeMultiplier(real_input1_multiplier, &input_1_mult_, &input_1_shift_); - MS_CHECK_TRUE((real_input2_multiplier >= 0) && (real_input2_multiplier <= 1), - "real_input2_multiplier should be in (0, 1)"); - QuantizeMultiplier(real_input2_multiplier, &input_2_mult_, &input_2_shift_); - MS_CHECK_TRUE((real_output_multiplier >= 0) && (real_output_multiplier <= 1), - "real_output_multiplier should be in (0, 1)"); - QuantizeMultiplier(real_output_multiplier, &out_mult_, &out_shift_); - - out_activation_min_ = std::numeric_limits::min(); - out_activation_max_ = std::numeric_limits::max(); - - MS_CHECK_TRUE(input1_->ElementsNum() == input2->ElementsNum(), "tensor length not match"); - - block_size_ = input1_->ElementsNum(); - - return RET_OK; -} - -int AddInt8Coder::DoCode(CoderContext *const context) { - Serializer code; - code.precision(kPrecision); - - Collect(context, - { - "CMSIS/NN/Include/arm_nnfunctions.h", - }, - { - "arm_elementwise_add_s8.c", - }); - - code.CodeFunction("arm_elementwise_add_s8", input1_, input2, input_1_offset_, input_1_mult_, input_1_shift_, - input_2_offset_, input_2_mult_, input_2_shift_, kLeftShift, output_tensor_, out_offset_, out_mult_, - out_shift_, out_activation_min_, out_activation_max_, block_size_); - - MS_LOG(INFO) << "AddInt8Coder has been called"; - context->AppendCode(code.str()); - return RET_OK; -} - -std::unique_ptr AddFusionInt8CoderCreator(const std::vector &in_tensors, - const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target, - int schema_version) { - if (node == nullptr) { - MS_LOG(ERROR) << "node is null"; - return nullptr; - } - if (in_tensors.size() != kTwo) { - MS_LOG(ERROR) << "in_tensors size error."; - return nullptr; - } - std::unique_ptr coder; - if (in_tensors[0]->ElementsNum() == in_tensors[1]->ElementsNum()) { - coder = std::make_unique(in_tensors, out_tensors, node, node_index, target); - } else { - coder = - std::make_unique(in_tensors, out_tensors, node, node_index, target); - } - if (coder == nullptr) { - return nullptr; - } - - coder->SetSchemaVersion(schema_version); - return coder; -} - -REG_OPERATOR_CODER(kCortex_M, kNumberTypeInt8, PrimitiveType_AddFusion, AddFusionInt8CoderCreator) -} // namespace mindspore::lite::micro::cmsis diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.h b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.h deleted file mode 100644 index 13e5a1c29..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2021 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_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_ADD_INT8_CODER_H_ -#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_ADD_INT8_CODER_H_ - -#include -#include "coder/opcoders/op_coder.h" - -namespace mindspore::lite::micro::cmsis { -class AddInt8Coder final : public OperatorCoder { - public: - AddInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target) - : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} - - ~AddInt8Coder() override = default; - int Prepare(CoderContext *const context) override; - - int DoCode(CoderContext *const context) override; - - private: - Tensor *input1_{nullptr}; - Tensor *input2{nullptr}; - - int32_t input_1_offset_{0}; - int32_t input_1_mult_{0}; - int32_t input_1_shift_{0}; - int32_t input_2_offset_{0}; - int32_t input_2_mult_{0}; - int32_t input_2_shift_{0}; - int32_t left_shift_{0}; - int32_t out_offset_{0}; - int32_t out_mult_{0}; - int32_t out_shift_{0}; - int32_t out_activation_min_{0}; - int32_t out_activation_max_{0}; - uint32_t block_size_{0}; -}; -} // namespace mindspore::lite::micro::cmsis -#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_ADD_INT8_CODER_H_ diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.cc b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.cc deleted file mode 100644 index b7f0a556a..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.cc +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright 2021 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 "coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h" -#include "nnacl_c/int8/quantize.h" - -namespace mindspore::lite::micro::cmsis { -int Conv2DBaseCoder::SetQuantArgs() { - int channel = output_tensor_->Channel(); - size_t channel_data_size = static_cast(channel) * sizeof(int32_t); - output_mult_ = reinterpret_cast(malloc(channel_data_size)); - MS_CHECK_PTR(output_mult_); - output_shift_ = reinterpret_cast(malloc(channel_data_size)); - MS_CHECK_PTR(output_shift_); - - const ::QuantArg *filter_quant_args = conv_quant_arg_->filter_quant_args_; - auto input_scale = static_cast(conv_quant_arg_->input_quant_args_[0].scale_); - auto output_scale = static_cast(conv_quant_arg_->output_quant_args_[0].scale_); - int32_t significand; - int channel_shift; - if (conv_quant_arg_->filter_arg_num_ > 1) { - for (int i = 0; i < channel; ++i) { - // If per-tensor quantization parameter is specified, broadcast it along the - // quantization dimension (channels_out). - MS_CHECK_TRUE(conv_quant_arg_->filter_arg_num_ == static_cast(channel), "quant num not match"); - const auto filter_scale = static_cast(filter_quant_args[i].scale_); - MS_CHECK_TRUE(output_scale > 0, "output_scale should larger than 0."); - const double effective_output_scale = input_scale * filter_scale / output_scale; - QuantizeMultiplier(effective_output_scale, &significand, &channel_shift); - output_mult_[i] = significand; - output_shift_[i] = channel_shift; - } - } else { - // broadcast multiplier and shift to all array if per-tensor - const auto filter_scale = static_cast(filter_quant_args[0].scale_); - MS_CHECK_TRUE(output_scale > 0, "output_scale should larger than 0."); - const double effective_output_scale = input_scale * filter_scale / output_scale; - QuantizeMultiplier(effective_output_scale, &significand, &channel_shift); - for (int i = 0; i < channel; ++i) { - output_mult_[i] = significand; - output_shift_[i] = channel_shift; - } - } - - return RET_OK; -} -} // namespace mindspore::lite::micro::cmsis diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h deleted file mode 100644 index c9bb04ee5..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2021 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_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_CONV2D_BASE_CODER_H_ -#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_CONV2D_BASE_CODER_H_ - -#include -#include -#include "coder/opcoders/base/conv2d_base_coder.h" -#include "nnacl_c/conv_parameter.h" - -namespace mindspore::lite::micro::cmsis { -class Conv2DBaseCoder : public micro::Conv2DBaseCoder { - public: - explicit Conv2DBaseCoder(const std::vector &in_tensors, const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target) - : micro::Conv2DBaseCoder(in_tensors, out_tensors, node, node_index, target) {} - - ~Conv2DBaseCoder() override { - free(output_mult_); - free(output_shift_); - } - - protected: - int SetQuantArgs(); - - int32_t *output_mult_{nullptr}; - int32_t *output_shift_{nullptr}; -}; -} // namespace mindspore::lite::micro::cmsis -#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_CONV2D_BASE_CODER_H_ diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc deleted file mode 100644 index 66bd94e29..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc +++ /dev/null @@ -1,188 +0,0 @@ -/** - * Copyright 2022 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 "coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h" -#include -#include -#include -#include "coder/opcoders/serializers/serializer.h" -#include "coder/opcoders/file_collector.h" -#include "coder/utils/common.h" -#include "src/common/prim_util.h" - -using mindspore::schema::PrimitiveType_Conv2DFusion; - -namespace mindspore::lite::micro::cmsis { -int Conv2DInt8Coder::Prepare(CoderContext *const context) { - Conv2DBaseCoder::Init(); - MS_CHECK_RET_CODE(micro::Conv2DBaseCoder::CheckLayout(input_tensor_), "CheckLayout failed"); - MS_CHECK_RET_CODE(micro::Conv2DBaseCoder::SetQuantParam(), "SetQuantParam failed"); - MS_CHECK_RET_CODE(Conv2DBaseCoder::SetQuantArgs(), "SetQuantArgs failed"); - MS_CHECK_RET_CODE(SetParameters(), "SetParameters failed"); - CheckSupportOptimize(); - MS_CHECK_RET_CODE(InitTmpBuffer(), "InitTmpBuffer failed"); - return RET_OK; -} - -int Conv2DInt8Coder::DoCode(CoderContext *const context) { - Serializer code; - code.precision(kPrecision); - Collect(context, - { - "CMSIS/NN/Include/arm_nnfunctions.h", - }, - {}); - if (opt_ != Convolve_1x1_fast) { - code.CodeFunction("memset", buffer_, 0, buffer_size_); - } - code.CodeArray("output_shift", output_shift_, output_ch_); - code.CodeArray("output_mult", output_mult_, output_ch_); - switch (opt_) { - case Basic: - Collect(context, {}, - { - "arm_convolve_s8.c", - "arm_nn_mat_mult_kernel_s8_s16.c", - "arm_q7_to_q15_with_offset.c", - }); - code.CodeFunction("arm_convolve_s8", input_tensor_, input_x_, input_y_, input_ch_, input_batches_, filter_tensor_, - output_ch_, kernel_x_, kernel_y_, pad_x_, pad_y_, stride_x_, stride_y_, bias_tensor_, - output_tensor_, "output_shift", "output_mult", out_offset_, input_offset_, out_activation_min_, - out_activation_max_, output_x_, output_y_, buffer_); - break; - case Convolve_1_x_n: - Collect(context, {}, - { - "arm_convolve_1_x_n_s8.c", - "arm_nn_mat_mul_core_1x_s8.c", - }); - code.CodeFunction("arm_convolve_1_x_n_s8", input_tensor_, input_x_, input_ch_, input_batches_, filter_tensor_, - output_ch_, kernel_x_, pad_x_, stride_x_, bias_tensor_, output_tensor_, "output_shift", - "output_mult", out_offset_, input_offset_, out_activation_min_, out_activation_max_, output_x_, - buffer_); - break; - case Convolve_1x1_fast: - Collect(context, {}, - { - "arm_convolve_1x1_s8_fast.c", - "arm_nn_mat_mult_nt_t_s8.c", - "arm_nn_mat_mul_core_4x_s8.c", - "arm_nn_mat_mul_core_1x_s8.c", - }); - code.CodeFunction("arm_convolve_1x1_s8_fast", input_tensor_, input_x_, input_y_, input_ch_, input_batches_, - filter_tensor_, output_ch_, pad_x_, pad_y_, stride_x_, stride_y_, bias_tensor_, output_tensor_, - "output_shift", "output_mult", out_offset_, input_offset_, out_activation_min_, - out_activation_max_, output_x_, output_y_, buffer_); - break; - default: - MS_LOG(ERROR) << "opt enum value is not defined"; - return RET_ERROR; - } - - context->AppendCode(code.str()); - return RET_OK; -} - -int Conv2DInt8Coder::SetParameters() { - MS_CHECK_TRUE(input_tensor_->Channel() == filter_tensor_->DimensionSize(kNHWC_C), - "input Channel and filter size not match!"); - MS_CHECK_TRUE(output_tensor_->Channel() == filter_tensor_->DimensionSize(kNHWC_N), - "output Channel and filter size not match!"); - - input_x_ = input_tensor_->Width(); - input_y_ = input_tensor_->Height(); - input_ch_ = input_tensor_->Channel(); - input_batches_ = input_tensor_->Batch(); - - kernel_x_ = filter_tensor_->DimensionSize(kNHWC_W); - kernel_y_ = filter_tensor_->DimensionSize(kNHWC_H); - pad_x_ = conv_param_->pad_l_; - pad_y_ = conv_param_->pad_u_; - - stride_x_ = conv_param_->stride_w_; - stride_y_ = conv_param_->stride_h_; - - MS_CHECK_TRUE(!input_tensor_->quant_params().empty(), "input quant_params is empty"); - MS_CHECK_TRUE(!output_tensor_->quant_params().empty(), "output quant_params is empty"); - LiteQuantParam input_quant_arg = input_tensor_->quant_params().at(0); - LiteQuantParam output_quant_arg = output_tensor_->quant_params().at(0); - - input_offset_ = -input_quant_arg.zeroPoint; - out_offset_ = output_quant_arg.zeroPoint; - - output_x_ = output_tensor_->DimensionSize(kNHWC_W); - output_y_ = output_tensor_->DimensionSize(kNHWC_H); - output_ch_ = output_tensor_->Channel(); - - CalculateActivationRangeQuantized(conv_param_->act_type_ == ActType_Relu, conv_param_->act_type_ == ActType_Relu6, - output_quant_arg.zeroPoint, static_cast(output_quant_arg.scale), - &out_activation_min_, &out_activation_max_); - return RET_OK; -} - -void Conv2DInt8Coder::CheckSupportOptimize() { - if ((pad_x_ == 0) && (pad_y_ == 0) && (input_ch_ % kTensorChannelBaseLengthFour == 0) && (stride_x_ == 1) && - (stride_y_ == 1) && (kernel_x_ == 1) && (kernel_y_ == 1)) { - opt_ = Convolve_1x1_fast; - return; - } - - if ((output_x_ == 1) && (input_x_ == 1) && (kernel_y_ == 1) && (output_x_ % kTensorChannelBaseLengthFour == 0) && - (input_batches_ == 1)) { - opt_ = Convolve_1_x_n; - return; - } - opt_ = Basic; -} - -int Conv2DInt8Coder::InitTmpBuffer() { - const size_t kPartial = 2; - switch (opt_) { - case Basic: - buffer_size_ = - static_cast(kPartial * input_tensor_->Channel() * filter_tensor_->Width() * filter_tensor_->Height()) * - sizeof(int16_t); - break; - case Convolve_1_x_n: - buffer_size_ = - static_cast(kPartial * input_tensor_->Channel() * filter_tensor_->Width() * filter_tensor_->Height()) * - sizeof(int16_t); - break; - case Convolve_1x1_fast: - // do nothing - buffer_size_ = 0; - return RET_OK; - default: - MS_LOG(ERROR) << "opt enum value is not defined"; - return RET_ERROR; - } - buffer_ = static_cast(allocator_->Malloc(kNumberTypeInt16, buffer_size_, kWorkspace)); - MS_CHECK_PTR(buffer_); - return RET_OK; -} - -std::unique_ptr CmsisConv2DInt8OpCoderCreator(const std::vector &in_tensors, - const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, - Target target, int schema_version) { - MS_CHECK_PTR_RET_NULL(node); - std::unique_ptr coder = - std::make_unique(in_tensors, out_tensors, node, node_index, target); - return coder; -} - -REG_OPERATOR_CODER(kCortex_M, kNumberTypeInt8, PrimitiveType_Conv2DFusion, CPUOpCoderCreator) -} // namespace mindspore::lite::micro::cmsis diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h deleted file mode 100644 index 1ead129ab..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright 2021 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_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_CONV2D_INT8_CODER_H_ -#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_CONV2D_INT8_CODER_H_ - -#include -#include -#include "coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h" -#include "nnacl_c/conv_parameter.h" - -namespace mindspore::lite::micro::cmsis { -class Conv2DInt8Coder final : public Conv2DBaseCoder { - public: - explicit Conv2DInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target) - : Conv2DBaseCoder(in_tensors, out_tensors, node, node_index, target) {} - int Prepare(CoderContext *context) override; - - int DoCode(CoderContext *ctx) override; - - ~Conv2DInt8Coder() override = default; - - private: - enum ConvOpt { Basic = 0, Convolve_1_x_n = 1, Convolve_1x1_fast = 2 }; - - void CheckSupportOptimize(); - - int SetParameters(); - - int InitTmpBuffer(); - - uint16_t input_x_{0}; - uint16_t input_y_{0}; - uint16_t input_ch_{0}; - uint16_t input_batches_{0}; - uint16_t output_ch_{0}; - uint16_t kernel_x_{0}; - uint16_t kernel_y_{0}; - uint16_t pad_x_{0}; - uint16_t pad_y_{0}; - uint16_t stride_x_{0}; - uint16_t stride_y_{0}; - int32_t out_offset_{0}; - int32_t input_offset_{0}; - int32_t out_activation_min_{0}; - int32_t out_activation_max_{0}; - uint16_t output_x_{0}; - uint16_t output_y_{0}; - - int16_t *buffer_{nullptr}; - size_t buffer_size_{0}; - - ConvOpt opt_{ConvOpt::Basic}; -}; -} // namespace mindspore::lite::micro::cmsis -#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_CONV2D_INT8_CODER_H_ diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc deleted file mode 100644 index bdfb10e80..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc +++ /dev/null @@ -1,163 +0,0 @@ -/** - * Copyright 2021 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 "coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h" -#include -#include "coder/opcoders/serializers/serializer.h" -#include "coder/opcoders/file_collector.h" -#include "coder/log.h" -#include "coder/utils/common.h" - -namespace mindspore::lite::micro::cmsis { -int DWConvInt8Coder::Prepare(CoderContext *const context) { - Conv2DBaseCoder::Init(); - MS_CHECK_RET_CODE(micro::Conv2DBaseCoder::CheckLayout(input_tensor_), "Check layout failed."); - MS_CHECK_RET_CODE(micro::Conv2DBaseCoder::SetQuantParam(), "SetQuantParam failed"); - MS_CHECK_RET_CODE(Conv2DBaseCoder::SetQuantArgs(), "SetQuantArgs failed"); - MS_CHECK_RET_CODE(InitWeightBias(), "InitWeightBias failed"); - MS_CHECK_RET_CODE(SetParameters(), "SetParameters failed"); - CheckSupportOptimize(); - MS_CHECK_RET_CODE(InitTmpBuffer(), "InitTmpBuffer failed"); - return RET_OK; -} - -int DWConvInt8Coder::DoCode(CoderContext *const context) { - Serializer code; - code.precision(kPrecision); - - Collect(context, - { - "CMSIS/NN/Include/arm_nnfunctions.h", - }, - {}); - code.CodeArray("output_shift", output_shift_, output_ch_); - code.CodeArray("output_mult", output_mult_, output_ch_); - switch (optimize_) { - case Conv_3x3: - Collect(context, {}, - { - "arm_depthwise_conv_3x3_s8.c", - }); - code.CodeFunction("arm_depthwise_conv_3x3_s8", input_tensor_, input_x_, input_y_, input_ch_, filter_tensor_, - output_ch_, pad_x_, pad_y_, stride_x_, stride_y_, bias_tensor_, output_tensor_, "output_shift", - "output_mult", output_x_, output_y_, output_offset_, input_offset_, output_activation_min_, - output_activation_max_, dilation_x_, dilation_y_, "NULL"); - break; - case Conv_opt: - // arm_depthwise_conv_s8_opt also depends on arm_depthwise_conv_s8 - Collect(context, {}, - { - "arm_depthwise_conv_s8.c", - "arm_depthwise_conv_s8_opt.c", - }); - code.CodeFunction("arm_depthwise_conv_s8_opt", input_tensor_, input_x_, input_y_, input_ch_, filter_tensor_, - output_ch_, kernel_x_, kernel_y_, pad_x_, pad_y_, stride_x_, stride_y_, bias_tensor_, - output_tensor_, "output_shift", "output_mult", output_x_, output_y_, output_offset_, - input_offset_, output_activation_min_, output_activation_max_, dilation_x_, dilation_y_, - "NULL"); - break; - case Basic: - Collect(context, {}, - { - "arm_depthwise_conv_s8.c", - }); - code.CodeFunction("arm_depthwise_conv_s8", input_tensor_, input_x_, input_y_, input_ch_, filter_tensor_, - output_ch_, ch_mult_, kernel_x_, kernel_y_, pad_x_, pad_y_, stride_x_, stride_y_, bias_tensor_, - output_tensor_, "output_shift", "output_mult", output_x_, output_y_, output_offset_, - input_offset_, output_activation_min_, output_activation_max_, dilation_x_, dilation_y_, - "NULL"); - break; - default: - MS_LOG(ERROR) << "unsupported optimize_r"; - break; - } - context->AppendCode(code.str()); - return RET_OK; -} - -int DWConvInt8Coder::InitWeightBias() { - auto *origin_weight = reinterpret_cast(filter_tensor_->data()); - MS_CHECK_PTR(origin_weight); - auto pack_weight_size = - static_cast(filter_tensor_->Batch() * filter_tensor_->Height() * filter_tensor_->Width()); - packed_weight_ = - static_cast(allocator_->Malloc(kNumberTypeInt8, pack_weight_size * sizeof(int8_t), kOfflinePackWeight)); - MS_ASSERT(packed_weight_); - PackNCHWToNHWCInt8(origin_weight, packed_weight_, 1, filter_tensor_->Height() * filter_tensor_->Width(), - filter_tensor_->Batch()); - return RET_OK; -} - -int DWConvInt8Coder::SetParameters() { - input_x_ = input_tensor_->Width(); - input_y_ = input_tensor_->Height(); - input_ch_ = input_tensor_->Channel(); - output_ch_ = output_tensor_->Channel(); - - // depth_multiplier - MS_CHECK_TRUE(input_tensor_->Channel() > 0, "input_tensor_->Channel() should larger than 0."); - ch_mult_ = output_tensor_->Channel() / input_tensor_->Channel(); - - kernel_x_ = filter_tensor_->Width(); - kernel_y_ = filter_tensor_->Height(); - - pad_y_ = conv_param_->pad_u_; - pad_x_ = conv_param_->pad_l_; - - stride_y_ = conv_param_->stride_h_; - stride_x_ = conv_param_->stride_w_; - - MS_CHECK_TRUE(!input_tensor_->quant_params().empty(), "input quant params shouldn't be empty"); - LiteQuantParam input_quant_arg = input_tensor_->quant_params().at(0); - MS_CHECK_TRUE(!output_tensor_->quant_params().empty(), "output quant params shouldn't be empty"); - LiteQuantParam output_quant_arg = output_tensor_->quant_params().at(0); - - output_x_ = output_tensor_->Width(); - output_y_ = output_tensor_->Height(); - input_offset_ = -input_quant_arg.zeroPoint; - output_offset_ = output_quant_arg.zeroPoint; - - CalculateActivationRangeQuantized(conv_param_->act_type_ == ActType_Relu, conv_param_->act_type_ == ActType_Relu6, - output_quant_arg.zeroPoint, output_quant_arg.scale, &output_activation_min_, - &output_activation_max_); - return RET_OK; -} - -void DWConvInt8Coder::CheckSupportOptimize() { - if (ch_mult_ == 1) { - if ((kernel_x_ == kKernelWHSizeThree) && (kernel_y_ == kKernelWHSizeThree) && (pad_y_ <= 1)) { - optimize_ = Conv_3x3; - buffer_size_ = 0; - } else { - optimize_ = Conv_opt; - buffer_size_ = input_ch_ * kernel_x_ * kernel_y_ * sizeof(int16_t); - } - } else { - optimize_ = Basic; - buffer_size_ = 0; - } -} - -int DWConvInt8Coder::InitTmpBuffer() { - if (buffer_size_ != 0) { - buffer = static_cast(allocator_->Malloc(kNumberTypeInt16, buffer_size_, kWorkspace)); - MS_CHECK_PTR(buffer); - } else { - buffer = nullptr; - } - return 0; -} -} // namespace mindspore::lite::micro::cmsis diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h deleted file mode 100644 index dc5ffdb58..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright 2021 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_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_DWCONV_INT8_CODER_H_ -#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_DWCONV_INT8_CODER_H_ - -#include -#include "coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h" -#include "src/litert/kernel/cpu/int8/convolution_depthwise_int8.h" - -namespace mindspore::lite::micro::cmsis { -class DWConvInt8Coder final : public Conv2DBaseCoder { - public: - DWConvInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target) - : Conv2DBaseCoder(in_tensors, out_tensors, node, node_index, target) {} - - ~DWConvInt8Coder() override = default; - - int Prepare(CoderContext *context) override; - - int DoCode(CoderContext *context) override; - - private: - enum DwConvOpt { - Basic = 0, - Conv_3x3 = 1, - Conv_opt = 2, - }; - - int SetParameters(); - - void CheckSupportOptimize(); - - int InitTmpBuffer(); - - int InitWeightBias(); - - int32_t input_x_{0}; - int32_t input_y_{0}; - int32_t input_ch_{0}; - int32_t output_ch_{0}; - int32_t ch_mult_{0}; - int32_t kernel_x_{0}; - int32_t kernel_y_{0}; - int32_t pad_x_{0}; - int32_t pad_y_{0}; - int32_t stride_x_{0}; - int32_t stride_y_{0}; - int32_t output_x_{0}; - int32_t output_y_{0}; - int32_t output_offset_{0}; - int32_t input_offset_{0}; - int32_t output_activation_min_{0}; - int32_t output_activation_max_{0}; - uint16_t dilation_x_{0}; - uint16_t dilation_y_{0}; - - int8_t *packed_weight_{nullptr}; - DwConvOpt optimize_{Basic}; - size_t buffer_size_{0}; - int16_t *buffer{nullptr}; -}; -} // namespace mindspore::lite::micro::cmsis -#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_DWCONV_INT8_CODER_H_ diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.cc b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.cc deleted file mode 100644 index fe2717c87..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.cc +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Copyright 2022 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 "coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h" -#include "coder/opcoders/serializers/serializer.h" -#include "coder/opcoders/file_collector.h" -#include "coder/utils/common.h" - -using mindspore::schema::PrimitiveType_FullConnection; - -namespace mindspore::lite::micro::cmsis { -int FullConnectionInt8Coder::Prepare(CoderContext *const context) { - FullConnectionBaseCoder::Init(); - ConfigInputOutput(); - MS_CHECK_RET_CODE(SetParameters(), "SetParameters failed"); - return RET_OK; -} - -void FullConnectionInt8Coder::ConfigInputOutput() { output_tensor_->set_format(mindspore::NHWC); } - -int FullConnectionInt8Coder::DoCode(CoderContext *const context) { - Serializer code; - code.precision(kPrecision); - - Collect(context, - { - "CMSIS/NN/Include/arm_nnfunctions.h", - }, - { - "arm_fully_connected_s8.c", - "arm_nn_vec_mat_mult_t_s8.c", - }); - - code.CodeFunction("arm_fully_connected_s8", input_tensor_, filter_tensor_, col_dim_, row_dim_, nb_batches_, - input_offset_, filter_offset_, out_multiplier_, out_shift_, output_offset_, bias_tensor_, - output_tensor_, output_activation_min_, output_activation_max_, "NULL"); - context->AppendCode(code.str()); - return RET_OK; -} - -int FullConnectionInt8Coder::SetParameters() { - MS_CHECK_TRUE(output_tensor_->shape().size() == kTensorDimensionSizeTwo, "output tensor size should be 2"); - MS_CHECK_TRUE(!input_tensor_->quant_params().empty(), "input quant_params is empty"); - MS_CHECK_TRUE(!filter_tensor_->quant_params().empty(), "filter quant_params is empty"); - MS_CHECK_TRUE(!output_tensor_->quant_params().empty(), "output quant_params is empty"); - LiteQuantParam input_quant_arg = input_tensor_->quant_params().at(0); - LiteQuantParam filter_quant_arg = filter_tensor_->quant_params().at(0); - LiteQuantParam output_quant_arg = output_tensor_->quant_params().at(0); - - double real_multiplier = input_quant_arg.scale * filter_quant_arg.scale / output_quant_arg.scale; - QuantizeMultiplier(real_multiplier, &out_multiplier_, &out_shift_); - CalculateActivationRangeQuantized(fc_param_->act_type_ == ActType_Relu, fc_param_->act_type_ == ActType_Relu6, - output_quant_arg.zeroPoint, output_quant_arg.scale, &output_activation_min_, - &output_activation_max_); - - input_offset_ = -input_quant_arg.zeroPoint; - filter_offset_ = -filter_quant_arg.zeroPoint; - output_offset_ = output_quant_arg.zeroPoint; - - col_dim_ = filter_tensor_->DimensionSize(filter_tensor_->shape().size() - 1); - row_dim_ = output_tensor_->DimensionSize(1); - nb_batches_ = input_tensor_->Batch(); - return RET_OK; -} - -REG_OPERATOR_CODER(kCortex_M, kNumberTypeInt8, PrimitiveType_FullConnection, CPUOpCoderCreator) -} // namespace mindspore::lite::micro::cmsis diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h deleted file mode 100644 index aa79e03bd..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2021 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_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_FULLCONNECTION_INT8_CODER_H_ -#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_FULLCONNECTION_INT8_CODER_H_ - -#include -#include -#include "coder/opcoders/op_coder.h" -#include "coder/opcoders/base/full_connection_base_coder.h" -#include "nnacl_c/int8/quantize.h" -namespace mindspore::lite::micro::cmsis { -class FullConnectionInt8Coder final : public FullConnectionBaseCoder { - public: - FullConnectionInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target) - : FullConnectionBaseCoder(in_tensors, out_tensors, node, node_index, target) {} - - int Prepare(CoderContext *const context) override; - - int DoCode(CoderContext *const context) override; - - ~FullConnectionInt8Coder() override = default; - - private: - int SetParameters(); - void ConfigInputOutput(); - - uint16_t col_dim_{0}; - uint16_t row_dim_{0}; - uint16_t nb_batches_{0}; - int32_t input_offset_{0}; - int32_t filter_offset_{0}; - int32_t out_multiplier_{0}; - int32_t out_shift_{0}; - int32_t output_offset_{0}; - int32_t output_activation_min_{0}; - int32_t output_activation_max_{0}; -}; -} // namespace mindspore::lite::micro::cmsis -#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_FULLCONNECTION_INT8_CODER_H_ diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc deleted file mode 100644 index 4b6bb69ea..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright 2022 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 "coder/opcoders/cmsis-nn/int8/mul_int8_coder.h" -#include -#include "coder/opcoders/serializers/serializer.h" -#include "nnacl_c/int8/quantize.h" -#include "coder/opcoders/file_collector.h" - -using mindspore::schema::PrimitiveType_MulFusion; - -namespace mindspore::lite::micro::cmsis { -int MulInt8Coder::Prepare(CoderContext *const context) { - input1_ = OperatorCoder::input_tensors().at(0); - input2_ = OperatorCoder::input_tensors().at(1); - - MS_CHECK_PTR(input1_); - MS_CHECK_PTR(input2_); - - MS_CHECK_TRUE(!input1_->quant_params().empty(), "input1_ quant_params is empty"); - MS_CHECK_TRUE(!input2_->quant_params().empty(), "input2_ quant_params is empty"); - MS_CHECK_TRUE(!output_tensor_->quant_params().empty(), "output quant_params is empty"); - - input_1_offset_ = -input1_->quant_params().at(0).zeroPoint; - input_2_offset_ = -input2_->quant_params().at(0).zeroPoint; - out_offset_ = output_tensor_->quant_params().at(0).zeroPoint; - const double input1_scale = input1_->quant_params().at(0).scale; - const double input2_scale = input2_->quant_params().at(0).scale; - const double output_scale = output_tensor_->quant_params().at(0).scale; - - const double real_multiplier = input1_scale * input2_scale / output_scale; - - QuantizeMultiplier(real_multiplier, &out_mult_, &out_shift_); - - CalculateActivationRangeQuantized(false, false, out_offset_, output_scale, &out_activation_min_, - &out_activation_max_); - - MS_CHECK_TRUE(input1_->ElementsNum() == input2_->ElementsNum(), "tensor length not match"); - - block_size_ = input1_->ElementsNum(); - - return RET_OK; -} - -int MulInt8Coder::DoCode(CoderContext *const context) { - Serializer code; - code.precision(kPrecision); - - Collect(context, - { - "CMSIS/NN/Include/arm_nnfunctions.h", - }, - { - "arm_elementwise_mul_s8.c", - }); - - code.CodeFunction("arm_elementwise_mul_s8", input1_, input2_, input_1_offset_, input_2_offset_, output_tensor_, - out_offset_, out_mult_, out_shift_, out_activation_min_, out_activation_max_, block_size_); - - MS_LOG(INFO) << "MulInt8Coder has been called"; - context->AppendCode(code.str()); - return RET_OK; -} -REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_MulFusion, CPUOpCoderCreator) -} // namespace mindspore::lite::micro::cmsis diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.h b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.h deleted file mode 100644 index a16d6956d..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2021 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_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_MUL_INT8_CODER_H_ -#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_MUL_INT8_CODER_H_ - -#include -#include "coder/opcoders/op_coder.h" - -namespace mindspore::lite::micro::cmsis { -class MulInt8Coder final : public OperatorCoder { - public: - MulInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target) - : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} - ~MulInt8Coder() override = default; - int Prepare(CoderContext *const context) override; - - int DoCode(CoderContext *const context) override; - - private: - Tensor *input1_{nullptr}; - Tensor *input2_{nullptr}; - - int32_t input_1_offset_{0}; - int32_t input_2_offset_{0}; - int32_t out_offset_{0}; - int32_t out_mult_{0}; - int32_t out_shift_{0}; - int32_t out_activation_min_{0}; - int32_t out_activation_max_{0}; - uint32_t block_size_{0}; -}; -} // namespace mindspore::lite::micro::cmsis -#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_MUL_INT8_CODER_H_ diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc deleted file mode 100644 index 2c1f8d256..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Copyright 2022 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 "coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h" -#include "coder/opcoders/serializers/serializer.h" -#include "coder/opcoders/file_collector.h" -#include "coder/utils/common.h" - -using mindspore::schema::PrimitiveType_AvgPoolFusion; -using mindspore::schema::PrimitiveType_MaxPoolFusion; - -namespace mindspore::lite::micro::cmsis { -int PoolingInt8Coder::Prepare(CoderContext *const context) { - this->pooling_parameter_ = reinterpret_cast(parameter_); - // get tensors - MS_CHECK_RET_CODE(SetParameters(), "SetParameters failed"); - - if (pooling_parameter_->pool_mode_ == PoolMode_AvgPool) { - buffer_size_ = input_tensor_->Channel() * sizeof(int32_t); - buffer_ = static_cast(allocator_->Malloc(kNumberTypeInt32, buffer_size_, kWorkspace)); - MS_CHECK_PTR(buffer_); - } - - return RET_OK; -} - -int PoolingInt8Coder::DoCode(CoderContext *const context) { - // init struct PoolingParameters - std::string pooling_func; - - if (pooling_parameter_->pool_mode_ == PoolMode_AvgPool) { - Collect(context, {}, - { - "arm_avgpool_s8.c", - }); - pooling_func = "arm_avgpool_s8"; - } else if (pooling_parameter_->pool_mode_ == PoolMode_MaxPool) { - Collect(context, {}, - { - "arm_max_pool_s8.c", - }); - pooling_func = "arm_max_pool_s8"; - } else { - MS_LOG(ERROR) << "unsupported pad mode"; - return RET_ERROR; - } - Collect(context, - { - "CMSIS/NN/Include/arm_nnfunctions.h", - }, - {}); - - Serializer code; - code.precision(kPrecision); - - code.CodeFunction(pooling_func, dim_src_height_, dim_src_width_, dim_dst_height_, dim_dst_width_, stride_height_, - stride_width_, dim_kernel_height_, dim_kernel_width_, padding_height_, padding_width_, act_min_, - act_max_, ch_src_, input_tensor_, buffer_, output_tensor_); - context->AppendCode(code.str()); - return RET_OK; -} - -int PoolingInt8Coder::SetParameters() { - dim_src_height_ = input_tensor_->Height(); - dim_src_width_ = input_tensor_->Width(); - dim_dst_height_ = output_tensor_->DimensionSize(1); - dim_dst_width_ = output_tensor_->DimensionSize(kTensorDimensionSizeTwo); - ch_src_ = input_tensor_->Channel(); - - stride_height_ = pooling_parameter_->stride_h_; - stride_width_ = pooling_parameter_->stride_w_; - - dim_kernel_height_ = pooling_parameter_->window_h_; - dim_kernel_width_ = pooling_parameter_->window_w_; - - // only use pad_u_ and pad_l_ because their value is consistent with tf - // ref: src/ops/conv2d.cc:ConvInferShape - padding_height_ = pooling_parameter_->pad_u_; - padding_width_ = pooling_parameter_->pad_l_; - - MS_CHECK_TRUE(!output_tensor_->quant_params().empty(), "output quant_params is empty"); - LiteQuantParam output_quant_arg = output_tensor_->quant_params().at(0); - CalculateActivationRangeQuantized(pooling_parameter_->act_type_ == ActType_Relu, - pooling_parameter_->act_type_ == ActType_Relu6, output_quant_arg.zeroPoint, - output_quant_arg.scale, &act_min_, &act_max_); - - MS_CHECK_TRUE(input_tensor_->Channel() == output_tensor_->Channel(), - "input Channel and output Channel size not match!"); - return RET_OK; -} - -REG_OPERATOR_CODER(kCortex_M, kNumberTypeInt8, PrimitiveType_AvgPoolFusion, CPUOpCoderCreator) -REG_OPERATOR_CODER(kCortex_M, kNumberTypeInt8, PrimitiveType_MaxPoolFusion, CPUOpCoderCreator) -} // namespace mindspore::lite::micro::cmsis diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h deleted file mode 100644 index daa157ed2..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright 2021 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_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_POOLING_INT8_CODER_H_ -#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_POOLING_INT8_CODER_H_ - -#include -#include -#include -#include "coder/opcoders/op_coder.h" -#include "nnacl_c/int8/pooling_int8.h" - -namespace mindspore::lite::micro::cmsis { -class PoolingInt8Coder final : public OperatorCoder { - public: - PoolingInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target) - : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} - ~PoolingInt8Coder() override = default; - - int Prepare(CoderContext *const context) override; - - int DoCode(CoderContext *const context) override; - - private: - int SetParameters(); - - int dim_src_height_{0}; - int dim_src_width_{0}; - int dim_dst_height_{0}; - int dim_dst_width_{0}; - int stride_height_{0}; - int stride_width_{0}; - int dim_kernel_height_{0}; - int dim_kernel_width_{0}; - int padding_height_{0}; - int padding_width_{0}; - int act_min_{0}; - int act_max_{0}; - int ch_src_{0}; - - int32_t *buffer_{nullptr}; - size_t buffer_size_{0}; - PoolingParameter *pooling_parameter_{nullptr}; -}; -} // namespace mindspore::lite::micro::cmsis -#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_POOLING_INT8_CODER_H_ diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.cc b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.cc deleted file mode 100644 index 3522d57f8..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.cc +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2022 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 "coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h" -#include -#include -#include "coder/opcoders/serializers/serializer.h" - -using mindspore::schema::PrimitiveType_Reshape; - -namespace mindspore::lite::micro::cmsis { -int ReshapeInt8Coder::DoCode(CoderContext *const context) { - int elements_num = input_tensor_->ElementsNum(); - - std::vector input_quant_args = input_tensor_->quant_params(); - std::vector output_quant_args = output_tensor_->quant_params(); - MS_CHECK_TRUE(!input_quant_args.empty(), "input quant_params is empty"); - MS_CHECK_TRUE(!output_quant_args.empty(), "output quant_params is empty"); - // in Int8Reshape, the following values are checked. then it will do a memory copy - // para.in_args_.scale_ == para.out_args_.scale_ && para.in_args_.zp_ == para.out_args_.zp_ - MS_CHECK_TRUE((input_quant_args.at(0).scale == output_quant_args.at(0).scale && - input_quant_args.at(0).zeroPoint == output_quant_args.at(0).zeroPoint), - "the quant arg of input and output should be the same!"); - - Serializer code; - code.precision(kPrecision); - - code.CodeFunction("memcpy", output_tensor_, input_tensor_, elements_num); - - MS_LOG(INFO) << "ReshapeInt8Coder has been called"; - context->AppendCode(code.str()); - return RET_OK; -} - -REG_OPERATOR_CODER(kCortex_M, kNumberTypeInt8, PrimitiveType_Reshape, CPUOpCoderCreator) -} // namespace mindspore::lite::micro::cmsis diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h deleted file mode 100644 index 9182b2d5b..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2021 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_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_RESHAPE_INT8_CODER_H_ -#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_RESHAPE_INT8_CODER_H_ - -#include -#include "coder/opcoders/op_coder.h" - -namespace mindspore::lite::micro::cmsis { -class ReshapeInt8Coder final : public OperatorCoder { - public: - ReshapeInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target) - : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} - - ~ReshapeInt8Coder() override = default; - int Prepare(CoderContext *const context) override { return RET_OK; } - - int DoCode(CoderContext *const context) override; -}; -} // namespace mindspore::lite::micro::cmsis -#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_RESHAPE_INT8_CODER_H_ diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc deleted file mode 100644 index 739d24c88..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright 2022 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 "coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h" -#include -#include "coder/opcoders/serializers/serializer.h" -#include "coder/opcoders/file_collector.h" - -using mindspore::schema::PrimitiveType_Softmax; -namespace mindspore::lite::micro::cmsis { -int SoftMaxInt8Coder::Prepare(CoderContext *const context) { - SoftmaxBaseCoder::Init(); - - MS_CHECK_TRUE(!input_tensor_->quant_params().empty(), "input quant_params is empty"); - LiteQuantParam in_quant_arg = input_tensor_->quant_params().at(0); - quant_params_.in_quant_args_.zp_ = -in_quant_arg.zeroPoint; - - std::vector out_quant_args = output_tensor_->quant_params(); - MS_CHECK_TRUE(!out_quant_args.empty(), "output quant_params is empty"); - quant_params_.out_quant_arg_.scale_ = static_cast(out_quant_args.at(0).scale); - quant_params_.out_quant_arg_.zp_ = out_quant_args.at(0).zeroPoint; - quant_params_.output_activation_min_ = std::numeric_limits::min(); - quant_params_.output_activation_max_ = std::numeric_limits::max(); - - const int total_signed_bits = 31; - const int input_integer_bits = 5; - const double input_real_multiplier = - MSMIN(in_quant_arg.scale * (1 << (unsigned int)(total_signed_bits - input_integer_bits)), - (1ll << total_signed_bits) - 1.0); - // mult, shift - QuantizeMultiplier(input_real_multiplier, &mult_, &shift_); - // Calculate Input Radius - const double max_input_rescaled = 1.0 * ((1 << input_integer_bits) - 1) * - (1ll << static_cast((total_signed_bits - input_integer_bits))) / - (1ll << static_cast(shift_)); - diff_min_ = -1.0 * static_cast(std::floor(max_input_rescaled)); - - const int trailing_dim = static_cast(input_tensor_->shape().size()) - 1; - const int dims_count = input_tensor_->shape().size(); - MS_CHECK_TRUE(trailing_dim >= 0 && trailing_dim < dims_count, "trailing_dim should be in [0, dims_count)"); - num_rows_ = 1; - for (int i = 0; i < dims_count; ++i) { - num_rows_ *= (i == trailing_dim) ? 1 : input_tensor_->DimensionSize(i); - } - - MS_CHECK_TRUE(input_tensor_->DimensionSize(trailing_dim) == output_tensor_->DimensionSize(trailing_dim), - "input and output DimensionSize mismatch"); - row_size_ = input_tensor_->DimensionSize(trailing_dim); - - ReSize(); - return RET_OK; -} - -int SoftMaxInt8Coder::DoCode(CoderContext *const context) { - Serializer code; - code.precision(kPrecision); - - Collect(context, - { - "CMSIS/NN/Include/arm_nnfunctions.h", - }, - { - "arm_softmax_s8.c", - }); - code.CodeFunction("arm_softmax_s8", input_tensor_, num_rows_, row_size_, mult_, shift_, diff_min_, output_tensor_); - MS_LOG(INFO) << "SoftMaxInt8Coder has been called"; - context->AppendCode(code.str()); - return RET_OK; -} -REG_OPERATOR_CODER(kCortex_M, kNumberTypeInt8, PrimitiveType_Softmax, CPUOpCoderCreator) -} // namespace mindspore::lite::micro::cmsis diff --git a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h b/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h deleted file mode 100644 index 514e3e494..000000000 --- a/mindspore-lite/tools/converter/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2021 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_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_SOFTMAX_INT8_CODER_H_ -#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_SOFTMAX_INT8_CODER_H_ - -#include -#include -#include -#include "coder/opcoders/base/softmax_base_coder.h" - -namespace mindspore::lite::micro::cmsis { -class SoftMaxInt8Coder final : public SoftmaxBaseCoder { - public: - SoftMaxInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, - const LiteGraph::Node *node, size_t node_index, Target target) - : SoftmaxBaseCoder(in_tensors, out_tensors, node, node_index, target) {} - - ~SoftMaxInt8Coder() override = default; - - int Prepare(CoderContext *const context) override; - - int DoCode(CoderContext *const context) override; - - private: - int32_t num_rows_{0}; - int32_t row_size_{0}; - int32_t mult_{0}; - int32_t shift_{0}; - int32_t diff_min_{0}; -}; -} // namespace mindspore::lite::micro::cmsis -#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_CODER_OPCODERS_CMSIS_NN_INT8_SOFTMAX_INT8_CODER_H_ -- Gitee