* -------------------------------------------------------------------------
* This file is part of the Vision SDK project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* Vision SDK is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
* Description: Infer on the input tensor.
* Author: MindX SDK
* Create: 2020
* History: NA
*/
#ifndef MXPI_TENSOR_INFER_H
#define MXPI_TENSOR_INFER_H
#include <queue>
#include <vector>
#include <thread>
#include <unistd.h>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include "MxBase/ErrorCode/ErrorCode.h"
#include "MxBase/MemoryHelper/MemoryHelper.h"
#include "MxBase/ModelInfer/ModelInferenceProcessor.h"
#include "MxBase/DeviceManager/DeviceManager.h"
#include "MxTools/PluginToolkit/base/MxPluginGenerator.h"
#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h"
#include "MxTools/PluginToolkit/buffer/MxpiBufferManager.h"
#include "MxTools/Proto/MxpiDataType.pb.h"
#include "MxPlugins/MxpiPluginsUtils/MxpiPluginsUtils.h"
class MxpiTensorInfer : public MxTools::MxPluginBase {
public:
* @api
* @param configParamMap.
* @return Error code.
*/
APP_ERROR Init(std::map<std::string, std::shared_ptr<void>>& configParamMap) override;
* @api
* @return Error code.
*/
APP_ERROR DeInit() override;
* @description: MxpiTensorInfer plugin process.
* @param mxpiBuffer: data receive from the previous.
* @return: Error code.
*/
APP_ERROR Process(std::vector<MxTools::MxpiBuffer*>& mxpiBuffer) override;
* @api
* @brief Definition the parameter of configure properties.
* @return std::vector<std::shared_ptr<void>>
*/
static std::vector<std::shared_ptr<void>> DefineProperties();
* @api
* @brief Define the number and data type of input ports.
* @return MxTools::MxpiPortInfo.
*/
static MxTools::MxpiPortInfo DefineInputPorts();
* @api
* @brief Define the number and data type of output ports.
* @return MxTools::MxpiPortInfo.
*/
static MxTools::MxpiPortInfo DefineOutputPorts();
private:
APP_ERROR CheckInputPortsStatus();
APP_ERROR InitConfig(std::map<std::string, std::shared_ptr<void>> &configParamMap);
void LogModelTensorsShape(const MxBase::ModelDesc& modelDesc);
void FreeData();
APP_ERROR TensorMemoryMalloc();
APP_ERROR CheckMetaData(std::vector<MxTools::MxpiBuffer *> &mxpiBuffer);
APP_ERROR DataPreparation(std::vector<MxTools::MxpiBuffer *>& mxpiBuffer);
APP_ERROR ConstructTensorPackageList(std::shared_ptr<MxTools::MxpiTensorPackageList>& tensorPackageList,
std::vector<MxTools::MxpiBuffer *> &mxpiBuffer, size_t dataSourceIndex, std::vector<bool>& isTensorPkgFlags);
APP_ERROR GetTensorPackageLists(std::vector<MxTools::MxpiBuffer *> &mxpiBuffer,
std::vector<std::shared_ptr<MxTools::MxpiTensorPackageList>>& tensorPackageLists,
std::vector<bool>& isTensorPkgFlags);
APP_ERROR AssembleTensorPackageLists(
std::vector<std::shared_ptr<MxTools::MxpiTensorPackageList>>& tensorPackageLists,
std::vector<bool>& isTensorPkgFlags);
APP_ERROR CheckInputTensors(MxTools::MxpiTensorPackage& tensorPackage, std::vector<bool>& isTensorFlags);
APP_ERROR CheckTensorShape(MxTools::MxpiTensorPackage& tensorPackage, std::vector<bool>& isTensorFlags);
APP_ERROR CheckTensorDatasize(MxTools::MxpiTensorPackage& tensorPackage);
APP_ERROR ConstructBuffersInfo(int tensorPackageNumber);
APP_ERROR DataProcess(int rounds, int batchSize);
APP_ERROR DeviceMemCpy(MxTools::MxpiTensorPackage& tensorPackage, int curPosition);
APP_ERROR ModelInfer(int batchsize, int realNumber);
APP_ERROR ConstructOutputTensor(int inferredNumber);
APP_ERROR SendDataToNextPlugin(MxPlugins::BuffersInfo& buffersInfo);
void DestroyExtraBuffers(std::vector<MxTools::MxpiBuffer *>& mxpiBuffer, size_t exceptPort);
void ClearResourcesAndSendErrorInfo(APP_ERROR errorCode);
void SendReadyData();
void CreateThread();
void TimeCall(void);
APP_ERROR TimeoutProcess();
int GetNearestBatchSize();
int GetUpperBatchSize();
int GetLowerBatchSize();
APP_ERROR ConstructTensorPackage(MxPlugins::BuffersInfo &buffersInfo, size_t eachDatasize,
MxBase::MemoryData &memory, int tensorPackageNumber, unsigned int i);
private:
MxBase::ModelInferenceProcessor model_ = {};
MxBase::ModelDesc modelDesc_ = {};
std::queue<MxTools::MxpiTensorPackage> dataQueue_ = {};
std::queue<MxPlugins::BuffersInfo> buffersQueue_ = {};
std::vector<MxBase::BaseTensor> inputTensors_ = {};
std::vector<MxBase::BaseTensor> outputTensors_ = {};
std::string dataSourcesString_ = "";
std::vector<std::string> dataSources_ = {};
std::ostringstream errorInfo_;
int outputDeviceId_ = -1;
bool singleBatchInfer_ = true;
bool outputHasBatchDim_ = true;
int maxBatchSize_ = 1;
int runningFlag_ = true;
uint32_t timeTravel_ = 0;
std::thread thread_;
std::mutex mtx_;
bool notifyFlag_ = false;
std::condition_variable conditionVariable_;
int dynamicStrategy_ = 0;
int skipModelCheck_ = 0;
std::queue<std::pair<MxTools::MxpiBuffer*, bool>> outputQueue_ = {};
std::mutex queueMtx_;
MxBase::DynamicInfo dynamicInfo_;
std::vector<MxTools::ImageSize> dynamicHWs_ = {};
size_t maxArea_ = 1;
double dynamicHWDiscount_ = 1.0;
};
#endif