Repository: opencv/opencv
File: modules/gapi/src/backends/onnx/gonnxbackend.cpp (C++)

Code before:
                     [&](const std::string& name) {
                        const auto it = params.const_inputs.find(name);
                        return it == params.const_inputs.end();
                     });
    }

    // Pre-allocate vectors (not buffers) for runtime info
    in_data.resize(params.num_in);
    out_data.resize(params.num_out);
}

ONNXCompiled::~ONNXCompiled() {
    if(workload_type) {
        workload_type->removeListener(workload_listener_id);
    }
}

void ONNXCompiled::setWorkLoadType(const std::string &type) {
    const char* keys[] = {"ep.dynamic.workload_type"};
    const char* values[] = {type.c_str()};
    this_session.SetEpDynamicOptions(keys, values, 1);
}

void ONNXCompiled::configureWorkloadType(cv::gapi::onnx::WorkloadTypeOnnxPtr workload) {
    workload_type = workload;
    workload_listener_id = workload_type->addListener(std::bind(&ONNXCompiled::setWorkLoadType, this, std::placeholders::_1));
}

std::vector<TensorInfo> ONNXCompiled::getTensorInfo(TensorPosition pos) {
    GAPI_Assert(pos == INPUT || pos == OUTPUT);

    const auto num_nodes = pos == INPUT
        ? this_session.GetInputCount()
        : this_session.GetOutputCount();

    std::vector<TensorInfo> tensor_info;
    tensor_info.reserve(num_nodes);

    Ort::AllocatorWithDefaultOptions allocator;
    for (auto i : ade::util::iota(num_nodes)) {
        const auto info = pos == INPUT
            ? this_session.GetInputTypeInfo(i)
            : this_session.GetOutputTypeInfo(i);
        tensor_info.emplace_back(info.GetTensorTypeAndShapeInfo());

        Ort::AllocatedStringPtr name_p = pos == INPUT
            ? this_session.GetInputNameAllocated(i, allocator)
            : this_session.GetOutputNameAllocated(i, allocator);
        tensor_info.back().name = std::string(name_p.get());
    }


Review comment (suggestion):
So, `m_workload_listener_id` can be here if it will be preserved after all the changes.

Code after:
                        const auto it = params.const_inputs.find(name);
                        return it == params.const_inputs.end();
                     });
    }

    // Pre-allocate vectors (not buffers) for runtime info
    in_data.resize(params.num_in);
    out_data.resize(params.num_out);
}

ONNXCompiled::~ONNXCompiled() {
    if (m_workload_type) {
        m_workload_type->removeListener(m_workload_listener_id);
    }
}

void ONNXCompiled::setWorkloadType(const std::string &type) {
    const char* keys[] = {"ep.dynamic.workload_type"};
    const char* values[] = {type.c_str()};
    this_session.SetEpDynamicOptions(keys, values, 1);
}

void ONNXCompiled::listenToWorkloadType(cv::gapi::onnx::WorkloadTypeONNXPtr workload) {
    m_workload_type = workload;
    m_workload_listener_id = m_workload_type->addListener(std::bind(&ONNXCompiled::setWorkloadType, this, std::placeholders::_1));
}

std::vector<TensorInfo> ONNXCompiled::getTensorInfo(TensorPosition pos) {
    GAPI_Assert(pos == INPUT || pos == OUTPUT);

    const auto num_nodes = pos == INPUT
        ? this_session.GetInputCount()
        : this_session.GetOutputCount();

    std::vector<TensorInfo> tensor_info;
    tensor_info.reserve(num_nodes);

    Ort::AllocatorWithDefaultOptions allocator;
    for (auto i : ade::util::iota(num_nodes)) {
        const auto info = pos == INPUT
            ? this_session.GetInputTypeInfo(i)
            : this_session.GetOutputTypeInfo(i);
        tensor_info.emplace_back(info.GetTensorTypeAndShapeInfo());

        Ort::AllocatedStringPtr name_p = pos == INPUT
            ? this_session.GetInputNameAllocated(i, allocator)
            : this_session.GetOutputNameAllocated(i, allocator);
        tensor_info.back().name = std::string(name_p.get());
    }

    return tensor_info;