BackendFactoryFunction get_backend_for_type()

in source/neuropod/internal/backend_registration.cc [284:341]


BackendFactoryFunction get_backend_for_type(const std::vector<BackendLoadSpec> &default_backend_overrides,
                                            const std::string &                 type,
                                            const std::string &                 target_version_range)
{
    init_registrar_if_needed();

    {
        // Attempt to find a registered backend that matches
        auto retval = find_registered_backend(type, target_version_range);
        if (retval != nullptr)
        {
            return retval;
        }
    }

    // Check to see if we've already loaded a backend for `type`
    // We can only load one backend version per type into the same process
    // (i.e. if we load TF 1.12.0, we can't load another version of TF into this process)
    // Using OPE overcomes this problem
    auto it = registered_backends_by_type->find(type);
    if (it != registered_backends_by_type->end())
    {
        NEUROPOD_ERROR(
            "Tried to get a backend for type '{}' and version range '{}' but failed. A backend for type "
            "'{}' was already registered, but it supports version '{}'. If you are trying "
            "to use multiple versions of the same framework, you must use OPE. See the docs at https://neuropod.ai",
            type,
            target_version_range,
            type,
            it->second.version);
    }

    // Try loading using the user provided overrides
    bool load_success = load_default_backend(default_backend_overrides, type, target_version_range);
    if (!load_success)
    {
        // If that didn't work, try loading using the default map
        load_success = load_default_backend(default_backend_for_type, type, target_version_range);
    }

    if (load_success)
    {
        // We loaded a library so we can check the registered backends again
        auto retval = find_registered_backend(type, target_version_range);
        if (retval != nullptr)
        {
            return retval;
        }
    }

    // Don't have anything that matches
    NEUROPOD_ERROR(
        "The model being loaded requires a Neuropod backend for type '{}' and version range '{}'. However, "
        "a backend satisfying these requirements was not found. See the installation instructions "
        "at https://neuropod.ai/installing to install a backend. Retry with log level TRACE for more information.",
        type,
        target_version_range);
}