diff --git a/.azure-pipelines/cuda-unit-test.yml b/.azure-pipelines/cuda-unit-test.yml index be06491e8..63bb65674 100644 --- a/.azure-pipelines/cuda-unit-test.yml +++ b/.azure-pipelines/cuda-unit-test.yml @@ -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 diff --git a/superbench/benchmarks/micro_benchmarks/cudnn_function/CMakeLists.txt b/superbench/benchmarks/micro_benchmarks/cudnn_function/CMakeLists.txt index 00ab0e470..beefffc2e 100644 --- a/superbench/benchmarks/micro_benchmarks/cudnn_function/CMakeLists.txt +++ b/superbench/benchmarks/micro_benchmarks/cudnn_function/CMakeLists.txt @@ -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) @@ -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() diff --git a/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_execution_config.h b/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_execution_config.h new file mode 100644 index 000000000..e06c2451d --- /dev/null +++ b/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_execution_config.h @@ -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 input_dims, input_stride, filter_dims, output_dims, output_stride; + const std::vector 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 diff --git a/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_function.h b/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_function.h index f23649f4a..674342dd8 100644 --- a/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_function.h +++ b/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_function.h @@ -7,7 +7,7 @@ #include #include -#include "cudnn_config.h" +#include "cudnn_execution_config.h" namespace cudnn_test { /** @@ -32,11 +32,15 @@ template 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 */ @@ -78,21 +82,28 @@ template class CudnnFunction : public CudnnConfig { }; /** - * @brief Generate some params used in the cudnn function + * @brief Create descriptors from the convolution workload */ -template void CudnnFunction::prepare_for_function() { +template +void CudnnFunction::prepare_descriptors(const CudnnConvolutionWorkload &workload) { // Generate descriptor - conv_desc_ = - ConvolutionDescriptor(get_array_length(), get_padA(), get_filter_strideA(), get_dilationA(), get_mode()); - x_desc_ = TensorDescriptorNd(get_input_dims(), get_input_stride()); - w_desc_ = FilterDescriptorNd(get_filter_dims()); - h_desc_ = TensorDescriptorNd(get_output_dims(), get_output_stride()); + conv_desc_ = ConvolutionDescriptor(workload.array_length, workload.padding, workload.filter_stride, + workload.dilation, workload.mode); + x_desc_ = TensorDescriptorNd(workload.input_dims, workload.input_stride); + w_desc_ = FilterDescriptorNd(workload.filter_dims); + h_desc_ = TensorDescriptorNd(workload.output_dims, workload.output_stride); +} +/** + * @brief Apply the existing math/algorithm policy and allocate workspace + */ +template void CudnnFunction::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(); @@ -101,22 +112,26 @@ template void CudnnFunction::prepare_for_func /** * @brief Malloc cuda memory and fill in value for data params used in the cudnn function */ -template void CudnnFunction::prepare_input() { +template +void CudnnFunction::prepare_input(const CudnnConvolutionWorkload &workload) { // Allocate memory for filter data - rand(&filter, get_filter_dims(), random_seed); + rand(&filter, workload.filter_dims, random_seed); // Allocate memory for input data - rand(&x, get_input_dims(), random_seed); + rand(&x, workload.input_dims, random_seed); // Allocate memory for output data - rand(&h, get_output_dims(), random_seed); + rand(&h, workload.output_dims, random_seed); } /** * @brief The main procedure for cudnn function test, including warmup, function test and time measurement */ template void CudnnFunction::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) { diff --git a/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_helper.h b/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_helper.h index 7047adbb4..b9844c07c 100644 --- a/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_helper.h +++ b/superbench/benchmarks/micro_benchmarks/cudnn_function/cudnn_helper.h @@ -7,8 +7,10 @@ #pragma once +#include #include #include +#include #include #include #include diff --git a/tests/benchmarks/micro_benchmarks/test_cudnn_execution_config.cpp b/tests/benchmarks/micro_benchmarks/test_cudnn_execution_config.cpp new file mode 100644 index 000000000..dc6ab82a7 --- /dev/null +++ b/tests/benchmarks/micro_benchmarks/test_cudnn_execution_config.cpp @@ -0,0 +1,62 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#include +#include + +#include "cudnn_execution_config.h" + +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{2, 8, 5, 5} && workload.padding == std::vector{0, 0}, + "workload changed with its source configuration"); + require(workload.input_stride == std::vector{200, 25, 5, 1} && + workload.filter_dims == std::vector{16, 8, 3, 3} && + workload.output_dims == std::vector{2, 16, 3, 3} && + workload.output_stride == std::vector{144, 9, 3, 1} && + workload.filter_stride == std::vector{1, 1} && workload.dilation == std::vector{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; +}