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
5 changes: 5 additions & 0 deletions .azure-pipelines/cuda-unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ steps:
- script: |
SB_MICRO_PATH=$PWD make cppbuild
displayName: Build benchmarks
- script: |
set -e
cd superbench/benchmarks/micro_benchmarks/cudnn_function/build
ctest -L cpu --output-on-failure --no-tests=error
displayName: Run C++ unit tests
- 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,16 @@ 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 ${CUDNN_CONFIG_TEST_SOURCE})
target_include_directories(cudnn_config_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_features(cudnn_config_test PRIVATE cxx_std_11)
add_test(NAME cudnn_config COMMAND cudnn_config_test)
set_tests_properties(cudnn_config PROPERTIES LABELS "cpu;cudnn")
endif()

install(TARGETS cudnn_benchmark ${TARGET_NAME} RUNTIME DESTINATION bin LIBRARY DESTINATION lib)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include "cudnn_config.h"

namespace cudnn_test {
// Storage and compute types remain in the existing CudnnFunction template dispatch.
struct CudnnConvolutionWorkload {
const std::vector<int> input_dims, input_stride, filter_dims, output_dims, output_stride;
const std::vector<int> padding, filter_stride, dilation;
const cudnnConvolutionMode_t mode;
const int array_length;

explicit CudnnConvolutionWorkload(CudnnConfig &config)
: input_dims(config.get_input_dims()), input_stride(config.get_input_stride()),
filter_dims(config.get_filter_dims()), output_dims(config.get_output_dims()),
output_stride(config.get_output_stride()), padding(config.get_padA()),
filter_stride(config.get_filter_strideA()), dilation(config.get_dilationA()), mode(config.get_mode()),
array_length(config.get_array_length()) {}
};

struct CudnnExecutionPolicy {
const cudnnMathType_t math_type;
const bool auto_algo;
const int algorithm;

// Automatic selection does not require a configured algorithm index.
explicit CudnnExecutionPolicy(CudnnConfig &config)
: math_type(config.get_use_tensor_op() ? CUDNN_TENSOR_OP_MATH : CUDNN_DEFAULT_MATH),
auto_algo(config.get_auto_algo()), algorithm(auto_algo ? 0 : config.get_algo()) {}
};
} // namespace cudnn_test
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <iomanip>
#include <tuple>

#include "cudnn_config.h"
#include "cudnn_execution_config.h"
Comment thread
gusui-msft marked this conversation as resolved.
Resolved

namespace cudnn_test {
/**
Expand All @@ -32,11 +32,15 @@ template <typename T1, typename T2> class CudnnFunction : public CudnnConfig {
/**
* @brief Malloc cuda memory and fill in value for data params used in the cudnn function
*/
void prepare_input();
void prepare_input(const CudnnConvolutionWorkload &workload);
/**
* @brief Generate some params used in the cudnn function
* @brief Create descriptors from the convolution workload
*/
void prepare_for_function();
void prepare_descriptors(const CudnnConvolutionWorkload &workload);
/**
* @brief Apply the existing math/algorithm policy and allocate workspace
*/
void prepare_execution(const CudnnExecutionPolicy &policy);
/**
* @brief Get and set convolution algorithm and workspace size used in cudnn convolution functions
*/
Expand Down Expand Up @@ -78,21 +82,28 @@ 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() {
template <typename T1, typename T2>
void CudnnFunction<T1, T2>::prepare_descriptors(const CudnnConvolutionWorkload &workload) {
// Generate descriptor
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());
conv_desc_ = ConvolutionDescriptor<T2>(workload.array_length, workload.padding, workload.filter_stride,
workload.dilation, workload.mode);
x_desc_ = TensorDescriptorNd<T1>(workload.input_dims, workload.input_stride);
w_desc_ = FilterDescriptorNd<T1>(workload.filter_dims);
h_desc_ = TensorDescriptorNd<T1>(workload.output_dims, workload.output_stride);
}

/**
* @brief Apply the existing math/algorithm policy and allocate workspace
*/
template <typename T1, typename T2> void CudnnFunction<T1, T2>::prepare_execution(const CudnnExecutionPolicy &policy) {
// 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_) {
CHECK_CUDNN_ERROR(cudnnSetConvolutionMathType(conv_desc_.desc(), policy.math_type));
if (policy.auto_algo) {
find_best_algo();
} else {
this->algo_ = policy.algorithm;
}
// Set convolution algorithm and workspace size
this->get_workspace_size();
Expand All @@ -101,22 +112,26 @@ template <typename T1, typename T2> void CudnnFunction<T1, T2>::prepare_for_func
/**
* @brief Malloc cuda memory and fill in value for data params used in the cudnn function
*/
template <typename T1, typename T2> void CudnnFunction<T1, T2>::prepare_input() {
template <typename T1, typename T2>
void CudnnFunction<T1, T2>::prepare_input(const CudnnConvolutionWorkload &workload) {
// Allocate memory for filter data
rand<T1>(&filter, get_filter_dims(), random_seed);
rand<T1>(&filter, workload.filter_dims, random_seed);
// Allocate memory for input data
rand<T1>(&x, get_input_dims(), random_seed);
rand<T1>(&x, workload.input_dims, random_seed);
// Allocate memory for output data
rand<T1>(&h, get_output_dims(), random_seed);
rand<T1>(&h, workload.output_dims, random_seed);
}
/**
* @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() {
const CudnnConvolutionWorkload workload(*this);
const CudnnExecutionPolicy policy(*this);
// Prepare some Prerequisites for function running
prepare_for_function();
prepare_descriptors(workload);
prepare_execution(policy);
// Allocate memory and fill with data of input and output tensor
prepare_input();
prepare_input(workload);

// Warm up
for (int i = 0; i < warm_up; ++i) {
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <iostream>
#include <stdexcept>

#include "cudnn_execution_config.h"
Comment thread
gusui-msft marked this conversation as resolved.
Resolved

using namespace cudnn_test;

static void require(bool condition, const char *message) {
if (!condition) {
throw std::runtime_error(message);
}
}

int main() {
try {
CudnnConfig config{};
config.set_input_dims({2, 8, 5, 5});
config.set_input_stride({200, 25, 5, 1});
config.set_filter_dims({16, 8, 3, 3});
config.set_output_dims({2, 16, 3, 3});
config.set_output_stride({144, 9, 3, 1});
config.set_padA({0, 0});
config.set_filter_strideA({1, 1});
config.set_dilationA({1, 1});
config.set_mode(CUDNN_CROSS_CORRELATION);
config.set_array_length(2);
const CudnnConvolutionWorkload workload(config);
config.set_input_dims({1, 1, 1, 1});
config.set_padA({2, 2});
require(workload.input_dims == std::vector<int>{2, 8, 5, 5} && workload.padding == std::vector<int>{0, 0},
"workload changed with its source configuration");
require(workload.input_stride == std::vector<int>{200, 25, 5, 1} &&
workload.filter_dims == std::vector<int>{16, 8, 3, 3} &&
workload.output_dims == std::vector<int>{2, 16, 3, 3} &&
workload.output_stride == std::vector<int>{144, 9, 3, 1} &&
workload.filter_stride == std::vector<int>{1, 1} && workload.dilation == std::vector<int>{1, 1} &&
workload.mode == CUDNN_CROSS_CORRELATION && workload.array_length == 2,
"workload parameters were not preserved");
for (bool tensor_op : {false, true}) {
for (bool automatic : {false, true}) {
config.set_use_tensor_op(tensor_op);
config.set_auto_algo(automatic);
config.set_algo(1);
const CudnnExecutionPolicy policy(config);
config.set_use_tensor_op(!tensor_op);
config.set_auto_algo(!automatic);
config.set_algo(7);
require(policy.math_type == (tensor_op ? CUDNN_TENSOR_OP_MATH : CUDNN_DEFAULT_MATH) &&
policy.auto_algo == automatic && (automatic || policy.algorithm == 1),
"legacy math/algorithm policy was not preserved");
}
}
std::cout << "Convolution workload and legacy execution policy snapshots passed" << std::endl;
} catch (const std::exception &error) {
std::cerr << error.what() << std::endl;
return 1;
}
return 0;
}
Loading