profile_contraction_scale.cpp 10.6 KB
Newer Older
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2023-2024, Advanced Micro Devices, Inc. All rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
#include <numeric>
#include <initializer_list>
#include <cstdlib>
#include <vector>

#include "profiler/profile_contraction_impl.hpp"
#include "profiler/profile_contraction_utils.hpp"
#include "profiler_operation_registry.hpp"

#define OP_NAME "contraction_scale"
#define OP_DESC "CONTRACTION+Scale"

static void print_helper_msg()
{
    std::cout << "arg1: tensor operation (" OP_NAME ": " OP_DESC ")\n"
20
21
              << "arg2: data type (0: fp32; 1: f64; 2: f16; 3: bf16)\n"
              << "arg3: compute data type (0: fp32; 1: f64; 2: f16; 3: bf16)\n"
22
23
              << "arg4: Number of dimension for M, N and K (one for all)\n"
              << "arg5: matrix layout (0: A[m0, m1, k0, k1] * B[k0, k1, n0, n1] + "
24
25
26
27
28
29
30
                 "D[m0, m1, n0, n1] = E[m0, m1, n0, n1];\n"
              << "                     1: A[m0, m1, k0, k1] * B[n0, n1, k0, k1] + "
                 "D[m0, m1, n0, n1] = E[m0, m1, n0, n1];\n"
              << "                     2: A[k0, k1, m0, m1] * B[k0, k1, n0, n1] + "
                 "D[m0, m1, n0, n1] = E[m0, m1, n0, n1];\n"
              << "                     3: A[k0, k1, m0, m1] * B[n0, n1, k0, k1] + "
                 "D[m0, m1, n0, n1] = E[m0, m1, n0, n1])\n"
31
32
              << "arg6: verification (0: no; 1: yes)\n"
              << "arg7: initialization (0: no init; 1: integer value; 2: decimal "
33
              << "value)\n"
34
35
36
37
38
              << "arg8: print tensor value (0: no; 1: yes)\n"
              << "arg9: time kernel (0: no, 1: yes)\n"
              << "arg10: alpha\n"
              << "arg11 to 16/28: M0, M1, N0, N1, K0, K1\n"
              << "arg17/29 to 32/63: Strides for A, B, E (skip for default)\n"
39
40
41
42
43
              << std::endl;
}

