gemm.hpp 4.66 KB
Newer Older
Thomas Ning's avatar
Thomas Ning committed
1
2

// SPDX-License-Identifier: MIT
Jakub Piasecki's avatar
Jakub Piasecki committed
3
// Copyright (c) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
Thomas Ning's avatar
Thomas Ning committed
4
5
6

#pragma once

7
#include <string>
Jakub Piasecki's avatar
Jakub Piasecki committed
8
#include "ck_tile/host.hpp"
Thomas Ning's avatar
Thomas Ning committed
9
#include "ck_tile/host/kernel_launch.hpp"
Jakub Piasecki's avatar
Jakub Piasecki committed
10
11
#include "ck_tile/ops/gemm.hpp"
#include "ck_tile/ops/epilogue.hpp"
Thomas Ning's avatar
Thomas Ning committed
12
13
14
15
16
17
18
19
20
21

template <typename DataType>
struct GemmBasicTypeConfig;

template <>
struct GemmBasicTypeConfig<ck_tile::half_t>
{
    using ADataType   = ck_tile::half_t;
    using BDataType   = ck_tile::half_t;
    using AccDataType = float;
22
    using CDataType   = ck_tile::half_t;
Thomas Ning's avatar
Thomas Ning committed
23
24
25
    // ToDo: Add more bias config to support different categories of GEMM.
};

Jakub Piasecki's avatar
Jakub Piasecki committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
template <>
struct GemmBasicTypeConfig<ck_tile::bf16_t>
{
    using ADataType   = ck_tile::bf16_t;
    using BDataType   = ck_tile::bf16_t;
    using AccDataType = float;
    using CDataType   = ck_tile::bf16_t;
};

template <>
struct GemmBasicTypeConfig<ck_tile::fp8_t>
{
    using ADataType   = ck_tile::fp8_t;
    using BDataType   = ck_tile::fp8_t;
    using AccDataType = float;
Jakub Piasecki's avatar
Jakub Piasecki committed
41
    using CDataType   = ck_tile::fp8_t;
Jakub Piasecki's avatar
Jakub Piasecki committed
42
43
44
45
46
47
48
49
};

template <>
struct GemmBasicTypeConfig<ck_tile::bf8_t>
{
    using ADataType   = ck_tile::bf8_t;
    using BDataType   = ck_tile::bf8_t;
    using AccDataType = float;
Jakub Piasecki's avatar
Jakub Piasecki committed
50
    using CDataType   = ck_tile::bf8_t;
Jakub Piasecki's avatar
Jakub Piasecki committed
51
52
};

Thomas Ning's avatar
Thomas Ning committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
template <typename T>
struct DataTypeTraits;

template <>
struct DataTypeTraits<float>
{
    static constexpr const char* name = "fp32";
};

template <>
struct DataTypeTraits<double>
{
    static constexpr const char* name = "fp64";
};

template <>
struct DataTypeTraits<ck_tile::half_t>
{
    static constexpr const char* name = "fp16";
};

Jakub Piasecki's avatar
Jakub Piasecki committed
74
75
76
77
78
template <>
struct DataTypeTraits<ck_tile::bf16_t>
{
    static constexpr const char* name = "bf16";
};
Thomas Ning's avatar
Thomas Ning committed
79

Jakub Piasecki's avatar
Jakub Piasecki committed
80
81
82
83
84
85
86
87
88
89
90
template <>
struct DataTypeTraits<ck_tile::fp8_t>
{
    static constexpr const char* name = "fp8";
};

template <>
struct DataTypeTraits<ck_tile::bf8_t>
{
    static constexpr const char* name = "bf8";
};
Thomas Ning's avatar
Thomas Ning committed
91

