gemm_util.hpp 8.65 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

Chao Liu's avatar
Chao Liu committed
4
#pragma once
Anthony Chang's avatar
Anthony Chang committed
5

Chao Liu's avatar
Chao Liu committed
6
7
#include "ck/ck.hpp"
#include "ck/tensor_operation/gpu/device/tensor_layout.hpp"
8
9

#include "ck/library/reference_tensor_operation/cpu/reference_gemm.hpp"
Chao Liu's avatar
Chao Liu committed
10
#include "ck/library/utility/check_err.hpp"
11
12
13
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_tensor.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
14
#include "ck/library/utility/literals.hpp"
Anthony Chang's avatar
Anthony Chang committed
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

namespace ck {
namespace gemm_util {

struct GemmParams
{
    GemmParams()
        : M(1024), N(1024), K(1024), StrideA(1024), StrideB(1024), StrideC(1024), alpha(1), beta(0)
    {
    }

    ck::index_t M;
    ck::index_t N;
    ck::index_t K;

    ck::index_t StrideA;
    ck::index_t StrideB;
    ck::index_t StrideC;

    float alpha;
    float beta;
};

template <typename GemmInstance,
          typename ADataType,
          typename BDataType,
          typename CDataType,
          typename AElementwiseOperation,
          typename BElementwiseOperation,
          typename CElementwiseOperation>
void RunHostGEMM(const Tensor<ADataType>& A,
                 const Tensor<BDataType>& B,
                 Tensor<CDataType>& C,
                 AElementwiseOperation a_element_op,
                 BElementwiseOperation b_element_op,
                 CElementwiseOperation c_element_op)
{
    auto ref_gemm    = GemmInstance{};
    auto ref_invoker = ref_gemm.MakeInvoker();

    auto ref_argument = ref_gemm.MakeArgument(A, B, C, a_element_op, b_element_op, c_element_op);

    ref_invoker.Run(ref_argument);
}

template <typename DeviceGemmPtr_,
          typename ADataType,
          typename BDataType,
          typename CDataType,
          typename AElementwiseOperation,
          typename BElementwiseOperation,
          typename CElementwiseOperation>
Jianfeng Yan's avatar
Jianfeng Yan committed
67
bool RunDeviceGEMM(DeviceGemmPtr_& gemmPtr,
Anthony Chang's avatar
Anthony Chang committed
68
69
70
71
72
73
74
75
                   const ck::gemm_util::GemmParams& params,
                   const Tensor<ADataType>& A,
                   const Tensor<BDataType>& B,
                   Tensor<CDataType>& C,
                   AElementwiseOperation a_element_op,
                   BElementwiseOperation b_element_op,
                   CElementwiseOperation c_element_op)
{
76
77
78
    DeviceMem a_m_k_device_buf(A.GetMemorySize());
    DeviceMem b_k_n_device_buf(B.GetMemorySize());
    DeviceMem c_m_n_device_buf(C.GetMemorySize());
Anthony Chang's avatar
Anthony Chang committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

    auto invoker_ptr = gemmPtr->MakeInvokerPointer();
    auto argument_ptr =
        gemmPtr->MakeArgumentPointer(static_cast<ADataType*>(a_m_k_device_buf.GetDeviceBuffer()),
                                     static_cast<BDataType*>(b_k_n_device_buf.GetDeviceBuffer()),
                                     static_cast<CDataType*>(c_m_n_device_buf.GetDeviceBuffer()),
                                     params.M,
                                     params.N,
                                     params.K,
                                     params.StrideA,
                                     params.StrideB,
                                     params.StrideC,
                                     a_element_op,
                                     b_element_op,
                                     c_element_op);

Jianfeng Yan's avatar
Jianfeng Yan committed
95
    if(gemmPtr->IsSupportedArgument(argument_ptr.get()))
Anthony Chang's avatar
Anthony Chang committed
96
    {
97
98
        a_m_k_device_buf.ToDevice(A.data());
        b_k_n_device_buf.ToDevice(B.data());
Jianfeng Yan's avatar
Jianfeng Yan committed
99
        invoker_ptr->Run(argument_ptr.get());
100
        c_m_n_device_buf.FromDevice(C.data());
Jianfeng Yan's avatar
Jianfeng Yan committed
101
102

        return true;
Anthony Chang's avatar
Anthony Chang committed
103
    }
Jianfeng Yan's avatar
Jianfeng Yan committed
104
105
106
107
108
    else
    {
        std::cout << "device_gemm with the specified compilation parameters does "
                     "not support this GEMM problem"
                  << std::endl;
Anthony Chang's avatar
Anthony Chang committed
109

Jianfeng Yan's avatar
Jianfeng Yan committed
110
111
        return false;
    }
Anthony Chang's avatar
Anthony Chang committed
112
113
114
115
116
117
}

template <typename DeviceGemmPtr_,
          typename ADataType,
          typename BDataType,
          typename CDataType,
118
          typename AccDataType,
Anthony Chang's avatar
Anthony Chang committed
119
120
121
122
123
124
125
126
127
128
          typename ALayout,
          typename BLayout,
          typename CLayout,
          typename AElementwiseOperation,
          typename BElementwiseOperation,
          typename CElementwiseOperation>
struct TestGemm
{
    auto PrepareGemmTensor(const ck::gemm_util::GemmParams& params)
    {
129
130
        using namespace ck::literals;

Anthony Chang's avatar
Anthony Chang committed
131
132
        auto f_host_tensor_descriptor =
            [](std::size_t row, std::size_t col, std::size_t stride, auto layout) {
133
                if constexpr(std::is_same_v<decltype(layout), ck::tensor_layout::gemm::RowMajor>)
Anthony Chang's avatar
Anthony Chang committed
134
                {
135
                    return HostTensorDescriptor({row, col}, {stride, 1_uz});
Anthony Chang's avatar
Anthony Chang committed
136
137
138
                }
                else
                {
139
                    return HostTensorDescriptor({row, col}, {1_uz, stride});
Anthony Chang's avatar
Anthony Chang committed
140
141
142
143
144
145
146
147
148
149
150
151
                }
            };

        Tensor<ADataType> a_m_k(
            f_host_tensor_descriptor(params.M, params.K, params.StrideA, ALayout{}));
        Tensor<BDataType> b_k_n(
            f_host_tensor_descriptor(params.K, params.N, params.StrideB, BLayout{}));
        Tensor<CDataType> c_m_n_host_result(
            f_host_tensor_descriptor(params.M, params.N, params.StrideC, CLayout{}));
        Tensor<CDataType> c_m_n_device_result(
            f_host_tensor_descriptor(params.M, params.N, params.StrideC, CLayout{}));

152
        auto f_generate_tensor_value = [](auto& tensor, auto type) {
Anthony Chang's avatar
Anthony Chang committed
153
154
            using dataType = decltype(type);

155
            tensor.GenerateTensorValue(GeneratorTensor_2<dataType>{-5, 5});
Anthony Chang's avatar
Anthony Chang committed
156
157
158
159
160
161
162
163
        };

        f_generate_tensor_value(a_m_k, ADataType{});
        f_generate_tensor_value(b_k_n, BDataType{});

        return std::make_tuple(a_m_k, b_k_n, c_m_n_host_result, c_m_n_device_result);
    }

164
    auto operator()(const DeviceGemmPtr_& gemmPtr)
Anthony Chang's avatar
Anthony Chang committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    {
        std::cout << "ALayout = " << ALayout{}.name << ", BLayout = " << BLayout{}.name
                  << ", CLayout = " << CLayout{}.name << std::endl;
        std::cout << gemmPtr->GetTypeString() << std::endl;

        // Arrange
        ck::gemm_util::GemmParams params;
        params.M       = 1024;
        params.N       = 1024;
        params.K       = 1024;
        params.StrideA = 1024;
        params.StrideB = 1024;
        params.StrideC = 1024;

        auto host_tensors = PrepareGemmTensor(params);

        const Tensor<ADataType>& a  = std::get<0>(host_tensors);
        const Tensor<BDataType>& b  = std::get<1>(host_tensors);
        Tensor<CDataType>& c_host   = std::get<2>(host_tensors);
        Tensor<CDataType>& c_device = std::get<3>(host_tensors);

        auto a_element_op = AElementwiseOperation{};
        auto b_element_op = BElementwiseOperation{};
        auto c_element_op = CElementwiseOperation{};

        using ReferenceGemmInstance =
            ck::tensor_operation::host::ReferenceGemm<ADataType,
                                                      BDataType,
                                                      CDataType,
194
                                                      AccDataType,
Anthony Chang's avatar
Anthony Chang committed
195
196
197
198
199
200
201
                                                      AElementwiseOperation,
                                                      BElementwiseOperation,
                                                      CElementwiseOperation>;
        ck::gemm_util::RunHostGEMM<ReferenceGemmInstance>(
            a, b, c_host, a_element_op, b_element_op, c_element_op);

        // Act
Jianfeng Yan's avatar
Jianfeng Yan committed
202
        bool is_supported = ck::gemm_util::RunDeviceGEMM(
Anthony Chang's avatar
Anthony Chang committed
203
204
            gemmPtr, params, a, b, c_device, a_element_op, b_element_op, c_element_op);

Jianfeng Yan's avatar
Jianfeng Yan committed
205
        if(is_supported)
Anthony Chang's avatar
Anthony Chang committed
206
        {
Jianfeng Yan's avatar
Jianfeng Yan committed
207
208
            // Assert
            bool res = false;
209
            if constexpr(std::is_same_v<CDataType, float>)
Jianfeng Yan's avatar
Jianfeng Yan committed
210
            {
211
                res = ck::utils::check_err(c_device, c_host);
Jianfeng Yan's avatar
Jianfeng Yan committed
212
213
                std::cout << (res ? "SUCCESS" : "FAILURE") << std::endl;
            }
214
            else if constexpr(std::is_same_v<CDataType, ck::half_t>)
Jianfeng Yan's avatar
Jianfeng Yan committed
215
            {
216
                res = ck::utils::check_err(c_device, c_host);
Jianfeng Yan's avatar
Jianfeng Yan committed
217
218
                std::cout << (res ? "SUCCESS" : "FAILURE") << std::endl;
            }
219
            else if constexpr(std::is_same_v<CDataType, ck::bhalf_t>)
Chao Liu's avatar
Chao Liu committed
220
            {
221
                res = ck::utils::check_err(c_device, c_host);
Chao Liu's avatar
Chao Liu committed
222
223
                std::cout << (res ? "SUCCESS" : "FAILURE") << std::endl;
            }
224
            else if constexpr(std::is_same_v<CDataType, int8_t>)
Jianfeng Yan's avatar
Jianfeng Yan committed
225
            {
226
                res = ck::utils::check_err(c_device, c_host);
Jianfeng Yan's avatar
Jianfeng Yan committed
227
228
                std::cout << (res ? "SUCCESS" : "FAILURE") << std::endl;
            }
229
            else if constexpr(std::is_same_v<CDataType, double>)
230
            {
231
                res = ck::utils::check_err(c_device, c_host);
232
233
                std::cout << (res ? "SUCCESS" : "FAILURE") << std::endl;
            }
Jianfeng Yan's avatar
Jianfeng Yan committed
234
235

            return res;
Anthony Chang's avatar
Anthony Chang committed
236
        }
Jianfeng Yan's avatar
Jianfeng Yan committed
237
        else
Anthony Chang's avatar
Anthony Chang committed
238
        {
Jianfeng Yan's avatar
Jianfeng Yan committed
239
            return true;
Anthony Chang's avatar
Anthony Chang committed
240
241
242
243
244
245
        }
    }
};

} // namespace gemm_util
} // namespace ck