gemm.hpp 3.97 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
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
    // ToDo: Add more bias config to support different categories of GEMM.
};

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";
};

using Types = GemmBasicTypeConfig<ck_tile::half_t>;

// Specific type aliases for easy access
using ADataType   = Types::ADataType;
using BDataType   = Types::BDataType;
using AccDataType = Types::AccDataType;
using CDataType   = Types::CDataType;

Jakub Piasecki's avatar
Jakub Piasecki committed
55
/** \brief Struct used for specifying desired gemm details*/
Jakub Piasecki's avatar
Jakub Piasecki committed
56
57
struct gemm_traits
{
Jakub Piasecki's avatar
Jakub Piasecki committed
58
59
60
61
    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
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
};

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
106
// host API
Jakub Piasecki's avatar
Jakub Piasecki committed
107

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

Jakub Piasecki's avatar
Jakub Piasecki committed
111
112
113
114
115
116
117
118
/**
 * \brief Invoke gemm function
 *
 * \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
119
120
121
float gemm(const gemm_traits& traits,
           const ck_tile::GemmHostArgs& args,
           const ck_tile::stream_config& s);