From e41e5e644a188239d0924f0e5638036f32224e97 Mon Sep 17 00:00:00 2001 From: hangq Date: Tue, 4 Aug 2020 09:28:45 +0800 Subject: [PATCH] move callback into include/lite_session.h --- mindspore/lite/include/lite_session.h | 15 +++- mindspore/lite/src/CMakeLists.txt | 1 + mindspore/lite/src/common/ms_tensor_utils.cc | 41 +++++++++ mindspore/lite/src/common/ms_tensor_utils.h | 30 +++++++ mindspore/lite/src/executor.cc | 11 ++- mindspore/lite/src/executor.h | 4 +- mindspore/lite/src/lite_kernel.h | 8 -- mindspore/lite/src/lite_session.cc | 21 ++--- mindspore/lite/src/lite_session.h | 5 +- .../src/runtime/opencl/opencl_executor.cc | 9 +- .../lite/src/runtime/opencl/opencl_executor.h | 3 +- mindspore/lite/test/CMakeLists.txt | 1 + .../converter/quantizer/post_training.cc | 83 ++++++++++--------- .../tools/converter/quantizer/post_training.h | 3 +- 14 files changed, 156 insertions(+), 79 deletions(-) create mode 100644 mindspore/lite/src/common/ms_tensor_utils.cc create mode 100644 mindspore/lite/src/common/ms_tensor_utils.h diff --git a/mindspore/lite/include/lite_session.h b/mindspore/lite/include/lite_session.h index c0118d678d..57683641c8 100644 --- a/mindspore/lite/include/lite_session.h +++ b/mindspore/lite/include/lite_session.h @@ -26,6 +26,13 @@ namespace mindspore { namespace session { +struct CallBackParam { + std::string name_callback_aram; +}; + +using KernelCallBack = std::function inputs, + std::vector outputs, const CallBackParam &opInfo)>; + /// \brief LiteSession defined by MindSpore Lite. class MS_API LiteSession { public: @@ -65,12 +72,15 @@ class MS_API LiteSession { /// \return A vector of MindSpore Lite MSTensor. virtual std::vector GetInputsByName(const std::string &node_name) const = 0; - /// \brief Run model compiled by this session. + /// \brief Run session with callback. + /// + /// \param[in] before Define a call_back_function called before running each node + /// \param[in] after Define a call_back_function called after running each node /// /// \note RunGraph should called after CompileGraph. /// /// \return ErrorCode of run graph. - virtual int RunGraph() = 0; + virtual int RunGraph(const KernelCallBack &before = nullptr, const KernelCallBack &after = nullptr) = 0; /// \brief Get output MindSpore Lite MSTensors of model. /// @@ -87,4 +97,3 @@ class MS_API LiteSession { } // namespace session } // namespace mindspore #endif // MINDSPORE_LITE_INCLUDE_LITE_SESSION_H - diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt index bec9eaedcc..23ef963dad 100644 --- a/mindspore/lite/src/CMakeLists.txt +++ b/mindspore/lite/src/CMakeLists.txt @@ -1,5 +1,6 @@ set(LITE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/common/graph_util.cc + ${CMAKE_CURRENT_SOURCE_DIR}/common/ms_tensor_utils.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/allocator.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/runtime_api.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/thread_pool.cc diff --git a/mindspore/lite/src/common/ms_tensor_utils.cc b/mindspore/lite/src/common/ms_tensor_utils.cc new file mode 100644 index 0000000000..529d9f47bc --- /dev/null +++ b/mindspore/lite/src/common/ms_tensor_utils.cc @@ -0,0 +1,41 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "src/common/ms_tensor_utils.h" + +#include +#include "utils/log_adapter.h" + +namespace mindspore { +namespace tensor { +using mindspore::lite::tensor::LiteTensor; +using mindspore::lite::tensor::Tensor; + +std::vector PackToMSTensors(const std::vector &in_tensors) { + std::vector ret; + for (auto *lite_tensor : in_tensors) { + MS_ASSERT(lite_tensor != nullptr); + auto *ms_tensor = new (std::nothrow) LiteTensor(lite_tensor); + if (ms_tensor == nullptr) { + MS_LOG(ERROR) << "new LiteTensor failed"; + return ret; + } + ret.emplace_back(); + } + return ret; +} +} // namespace tensor +} // namespace mindspore diff --git a/mindspore/lite/src/common/ms_tensor_utils.h b/mindspore/lite/src/common/ms_tensor_utils.h new file mode 100644 index 0000000000..fc68d0e951 --- /dev/null +++ b/mindspore/lite/src/common/ms_tensor_utils.h @@ -0,0 +1,30 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LITE_MS_TENSOR_UTILS_H +#define LITE_MS_TENSOR_UTILS_H + +#include +#include "include/ms_tensor.h" +#include "src/ir/tensor.h" + +namespace mindspore { +namespace tensor { +std::vector PackToMSTensors(const std::vector &in_tensors); +} +} // namespace mindspore + +#endif // LITE_MS_TENSOR_UTILS_H diff --git a/mindspore/lite/src/executor.cc b/mindspore/lite/src/executor.cc index 5b53c1269b..27289dfa0b 100644 --- a/mindspore/lite/src/executor.cc +++ b/mindspore/lite/src/executor.cc @@ -17,11 +17,12 @@ #include "mindspore/lite/src/executor.h" #include "src/runtime/kernel/arm/opclib/pack.h" #include "include/errorcode.h" +#include "src/common/ms_tensor_utils.h" namespace mindspore::lite { int Executor::Run(std::vector &inputs, std::vector &outputs, std::vector &kernels, Allocator *allocator, - const kernel::KernelCallBack &before, const kernel::KernelCallBack &after) { + const session::KernelCallBack &before, const session::KernelCallBack &after) { MS_ASSERT(nullptr != allocator); for (auto &inTensor : inputs) { if (inTensor == nullptr) { @@ -41,11 +42,11 @@ int Executor::Run(std::vector &inputs, std::vectorMallocData(); } - kernel::CallBackParam callbackParam; + session::CallBackParam callbackParam; callbackParam.name_callback_aram = kernel->Name(); if (before != nullptr) { - if (!before(kernel->GetInputs(), kernel->GetOutputs(), callbackParam)) { + if (!before(PackToMSTensors(kernel->GetInputs()), PackToMSTensors(kernel->GetOutputs()), callbackParam)) { MS_LOG(ERROR) << "run kernel before_callback failed, name: " << kernel->Name(); } } @@ -56,7 +57,7 @@ int Executor::Run(std::vector &inputs, std::vectorGetInputs(), kernel->GetOutputs(), callbackParam)) { + if (!after(PackToMSTensors(kernel->GetInputs()), PackToMSTensors(kernel->GetOutputs()), callbackParam)) { MS_LOG(ERROR) << "run kernel after_callback failed, name: " << kernel->Name(); } } @@ -120,5 +121,3 @@ int Executor::TransformTensorLayoutUint8(tensor::Tensor *tensor, schema::Format return RET_ERROR; } } // namespace mindspore::lite - - diff --git a/mindspore/lite/src/executor.h b/mindspore/lite/src/executor.h index 7a30ee3411..5ee084fa5f 100644 --- a/mindspore/lite/src/executor.h +++ b/mindspore/lite/src/executor.h @@ -20,6 +20,7 @@ #include #include "src/runtime/allocator.h" #include "src/lite_kernel.h" +#include "include/lite_session.h" namespace mindspore::lite { class Executor { @@ -30,7 +31,7 @@ class Executor { int Run(std::vector &inputs, std::vector &outputs, std::vector &kernels, Allocator *allocator = nullptr, - const kernel::KernelCallBack &before = nullptr, const kernel::KernelCallBack &after = nullptr); + const session::KernelCallBack &before = nullptr, const session::KernelCallBack &after = nullptr); protected: int TransformTensorLayoutFp32(tensor::Tensor *tensor, schema::Format dst_format, Allocator *allocator = nullptr); @@ -45,4 +46,3 @@ class Executor { } // namespace mindspore::lite #endif - diff --git a/mindspore/lite/src/lite_kernel.h b/mindspore/lite/src/lite_kernel.h index b9086ee675..f7f342047b 100644 --- a/mindspore/lite/src/lite_kernel.h +++ b/mindspore/lite/src/lite_kernel.h @@ -51,14 +51,6 @@ struct KernelKey { } }; -class LiteKernel; -struct CallBackParam { - std::string name_callback_aram; -}; - -using KernelCallBack = std::function inputs, - std::vector outputs, const CallBackParam &opInfo)>; - class LiteKernel { public: LiteKernel() = default; diff --git a/mindspore/lite/src/lite_session.cc b/mindspore/lite/src/lite_session.cc index 1197ef97c4..e5e141a13d 100644 --- a/mindspore/lite/src/lite_session.cc +++ b/mindspore/lite/src/lite_session.cc @@ -165,17 +165,15 @@ std::vector LiteSession::GetInputs() const { return ret; } -int LiteSession::RunGraph() { +int LiteSession::RunGraph(const session::KernelCallBack &before, const session::KernelCallBack &after) { MS_EXCEPTION_IF_NULL(this->context_); SetMaxWokerNum(context_->thread_num_); Executor executor; - return executor.Run(this->inputs, this->outputs, this->kernels, this->context_->allocator.get()); -} - -int LiteSession::RunGraph(const kernel::KernelCallBack &before, const kernel::KernelCallBack &after) { - MS_EXCEPTION_IF_NULL(this->context_); - Executor executor; - return executor.Run(this->inputs, this->outputs, this->kernels, this->context_->allocator.get(), before, after); + if (before == nullptr && after == nullptr) { + return executor.Run(this->inputs, this->outputs, this->kernels, this->context_->allocator.get()); + } else { + return executor.Run(this->inputs, this->outputs, this->kernels, this->context_->allocator.get(), before, after); + } } std::vector LiteSession::GetOutputs() const { @@ -262,11 +260,10 @@ session::LiteSession *session::LiteSession::CreateSession(lite::Context *context auto session = new lite::LiteSession(); auto ret = session->Init(context); if (ret != mindspore::lite::RET_OK) { - MS_LOG(ERROR) << "init sesssion failed"; - delete session; - return nullptr; + MS_LOG(ERROR) << "init sesssion failed"; + delete session; + return nullptr; } return session; } } // namespace mindspore - diff --git a/mindspore/lite/src/lite_session.h b/mindspore/lite/src/lite_session.h index 40e3f4499c..4c45809596 100644 --- a/mindspore/lite/src/lite_session.h +++ b/mindspore/lite/src/lite_session.h @@ -46,9 +46,8 @@ class LiteSession : public session::LiteSession { std::vector GetInputsByName(const std::string &name) const override; - int RunGraph() override; - - int RunGraph(const kernel::KernelCallBack &before = nullptr, const kernel::KernelCallBack &after = nullptr); + int RunGraph(const session::KernelCallBack &before = nullptr, + const session::KernelCallBack &after = nullptr) override; std::vector GetOutputs() const override; diff --git a/mindspore/lite/src/runtime/opencl/opencl_executor.cc b/mindspore/lite/src/runtime/opencl/opencl_executor.cc index 36d439d961..d59e4e2f70 100644 --- a/mindspore/lite/src/runtime/opencl/opencl_executor.cc +++ b/mindspore/lite/src/runtime/opencl/opencl_executor.cc @@ -17,11 +17,12 @@ #include "src/runtime/opencl/opencl_executor.h" #include "src/runtime/kernel/arm/opclib/pack.h" #include "include/errorcode.h" +#include "src/common/ms_tensor_utils.h" namespace mindspore::lite::opencl { int OpenCLExecutor::Run(std::vector &inputs, std::vector &outputs, std::vector &kernels, Allocator *allocator, - const kernel::KernelCallBack &before, const kernel::KernelCallBack &after) { + const session::KernelCallBack &before, const session::KernelCallBack &after) { MS_ASSERT(nullptr != allocator); for (auto &inTensor : inputs) { if (inTensor == nullptr) { @@ -46,11 +47,11 @@ int OpenCLExecutor::Run(std::vector &inputs, std::vectorMallocData(); } - kernel::CallBackParam callbackParam; + session::CallBackParam callbackParam; callbackParam.name_callback_aram = kernel->Name(); if (before != nullptr) { - if (!before(kernel->GetInputs(), kernel->GetOutputs(), callbackParam)) { + if (!before(PackToMSTensors(kernel->GetInputs()), PackToMSTensors(kernel->GetOutputs()), callbackParam)) { MS_LOG(ERROR) << "run kernel before_callback failed, name: " << kernel->Name(); } } @@ -61,7 +62,7 @@ int OpenCLExecutor::Run(std::vector &inputs, std::vectorGetInputs(), kernel->GetOutputs(), callbackParam)) { + if (!after(PackToMSTensors(kernel->GetInputs()), PackToMSTensors(kernel->GetOutputs()), callbackParam)) { MS_LOG(ERROR) << "run kernel after_callback failed, name: " << kernel->Name(); } } diff --git a/mindspore/lite/src/runtime/opencl/opencl_executor.h b/mindspore/lite/src/runtime/opencl/opencl_executor.h index 0b0cf4d43a..6c0308ff7b 100644 --- a/mindspore/lite/src/runtime/opencl/opencl_executor.h +++ b/mindspore/lite/src/runtime/opencl/opencl_executor.h @@ -22,6 +22,7 @@ #include "src/runtime/allocator.h" #include "src/lite_kernel.h" #include "src/executor.h" +#include "include/lite_session.h" namespace mindspore::lite::opencl { class OpenCLExecutor : Executor { @@ -34,7 +35,7 @@ class OpenCLExecutor : Executor { int Run(std::vector &inputs, std::vector &outputs, std::vector &kernels, Allocator *allocator = nullptr, - const kernel::KernelCallBack &before = nullptr, const kernel::KernelCallBack &after = nullptr); + const session::KernelCallBack &before = nullptr, const session::KernelCallBack &after = nullptr); protected: int TransformTensorLayoutFp32(tensor::Tensor *tensor, schema::Format dst_format); diff --git a/mindspore/lite/test/CMakeLists.txt b/mindspore/lite/test/CMakeLists.txt index 0bfa4b1dbd..14dd8a5115 100644 --- a/mindspore/lite/test/CMakeLists.txt +++ b/mindspore/lite/test/CMakeLists.txt @@ -154,6 +154,7 @@ set(TEST_LITE_SRC ${LITE_DIR}/src/common/file_utils.cc ${LITE_DIR}/src/common/file_utils_ext.cc ${LITE_DIR}/src/common/utils.cc + ${LITE_DIR}/src/common/ms_tensor_utils.cc ${LITE_DIR}/tools/common/graph_util.cc ${LITE_DIR}/tools/common/tensor_util.cc ${LITE_DIR}/tools/common/node_util.cc diff --git a/mindspore/lite/tools/converter/quantizer/post_training.cc b/mindspore/lite/tools/converter/quantizer/post_training.cc index 3ce66ea1fe..c1dd6f6c7b 100644 --- a/mindspore/lite/tools/converter/quantizer/post_training.cc +++ b/mindspore/lite/tools/converter/quantizer/post_training.cc @@ -745,15 +745,16 @@ STATUS PostTrainingQuantizer::PreProcess() { } STATUS PostTrainingQuantizer::CheckTensorVec(const std::string &nodeName, - const std::vector &tensorVec) const { + const std::vector &tensorVec) const { if (tensorVec.size() < 1) { MS_LOG(ERROR) << "node: " << nodeName << " input tensors is 0"; return RET_ERROR; } - tensor::Tensor *tensor = tensorVec[0]; + auto *tensor = tensorVec[0]; if (tensor->data_type() != kNumberTypeFloat32) { //&& tensor->RefCount() != MSCONST_WEIGHT_REFCOUNT - MS_LOG(DEBUG) << "node: " << nodeName << " will not quantize" << " tensor data_type: " << tensor->data_type(); + MS_LOG(DEBUG) << "node: " << nodeName << " will not quantize" + << " tensor data_type: " << tensor->data_type(); return RET_ERROR; } return RET_OK; @@ -786,28 +787,30 @@ STATUS PostTrainingQuantizer::DoInference() { int opExecResult; }; */ - mindspore::kernel::KernelCallBack beforeCallBack = [&](const std::vector &beforeInputs, - const std::vector &beforeOutputs, - const mindspore::kernel::CallBackParam &callParam) -> bool { + mindspore::session::KernelCallBack beforeCallBack = + [&](const std::vector &beforeInputs, + const std::vector &beforeOutputs, + const mindspore::session::CallBackParam &callParam) -> bool { if (PostTrainingQuantizer::CheckTensorVec(callParam.name_callback_aram, beforeInputs) != RET_OK) { return false; } auto tensor = beforeInputs[0]; - const float *tData = static_cast(tensor->Data()); + const float *tData = static_cast(tensor->MutableData()); size_t shapeSize = tensor->ElementsNum(); vector data(tData, tData + shapeSize); this->calibrator_->RecordMaxValue(callParam.name_callback_aram, data, this->calibrator_->GetInputDivergInfo()); return true; }; // func - mindspore::kernel::KernelCallBack afterCallBack = [&](const std::vector &afterInputs, - const std::vector &afterOutputs, - const mindspore::kernel::CallBackParam &callParam) -> bool { + mindspore::session::KernelCallBack afterCallBack = [&]( + const std::vector &afterInputs, + const std::vector &afterOutputs, + const mindspore::session::CallBackParam &callParam) -> bool { if (PostTrainingQuantizer::CheckTensorVec(callParam.name_callback_aram, afterOutputs) != RET_OK) { return false; } auto tensor = afterOutputs[0]; - const float *tensor_data = static_cast(tensor->Data()); + const float *tensor_data = static_cast(tensor->MutableData()); size_t shape_size = tensor->ElementsNum(); vector data(tensor_data, tensor_data + shape_size); this->calibrator_->RecordMaxValue(callParam.name_callback_aram, data, this->calibrator_->GetOutputDivergInfo()); @@ -837,35 +840,37 @@ STATUS PostTrainingQuantizer::CollectDataFrequency() { return RET_ERROR; } - mindspore::kernel::KernelCallBack beforeCallBack = [&](const std::vector &beforeInputs, - const std::vector &beforeOutputs, - const mindspore::kernel::CallBackParam &callParam) { - if (PostTrainingQuantizer::CheckTensorVec(callParam.name_callback_aram, beforeInputs) != RET_OK) { - return false; - } - auto tensor = beforeInputs[0]; - const float *tensor_data = static_cast(tensor->Data()); - size_t shape_size = tensor->ElementsNum(); - vector data(tensor_data, tensor_data + shape_size); - this->calibrator_->UpdateDataFrequency(callParam.name_callback_aram, data, tensor->shape(), - this->calibrator_->GetInputDivergInfo()); - return true; - }; + mindspore::session::KernelCallBack beforeCallBack = + [&](const std::vector &beforeInputs, + const std::vector &beforeOutputs, + const mindspore::session::CallBackParam &callParam) { + if (PostTrainingQuantizer::CheckTensorVec(callParam.name_callback_aram, beforeInputs) != RET_OK) { + return false; + } + auto tensor = beforeInputs[0]; + const float *tensor_data = static_cast(tensor->MutableData()); + size_t shape_size = tensor->ElementsNum(); + vector data(tensor_data, tensor_data + shape_size); + this->calibrator_->UpdateDataFrequency(callParam.name_callback_aram, data, tensor->shape(), + this->calibrator_->GetInputDivergInfo()); + return true; + }; - mindspore::kernel::KernelCallBack afterCallBack = [&](const std::vector &after_inputs, - const std::vector &after_outputs, - const mindspore::kernel::CallBackParam &call_param) { - if (PostTrainingQuantizer::CheckTensorVec(call_param.name_callback_aram, after_outputs) != RET_OK) { - return false; - } - auto tensor = after_outputs[0]; - const float *tenosr_data = static_cast(tensor->Data()); - size_t shape_size = tensor->ElementsNum(); - vector data(tenosr_data, tenosr_data + shape_size); - this->calibrator_->UpdateDataFrequency(call_param.name_callback_aram, data, tensor->shape(), - this->calibrator_->GetOutputDivergInfo()); - return true; - }; + mindspore::session::KernelCallBack afterCallBack = + [&](const std::vector &after_inputs, + const std::vector &after_outputs, + const mindspore::session::CallBackParam &call_param) { + if (PostTrainingQuantizer::CheckTensorVec(call_param.name_callback_aram, after_outputs) != RET_OK) { + return false; + } + auto tensor = after_outputs[0]; + const float *tenosr_data = static_cast(tensor->MutableData()); + size_t shape_size = tensor->ElementsNum(); + vector data(tenosr_data, tenosr_data + shape_size); + this->calibrator_->UpdateDataFrequency(call_param.name_callback_aram, data, tensor->shape(), + this->calibrator_->GetOutputDivergInfo()); + return true; + }; status = session_->RunGraph(beforeCallBack, afterCallBack); if (status != RET_OK) { MS_LOG(ERROR) << "run model failed!"; diff --git a/mindspore/lite/tools/converter/quantizer/post_training.h b/mindspore/lite/tools/converter/quantizer/post_training.h index 3d83c4c923..13d35da6bc 100644 --- a/mindspore/lite/tools/converter/quantizer/post_training.h +++ b/mindspore/lite/tools/converter/quantizer/post_training.h @@ -27,6 +27,7 @@ #include "tools/converter/quantizer/quantizer.h" #include "src/ir/primitive_t_value.h" #include "tools/converter/converter.h" +#include "include/ms_tensor.h" namespace mindspore { namespace lite { @@ -71,7 +72,7 @@ class PostTrainingQuantizer : public Quantizer { STATUS PreProcess(); - STATUS CheckTensorVec(const std::string &nodeName, const std::vector &tensorVec) const; + STATUS CheckTensorVec(const std::string &nodeName, const std::vector &tensorVec) const; STATUS DoInference();