-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathpointwise.cpp
More file actions
141 lines (115 loc) · 6.13 KB
/
Copy pathpointwise.cpp
File metadata and controls
141 lines (115 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: MIT
*/
#include <catch2/catch_test_macros.hpp>
#include "../utils/helpers.h"
#include <cudnn_frontend.h>
TEST_CASE("Reduction", "[reduction]") {
namespace fe = cudnn_frontend;
constexpr int n = 64;
if (cudnnGetVersion() < 8600) {
SKIP("TEST REQUIRES minimum cudnn version 8.6.0");
}
Surface<float> A_gpu(n * n * n * n);
fe::graph::Graph graph{};
auto A = graph.tensor(fe::graph::Tensor_attributes()
.set_dim({n, n, n, n})
.set_stride({n * n * n, 1, n * n, n})
.set_data_type(fe::DataType_t::FLOAT));
auto C = graph.reduction(A,
fe::graph::Reduction_attributes()
.set_mode(fe::ReductionMode_t::MAX)
.set_compute_data_type(fe::DataType_t::FLOAT));
C->set_output(true).set_data_type(fe::DataType_t::FLOAT).set_dim({1, 1, 1, 1});
REQUIRE(graph.validate().is_good());
// Create a unique_ptr for the cuDNN handle
auto handle_ptr = create_cudnn_handle();
auto handle = *handle_ptr;
REQUIRE(graph.build_operation_graph(handle).is_good());
REQUIRE(graph.create_execution_plans({fe::HeurMode_t::A}).is_good());
REQUIRE(graph.build_plans(fe::BuildPlanPolicy_t::HEURISTICS_CHOICE).is_good());
Surface<float> C_gpu(n * n * n * n);
std::unordered_map<std::shared_ptr<fe::graph::Tensor_attributes>, void*> variant_pack = {{A, A_gpu.devPtr},
{C, C_gpu.devPtr}};
int64_t workspace_size = 0;
REQUIRE(graph.get_workspace_size(workspace_size).is_good());
Surface<int8_t> workspace(workspace_size);
REQUIRE(graph.execute(handle, variant_pack, workspace.devPtr).is_good());
}
TEST_CASE("Fused scalar", "[scalar][graph]") {
namespace fe = cudnn_frontend;
constexpr int n = 4;
fe::graph::Graph graph{};
auto A = graph.tensor(fe::graph::Tensor_attributes()
.set_dim({n, n, n})
.set_stride({n * n, n, 1})
.set_data_type(fe::DataType_t::HALF));
auto C = graph.pointwise(A,
graph.tensor(5.0f),
fe::graph::Pointwise_attributes()
.set_mode(fe::PointwiseMode_t::ADD)
.set_compute_data_type(fe::DataType_t::FLOAT));
C->set_output(true).set_data_type(fe::DataType_t::HALF);
REQUIRE(graph.validate().is_good());
// Create a unique_ptr for the cuDNN handle
auto handle_ptr = create_cudnn_handle();
auto handle = *handle_ptr;
REQUIRE(graph.build_operation_graph(handle).is_good());
REQUIRE(graph.create_execution_plans({fe::HeurMode_t::A}).is_good());
REQUIRE(graph.build_plans(fe::BuildPlanPolicy_t::HEURISTICS_CHOICE).is_good());
Surface<half> C_gpu(n * n * n);
Surface<half> A_gpu(n * n * n);
std::unordered_map<std::shared_ptr<fe::graph::Tensor_attributes>, void*> variant_pack = {{A, A_gpu.devPtr},
{C, C_gpu.devPtr}};
int64_t workspace_size = 0;
REQUIRE(graph.get_workspace_size(workspace_size).is_good());
Surface<int8_t> workspace(workspace_size);
REQUIRE(graph.execute(handle, variant_pack, workspace.devPtr).is_good());
}
TEST_CASE("Fused Amax Reduction and type conversion", "[reduction]") {
namespace fe = cudnn_frontend;
constexpr int n = 64;
if (cudnnGetVersion() < 8600) {
SKIP("TEST REQUIRES minimum cudnn version 8.6.0");
}
if (check_device_arch_newer_than("hopper") == false) {
SKIP("TEST REQUIRES device hopper arch or newer");
}
fe::graph::Graph graph{};
auto A = graph.tensor(fe::graph::Tensor_attributes()
.set_dim({n, n, n, n})
.set_stride({n * n * n, 1, n * n, n})
.set_data_type(fe::DataType_t::FLOAT));
auto scale = graph.tensor(fe::graph::Tensor_attributes()
.set_dim({1, 1, 1, 1})
.set_stride({1, 1, 1, 1})
.set_data_type(fe::DataType_t::FLOAT));
auto amax = graph.reduction(A,
fe::graph::Reduction_attributes()
.set_mode(fe::ReductionMode_t::AMAX)
.set_compute_data_type(fe::DataType_t::FLOAT));
amax->set_output(true).set_data_type(fe::DataType_t::FLOAT).set_dim({1, 1, 1, 1});
auto scale_options = fe::graph::Pointwise_attributes()
.set_mode(fe::PointwiseMode_t::MUL)
.set_compute_data_type(fe::DataType_t::FLOAT);
auto C = graph.pointwise(A, scale, scale_options);
C->set_output(true).set_data_type(fe::DataType_t::FP8_E4M3);
REQUIRE(graph.validate().is_good());
// Create a unique_ptr for the cuDNN handle
auto handle_ptr = create_cudnn_handle();
auto handle = *handle_ptr;
REQUIRE(graph.build_operation_graph(handle).is_good());
REQUIRE(graph.create_execution_plans({fe::HeurMode_t::A}).is_good());
REQUIRE(graph.build_plans(fe::BuildPlanPolicy_t::HEURISTICS_CHOICE).is_good());
Surface<float> A_gpu(n * n * n * n);
Surface<float> scale_gpu(1);
Surface<float> amax_gpu(1);
Surface<int8_t> C_gpu(n * n * n * n); // Substitute for fp8
std::unordered_map<std::shared_ptr<fe::graph::Tensor_attributes>, void*> variant_pack = {
{A, A_gpu.devPtr}, {scale, scale_gpu.devPtr}, {amax, amax_gpu.devPtr}, {C, C_gpu.devPtr}};
int64_t workspace_size = 0;
REQUIRE(graph.get_workspace_size(workspace_size).is_good());
Surface<int8_t> workspace(workspace_size);
REQUIRE(graph.execute(handle, variant_pack, workspace.devPtr).is_good());
}