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

Code before:
                     std::back_inserter(in_names_without_const),
                     [&](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.has_value())
    {
        workload_type.value().get().removeListener(workloadId);
    }
}

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::setWorkLoadRef(cv::gapi::wip::ov::WorkloadTypeRef workload) {
    workload_type = cv::util::make_optional(workload);
    workloadId = workload_type.value().get().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):
I think this method should be named another way, It adds listeners, configures `workload_type` variable in `ONNXCompiled` class, but it actually doesn't set `workload_type` of ONNX to some value.

Code after:
                     [&](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 (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());
    }