Merge pull request !3910 from hangq/mastertags/v0.7.0-beta
| @@ -26,6 +26,13 @@ | |||||
| namespace mindspore { | namespace mindspore { | ||||
| namespace session { | namespace session { | ||||
| struct CallBackParam { | |||||
| std::string name_callback_aram; | |||||
| }; | |||||
| using KernelCallBack = std::function<bool(std::vector<tensor::MSTensor *> inputs, | |||||
| std::vector<tensor::MSTensor *> outputs, const CallBackParam &opInfo)>; | |||||
| /// \brief LiteSession defined by MindSpore Lite. | /// \brief LiteSession defined by MindSpore Lite. | ||||
| class MS_API LiteSession { | class MS_API LiteSession { | ||||
| public: | public: | ||||
| @@ -65,12 +72,15 @@ class MS_API LiteSession { | |||||
| /// \return A vector of MindSpore Lite MSTensor. | /// \return A vector of MindSpore Lite MSTensor. | ||||
| virtual std::vector<tensor::MSTensor *> GetInputsByName(const std::string &node_name) const = 0; | virtual std::vector<tensor::MSTensor *> 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. | /// \note RunGraph should called after CompileGraph. | ||||
| /// | /// | ||||
| /// \return ErrorCode of run graph. | /// \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. | /// \brief Get output MindSpore Lite MSTensors of model. | ||||
| /// | /// | ||||
| @@ -87,4 +97,3 @@ class MS_API LiteSession { | |||||
| } // namespace session | } // namespace session | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| #endif // MINDSPORE_LITE_INCLUDE_LITE_SESSION_H | #endif // MINDSPORE_LITE_INCLUDE_LITE_SESSION_H | ||||
| @@ -1,5 +1,6 @@ | |||||
| set(LITE_SRC | set(LITE_SRC | ||||
| ${CMAKE_CURRENT_SOURCE_DIR}/common/graph_util.cc | ${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/allocator.cc | ||||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/runtime_api.cc | ${CMAKE_CURRENT_SOURCE_DIR}/runtime/runtime_api.cc | ||||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/thread_pool.cc | ${CMAKE_CURRENT_SOURCE_DIR}/runtime/thread_pool.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 <vector> | |||||
| #include "utils/log_adapter.h" | |||||
| namespace mindspore { | |||||
| namespace tensor { | |||||
| using mindspore::lite::tensor::LiteTensor; | |||||
| using mindspore::lite::tensor::Tensor; | |||||
| std::vector<MSTensor *> PackToMSTensors(const std::vector<Tensor *> &in_tensors) { | |||||
| std::vector<MSTensor *> 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 | |||||
| @@ -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 <vector> | |||||
| #include "include/ms_tensor.h" | |||||
| #include "src/ir/tensor.h" | |||||
| namespace mindspore { | |||||
| namespace tensor { | |||||
| std::vector<MSTensor *> PackToMSTensors(const std::vector<mindspore::lite::tensor::Tensor *> &in_tensors); | |||||
| } | |||||
| } // namespace mindspore | |||||
| #endif // LITE_MS_TENSOR_UTILS_H | |||||
| @@ -17,11 +17,12 @@ | |||||
| #include "mindspore/lite/src/executor.h" | #include "mindspore/lite/src/executor.h" | ||||
| #include "src/runtime/kernel/arm/opclib/pack.h" | #include "src/runtime/kernel/arm/opclib/pack.h" | ||||
| #include "include/errorcode.h" | #include "include/errorcode.h" | ||||
| #include "src/common/ms_tensor_utils.h" | |||||
| namespace mindspore::lite { | namespace mindspore::lite { | ||||
| int Executor::Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Tensor *> &outputs, | int Executor::Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Tensor *> &outputs, | ||||
| std::vector<kernel::LiteKernel *> &kernels, Allocator *allocator, | std::vector<kernel::LiteKernel *> &kernels, Allocator *allocator, | ||||
| const kernel::KernelCallBack &before, const kernel::KernelCallBack &after) { | |||||
| const session::KernelCallBack &before, const session::KernelCallBack &after) { | |||||
| MS_ASSERT(nullptr != allocator); | MS_ASSERT(nullptr != allocator); | ||||
| for (auto &inTensor : inputs) { | for (auto &inTensor : inputs) { | ||||
| if (inTensor == nullptr) { | if (inTensor == nullptr) { | ||||
| @@ -41,11 +42,11 @@ int Executor::Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Ten | |||||
| MS_ASSERT(nullptr != output); | MS_ASSERT(nullptr != output); | ||||
| output->MallocData(); | output->MallocData(); | ||||
| } | } | ||||
| kernel::CallBackParam callbackParam; | |||||
| session::CallBackParam callbackParam; | |||||
| callbackParam.name_callback_aram = kernel->Name(); | callbackParam.name_callback_aram = kernel->Name(); | ||||
| if (before != nullptr) { | 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(); | MS_LOG(ERROR) << "run kernel before_callback failed, name: " << kernel->Name(); | ||||
| } | } | ||||
| } | } | ||||
| @@ -56,7 +57,7 @@ int Executor::Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Ten | |||||
| } | } | ||||
| if (after != nullptr) { | if (after != nullptr) { | ||||
| if (!after(kernel->GetInputs(), kernel->GetOutputs(), callbackParam)) { | |||||
| if (!after(PackToMSTensors(kernel->GetInputs()), PackToMSTensors(kernel->GetOutputs()), callbackParam)) { | |||||
| MS_LOG(ERROR) << "run kernel after_callback failed, name: " << kernel->Name(); | 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; | return RET_ERROR; | ||||
| } | } | ||||
| } // namespace mindspore::lite | } // namespace mindspore::lite | ||||
| @@ -20,6 +20,7 @@ | |||||
| #include <vector> | #include <vector> | ||||
| #include "src/runtime/allocator.h" | #include "src/runtime/allocator.h" | ||||
| #include "src/lite_kernel.h" | #include "src/lite_kernel.h" | ||||
| #include "include/lite_session.h" | |||||
| namespace mindspore::lite { | namespace mindspore::lite { | ||||
| class Executor { | class Executor { | ||||
| @@ -30,7 +31,7 @@ class Executor { | |||||
| int Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Tensor *> &outputs, | int Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Tensor *> &outputs, | ||||
| std::vector<kernel::LiteKernel *> &kernels, Allocator *allocator = nullptr, | std::vector<kernel::LiteKernel *> &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: | protected: | ||||
| int TransformTensorLayoutFp32(tensor::Tensor *tensor, schema::Format dst_format, Allocator *allocator = nullptr); | int TransformTensorLayoutFp32(tensor::Tensor *tensor, schema::Format dst_format, Allocator *allocator = nullptr); | ||||
| @@ -45,4 +46,3 @@ class Executor { | |||||
| } // namespace mindspore::lite | } // namespace mindspore::lite | ||||
| #endif | #endif | ||||
| @@ -51,14 +51,6 @@ struct KernelKey { | |||||
| } | } | ||||
| }; | }; | ||||
| class LiteKernel; | |||||
| struct CallBackParam { | |||||
| std::string name_callback_aram; | |||||
| }; | |||||
| using KernelCallBack = std::function<bool(std::vector<lite::tensor::Tensor *> inputs, | |||||
| std::vector<lite::tensor::Tensor *> outputs, const CallBackParam &opInfo)>; | |||||
| class LiteKernel { | class LiteKernel { | ||||
| public: | public: | ||||
| LiteKernel() = default; | LiteKernel() = default; | ||||
| @@ -165,17 +165,15 @@ std::vector<mindspore::tensor::MSTensor *> LiteSession::GetInputs() const { | |||||
| return ret; | return ret; | ||||
| } | } | ||||
| int LiteSession::RunGraph() { | |||||
| int LiteSession::RunGraph(const session::KernelCallBack &before, const session::KernelCallBack &after) { | |||||
| MS_EXCEPTION_IF_NULL(this->context_); | MS_EXCEPTION_IF_NULL(this->context_); | ||||
| SetMaxWokerNum(context_->thread_num_); | SetMaxWokerNum(context_->thread_num_); | ||||
| Executor executor; | 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<mindspore::tensor::MSTensor *> LiteSession::GetOutputs() const { | std::vector<mindspore::tensor::MSTensor *> LiteSession::GetOutputs() const { | ||||
| @@ -262,11 +260,10 @@ session::LiteSession *session::LiteSession::CreateSession(lite::Context *context | |||||
| auto session = new lite::LiteSession(); | auto session = new lite::LiteSession(); | ||||
| auto ret = session->Init(context); | auto ret = session->Init(context); | ||||
| if (ret != mindspore::lite::RET_OK) { | 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; | return session; | ||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -46,9 +46,8 @@ class LiteSession : public session::LiteSession { | |||||
| std::vector<mindspore::tensor::MSTensor *> GetInputsByName(const std::string &name) const override; | std::vector<mindspore::tensor::MSTensor *> 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<mindspore::tensor::MSTensor *> GetOutputs() const override; | std::vector<mindspore::tensor::MSTensor *> GetOutputs() const override; | ||||
| @@ -17,11 +17,12 @@ | |||||
| #include "src/runtime/opencl/opencl_executor.h" | #include "src/runtime/opencl/opencl_executor.h" | ||||
| #include "src/runtime/kernel/arm/opclib/pack.h" | #include "src/runtime/kernel/arm/opclib/pack.h" | ||||
| #include "include/errorcode.h" | #include "include/errorcode.h" | ||||
| #include "src/common/ms_tensor_utils.h" | |||||
| namespace mindspore::lite::opencl { | namespace mindspore::lite::opencl { | ||||
| int OpenCLExecutor::Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Tensor *> &outputs, | int OpenCLExecutor::Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Tensor *> &outputs, | ||||
| std::vector<kernel::LiteKernel *> &kernels, Allocator *allocator, | std::vector<kernel::LiteKernel *> &kernels, Allocator *allocator, | ||||
| const kernel::KernelCallBack &before, const kernel::KernelCallBack &after) { | |||||
| const session::KernelCallBack &before, const session::KernelCallBack &after) { | |||||
| MS_ASSERT(nullptr != allocator); | MS_ASSERT(nullptr != allocator); | ||||
| for (auto &inTensor : inputs) { | for (auto &inTensor : inputs) { | ||||
| if (inTensor == nullptr) { | if (inTensor == nullptr) { | ||||
| @@ -46,11 +47,11 @@ int OpenCLExecutor::Run(std::vector<tensor::Tensor *> &inputs, std::vector<tenso | |||||
| MS_ASSERT(nullptr != output); | MS_ASSERT(nullptr != output); | ||||
| output->MallocData(); | output->MallocData(); | ||||
| } | } | ||||
| kernel::CallBackParam callbackParam; | |||||
| session::CallBackParam callbackParam; | |||||
| callbackParam.name_callback_aram = kernel->Name(); | callbackParam.name_callback_aram = kernel->Name(); | ||||
| if (before != nullptr) { | 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(); | MS_LOG(ERROR) << "run kernel before_callback failed, name: " << kernel->Name(); | ||||
| } | } | ||||
| } | } | ||||
| @@ -61,7 +62,7 @@ int OpenCLExecutor::Run(std::vector<tensor::Tensor *> &inputs, std::vector<tenso | |||||
| } | } | ||||
| if (after != nullptr) { | if (after != nullptr) { | ||||
| if (!after(kernel->GetInputs(), kernel->GetOutputs(), callbackParam)) { | |||||
| if (!after(PackToMSTensors(kernel->GetInputs()), PackToMSTensors(kernel->GetOutputs()), callbackParam)) { | |||||
| MS_LOG(ERROR) << "run kernel after_callback failed, name: " << kernel->Name(); | MS_LOG(ERROR) << "run kernel after_callback failed, name: " << kernel->Name(); | ||||
| } | } | ||||
| } | } | ||||
| @@ -22,6 +22,7 @@ | |||||
| #include "src/runtime/allocator.h" | #include "src/runtime/allocator.h" | ||||
| #include "src/lite_kernel.h" | #include "src/lite_kernel.h" | ||||
| #include "src/executor.h" | #include "src/executor.h" | ||||
| #include "include/lite_session.h" | |||||
| namespace mindspore::lite::opencl { | namespace mindspore::lite::opencl { | ||||
| class OpenCLExecutor : Executor { | class OpenCLExecutor : Executor { | ||||
| @@ -34,7 +35,7 @@ class OpenCLExecutor : Executor { | |||||
| int Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Tensor *> &outputs, | int Run(std::vector<tensor::Tensor *> &inputs, std::vector<tensor::Tensor *> &outputs, | ||||
| std::vector<kernel::LiteKernel *> &kernels, Allocator *allocator = nullptr, | std::vector<kernel::LiteKernel *> &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: | protected: | ||||
| int TransformTensorLayoutFp32(tensor::Tensor *tensor, schema::Format dst_format); | int TransformTensorLayoutFp32(tensor::Tensor *tensor, schema::Format dst_format); | ||||
| @@ -154,6 +154,7 @@ set(TEST_LITE_SRC | |||||
| ${LITE_DIR}/src/common/file_utils.cc | ${LITE_DIR}/src/common/file_utils.cc | ||||
| ${LITE_DIR}/src/common/file_utils_ext.cc | ${LITE_DIR}/src/common/file_utils_ext.cc | ||||
| ${LITE_DIR}/src/common/utils.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/graph_util.cc | ||||
| ${LITE_DIR}/tools/common/tensor_util.cc | ${LITE_DIR}/tools/common/tensor_util.cc | ||||
| ${LITE_DIR}/tools/common/node_util.cc | ${LITE_DIR}/tools/common/node_util.cc | ||||
| @@ -745,15 +745,16 @@ STATUS PostTrainingQuantizer::PreProcess() { | |||||
| } | } | ||||
| STATUS PostTrainingQuantizer::CheckTensorVec(const std::string &nodeName, | STATUS PostTrainingQuantizer::CheckTensorVec(const std::string &nodeName, | ||||
| const std::vector<lite::tensor::Tensor *> &tensorVec) const { | |||||
| const std::vector<mindspore::tensor::MSTensor *> &tensorVec) const { | |||||
| if (tensorVec.size() < 1) { | if (tensorVec.size() < 1) { | ||||
| MS_LOG(ERROR) << "node: " << nodeName << " input tensors is 0"; | MS_LOG(ERROR) << "node: " << nodeName << " input tensors is 0"; | ||||
| return RET_ERROR; | return RET_ERROR; | ||||
| } | } | ||||
| tensor::Tensor *tensor = tensorVec[0]; | |||||
| auto *tensor = tensorVec[0]; | |||||
| if (tensor->data_type() != kNumberTypeFloat32) { | if (tensor->data_type() != kNumberTypeFloat32) { | ||||
| //&& tensor->RefCount() != MSCONST_WEIGHT_REFCOUNT | //&& 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_ERROR; | ||||
| } | } | ||||
| return RET_OK; | return RET_OK; | ||||
| @@ -786,28 +787,30 @@ STATUS PostTrainingQuantizer::DoInference() { | |||||
| int opExecResult; | int opExecResult; | ||||
| }; | }; | ||||
| */ | */ | ||||
| mindspore::kernel::KernelCallBack beforeCallBack = [&](const std::vector<lite::tensor::Tensor *> &beforeInputs, | |||||
| const std::vector<lite::tensor::Tensor *> &beforeOutputs, | |||||
| const mindspore::kernel::CallBackParam &callParam) -> bool { | |||||
| mindspore::session::KernelCallBack beforeCallBack = | |||||
| [&](const std::vector<mindspore::tensor::MSTensor *> &beforeInputs, | |||||
| const std::vector<mindspore::tensor::MSTensor *> &beforeOutputs, | |||||
| const mindspore::session::CallBackParam &callParam) -> bool { | |||||
| if (PostTrainingQuantizer::CheckTensorVec(callParam.name_callback_aram, beforeInputs) != RET_OK) { | if (PostTrainingQuantizer::CheckTensorVec(callParam.name_callback_aram, beforeInputs) != RET_OK) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| auto tensor = beforeInputs[0]; | auto tensor = beforeInputs[0]; | ||||
| const float *tData = static_cast<const float *>(tensor->Data()); | |||||
| const float *tData = static_cast<const float *>(tensor->MutableData()); | |||||
| size_t shapeSize = tensor->ElementsNum(); | size_t shapeSize = tensor->ElementsNum(); | ||||
| vector<float> data(tData, tData + shapeSize); | vector<float> data(tData, tData + shapeSize); | ||||
| this->calibrator_->RecordMaxValue(callParam.name_callback_aram, data, this->calibrator_->GetInputDivergInfo()); | this->calibrator_->RecordMaxValue(callParam.name_callback_aram, data, this->calibrator_->GetInputDivergInfo()); | ||||
| return true; | return true; | ||||
| }; | }; | ||||
| // func | // func | ||||
| mindspore::kernel::KernelCallBack afterCallBack = [&](const std::vector<lite::tensor::Tensor *> &afterInputs, | |||||
| const std::vector<lite::tensor::Tensor *> &afterOutputs, | |||||
| const mindspore::kernel::CallBackParam &callParam) -> bool { | |||||
| mindspore::session::KernelCallBack afterCallBack = [&]( | |||||
| const std::vector<mindspore::tensor::MSTensor *> &afterInputs, | |||||
| const std::vector<mindspore::tensor::MSTensor *> &afterOutputs, | |||||
| const mindspore::session::CallBackParam &callParam) -> bool { | |||||
| if (PostTrainingQuantizer::CheckTensorVec(callParam.name_callback_aram, afterOutputs) != RET_OK) { | if (PostTrainingQuantizer::CheckTensorVec(callParam.name_callback_aram, afterOutputs) != RET_OK) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| auto tensor = afterOutputs[0]; | auto tensor = afterOutputs[0]; | ||||
| const float *tensor_data = static_cast<const float *>(tensor->Data()); | |||||
| const float *tensor_data = static_cast<const float *>(tensor->MutableData()); | |||||
| size_t shape_size = tensor->ElementsNum(); | size_t shape_size = tensor->ElementsNum(); | ||||
| vector<float> data(tensor_data, tensor_data + shape_size); | vector<float> data(tensor_data, tensor_data + shape_size); | ||||
| this->calibrator_->RecordMaxValue(callParam.name_callback_aram, data, this->calibrator_->GetOutputDivergInfo()); | this->calibrator_->RecordMaxValue(callParam.name_callback_aram, data, this->calibrator_->GetOutputDivergInfo()); | ||||
| @@ -837,35 +840,37 @@ STATUS PostTrainingQuantizer::CollectDataFrequency() { | |||||
| return RET_ERROR; | return RET_ERROR; | ||||
| } | } | ||||
| mindspore::kernel::KernelCallBack beforeCallBack = [&](const std::vector<lite::tensor::Tensor *> &beforeInputs, | |||||
| const std::vector<lite::tensor::Tensor *> &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<const float *>(tensor->Data()); | |||||
| size_t shape_size = tensor->ElementsNum(); | |||||
| vector<float> 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<mindspore::tensor::MSTensor *> &beforeInputs, | |||||
| const std::vector<mindspore::tensor::MSTensor *> &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<const float *>(tensor->MutableData()); | |||||
| size_t shape_size = tensor->ElementsNum(); | |||||
| vector<float> 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<lite::tensor::Tensor *> &after_inputs, | |||||
| const std::vector<lite::tensor::Tensor *> &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<const float *>(tensor->Data()); | |||||
| size_t shape_size = tensor->ElementsNum(); | |||||
| vector<float> 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<mindspore::tensor::MSTensor *> &after_inputs, | |||||
| const std::vector<mindspore::tensor::MSTensor *> &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<const float *>(tensor->MutableData()); | |||||
| size_t shape_size = tensor->ElementsNum(); | |||||
| vector<float> 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); | status = session_->RunGraph(beforeCallBack, afterCallBack); | ||||
| if (status != RET_OK) { | if (status != RET_OK) { | ||||
| MS_LOG(ERROR) << "run model failed!"; | MS_LOG(ERROR) << "run model failed!"; | ||||
| @@ -27,6 +27,7 @@ | |||||
| #include "tools/converter/quantizer/quantizer.h" | #include "tools/converter/quantizer/quantizer.h" | ||||
| #include "src/ir/primitive_t_value.h" | #include "src/ir/primitive_t_value.h" | ||||
| #include "tools/converter/converter.h" | #include "tools/converter/converter.h" | ||||
| #include "include/ms_tensor.h" | |||||
| namespace mindspore { | namespace mindspore { | ||||
| namespace lite { | namespace lite { | ||||
| @@ -71,7 +72,7 @@ class PostTrainingQuantizer : public Quantizer { | |||||
| STATUS PreProcess(); | STATUS PreProcess(); | ||||
| STATUS CheckTensorVec(const std::string &nodeName, const std::vector<tensor::Tensor *> &tensorVec) const; | |||||
| STATUS CheckTensorVec(const std::string &nodeName, const std::vector<mindspore::tensor::MSTensor *> &tensorVec) const; | |||||
| STATUS DoInference(); | STATUS DoInference(); | ||||