int profile_contraction_scale(int argc, char* argv[])
{
44
    const bool default_strides = argc == 17 || argc == 29;
45

46
    if(argc != 29 && argc != 65 && !default_strides)
47
48
49
50
51
52
    {
        print_helper_msg();
        exit(1);
    }

    const auto data_type          = static_cast<ContractionDataType>(std::stoi(argv[2]));
53
    const auto compute_data_type  = static_cast<ContractionComputeDataType>(std::stoi(argv[3]));
54
55
56
57
58
59
60
    const ck::index_t NumDimMNK   = std::stoi(argv[4]);
    const auto layout             = static_cast<ContractionMatrixLayout>(std::stoi(argv[5]));
    const bool do_verification    = std::stoi(argv[6]);
    const ck::index_t init_method = std::stoi(argv[7]);
    const bool do_log             = std::stoi(argv[8]);
    const bool time_kernel        = std::stoi(argv[9]);
    const float alpha             = std::stof(argv[10]);
61
62
63
64

    std::vector<ck::index_t> M;
    std::vector<ck::index_t> N;
    std::vector<ck::index_t> K;
65
66
67
68
69
70
71
72
    const ck::index_t dims_arg_num = 11;
    collect_index_params(argv, M, dims_arg_num, NumDimMNK);
    collect_index_params(argv, N, dims_arg_num + NumDimMNK, NumDimMNK);
    collect_index_params(argv, K, dims_arg_num + NumDimMNK * 2, NumDimMNK);

    std::vector<ck::index_t> StridesA(NumDimMNK * 2);
    std::vector<ck::index_t> StridesB(NumDimMNK * 2);
    std::vector<ck::index_t> StridesE(NumDimMNK * 2);
73
74
    if(!default_strides)
    {
75
76
77
        collect_index_params(argv, StridesA, dims_arg_num + NumDimMNK * 3, NumDimMNK * 2);
        collect_index_params(argv, StridesB, dims_arg_num + NumDimMNK * 5, NumDimMNK * 2);
        collect_index_params(argv, StridesE, dims_arg_num + NumDimMNK * 7, NumDimMNK * 2);
78
79
    }

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    using F16  = ck::half_t;
    using BF16 = ck::bhalf_t;
    using F32  = float;
    using F64  = double;

    auto profile =
        [&](auto a_layout, auto b_layout, auto cde_layout, auto type, auto compute_type) {
            using ALayout   = decltype(a_layout);
            using BLayout   = decltype(b_layout);
            using CDELayout = decltype(cde_layout);

            using DataType        = decltype(type);
            using ComputeDataType = decltype(compute_type);

            if(default_strides)
            {
96
97
98
99
100
101
102
103
104
105
                auto merge_dims = [](const std::vector<ck::index_t>& dims01,
                                     const std::vector<ck::index_t>& dims23) {
                    std::vector<ck::index_t> dims_szt(dims01.begin(), dims01.end());
                    dims_szt.insert(dims_szt.end(), dims23.begin(), dims23.end());
                    return dims_szt;
                };

                assign_default_strides(a_layout, StridesA, merge_dims(M, K));
                assign_default_strides(b_layout, StridesB, merge_dims(N, K));
                assign_default_strides(cde_layout, StridesE, merge_dims(M, N));
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
            if(NumDimMNK == 2)
            {
                bool pass = ck::profiler::profile_contraction_impl<2,
                                                                   ALayout,
                                                                   BLayout,
                                                                   CDELayout,
                                                                   DataType,
                                                                   ComputeDataType,
                                                                   ck::Tuple<>,
                                                                   Scale>(do_verification,
                                                                          init_method,
                                                                          do_log,
                                                                          time_kernel,
                                                                          Scale{alpha},
                                                                          M,
                                                                          N,
                                                                          K,
                                                                          StridesA,
                                                                          StridesB,
                                                                          StridesE,
                                                                          StridesE);

                return pass;
            }
            else if(NumDimMNK == 6)
            {
                bool pass = ck::profiler::profile_contraction_impl<6,
                                                                   ALayout,
                                                                   BLayout,
                                                                   CDELayout,
                                                                   DataType,
                                                                   ComputeDataType,
                                                                   ck::Tuple<>,
                                                                   Scale>(do_verification,
                                                                          init_method,
                                                                          do_log,
                                                                          time_kernel,
                                                                          Scale{alpha},
                                                                          M,
                                                                          N,
                                                                          K,
                                                                          StridesA,
                                                                          StridesB,
                                                                          StridesE,
                                                                          StridesE);

                return pass;
            }
            else
            {
                throw std::runtime_error("Not supported NumDimMNK");
                return false;
            }
161
162
163
164
        };

    auto run_profile_for_datatype = [&](auto type, auto compute_type) {
        if(layout == ContractionMatrixLayout::MK_KN_MN_MN)
165
        {
166
            return profile(Row{}, Row{}, Row{}, type, compute_type);
167
        }
168
169
170
171
172
173
174
175
176
177
178
179
180
        else if(layout == ContractionMatrixLayout::MK_NK_MN_MN)
        {
            return profile(Row{}, Col{}, Row{}, type, compute_type);
        }
        else if(layout == ContractionMatrixLayout::KM_KN_MN_MN)
        {
            return profile(Col{}, Row{}, Row{}, type, compute_type);
        }
        else if(layout == ContractionMatrixLayout::KM_NK_MN_MN)
        {
            return profile(Col{}, Col{}, Row{}, type, compute_type);
        }
        return false;
181
182
    };

183
    if(data_type == ContractionDataType::F32_F32_F32_F32)
184
    {
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
        if(compute_data_type == ContractionComputeDataType::F32)
        {
            return run_profile_for_datatype(F32{}, F32{});
        }
        else if(compute_data_type == ContractionComputeDataType::F16)
        {
            return run_profile_for_datatype(F32{}, F16{});
        }
        else if(compute_data_type == ContractionComputeDataType::BF16)
        {
            return run_profile_for_datatype(F32{}, BF16{});
        }
        else
        {
            std::cout << "Incorrect combination of data type and compute data type." << std::endl;
            return 1;
        }
202
    }
203
    else if(data_type == ContractionDataType::F64_F64_F64_F64)
204
    {
205
206
207
208
209
210
211
212
213
214
215
216
217
        if(compute_data_type == ContractionComputeDataType::F64)
        {
            return run_profile_for_datatype(F64{}, F64{});
        }
        else if(compute_data_type == ContractionComputeDataType::F32)
        {
            return run_profile_for_datatype(F64{}, F32{});
        }
        else
        {
            std::cout << "Incorrect combination of data type and compute data type." << std::endl;
            return 1;
        }
218
    }
219
    else if(data_type == ContractionDataType::F16_F16_F16_F16)
220
    {
221
222
223
224
225
226
227
228
229
        if(compute_data_type == ContractionComputeDataType::F32)
        {
            return run_profile_for_datatype(F16{}, F32{});
        }
        else
        {
            std::cout << "Incorrect combination of data type and compute data type." << std::endl;
            return 1;
        }
230
    }
231
    else if(data_type == ContractionDataType::BF16_BF16_BF16_BF16)
232
    {
233
234
235
236
237
238
239
240
241
        if(compute_data_type == ContractionComputeDataType::F32)
        {
            return run_profile_for_datatype(BF16{}, F32{});
        }
        else
        {
            std::cout << "Incorrect combination of data type and compute data type." << std::endl;
            return 1;
        }
242
    }
243
    return 1;
244
245
246
}

REGISTER_PROFILER_OPERATION(OP_NAME, OP_DESC, profile_contraction_scale);