Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .azure-pipelines/cuda-unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ steps:
- script: |
SB_MICRO_PATH=$PWD make cppbuild
displayName: Build benchmarks
- script: |
set -e
cd superbench/benchmarks/micro_benchmarks/cudnn_function/build
cmake --build . --target cudnn_config_test
ctest -R '^cudnn_config$' --output-on-failure --no-tests=error
ctest -R '^cudnn_algorithm_calls$' --output-on-failure --no-tests=error
displayName: Test cuDNN algorithm selection and execution
- script: |
SB_MICRO_PATH=$PWD python3 setup.py test
displayName: Run unit tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
cmake_minimum_required(VERSION 3.18)
project(cudnn_benchmark LANGUAGES CXX)

include(CTest)

find_package(CUDAToolkit QUIET)
if(CUDAToolkit_FOUND)
include(../cuda_common.cmake)
Expand All @@ -30,5 +32,17 @@ if(CUDAToolkit_FOUND)

add_executable(cudnn_benchmark cudnn_test.cpp)
target_link_libraries(cudnn_benchmark ${TARGET_NAME} nlohmann_json::nlohmann_json CUDA::cudart ${CUDNN_LIBRARY})

set(CUDNN_CONFIG_TEST_SOURCE
${CMAKE_CURRENT_SOURCE_DIR}/../../../../tests/benchmarks/micro_benchmarks/test_cudnn_execution_config.cpp)
if(BUILD_TESTING AND EXISTS "${CUDNN_CONFIG_TEST_SOURCE}")
add_executable(cudnn_config_test EXCLUDE_FROM_ALL ${CUDNN_CONFIG_TEST_SOURCE})
target_include_directories(cudnn_config_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_features(cudnn_config_test PRIVATE cxx_std_14)
target_link_libraries(cudnn_config_test PRIVATE ${TARGET_NAME} CUDA::cudart ${CUDNN_LIBRARY})
add_test(NAME cudnn_config COMMAND cudnn_config_test)
Comment thread
gusui-msft marked this conversation as resolved.
Resolved
add_test(NAME cudnn_algorithm_calls COMMAND cudnn_config_test --gpu)
endif()

install(TARGETS cudnn_benchmark ${TARGET_NAME} RUNTIME DESTINATION bin LIBRARY DESTINATION lib)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,34 @@ namespace cudnn_test {
* @tparam T2 conv type
*/
template <typename T1, typename T2> class ConvolutionBackwardDataFunction : public CudnnFunction<T1, T2> {
cudnnConvolutionBwdDataAlgo_t bwd_data_algo_;
cudnnConvolutionBwdDataAlgo_t selected_algorithm_;
/**
* @brief Execute the kernel/function
*/
virtual void kernel_entry() {
void kernel_entry() override {
CHECK_CUDNN_ERROR(cudnnConvolutionBackwardData(
this->cudnn_handle, &this->alpha_, this->w_desc_.desc(), this->filter, this->x_desc_.desc(), this->x,
this->conv_desc_.desc(), this->bwd_data_algo_, this->fwd_workspace_, this->fwd_workspace_size_,
&this->beta_, this->h_desc_.desc(), this->h));
this->conv_desc_.desc(), selected_algorithm_, this->fwd_workspace_, this->fwd_workspace_size_, &this->beta_,
this->h_desc_.desc(), this->h));
}
/**
* @brief Get and set convolution algorithm and workspace size used in cudnn convolution functions
* @brief Query workspace bytes for the selected backward-data algorithm
*/
virtual void get_workspace_size() {
bwd_data_algo_ = cudnnConvolutionBwdDataAlgo_t(this->algo_);
size_t get_workspace_size() const override {
size_t bytes;
CHECK_CUDNN_ERROR(cudnnGetConvolutionBackwardDataWorkspaceSize(
this->cudnn_handle, this->w_desc_.desc(), this->x_desc_.desc(), this->conv_desc_.desc(),
this->h_desc_.desc(), this->bwd_data_algo_, &this->fwd_workspace_size_));
this->h_desc_.desc(), selected_algorithm_, &bytes));
return bytes;
}

/**
* @brief Find the best algorithm for cudnn convolution functions
* @brief Resolve the backward-data algorithm before querying workspace or running the convolution
*/
virtual void find_best_algo() {
int algo_count;
cudnnConvolutionBwdDataAlgoPerf_t perf_results;
CHECK_CUDNN_ERROR(cudnnFindConvolutionBackwardDataAlgorithm(
this->cudnn_handle, this->w_desc_.desc(), this->x_desc_.desc(), this->conv_desc_.desc(),
this->h_desc_.desc(), 1, &algo_count, &perf_results));
this->algo_ = perf_results.algo;
void select_algorithm() override {
selected_algorithm_ = select_convolution_algorithm<cudnnConvolutionBwdDataAlgoPerf_t>(
*this, cudnnFindConvolutionBackwardDataAlgorithm, this->cudnn_handle, this->w_desc_.desc(),
this->x_desc_.desc(), this->conv_desc_.desc(), this->h_desc_.desc());
}

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,33 @@ namespace cudnn_test {
* @tparam T2 conv type
*/
template <typename T1, typename T2> class ConvolutionBackwardFilterFunction : public CudnnFunction<T1, T2> {
cudnnConvolutionBwdFilterAlgo_t bwd_filter_algo_;
cudnnConvolutionBwdFilterAlgo_t selected_algorithm_;
/**
* @brief Execute the kernel/function
*/
virtual void kernel_entry() {
void kernel_entry() override {
CHECK_CUDNN_ERROR(cudnnConvolutionBackwardFilter(
this->cudnn_handle, &this->alpha_, this->x_desc_.desc(), this->x, this->h_desc_.desc(), this->h,
this->conv_desc_.desc(), this->bwd_filter_algo_, this->fwd_workspace_, this->fwd_workspace_size_,
&this->beta_, this->w_desc_.desc(), this->filter));
this->conv_desc_.desc(), selected_algorithm_, this->fwd_workspace_, this->fwd_workspace_size_, &this->beta_,
this->w_desc_.desc(), this->filter));
}
/**
* @brief Get and set convolution algorithm and workspace size used in cudnn convolution functions
* @brief Query workspace bytes for the selected backward-filter algorithm
*/
virtual void get_workspace_size() {
bwd_filter_algo_ = cudnnConvolutionBwdFilterAlgo_t(this->algo_);
size_t get_workspace_size() const override {
size_t bytes;
CHECK_CUDNN_ERROR(cudnnGetConvolutionBackwardFilterWorkspaceSize(
this->cudnn_handle, this->x_desc_.desc(), this->h_desc_.desc(), this->conv_desc_.desc(),
this->w_desc_.desc(), this->bwd_filter_algo_, &this->fwd_workspace_size_));
this->w_desc_.desc(), selected_algorithm_, &bytes));
return bytes;
}
/**
* @brief Find the best algorithm for cudnn convolution functions
* @brief Resolve the backward-filter algorithm before querying workspace or running the convolution
*/
virtual void find_best_algo() {
int algo_count;
cudnnConvolutionBwdFilterAlgoPerf_t perf_results;
CHECK_CUDNN_ERROR(cudnnFindConvolutionBackwardFilterAlgorithm(
this->cudnn_handle, this->x_desc_.desc(), this->h_desc_.desc(), this->conv_desc_.desc(),
this->w_desc_.desc(), 1, &algo_count, &perf_results));
this->algo_ = perf_results.algo;
void select_algorithm() override {
selected_algorithm_ = select_convolution_algorithm<cudnnConvolutionBwdFilterAlgoPerf_t>(
*this, cudnnFindConvolutionBackwardFilterAlgorithm, this->cudnn_handle, this->x_desc_.desc(),
this->h_desc_.desc(), this->conv_desc_.desc(), this->w_desc_.desc());
}

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,33 @@ namespace cudnn_test {
* @tparam T2 conv type
*/
template <typename T1, typename T2> class ConvolutionForwardFunction : public CudnnFunction<T1, T2> {
cudnnConvolutionFwdAlgo_t fwd_algo_;
cudnnConvolutionFwdAlgo_t selected_algorithm_;
/**
* @brief Execute the kernel/function
*/
virtual void kernel_entry() {
void kernel_entry() override {
CHECK_CUDNN_ERROR(cudnnConvolutionForward(this->cudnn_handle, &this->alpha_, this->x_desc_.desc(), this->x,
this->w_desc_.desc(), this->filter, this->conv_desc_.desc(),
this->fwd_algo_, this->fwd_workspace_, this->fwd_workspace_size_,
selected_algorithm_, this->fwd_workspace_, this->fwd_workspace_size_,
&this->beta_, this->h_desc_.desc(), this->h));
}
/**
* @brief Get and set convolution algorithm and workspace size used in cudnn convolution functions
* @brief Query workspace bytes for the selected forward algorithm
*/
virtual void get_workspace_size() {
fwd_algo_ = cudnnConvolutionFwdAlgo_t(this->algo_);
CHECK_CUDNN_ERROR(cudnnGetConvolutionForwardWorkspaceSize(
this->cudnn_handle, this->x_desc_.desc(), this->w_desc_.desc(), this->conv_desc_.desc(),
this->h_desc_.desc(), this->fwd_algo_, &this->fwd_workspace_size_));
size_t get_workspace_size() const override {
size_t bytes;
CHECK_CUDNN_ERROR(cudnnGetConvolutionForwardWorkspaceSize(this->cudnn_handle, this->x_desc_.desc(),
this->w_desc_.desc(), this->conv_desc_.desc(),
this->h_desc_.desc(), selected_algorithm_, &bytes));
return bytes;
}
/**
* @brief Find the best algorithm for cudnn convolution functions
* @brief Resolve the forward algorithm before querying workspace or running the convolution
*/
virtual void find_best_algo() {
int algo_count;
cudnnConvolutionFwdAlgoPerf_t perf_results;
CHECK_CUDNN_ERROR(cudnnFindConvolutionForwardAlgorithm(this->cudnn_handle, this->x_desc_.desc(),
this->w_desc_.desc(), this->conv_desc_.desc(),
this->h_desc_.desc(), 1, &algo_count, &perf_results));
this->algo_ = perf_results.algo;
void select_algorithm() override {
selected_algorithm_ = select_convolution_algorithm<cudnnConvolutionFwdAlgoPerf_t>(
*this, cudnnFindConvolutionForwardAlgorithm, this->cudnn_handle, this->x_desc_.desc(), this->w_desc_.desc(),
this->conv_desc_.desc(), this->h_desc_.desc());
}

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@

#include <chrono>
#include <iomanip>
#include <stdexcept>
#include <tuple>

#include "cudnn_config.h"

namespace cudnn_test {
template <typename Perf, typename Find, typename... Args>
auto select_convolution_algorithm(CudnnConfig &config, Find find, Args... args) -> decltype(Perf::algo) {
if (!config.get_auto_algo())
return static_cast<decltype(Perf::algo)>(config.get_algo());

int count = 0;
Perf candidate;
CHECK_CUDNN_ERROR(find(args..., 1, &count, &candidate));
if (count != 1)
throw std::runtime_error(config.get_name() + ": expected one cuDNN algorithm, got " + std::to_string(count));
CHECK_CUDNN_ERROR(candidate.status);
return candidate.algo;
}

/**
* @brief Generation of cudnn functions' params and run the benchmark of this function
*
Expand All @@ -25,7 +40,7 @@ template <typename T1, typename T2> class CudnnFunction : public CudnnConfig {
TensorDescriptorNd<T1> h_desc_;
size_t fwd_workspace_size_;
float *fwd_workspace_;
T1 *x, *filter, *h;
T1 *x = nullptr, *filter = nullptr, *h = nullptr;
const float alpha_ = 1.f;
const float beta_ = 0.f;

Expand All @@ -34,21 +49,22 @@ template <typename T1, typename T2> class CudnnFunction : public CudnnConfig {
*/
void prepare_input();
/**
* @brief Generate some params used in the cudnn function
* @brief Create descriptors from the convolution workload
*/
void prepare_for_function();
void prepare_descriptors();
/**
* @brief Get and set convolution algorithm and workspace size used in cudnn convolution functions
* @brief Apply the existing math/algorithm policy and allocate workspace
*/
virtual void get_workspace_size() {}
void prepare_execution();
/**
* @brief launch the kernel/function
* @brief Select the operation-specific algorithm used by both the workspace query and the convolution
*/
virtual void kernel_entry() {}
virtual void select_algorithm() = 0;
virtual size_t get_workspace_size() const = 0;
/**
* @brief Find the best algorithm for cudnn convolution functions
* @brief launch the kernel/function
*/
virtual void find_best_algo() {}
virtual void kernel_entry() = 0;

public:
/**
Expand Down Expand Up @@ -78,24 +94,24 @@ template <typename T1, typename T2> class CudnnFunction : public CudnnConfig {
};

/**
* @brief Generate some params used in the cudnn function
* @brief Create descriptors from the convolution workload
*/
template <typename T1, typename T2> void CudnnFunction<T1, T2>::prepare_for_function() {
// Generate descriptor
template <typename T1, typename T2> void CudnnFunction<T1, T2>::prepare_descriptors() {
conv_desc_ =
ConvolutionDescriptor<T2>(get_array_length(), get_padA(), get_filter_strideA(), get_dilationA(), get_mode());
x_desc_ = TensorDescriptorNd<T1>(get_input_dims(), get_input_stride());
w_desc_ = FilterDescriptorNd<T1>(get_filter_dims());
h_desc_ = TensorDescriptorNd<T1>(get_output_dims(), get_output_stride());
}

// Set Convolution MathType
cudnnMathType_t algo = get_use_tensor_op() ? CUDNN_TENSOR_OP_MATH : CUDNN_DEFAULT_MATH;
CHECK_CUDNN_ERROR(cudnnSetConvolutionMathType(conv_desc_.desc(), algo));
if (this->auto_algo_) {
find_best_algo();
}
// Set convolution algorithm and workspace size
this->get_workspace_size();
/**
* @brief Apply the existing math/algorithm policy and allocate workspace
*/
template <typename T1, typename T2> void CudnnFunction<T1, T2>::prepare_execution() {
const auto math_type = get_use_tensor_op() ? CUDNN_TENSOR_OP_MATH : CUDNN_DEFAULT_MATH;
CHECK_CUDNN_ERROR(cudnnSetConvolutionMathType(conv_desc_.desc(), math_type));
select_algorithm();
fwd_workspace_size_ = get_workspace_size();
zeros<float>(&fwd_workspace_, std::vector<int>{static_cast<int>(this->fwd_workspace_size_ / sizeof(float)), 1});
}
/**
Expand All @@ -113,9 +129,8 @@ template <typename T1, typename T2> void CudnnFunction<T1, T2>::prepare_input()
* @brief The main procedure for cudnn function test, including warmup, function test and time measurement
*/
template <typename T1, typename T2> void CudnnFunction<T1, T2>::benchmark() {
// Prepare some Prerequisites for function running
prepare_for_function();
// Allocate memory and fill with data of input and output tensor
prepare_descriptors();
prepare_execution();
prepare_input();

// Warm up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

#pragma once

#include <functional>
#include <iostream>
#include <memory>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -145,7 +147,7 @@ template <typename T> class FilterDescriptorNd {
CHECK_CUDNN_ERROR(cudnnSetFilterNdDescriptor(*desc_, type, tensor_format, dim.size(), &dim[0]));
}

cudnnFilterDescriptor_t desc() { return *desc_; }
cudnnFilterDescriptor_t desc() const { return *desc_; }
};

/**
Expand Down
Loading
Loading