Files
TheRock/rocm-libraries/projects/hipdnn/frontend/include/hipdnn_frontend/detail/BackendWrapper.hpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

123 lines
4.1 KiB
C++

// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#pragma once
#include <optional>
#include <hipdnn_data_sdk/utilities/VersionUtils.hpp>
#include <hipdnn_frontend/Utilities.hpp>
#include <hipdnn_frontend/detail/HipdnnBackendInterface.hpp>
#include <hipdnn_frontend/detail/IncompatibleBackend.hpp>
#include <hipdnn_frontend/version.h>
#ifndef HIPDNN_FRONTEND_RUNTIME_LOAD_BACKEND
#include <hipdnn_frontend/detail/HipdnnDirectBackendWrapper.hpp>
#else
#include <atomic>
#include <hipdnn_frontend/detail/DynamicBackendLibrary.hpp>
#include <hipdnn_frontend/detail/HipdnnDynamicBackendWrapper.hpp>
#endif
namespace hipdnn_frontend::detail
{
// Parses the backend version string and checks it against the frontend's major
// version. Returns the parsed version on success, or std::nullopt (after
// logging) if the version is missing, malformed, or incompatible. Shared by the
// direct-link and runtime-load factories.
inline std::optional<hipdnn_data_sdk::utilities::Version> checkBackendVersion(const char* version)
{
using namespace hipdnn_data_sdk::utilities;
if(version == nullptr)
{
HIPDNN_FE_LOG_ERROR("Error parsing backend version: version is nullptr");
return std::nullopt;
}
Version backendVersion;
try
{
backendVersion = Version{std::string{version}};
}
catch(const std::invalid_argument& error)
{
HIPDNN_FE_LOG_ERROR("Error parsing backend version: " + std::string{error.what()});
return std::nullopt;
}
if(HIPDNN_FRONTEND_VERSION_MAJOR != backendVersion.major)
{
HIPDNN_FE_LOG_ERROR("Backend major version (" + std::to_string(backendVersion.major)
+ ") does not match frontend major version ("
+ std::to_string(HIPDNN_FRONTEND_VERSION_MAJOR) + ")");
return std::nullopt;
}
return backendVersion;
}
#ifndef HIPDNN_FRONTEND_RUNTIME_LOAD_BACKEND
// Attempts to create a direct-link backend interface, falling back to
// IncompatibleBackend if it fails to satisfy requirements. version is taken as
// an argument to facilitate easier testing.
inline std::shared_ptr<IHipdnnBackend> tryToUseBackendInterface(const char* version)
{
auto backendVersion = checkBackendVersion(version);
if(!backendVersion)
{
return std::make_shared<IncompatibleBackendWrapper>();
}
return std::make_shared<HipdnnDirectBackendWrapper>(*backendVersion);
}
#else
// Attempts to create a runtime-load backend interface. Loads the backend
// library, resolves the version string via dlsym, validates it, and falls back
// to IncompatibleBackend on any failure.
inline std::shared_ptr<IHipdnnBackend> tryToUseDynamicBackendInterface()
{
if(backendLibraryHandle() == nullptr)
{
HIPDNN_FE_LOG_ERROR("Failed to load hipDNN backend library for runtime backend loading");
return std::make_shared<IncompatibleBackendWrapper>();
}
// Resolve the version string without an instance so the version can be
// validated before the wrapper is constructed. decltype is unevaluated and
// does not create a link-time reference to the symbol.
static std::atomic<void*> s_versionCache{nullptr};
auto versionFn = resolveBackendSymbol<decltype(&hipdnnVersionString_ext)>(
s_versionCache, "hipdnnVersionString_ext");
const char* version = versionFn != nullptr ? versionFn() : nullptr;
auto backendVersion = checkBackendVersion(version);
if(!backendVersion)
{
return std::make_shared<IncompatibleBackendWrapper>();
}
return std::make_shared<HipdnnDynamicBackendWrapper>(*backendVersion);
}
#endif // HIPDNN_FRONTEND_RUNTIME_LOAD_BACKEND
// Allow overriding the backend implementation by setting a custom backend instance.
inline static std::shared_ptr<IHipdnnBackend> hipdnnBackend()
{
return IHipdnnBackend::getOrCreateInstance([] {
#ifdef HIPDNN_FRONTEND_RUNTIME_LOAD_BACKEND
return tryToUseDynamicBackendInterface();
#else
return tryToUseBackendInterface(hipdnnVersionString_ext());
#endif
});
}
} // namespace hipdnn_frontend::detail