Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions src/models/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ class Model {
private:
/* data */
public:
Model(/* args */) {};
virtual ~Model() {};
Model(/* args */){};
virtual ~Model(){};

const char *name;

Expand Down Expand Up @@ -259,11 +259,12 @@ class Model {
const std::vector<Ort::AllocatedStringPtr> &inputNames,
const std::vector<Ort::AllocatedStringPtr> &outputNames,
const std::vector<Ort::Value> &inputTensor,
std::vector<Ort::Value> &outputTensor)
std::vector<Ort::Value> &outputTensor,
const std::vector<std::vector<int64_t>> &outputDims,
std::vector<std::vector<float>> &outputTensorValues, const std::string &useGPU)
{
if (inputNames.size() == 0 || outputNames.size() == 0 || inputTensor.size() == 0 ||
outputTensor.size() == 0) {
obs_log(LOG_INFO, "Skip network inference. Inputs or outputs are null.");
if (inputNames.size() == 0 || outputNames.size() == 0 || inputTensor.size() == 0) {
obs_log(LOG_INFO, "Skip network inference. Inputs are null.");
return;
}

Expand All @@ -277,6 +278,40 @@ class Model {
rawOutputNames.push_back(outputName.get());
}

// For GPU execution providers, we need to recreate output tensors for each inference
// to avoid tensor reuse issues that cause processing to stop after one frame
// Explicitly check for known GPU providers: cuda, rocm, migraphx, tensorrt, coreml
// Note: DirectML is not currently supported in this plugin despite documentation mentions
bool requiresTensorRecreation = (useGPU == "cuda" || useGPU == "rocm" || useGPU == "migraphx" ||
useGPU == "tensorrt" || useGPU == "coreml");

if (requiresTensorRecreation) {
// Clear and recreate output tensors for GPU inference
outputTensor.clear();

Ort::MemoryInfo cpuMemoryInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtDeviceAllocator,
OrtMemType::OrtMemTypeDefault);

for (size_t i = 0; i < outputDims.size(); i++) {
outputTensor.push_back(Ort::Value::CreateTensor<float>(
cpuMemoryInfo, outputTensorValues[i].data(), outputTensorValues[i].size(),
outputDims[i].data(), outputDims[i].size()));
}
} else {
// For CPU mode, output tensors should have been pre-allocated during initialization
if (outputTensor.size() == 0) {
obs_log(LOG_ERROR,
"Output tensor is empty for CPU mode. Tensor allocation may have failed.");
return;
}
}

// Final defensive check that outputTensor is valid before calling inference
if (outputTensor.size() == 0) {
obs_log(LOG_ERROR, "Output tensor is empty. Cannot proceed with inference.");
return;
}

session->Run(Ort::RunOptions{nullptr}, rawInputNames.data(), inputTensor.data(), inputNames.size(),
rawOutputNames.data(), outputTensor.data(), outputNames.size());
}
Expand Down
3 changes: 2 additions & 1 deletion src/ort-utils/ort-session-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ bool runFilterModelInference(filter_data *tf, const cv::Mat &imageBGRA, cv::Mat
tf->model->loadInputToTensor(preprocessedImage, inputWidth, inputHeight, tf->inputTensorValues);

// Run network inference
tf->model->runNetworkInference(tf->session, tf->inputNames, tf->outputNames, tf->inputTensor, tf->outputTensor);
tf->model->runNetworkInference(tf->session, tf->inputNames, tf->outputNames, tf->inputTensor, tf->outputTensor,
tf->outputDims, tf->outputTensorValues, tf->useGPU);

// Get output
// Map network output to cv::Mat
Expand Down
Loading