diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-07-02 19:44:08 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-07-02 19:44:08 +0200 |
| commit | 6a10d3577232f1fd646a20f22a4a1ab4ec29f28a (patch) | |
| tree | e4a077b064876ddb39144935fa256a4ab0692488 /deps/cudnn-frontend/include/cudnn_frontend/backend | |
| parent | 98a713e86c23bc6098bf7a5558507a9f27a7a1c1 (diff) | |
| download | ffwd-6a10d3577232f1fd646a20f22a4a1ab4ec29f28a.tar.gz ffwd-6a10d3577232f1fd646a20f22a4a1ab4ec29f28a.zip | |
cuda: add cuDNN fused attention path with runtime fallback
Diffstat (limited to 'deps/cudnn-frontend/include/cudnn_frontend/backend')
5 files changed, 808 insertions, 0 deletions
diff --git a/deps/cudnn-frontend/include/cudnn_frontend/backend/backend_descriptor.h b/deps/cudnn-frontend/include/cudnn_frontend/backend/backend_descriptor.h new file mode 100644 index 0000000..2cd68c0 --- /dev/null +++ b/deps/cudnn-frontend/include/cudnn_frontend/backend/backend_descriptor.h @@ -0,0 +1,138 @@ +#pragma once + +#include <memory> + +#include "../graph_helpers.h" +#include "cudnn.h" + +namespace cudnn_frontend::detail { + +/** + * @brief RAII wrapper around a `cudnnBackendDescriptor_t` object. + * + * This class provides a convenient way to manage the lifetime of a `cudnnBackendDescriptor_t` + * object using the RAII (Resource Acquisition Is Initialization) idiom. It automatically + * creates the descriptor when the object is constructed and destroys it when the object + * is destroyed, ensuring proper resource management and preventing memory leaks. + * + * @note The constructor of this class does not throw exceptions. Instead, it stores the + * status of the descriptor creation operation in the `status` member variable. Callers + * should check this status and handle any errors accordingly. + */ +class backend_descriptor { + public: + /** + * @brief Constructs a `backend_descriptor` object. + * + * @param type The type of the backend descriptor to create. + */ + backend_descriptor(cudnnBackendDescriptorType_t type) { status = detail::create_descriptor(type, &desc); } + + /** + * @brief Move constructor. + * + * Transfers the ownership of the `cudnnBackendDescriptor_t` object to the new + * `backend_descriptor` instance. + * + * @param other The source `backend_descriptor` object. + */ + backend_descriptor(backend_descriptor&& other) noexcept : desc(other.desc), status(other.status) { + other.desc = nullptr; + other.status = CUDNN_STATUS_NOT_INITIALIZED; + } + + /** + * @brief Move assignment operator. + * + * Transfers the ownership of the `cudnnBackendDescriptor_t` object to the new + * `backend_descriptor` instance. + * + * @param other The source `backend_descriptor` object. + * @return A reference to the current `backend_descriptor` object. + */ + backend_descriptor& + operator=(backend_descriptor&& other) noexcept { + if (this != &other) { + desc = other.desc; + status = other.status; + + other.desc = nullptr; + } + return *this; + } + + /** + * @brief Destructor. + * + * Destroys the `cudnnBackendDescriptor_t` object and frees the associated resources. + */ + ~backend_descriptor() { + if (desc) { + detail::destroy_descriptor(desc); + } + } + + /** + * @brief Deleted copy constructor and assignment operator. + * + * `backend_descriptor` objects are not copyable to prevent unintended resource + * sharing and potential memory leaks. + */ + backend_descriptor(backend_descriptor const&) = delete; + backend_descriptor& + operator=(backend_descriptor const&) = delete; + + /** + * @brief Initializes a `backend_descriptor` object. + * + * @param type The type of the backend descriptor to create. + */ + error_t + initialize(cudnnBackendDescriptorType_t type) { + _CUDNN_CHECK_CUDNN_ERROR(detail::create_descriptor(type, &desc)); + return {error_code_t::OK, ""}; + } + + /** + * @brief Finalizes a `backend_descriptor` object. + * + */ + error_t + finalize() { + _CUDNN_CHECK_CUDNN_ERROR(detail::finalize(desc)); + return {error_code_t::OK, ""}; + } + + /** + * @brief Accessor for the underlying `cudnnBackendDescriptor_t` object. + * + * @return A const reference to `cudnnBackendDescriptor_t`, the raw pointer to the backend descriptor. + */ + cudnnBackendDescriptor_t const& + get_ptr() const { + return desc; + } + + /** + * @brief Accessor for the status of the backend descriptor creation. + * + * @return `cudnnStatus_t` The status of the backend descriptor creation operation. + */ + cudnnStatus_t + get_status() const { + return status; + } + + /** + * @brief Constructs a default `backend_descriptor` object, but without initializing descriptor + * + * Used to return an error code to user for incorrect cuDNN version + */ + backend_descriptor() = default; + + private: + cudnnBackendDescriptor_t desc = nullptr; //!< Raw pointer to the backend descriptor. + cudnnStatus_t status = CUDNN_STATUS_NOT_INITIALIZED; //!< Status of the descriptor creation operation. +}; + +} // namespace cudnn_frontend::detail
\ No newline at end of file diff --git a/deps/cudnn-frontend/include/cudnn_frontend/backend/device_properties.h b/deps/cudnn-frontend/include/cudnn_frontend/backend/device_properties.h new file mode 100644 index 0000000..d31c555 --- /dev/null +++ b/deps/cudnn-frontend/include/cudnn_frontend/backend/device_properties.h @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#pragma once + +#include <algorithm> +#include <array> +#include <functional> +#include <memory> +#include <sstream> +#include <utility> + +#include "../graph_helpers.h" +#include "backend_descriptor.h" + +namespace cudnn_frontend { +/// +/// DeviceProperties Class +/// Wraps the device_properties backend descriptor +/// Wraps backend utility functions for user's convenience +/// Backend accessor functions: size() +/// Contains internal utilities for device properties finalization and operation graph attributes +/// +class DeviceProperties : public detail::backend_descriptor { + public: + // Uses the default backend constructor so that we can check for initialization error during build() + DeviceProperties() = default; + + std::string + describe() const { + std::stringstream ss; + ss << "CUDNN_BACKEND_DEVICEPROP_DESCRIPTOR : " << std::endl; + return ss.str(); + } + + inline DeviceProperties& + set_device_id(int32_t device_id) { + this->device_id = device_id; + return *this; + } + + inline DeviceProperties& + set_handle(cudnnHandle_t handle) { + this->handle = handle; + return *this; + } + + // Used to check device properties status (particularly after initialization) + error_t + status() const { + if (get_status() != CUDNN_STATUS_SUCCESS) { + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_BACKEND_DEVICEPROP_DESCRIPTOR: Check CUDNN_VERSION >= 9.8"}; + } + return {}; + } + + error_t + serialize(std::vector<uint8_t>& serialization_buf) const { +#if (CUDNN_VERSION >= 90800) + RETURN_CUDNN_FRONTEND_ERROR_IF(detail::get_backend_version() < 90800, + error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_DEVICEPROP_JSON_REPRESENTATION is only available starting 9.8."); + + int64_t serializationSize; + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute( + get_ptr(), CUDNN_ATTR_DEVICEPROP_JSON_REPRESENTATION, CUDNN_TYPE_CHAR, 0, &serializationSize, nullptr)); + serialization_buf.resize(static_cast<size_t>(serializationSize)); + + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute(get_ptr(), + CUDNN_ATTR_DEVICEPROP_JSON_REPRESENTATION, + CUDNN_TYPE_CHAR, + serializationSize, + &serializationSize, + serialization_buf.data())); + return {}; +#else + (void)serialization_buf; + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_DEVICEPROP_JSON_REPRESENTATION is only available starting 9.8."}; +#endif + } + + error_t + deserialize(const std::vector<uint8_t>& serialized_buf) { +#if (CUDNN_VERSION >= 90800) + RETURN_CUDNN_FRONTEND_ERROR_IF(detail::get_backend_version() < 90800, + error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_DEVICEPROP_JSON_REPRESENTATION is only available starting 9.8."); + + // Check if the device properties is already initialized + RETURN_CUDNN_FRONTEND_ERROR_IF( + get_ptr() != nullptr, error_code_t::CUDNN_BACKEND_API_FAILED, "Device properties is already initialized."); + + // Initialize the device properties descriptor + CHECK_CUDNN_FRONTEND_ERROR(initialize(CUDNN_BACKEND_DEVICEPROP_DESCRIPTOR)); + + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute(get_ptr(), + CUDNN_ATTR_DEVICEPROP_JSON_REPRESENTATION, + CUDNN_TYPE_CHAR, + serialized_buf.size(), + serialized_buf.data())); + + CHECK_CUDNN_FRONTEND_ERROR(finalize()); + return {}; +#else + (void)serialized_buf; + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_DEVICEPROP_JSON_REPRESENTATION is only available starting 9.8."}; +#endif + } + + // Check for both compile-time and runtime cuDNN version + error_t + build() { +#if (CUDNN_VERSION >= 90800) + RETURN_CUDNN_FRONTEND_ERROR_IF(detail::get_backend_version() < 90800, + error_code_t::GRAPH_NOT_SUPPORTED, + "CUDNN_BACKEND_DEVICEPROP_DESCRIPTOR is only available starting 9.8."); + if (get_ptr() == nullptr) { + CHECK_CUDNN_FRONTEND_ERROR(initialize(CUDNN_BACKEND_DEVICEPROP_DESCRIPTOR)); + } + + if (handle != nullptr) { + _CUDNN_CHECK_CUDNN_ERROR( + detail::set_attribute(get_ptr(), CUDNN_ATTR_DEVICEPROP_HANDLE, CUDNN_TYPE_HANDLE, 1, &handle)); + } + + if (device_id >= 0) { + _CUDNN_CHECK_CUDNN_ERROR( + detail::set_attribute(get_ptr(), CUDNN_ATTR_DEVICEPROP_DEVICE_ID, CUDNN_TYPE_INT32, 1, &device_id)); + } + + CHECK_CUDNN_FRONTEND_ERROR(finalize()); + return {}; +#else + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_BACKEND_DEVICEPROP_DESCRIPTOR is only available starting 9.8."}; +#endif + } + + private: + cudnnHandle_t handle = nullptr; + int32_t device_id = 0; +}; +} // namespace cudnn_frontend
\ No newline at end of file diff --git a/deps/cudnn-frontend/include/cudnn_frontend/backend/execution_helpers.h b/deps/cudnn-frontend/include/cudnn_frontend/backend/execution_helpers.h new file mode 100644 index 0000000..7138d48 --- /dev/null +++ b/deps/cudnn-frontend/include/cudnn_frontend/backend/execution_helpers.h @@ -0,0 +1,125 @@ +#pragma once + +#include <vector> + +#include "cudnn.h" + +#include "backend_descriptor.h" + +namespace cudnn_frontend::detail { +/** + * @brief Creates a CUDNN backend variant pack descriptor. + * + * This function creates a `backend_descriptor` object representing a CUDNN backend variant pack + * descriptor. The variant pack descriptor is configured with the provided device pointers, unique + * IDs, and a workspace pointer. + * + * @param[out] variant_pack The created `backend_descriptor` object representing the variant pack. + * @param device_ptrs A pointer to an array of device pointers to be associated with the variant pack. + * @param uids A vector of unique IDs to be associated with the variant pack. + * @param workspace_ptr A pointer to the workspace memory to be associated with the variant pack. + * @return `error_t` A tuple containing the error code and an optional error message. + * The error code is `error_code_t::OK` on success, or an appropriate error code on failure. + */ +inline error_t +create_variant_pack(backend_descriptor& variant_pack, + void* const* device_ptrs, + std::vector<int64_t> const& uids, + void* workspace_ptr) { + RETURN_CUDNN_FRONTEND_ERROR_IF(device_ptrs == nullptr && !uids.empty(), + error_code_t::INVALID_VARIANT_PACK, + "device_ptrs must be non-null when uids are provided."); + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + variant_pack.get_ptr(), CUDNN_ATTR_VARIANT_PACK_WORKSPACE, CUDNN_TYPE_VOID_PTR, 1, &workspace_ptr)); + + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + variant_pack.get_ptr(), CUDNN_ATTR_VARIANT_PACK_DATA_POINTERS, CUDNN_TYPE_VOID_PTR, uids.size(), device_ptrs)); + + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + variant_pack.get_ptr(), CUDNN_ATTR_VARIANT_PACK_UNIQUE_IDS, CUDNN_TYPE_INT64, uids.size(), uids.data())); + + _CUDNN_CHECK_CUDNN_ERROR(detail::finalize(variant_pack.get_ptr())); + + return {error_code_t::OK, ""}; +} + +inline error_t +create_variant_pack(backend_descriptor& variant_pack, + std::vector<void*>& device_ptrs, + std::vector<int64_t> const& uids, + void* workspace_ptr) { + RETURN_CUDNN_FRONTEND_ERROR_IF(device_ptrs.size() != uids.size(), + error_code_t::INVALID_VARIANT_PACK, + "device_ptrs and uids must have the same length."); + return create_variant_pack(variant_pack, device_ptrs.data(), uids, workspace_ptr); +} + +inline error_t +create_variant_pack(backend_descriptor& variant_pack, + void* const* device_ptrs, + std::vector<int64_t> const& uids, + void* workspace_ptr, + std::vector<int64_t> const& override_uids, + std::vector<std::vector<int64_t>> const& override_shapes, + std::vector<std::vector<int64_t>> const& override_strides) { + RETURN_CUDNN_FRONTEND_ERROR_IF(device_ptrs == nullptr && !uids.empty(), + error_code_t::INVALID_VARIANT_PACK, + "device_ptrs must be non-null when uids are provided."); + auto cudnn_ver_error = error_t{error_code_t::GRAPH_NOT_SUPPORTED, "Dynamic shapes requires cuDNN v9.18.0"}; + + NV_CUDNN_FE_DYNAMIC_CHECK_CUDNN_BACKEND_VERSION(91800, cudnn_ver_error); + + CUDNN_FRONTEND_UNUSED(override_uids); + CUDNN_FRONTEND_UNUSED(override_shapes); + CUDNN_FRONTEND_UNUSED(override_strides); + + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + variant_pack.get_ptr(), CUDNN_ATTR_VARIANT_PACK_WORKSPACE, CUDNN_TYPE_VOID_PTR, 1, &workspace_ptr)); + + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + variant_pack.get_ptr(), CUDNN_ATTR_VARIANT_PACK_DATA_POINTERS, CUDNN_TYPE_VOID_PTR, uids.size(), device_ptrs)); + + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + variant_pack.get_ptr(), CUDNN_ATTR_VARIANT_PACK_UNIQUE_IDS, CUDNN_TYPE_INT64, uids.size(), uids.data())); + +#if (CUDNN_VERSION >= 91800) + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute(variant_pack.get_ptr(), + CUDNN_ATTR_VARIANT_PACK_OVERRIDE_UNIQUE_IDS, + CUDNN_TYPE_INT64, + override_uids.size(), + override_uids.data())); + + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute(variant_pack.get_ptr(), + CUDNN_ATTR_VARIANT_PACK_OVERRIDE_SHAPES, + CUDNN_TYPE_VOID_PTR, + 1, + (void*)&override_shapes)); + + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute(variant_pack.get_ptr(), + CUDNN_ATTR_VARIANT_PACK_OVERRIDE_STRIDES, + CUDNN_TYPE_VOID_PTR, + 1, + (void*)&override_strides)); +#endif + + _CUDNN_CHECK_CUDNN_ERROR(detail::finalize(variant_pack.get_ptr())); + + return {error_code_t::OK, ""}; +} + +inline error_t +create_variant_pack(backend_descriptor& variant_pack, + std::vector<void*>& device_ptrs, + std::vector<int64_t> const& uids, + void* workspace_ptr, + std::vector<int64_t> const& override_uids, + std::vector<std::vector<int64_t>> const& override_shapes, + std::vector<std::vector<int64_t>> const& override_strides) { + RETURN_CUDNN_FRONTEND_ERROR_IF(device_ptrs.size() != uids.size(), + error_code_t::INVALID_VARIANT_PACK, + "device_ptrs and uids must have the same length."); + return create_variant_pack( + variant_pack, device_ptrs.data(), uids, workspace_ptr, override_uids, override_shapes, override_strides); +} + +} // namespace cudnn_frontend::detail diff --git a/deps/cudnn-frontend/include/cudnn_frontend/backend/kernel_cache.h b/deps/cudnn-frontend/include/cudnn_frontend/backend/kernel_cache.h new file mode 100644 index 0000000..ef173b2 --- /dev/null +++ b/deps/cudnn-frontend/include/cudnn_frontend/backend/kernel_cache.h @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#pragma once + +#include <algorithm> +#include <array> +#include <functional> +#include <memory> +#include <sstream> +#include <utility> + +#include "../graph_helpers.h" +#include "backend_descriptor.h" + +namespace cudnn_frontend { +namespace graph { +class Graph; +} // namespace graph +/// +/// KernelCache Class +/// Wraps the kernel_cache backend descriptor +/// Wraps backend utility functions for user's convenience +/// Backend accessor functions: size() +/// Contains internal utilities for kernel cache finalization and operation graph attributes +/// +class KernelCache : public detail::backend_descriptor { + public: + friend class graph::Graph; + // Uses the default backend constructor so that we can check for initialization error during build() + KernelCache() : backend_descriptor() {} + + std::string + describe() const { + std::stringstream ss; + ss << "CUDNN_BACKEND_KERNEL_CACHE_DESCRIPTOR : " << std::endl; + return ss.str(); + } + + bool + is_finalized() { + return finalized; + } + + // Used to check kernel cache status (particularly after initialization) + error_t + status() { + if (get_status() != CUDNN_STATUS_SUCCESS) { + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_BACKEND_KERNEL_CACHE_DESCRIPTOR: Check CUDNN_VERSION >= 9.4"}; + } + return {}; + } + + error_t + to_json(std::string &str_json) const { + str_json.clear(); +#if (CUDNN_VERSION >= 91000) + RETURN_CUDNN_FRONTEND_ERROR_IF(detail::get_backend_version() < 91000, + error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_KERNEL_CACHE_JSON_REPRESENTATION is only available starting 9.10."); + + int64_t serializationSize; + std::vector<char> serialization_buf; + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute( + get_ptr(), CUDNN_ATTR_KERNEL_CACHE_JSON_REPRESENTATION, CUDNN_TYPE_CHAR, 0, &serializationSize, nullptr)); + serialization_buf.resize(static_cast<size_t>(serializationSize)); + + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute(get_ptr(), + CUDNN_ATTR_KERNEL_CACHE_JSON_REPRESENTATION, + CUDNN_TYPE_CHAR, + serializationSize, + &serializationSize, + serialization_buf.data())); + std::string json_string(serialization_buf.begin(), serialization_buf.end()); + str_json = std::move(json_string); + return {}; +#else + (void)str_json; + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_KERNEL_CACHE_JSON_REPRESENTATION is only available starting 9.10."}; +#endif + } + + error_t + from_json(const std::string &json_cache) { +#if (CUDNN_VERSION >= 91000) + RETURN_CUDNN_FRONTEND_ERROR_IF(detail::get_backend_version() < 91000, + error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_KERNEL_CACHE_JSON_REPRESENTATION is only available starting 9.10."); + + // Check if the kernel cache is already initialized + RETURN_CUDNN_FRONTEND_ERROR_IF( + get_ptr() != nullptr, error_code_t::CUDNN_BACKEND_API_FAILED, "Kernel cache is already initialized."); + + // // Initialize the kernel cache descriptor + CHECK_CUDNN_FRONTEND_ERROR(initialize(CUDNN_BACKEND_KERNEL_CACHE_DESCRIPTOR)); + + std::vector<char> serialization_buf; + serialization_buf.assign(json_cache.begin(), json_cache.end()); + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute(get_ptr(), + CUDNN_ATTR_KERNEL_CACHE_JSON_REPRESENTATION, + CUDNN_TYPE_CHAR, + serialization_buf.size(), + serialization_buf.data())); + return {}; +#else + (void)json_cache; + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_KERNEL_CACHE_JSON_REPRESENTATION is only available starting 9.10."}; +#endif + } + + // Responsible for initializing, setting operation graph attribute, and finalizing kernel cache + // Check for both compile-time and runtime cuDNN version + error_t + build(cudnnBackendDescriptor_t op_graph) { +#if (CUDNN_VERSION >= 90400) + RETURN_CUDNN_FRONTEND_ERROR_IF(detail::get_backend_version() < 90400, + error_code_t::GRAPH_NOT_SUPPORTED, + "CUDNN_BACKEND_KERNEL_CACHE_DESCRIPTOR is only available starting 9.4."); + if (get_ptr() == nullptr) { + CHECK_CUDNN_FRONTEND_ERROR(initialize(CUDNN_BACKEND_KERNEL_CACHE_DESCRIPTOR)); + } +#if (CUDNN_VERSION >= 90500) + RETURN_CUDNN_FRONTEND_ERROR_IF(detail::get_backend_version() < 90500, + error_code_t::GRAPH_NOT_SUPPORTED, + "CUDNN_ATTR_KERNEL_CACHE_OPERATION_GRAPH is only available starting 9.5."); + if (op_graph) { + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + get_ptr(), CUDNN_ATTR_KERNEL_CACHE_OPERATION_GRAPH, CUDNN_TYPE_BACKEND_DESCRIPTOR, 1, &op_graph)); + } +#else + (void)op_graph; +#endif + CHECK_CUDNN_FRONTEND_ERROR(finalize()); + finalized = true; + return {}; +#else + (void)op_graph; + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_BACKEND_KERNEL_CACHE_DESCRIPTOR is only available starting 9.4."}; +#endif + } + + private: + bool finalized = false; +}; +} // namespace cudnn_frontend
\ No newline at end of file diff --git a/deps/cudnn-frontend/include/cudnn_frontend/backend/plan_helpers.h b/deps/cudnn-frontend/include/cudnn_frontend/backend/plan_helpers.h new file mode 100644 index 0000000..4ef47e2 --- /dev/null +++ b/deps/cudnn-frontend/include/cudnn_frontend/backend/plan_helpers.h @@ -0,0 +1,212 @@ +#pragma once + +#include <vector> + +#include "cudnn.h" + +#include "backend_descriptor.h" +#include "../knobs.h" + +namespace cudnn_frontend::detail { +/** + * @brief Creates a CUDNN backend variant pack descriptor. + * + * This function creates a `backend_descriptor` object representing a CUDNN backend variant pack + * descriptor. The variant pack descriptor is configured with the provided device pointers, unique + * IDs, and a workspace pointer. + * + * @param[out] variant_pack The created `backend_descriptor` object representing the variant pack. + * @param device_ptrs A vector of device pointers to be associated with the variant pack. + * @param uids A vector of unique IDs to be associated with the variant pack. + * @param workspace_ptr A pointer to the workspace memory to be associated with the variant pack. + * @return `error_t` A tuple containing the error code and an optional error message. + * The error code is `error_code_t::OK` on success, or an appropriate error code on failure. + */ +inline error_t +get_workspace_size(ManagedOpaqueDescriptor& engine_config, int64_t& workspace) { +#if CUDNN_VERSION >= 90200 + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute(engine_config->get_backend_descriptor(), + CUDNN_ATTR_ENGINECFG_WORKSPACE_SIZE, + CUDNN_TYPE_INT64, + 1, + nullptr, + &workspace)); + return {error_code_t::OK, ""}; +#else + (void)engine_config; + (void)workspace; + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_ENGINECFG_WORKSPACE_SIZE is only available starting 9.2."}; +#endif +} + +inline error_t +get_shared_memory_size(ManagedOpaqueDescriptor& engine_config, int32_t& shared_memory_size) { +#if CUDNN_VERSION >= 90200 + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute(engine_config->get_backend_descriptor(), + CUDNN_ATTR_ENGINECFG_SHARED_MEMORY_USED, + CUDNN_TYPE_INT32, + 1, + nullptr, + &shared_memory_size)); + return {error_code_t::OK, ""}; +#else + (void)engine_config; + (void)shared_memory_size; + return {error_code_t::CUDNN_BACKEND_API_FAILED, + "CUDNN_ATTR_ENGINECFG_SHARED_MEMORY_USED is only available starting 9.2."}; +#endif +} + +inline error_t +create_engine(backend_descriptor& engine, + int64_t const engine_id, + cudnnBackendDescriptor_t op_graph, + std::shared_ptr<const DeviceProperties> device_properties = nullptr) { + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + engine.get_ptr(), CUDNN_ATTR_ENGINE_OPERATION_GRAPH, CUDNN_TYPE_BACKEND_DESCRIPTOR, 1, &op_graph)); + + // Validate before setting + int64_t count; + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute( + op_graph, CUDNN_ATTR_OPERATIONGRAPH_ENGINE_GLOBAL_COUNT, CUDNN_TYPE_INT64, 1, nullptr, &count)); + RETURN_CUDNN_FRONTEND_ERROR_IF( + engine_id >= count || engine_id < 0, error_code_t::INVALID_VALUE, "Invalid engine id."); + + _CUDNN_CHECK_CUDNN_ERROR( + detail::set_attribute(engine.get_ptr(), CUDNN_ATTR_ENGINE_GLOBAL_INDEX, CUDNN_TYPE_INT64, 1, &engine_id)); + + if (device_properties != nullptr) { +#if (CUDNN_VERSION >= 90800) + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute(engine.get_ptr(), + CUDNN_ATTR_ENGINE_DEVICEPROP, + CUDNN_TYPE_BACKEND_DESCRIPTOR, + 1, + &device_properties->get_ptr())); +#endif + } + + _CUDNN_CHECK_CUDNN_ERROR(detail::finalize(engine.get_ptr())); + + return {error_code_t::OK, ""}; +} + +inline error_t +query_knobs(int64_t const engine_id, cudnnBackendDescriptor_t op_graph, std::vector<Knob>& knobs) { + detail::backend_descriptor engine(CUDNN_BACKEND_ENGINE_DESCRIPTOR); + RETURN_CUDNN_FRONTEND_ERROR_IF(engine.get_status() != CUDNN_STATUS_SUCCESS, + error_code_t::CUDNN_BACKEND_API_FAILED, + "Failed to create engine's backend descriptor."); + CHECK_CUDNN_FRONTEND_ERROR(detail::create_engine(engine, engine_id, op_graph)); + + // Initialize a backend descriptor for each knob type + // The size of the array should be CUDNN_KNOB_TYPE_COUNTS, as currently we dont know how many knobs the engine will + // support + std::array<backend_descriptor, CUDNN_KNOB_TYPE_COUNTS> frontend_knobs; + for (size_t i = 0; i < CUDNN_KNOB_TYPE_COUNTS; i++) { + backend_descriptor frontend_knob(CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR); + RETURN_CUDNN_FRONTEND_ERROR_IF(frontend_knob.get_status() != CUDNN_STATUS_SUCCESS, + error_code_t::CUDNN_BACKEND_API_FAILED, + "Failed to create knob's backend descriptor."); + frontend_knobs[i] = std::move(frontend_knob); + } + + // Create an auxillary array to hold the raw knob descriptors + std::array<cudnnBackendDescriptor_t, CUDNN_KNOB_TYPE_COUNTS> backend_knobs; + for (size_t i = 0; i < CUDNN_KNOB_TYPE_COUNTS; i++) { + backend_knobs[i] = frontend_knobs[i].get_ptr(); + } + + // This is the actual number of knobs that is supported by the engine + int64_t knobs_size; + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute(engine.get_ptr(), + CUDNN_ATTR_ENGINE_KNOB_INFO, + CUDNN_TYPE_BACKEND_DESCRIPTOR, + CUDNN_KNOB_TYPE_COUNTS, + &knobs_size, + backend_knobs.data())); + + for (int64_t i = 0; i < knobs_size; i++) { + cudnnBackendKnobType_t type; + int64_t elemCount; + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute( + frontend_knobs[i].get_ptr(), CUDNN_ATTR_KNOB_INFO_TYPE, CUDNN_TYPE_KNOB_TYPE, 1, &elemCount, &type)); + + int64_t maxValue; + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute(frontend_knobs[i].get_ptr(), + CUDNN_ATTR_KNOB_INFO_MAXIMUM_VALUE, + CUDNN_TYPE_INT64, + 1, + &elemCount, + &maxValue)); + + int64_t minValue; + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute(frontend_knobs[i].get_ptr(), + CUDNN_ATTR_KNOB_INFO_MINIMUM_VALUE, + CUDNN_TYPE_INT64, + 1, + &elemCount, + &minValue)); + + int64_t stride; + _CUDNN_CHECK_CUDNN_ERROR(detail::get_attribute( + frontend_knobs[i].get_ptr(), CUDNN_ATTR_KNOB_INFO_STRIDE, CUDNN_TYPE_INT64, 1, &elemCount, &stride)); + + auto frontend_knob_type = convert_from_backend_knob_type(type); + knobs.emplace_back(frontend_knob_type, maxValue, minValue, stride); + } + + return {error_code_t::OK, ""}; +} + +inline error_t +set_knob_choices(std::unordered_map<KnobType_t, int64_t> const& user_choices, + std::vector<detail::backend_descriptor>& knob_choices) { + for (auto const& [type, choice] : user_choices) { + backend_descriptor knob_choice(CUDNN_BACKEND_KNOB_CHOICE_DESCRIPTOR); + RETURN_CUDNN_FRONTEND_ERROR_IF(knob_choice.get_status() != CUDNN_STATUS_SUCCESS, + error_code_t::CUDNN_BACKEND_API_FAILED, + "Failed to create knob_choice's backend descriptor."); + + cudnnBackendKnobType_t backend_type; + _CUDNN_CHECK_CUDNN_ERROR(convert_to_backend_knob_type(type, backend_type)); + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + knob_choice.get_ptr(), CUDNN_ATTR_KNOB_CHOICE_KNOB_TYPE, CUDNN_TYPE_KNOB_TYPE, 1, &backend_type)); + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute( + knob_choice.get_ptr(), CUDNN_ATTR_KNOB_CHOICE_KNOB_VALUE, CUDNN_TYPE_INT64, 1, &choice)); + + _CUDNN_CHECK_CUDNN_ERROR(detail::finalize(knob_choice.get_ptr())); + + knob_choices.push_back(std::move(knob_choice)); + } + + return {error_code_t::OK, ""}; +} + +inline error_t +create_engine_config(ManagedOpaqueDescriptor& engine_config, + backend_descriptor& engine, + std::vector<detail::backend_descriptor>& knob_choices) { + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute(engine_config->get_backend_descriptor(), + CUDNN_ATTR_ENGINECFG_ENGINE, + CUDNN_TYPE_BACKEND_DESCRIPTOR, + 1, + &(engine.get_ptr()))); + + std::vector<cudnnBackendDescriptor_t> backend_knob_choices(CUDNN_KNOB_TYPE_COUNTS); + for (size_t i = 0; i < knob_choices.size(); i++) { + backend_knob_choices[i] = knob_choices[i].get_ptr(); + } + _CUDNN_CHECK_CUDNN_ERROR(detail::set_attribute(engine_config->get_backend_descriptor(), + CUDNN_ATTR_ENGINECFG_KNOB_CHOICES, + CUDNN_TYPE_BACKEND_DESCRIPTOR, + knob_choices.size(), + backend_knob_choices.data())); + + // Finalizing the descriptor + _CUDNN_CHECK_CUDNN_ERROR(detail::finalize(engine_config->get_backend_descriptor())); + + return {error_code_t::OK, ""}; +} + +} // namespace cudnn_frontend::detail |