Jakub Piasecki's avatar
Jakub Piasecki committed
92
/** @brief Struct used for specifying desired gemm details*/
Jakub Piasecki's avatar
Jakub Piasecki committed
93
94
struct gemm_traits
{
Jakub Piasecki's avatar
Jakub Piasecki committed
95
96
97
98
    std::string data_type; /** Tensors datatype, can be set to either fp16 or bf16*/
    bool is_a_rowmajor;    /** Whether A matrix is rowmajor */
    bool is_b_rowmajor;    /** Whether B matrix is rowmajor */
    bool is_c_rowmajor;    /** Whether C matrix is rowmajor */
Jakub Piasecki's avatar
Jakub Piasecki committed
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
142
};

template <typename ADataType_,
          typename BDataType_,
          typename AccDataType_,
          typename CDataType_,
          typename ALayout_,
          typename BLayout_,
          typename CLayout_,
          ck_tile::index_t M_Tile_,
          ck_tile::index_t N_Tile_,
          ck_tile::index_t K_Tile_,
          ck_tile::index_t M_Warp_,
          ck_tile::index_t N_Warp_,
          ck_tile::index_t K_Warp_,
          ck_tile::index_t M_Warp_Tile_,
          ck_tile::index_t N_Warp_Tile_,
          ck_tile::index_t K_Warp_Tile_,
          bool kPadM_,
          bool kPadN_,
          bool kPadK_>
struct gemm_traits_
{
    using ADataType                               = ck_tile::remove_cvref_t<ADataType_>;
    using BDataType                               = ck_tile::remove_cvref_t<BDataType_>;
    using AccDataType                             = ck_tile::remove_cvref_t<AccDataType_>;
    using CDataType                               = ck_tile::remove_cvref_t<CDataType_>;
    using ALayout                                 = ck_tile::remove_cvref_t<ALayout_>;
    using BLayout                                 = ck_tile::remove_cvref_t<BLayout_>;
    using CLayout                                 = ck_tile::remove_cvref_t<CLayout_>;
    static constexpr ck_tile::index_t M_Tile      = M_Tile_;
    static constexpr ck_tile::index_t N_Tile      = N_Tile_;
    static constexpr ck_tile::index_t K_Tile      = K_Tile_;
    static constexpr ck_tile::index_t M_Warp      = M_Warp_;
    static constexpr ck_tile::index_t N_Warp      = N_Warp_;
    static constexpr ck_tile::index_t K_Warp      = K_Warp_;
    static constexpr ck_tile::index_t M_Warp_Tile = M_Warp_Tile_;
    static constexpr ck_tile::index_t N_Warp_Tile = N_Warp_Tile_;
    static constexpr ck_tile::index_t K_Warp_Tile = K_Warp_Tile_;
    static constexpr bool kPadM                   = kPadM_;
    static constexpr bool kPadN                   = kPadN_;
    static constexpr bool kPadK                   = kPadK_;
};

Thomas Ning's avatar
Thomas Ning committed
143
// host API
Jakub Piasecki's avatar
Jakub Piasecki committed
144

Jakub Piasecki's avatar
Jakub Piasecki committed
145
146
147
template <typename Traits_>
float gemm_(const ck_tile::GemmHostArgs& args, const ck_tile::stream_config& s);

Jakub Piasecki's avatar
Jakub Piasecki committed
148
/**
Jakub Piasecki's avatar
Jakub Piasecki committed
149
 * @brief Invoke gemm function
Jakub Piasecki's avatar
Jakub Piasecki committed
150
 *
Jakub Piasecki's avatar
Jakub Piasecki committed
151
152
153
154
 * @param traits Gemm traits which are used for choosing best instance.
 * @param args Runtime gemm host arguments.
 * @param s Stream configuration.
 * @return Time of execution.
Jakub Piasecki's avatar
Jakub Piasecki committed
155
 */
Jakub Piasecki's avatar
Jakub Piasecki committed
156
157
158
float gemm(const gemm_traits& traits,
           const ck_tile::GemmHostArgs& args,
           const ck_tile::stream_config& s);