-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathfp8_matmul.cpp
More file actions
114 lines (89 loc) · 4.31 KB
/
Copy pathfp8_matmul.cpp
File metadata and controls
114 lines (89 loc) · 4.31 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: MIT
*/
#include <catch2/catch_test_macros.hpp>
#include <random>
#include "../utils/helpers.h"
#include <cudnn_frontend.h>
TEST_CASE("Matmul fp8 precision", "[matmul][graph]") {
if (cudnnGetCudartVersion() < 12000) {
SKIP("Test requires cuda toolkit 12.0 or above");
}
if ((is_hopper_arch() && cudnnGetVersion() >= 90000) == false) {
SKIP("FP8 gemm not supported pre-Hopper or pre-cudnn-9.0.0");
}
namespace fe = cudnn_frontend;
// matmul problem size
int64_t const b = 16;
int64_t const m = 32;
int64_t const n = 64;
int64_t const k = 128;
// Initialize input tensors with int8_t as proxy for fp8
Surface<int8_t> A_gpu(b * m * k);
Surface<int8_t> B_gpu(b * k * n);
Surface<float> A_descale_gpu(1);
Surface<float> B_descale_gpu(1);
fe::graph::Graph graph{};
// Create the two non-virtual input tensors A and B.
// There are read from global memory.
auto A_attributes = fe::graph::Tensor_attributes()
.set_name("A")
.set_dim({b, m, k})
.set_stride({m * k, k, 1})
.set_data_type(fe::DataType_t::FP8_E4M3);
auto A = graph.tensor(A_attributes);
auto B_attributes = fe::graph::Tensor_attributes()
.set_name("B")
.set_dim({b, k, n})
.set_stride({k * n, 1, k})
.set_data_type(fe::DataType_t::FP8_E4M3);
auto B = graph.tensor(B_attributes);
auto A_descale_attributes =
fe::graph::Tensor_attributes().set_name("A").set_dim({1, 1, 1}).set_stride({1, 1, 1}).set_data_type(
fe::DataType_t::FLOAT);
auto B_descale_attributes =
fe::graph::Tensor_attributes().set_name("B").set_dim({1, 1, 1}).set_stride({1, 1, 1}).set_data_type(
fe::DataType_t::FLOAT);
auto A_descale = graph.tensor(A_descale_attributes);
auto B_descale = graph.tensor(B_descale_attributes);
auto matmul_attributes =
// fe::graph::Matmul_attributes().set_name("GEMM").set_compute_data_type(fe::DataType_t::FLOAT);
fe::graph::Matmul_attributes().set_name("GEMM").set_compute_data_type(fe::DataType_t::FLOAT);
auto C = graph.matmul(A, B, matmul_attributes);
C->set_data_type(fe::DataType_t::FLOAT);
// Add scale_A operation
auto pw_0_attributes = fe::graph::Pointwise_attributes()
// .set_name("pw0_Mul")
.set_mode(fe::PointwiseMode_t::MUL)
.set_compute_data_type(fe::DataType_t::FLOAT);
auto C_after_pw_0 = graph.pointwise(C, A_descale, pw_0_attributes);
C_after_pw_0->set_data_type(fe::DataType_t::FLOAT);
// Add descale_B operation
auto pw_1_attributes = fe::graph::Pointwise_attributes()
// .set_name("pw1_Mul")
.set_mode(fe::PointwiseMode_t::MUL)
.set_compute_data_type(fe::DataType_t::FLOAT);
auto C_after_pw_1 = graph.pointwise(C_after_pw_0, B_descale, pw_1_attributes);
C_after_pw_1->set_output(true).set_data_type(fe::DataType_t::BFLOAT16);
std::cout << graph << std::endl;
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.check_support(handle).is_good());
REQUIRE(graph.build_plans(handle, fe::BuildPlanPolicy_t::HEURISTICS_CHOICE).is_good());
Surface<float> C_gpu(b * m * n);
int64_t workspace_size = 0;
REQUIRE(graph.get_workspace_size(workspace_size).is_good());
Surface<int8_t> workspace(workspace_size);
std::unordered_map<std::shared_ptr<fe::graph::Tensor_attributes>, void*> variant_pack = {
{A, A_gpu.devPtr},
{B, B_gpu.devPtr},
{C_after_pw_1, C_gpu.devPtr},
{A_descale, A_descale_gpu.devPtr},
{B_descale, B_descale_gpu.devPtr}};
REQUIRE(graph.execute(handle, variant_pack, workspace.devPtr).is_good());
}