cudnn_frontend_EngineConfig.h
Go to the documentation of this file.
/*
* Copyright (c) 2021, 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 <cudnn.h>
#include <cudnn_backend.h>
#include "cudnn_frontend_Engine.h"
#include "cudnn_frontend_utils.h"
namespace cudnn_frontend {
class EngineConfig_v8 : public BackendDescriptor {
public:
friend class EngineConfigBuilder_v8;
std::string
describe() const override {
std::stringstream ss;
ss << "CUDNN_BACKEND_ENGINECFG_DESCRIPTOR :";
ss << " Number of knobs: " << numKnobs;
return ss.str();
}
EngineConfig_v8 &
operator=(EngineConfig_v8 &&from) = default;
EngineConfig_v8(EngineConfig_v8 &&from) = default;
~EngineConfig_v8() = default;
std::string const &
getTag() const {
return opGraphTag;
}
private:
EngineConfig_v8() : BackendDescriptor() {
cudnnStatus_t status;
for (uint64_t i = 0; i < bChoices.size(); i++) {
bChoices[i] = make_shared_backend_pointer(CUDNN_BACKEND_KNOB_CHOICE_DESCRIPTOR);
if (bChoices[i]->is_good() == false) {
status = bChoices[i]->get_status();
set_error_and_throw_exception(
this,
status,
"CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: CUDNN_BACKEND_KNOB_CHOICE_DESCRIPTOR cudnnCreate Failed");
break;
}
}
}
EngineConfig_v8(EngineConfig_v8 const &) = delete;
EngineConfig_v8 &
operator=(EngineConfig_v8 const &) = delete;
ManagedOpaqueDescriptor engine = nullptr;
int64_t numKnobs = 0;
std::string opGraphTag;
bool set_knobs_attr = false;
std::array<ManagedOpaqueDescriptor, CUDNN_KNOB_TYPE_COUNTS> bChoices = {};
};
class EngineConfigBuilder_v8 {
public:
auto
setEngine(Engine_v8 const &engine_) -> EngineConfigBuilder_v8 & {
m_engine_config.engine = engine_.get_desc();
m_engine_config.opGraphTag = engine_.getTag();
auto &knobs = engine_.getFinalizedKnobs();
m_engine_config.numKnobs = knobs.size();
m_engine_config.set_knobs_attr = engine_.knobs_set();
for (std::uint32_t i = 0; i < knobs.size(); i++) {
cudnnStatus_t status;
cudnnBackendKnobType_t type = knobs[i].getKnobType();
int64_t value = knobs[i].getChoice();
status = cudnnBackendSetAttribute(m_engine_config.bChoices[i]->get_backend_descriptor(),
CUDNN_ATTR_KNOB_CHOICE_KNOB_TYPE,
CUDNN_TYPE_KNOB_TYPE,
1,
&type);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(&m_engine_config,
status,
"CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: "
"CUDNN_BACKEND_KNOB_CHOICE_DESCRIPTOR SetAttribute "
"CUDNN_ATTR_KNOB_CHOICE_KNOB_TYPE Failed");
}
status = cudnnBackendSetAttribute(m_engine_config.bChoices[i]->get_backend_descriptor(),
CUDNN_ATTR_KNOB_CHOICE_KNOB_VALUE,
CUDNN_TYPE_INT64,
1,
&value);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(&m_engine_config,
status,
"CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: "
"CUDNN_BACKEND_KNOB_CHOICE_DESCRIPTOR SetAttribute "
"CUDNN_ATTR_KNOB_CHOICE_KNOB_VALUE Failed");
}
status = cudnnBackendFinalize(m_engine_config.bChoices[i]->get_backend_descriptor());
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_engine_config,
status,
"CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: CUDNN_BACKEND_KNOB_CHOICE_DESCRIPTOR cudnnFinalize Failed");
}
}
return *this;
}
EngineConfig_v8 &&
build() {
if (m_engine_config.status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(&m_engine_config,
m_engine_config.status,
"CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: is not created properly");
return std::move(m_engine_config);
}
if (m_engine_config.engine == nullptr) {
set_error_and_throw_exception(
&m_engine_config,
CUDNN_STATUS_BAD_PARAM,
"CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: Check and Set the CUDNN_ATTR_ENGINECFG_ENGINE.");
return std::move(m_engine_config);
}
// Create a descriptor. Memory allocation happens here.
auto status = m_engine_config.initialize_managed_backend_pointer(CUDNN_BACKEND_ENGINECFG_DESCRIPTOR);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_engine_config, status, "CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: cudnnCreate Failed");
return std::move(m_engine_config);
}
status = cudnnBackendSetAttribute(m_engine_config.pointer->get_backend_descriptor(),
CUDNN_ATTR_ENGINECFG_ENGINE,
CUDNN_TYPE_BACKEND_DESCRIPTOR,
1,
&(m_engine_config.engine->get_backend_descriptor()));
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_engine_config,
status,
"CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: SetAttribute CUDNN_ATTR_ENGINECFG_ENGINE Failed");
return std::move(m_engine_config);
}
if (m_engine_config.set_knobs_attr && m_engine_config.numKnobs > 0) {
std::array<cudnnBackendDescriptor_t, CUDNN_KNOB_TYPE_COUNTS> bChoices_;
for (auto i = 0; i < m_engine_config.numKnobs; i++) {
bChoices_[i] = m_engine_config.bChoices[i]->get_backend_descriptor();
}
status = cudnnBackendSetAttribute(m_engine_config.pointer->get_backend_descriptor(),
CUDNN_ATTR_ENGINECFG_KNOB_CHOICES,
CUDNN_TYPE_BACKEND_DESCRIPTOR,
m_engine_config.numKnobs,
bChoices_.data());
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_engine_config,
status,
"CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: SetAttribute CUDNN_ATTR_ENGINECFG_KNOB_CHOICES Failed");
return std::move(m_engine_config);
}
}
// Finalizing the descriptor
status = cudnnBackendFinalize(m_engine_config.pointer->get_backend_descriptor());
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_engine_config, status, "CUDNN_BACKEND_ENGINECFG_DESCRIPTOR: cudnnFinalize Failed");
return std::move(m_engine_config);
}
getLogger() << "[cudnn_frontend] " << m_engine_config << std::endl;
return std::move(m_engine_config);
}
explicit EngineConfigBuilder_v8() = default;
~EngineConfigBuilder_v8() = default;
EngineConfigBuilder_v8(EngineConfigBuilder_v8 &&) = delete;
EngineConfigBuilder_v8(EngineConfigBuilder_v8 const &) = delete;
EngineConfigBuilder_v8 &
operator=(EngineConfigBuilder_v8 const &) = delete;
private:
EngineConfig_v8 m_engine_config;
};
using EngineConfigList = std::vector<ManagedOpaqueDescriptor>;
}
cudnn_frontend::getLogger
ConditionalStreamer & getLogger()
Definition: cudnn_frontend_Logging.h:72
cudnn_frontend::set_error_and_throw_exception
static void set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message)
Definition: cudnn_frontend_utils.h:138
cudnn_frontend::EngineConfigBuilder_v8::m_engine_config
EngineConfig_v8 m_engine_config
Definition: cudnn_frontend_EngineConfig.h:237
cudnn_frontend::EngineConfig_v8::engine
ManagedOpaqueDescriptor engine
Definition: cudnn_frontend_EngineConfig.h:94
cudnn_frontend::EngineConfig_v8::opGraphTag
std::string opGraphTag
Definition: cudnn_frontend_EngineConfig.h:96
cudnn_frontend::EngineConfig_v8::EngineConfig_v8
EngineConfig_v8()
Definition: cudnn_frontend_EngineConfig.h:76
cudnn_frontend::EngineConfig_v8::operator=
EngineConfig_v8 & operator=(EngineConfig_v8 &&from)=default
cudnn_frontend::BackendDescriptor
Definition: cudnn_backend_base.h:99
cudnn_frontend::make_shared_backend_pointer
static ManagedOpaqueDescriptor make_shared_backend_pointer(cudnnBackendDescriptorType_t type)
Definition: cudnn_backend_base.h:88
cudnn_frontend::EngineConfig_v8
Definition: cudnn_frontend_EngineConfig.h:51
cudnn_frontend::EngineConfig_v8::describe
std::string describe() const override
Return a string describing the backend Descriptor.
Definition: cudnn_frontend_EngineConfig.h:56
cudnn_frontend::EngineConfigBuilder_v8
Definition: cudnn_frontend_EngineConfig.h:104
cudnn_frontend::EngineConfig_v8::~EngineConfig_v8
~EngineConfig_v8()=default
cudnn_frontend::EngineConfigBuilder_v8::setEngine
auto setEngine(Engine_v8 const &engine_) -> EngineConfigBuilder_v8 &
Set engine for the EngineConfig_v8.
Definition: cudnn_frontend_EngineConfig.h:112
cudnn_frontend_utils.h
cudnn_frontend::EngineConfig_v8::EngineConfigBuilder_v8
friend class EngineConfigBuilder_v8
Definition: cudnn_frontend_EngineConfig.h:53
cudnn_frontend::EngineConfig_v8::numKnobs
int64_t numKnobs
Definition: cudnn_frontend_EngineConfig.h:95
cudnn_frontend::Engine_v8
Definition: cudnn_frontend_Engine.h:52
cudnn_frontend::ManagedOpaqueDescriptor
std::shared_ptr< OpaqueBackendPointer > ManagedOpaqueDescriptor
Definition: cudnn_backend_base.h:84
cudnn_frontend
Definition: cudnn_backend_base.h:29
cudnn_frontend::EngineConfig_v8::set_knobs_attr
bool set_knobs_attr
Definition: cudnn_frontend_EngineConfig.h:97
type
cudnn_frontend::EngineConfigBuilder_v8::build
EngineConfig_v8 && build()
Definition: cudnn_frontend_EngineConfig.h:164
cudnn_frontend::EngineConfig_v8::getTag
std::string const & getTag() const
Definition: cudnn_frontend_EngineConfig.h:71
cudnn_frontend_Engine.h
cudnn_frontend::EngineConfigList
std::vector< ManagedOpaqueDescriptor > EngineConfigList
Definition: cudnn_frontend_EngineConfig.h:248
cudnn_frontend::EngineConfig_v8::bChoices
std::array< ManagedOpaqueDescriptor, CUDNN_KNOB_TYPE_COUNTS > bChoices
Opaque pointer to the backend knobs.
Definition: cudnn_frontend_EngineConfig.h:98
cudnn_frontend::BackendDescriptor::status
cudnnStatus_t status
Shared pointer of the OpaqueBackendPointer.
Definition: cudnn_backend_base.h:160