contraction_xdl_fp32.cpp 27.5 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
#include <iostream>
2
#include <fstream>
Chao Liu's avatar
Chao Liu committed
3
4
5
6
7
8
9
10
11
12
#include <numeric>
#include <initializer_list>
#include <cstdlib>
#include <stdlib.h>
#include "check_err.hpp"
#include "config.hpp"
#include "device.hpp"
#include "host_tensor.hpp"
#include "host_tensor_generator.hpp"
#include "device_tensor.hpp"
Chao Liu's avatar
Chao Liu committed
13
#include "device_contraction_xdl_cshuffle.hpp"
Chao Liu's avatar
Chao Liu committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "element_wise_operation.hpp"
#include "reference_gemm.hpp"
#include "gemm_specialization.hpp"

template <ck::index_t... Is>
using S = ck::Sequence<Is...>;

using F32 = float;

using Row = ck::tensor_layout::gemm::RowMajor;
using Col = ck::tensor_layout::gemm::ColumnMajor;

using PassThrough = ck::tensor_operation::element_wise::PassThrough;

using ADataType   = float;
using BDataType   = float;
using CDataType   = float;
using AccDataType = float;

Chao Liu's avatar
Chao Liu committed
33
34
35
static constexpr ck::index_t NumDimM = 2;
static constexpr ck::index_t NumDimN = 2;
static constexpr ck::index_t NumDimK = 2;
Chao Liu's avatar
Chao Liu committed
36
37
38
39
40

using AElementOp = ck::tensor_operation::element_wise::PassThrough;
using BElementOp = ck::tensor_operation::element_wise::PassThrough;
using CElementOp = ck::tensor_operation::element_wise::PassThrough;

41
42
//static constexpr auto GemmDefault = ck::tensor_operation::device::GemmSpecialization::Default;
static constexpr auto GemmDefault = ck::tensor_operation::device::GemmSpecialization::MNKPadding;
Chao Liu's avatar
Chao Liu committed
43

Chao Liu's avatar
Chao Liu committed
44
45
46
47
48
49
50

// (M0 * M1 ...) % MPerBlock == 0
// (N0 * N1 ...) % NPerBlock == 0
// (K0 * K1 ...) % KPerBlock == 0
//
//
//
Chao Liu's avatar
Chao Liu committed
51
// clang-format off
Chao Liu's avatar
Chao Liu committed
52
53
// Fast changing dimension in A/B/C are K/N/N dimensions
using ContractionInstanceKNN = ck::tensor_operation::device::
Chao Liu's avatar
Chao Liu committed
54
55
56
57
        //############################| NumDimM| NumDimN| NumDimK| AData| BData| CData| AccData| CShuffle|           A|           B|           C|           GEMM| NumGemmK| Block|  MPer|  NPer|  KPer| AK1| BK1| MPer| NPer| MXdl| NXdl|  ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockLds|  BBlockTransfer| BBlockTransfer| BBlockTransfer| BlockTransfer| BBlockTransfer| BBlockTransfer| BBlockLds|    CShuffle|    CShuffle| CBlockTransferClusterLengths|  CBlockTransfer|
        //############################|        |        |        |  Type|  Type|  Type|    Type| DataType| Elementwise| Elementwise| Elementwise| Spacialization| Prefetch|  Size| Block| Block| Block|    |    |  XDL|  XDL|  Per|  Per|   ThreadCluster|  ThreadCluster| SrcAccessOrder|   SrcVectorDim|      SrcScalar|      DstScalar| AddExtraM|   ThreadCluster|  ThreadCluster| SrcAccessOrder|  SrcVectorDim|      SrcScalar|      DstScalar| AddExtraN| MXdlPerWave| NXdlPerWave|         _MBlock_MWaveMPerXdl| ScalarPerVector|
        //############################|        |        |        |      |      |      |        |         |   Operation|   Operation|   Operation|               |    Stage|      |      |      |      |    |    |     |     | Wave| Wave| Lengths_K0_M_K1|   ArrangeOrder|               |               |      PerVector|   PerVector_K1|          | Lengths_K0_N_K1|   ArrangeOrder|               |              |      PerVector|   PerVector_K1|          |  PerShuffle|  PerShuffle|         _NBlock_NWaveNPerXdl|   _NWaveNPerXdl|
        //############################|        |        |        |      |      |      |        |         |            |            |            |               |         |      |      |      |      |    |    |     |     |     |     |                |               |               |               |               |               |          |                |               |               |              |               |               |          |            |            |                             |                |
Chao Liu's avatar
Chao Liu committed
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
        DeviceContraction_Xdl_CShuffle< NumDimM, NumDimN, NumDimK,   F32,   F32,   F32,     F32,      F32, PassThrough, PassThrough, PassThrough,    GemmDefault,        1,   256,   256,   128,    16,   4,   1,   32,   32,    4,    2,     S<4, 64, 1>,     S<1, 0, 2>,     S<1, 0, 2>,              2,              4,              4,         1,     S<8, 32, 1>,     S<0, 2, 1>,     S<0, 2, 1>,             1,              4,              1,         0,           1,           1,              S<1, 16, 1, 16>,               4>;

// Fast changing dimension in A/B/C are K/K/N dimensions
using ContractionInstanceKKN = ck::tensor_operation::device::
        //############################| NumDimM| NumDimN| NumDimK| AData| BData| CData| AccData| CShuffle|           A|           B|           C|           GEMM| NumGemmK| Block|  MPer|  NPer|  KPer| AK1| BK1| MPer| NPer| MXdl| NXdl|  ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockLds|  BBlockTransfer| BBlockTransfer| BBlockTransfer| BlockTransfer| BBlockTransfer| BBlockTransfer| BBlockLds|    CShuffle|    CShuffle| CBlockTransferClusterLengths|  CBlockTransfer|
        //############################|        |        |        |  Type|  Type|  Type|    Type| DataType| Elementwise| Elementwise| Elementwise| Spacialization| Prefetch|  Size| Block| Block| Block|    |    |  XDL|  XDL|  Per|  Per|   ThreadCluster|  ThreadCluster| SrcAccessOrder|   SrcVectorDim|      SrcScalar|      DstScalar| AddExtraM|   ThreadCluster|  ThreadCluster| SrcAccessOrder|  SrcVectorDim|      SrcScalar|      DstScalar| AddExtraN| MXdlPerWave| NXdlPerWave|         _MBlock_MWaveMPerXdl| ScalarPerVector|
        //############################|        |        |        |      |      |      |        |         |   Operation|   Operation|   Operation|               |    Stage|      |      |      |      |    |    |     |     | Wave| Wave| Lengths_K0_M_K1|   ArrangeOrder|               |               |      PerVector|   PerVector_K1|          | Lengths_K0_N_K1|   ArrangeOrder|               |              |      PerVector|   PerVector_K1|          |  PerShuffle|  PerShuffle|         _NBlock_NWaveNPerXdl|   _NWaveNPerXdl|
        //############################|        |        |        |      |      |      |        |         |            |            |            |               |         |      |      |      |      |    |    |     |     |     |     |                |               |               |               |               |               |          |                |               |               |              |               |               |          |            |            |                             |                |
        DeviceContraction_Xdl_CShuffle< NumDimM, NumDimN, NumDimK,   F32,   F32,   F32,     F32,      F32, PassThrough, PassThrough, PassThrough,    GemmDefault,        1,   256,   256,   128,    16,   4,   4,   32,   32,    4,    2,     S<4, 64, 1>,     S<1, 0, 2>,     S<1, 0, 2>,              2,              4,              4,         1,     S<4, 64, 1>,     S<1, 0, 2>,     S<1, 0, 2>,             2,              4,              4,         1,           1,           1,              S<1, 16, 1, 16>,               4>;

// Fast changing dimension in A/B/C are M/N/N dimensions
using ContractionInstanceMNN = ck::tensor_operation::device::
        //############################| NumDimM| NumDimN| NumDimK| AData| BData| CData| AccData| CShuffle|           A|           B|           C|           GEMM| NumGemmK| Block|  MPer|  NPer|  KPer| AK1| BK1| MPer| NPer| MXdl| NXdl|  ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockLds|  BBlockTransfer| BBlockTransfer| BBlockTransfer| BlockTransfer| BBlockTransfer| BBlockTransfer| BBlockLds|    CShuffle|    CShuffle| CBlockTransferClusterLengths|  CBlockTransfer|
        //############################|        |        |        |  Type|  Type|  Type|    Type| DataType| Elementwise| Elementwise| Elementwise| Spacialization| Prefetch|  Size| Block| Block| Block|    |    |  XDL|  XDL|  Per|  Per|   ThreadCluster|  ThreadCluster| SrcAccessOrder|   SrcVectorDim|      SrcScalar|      DstScalar| AddExtraM|   ThreadCluster|  ThreadCluster| SrcAccessOrder|  SrcVectorDim|      SrcScalar|      DstScalar| AddExtraN| MXdlPerWave| NXdlPerWave|         _MBlock_MWaveMPerXdl| ScalarPerVector|
        //############################|        |        |        |      |      |      |        |         |   Operation|   Operation|   Operation|               |    Stage|      |      |      |      |    |    |     |     | Wave| Wave| Lengths_K0_M_K1|   ArrangeOrder|               |               |      PerVector|   PerVector_K1|          | Lengths_K0_N_K1|   ArrangeOrder|               |              |      PerVector|   PerVector_K1|          |  PerShuffle|  PerShuffle|         _NBlock_NWaveNPerXdl|   _NWaveNPerXdl|
        //############################|        |        |        |      |      |      |        |         |            |            |            |               |         |      |      |      |      |    |    |     |     |     |     |                |               |               |               |               |               |          |                |               |               |              |               |               |          |            |            |                             |                |
        DeviceContraction_Xdl_CShuffle< NumDimM, NumDimN, NumDimK,   F32,   F32,   F32,     F32,      F32, PassThrough, PassThrough, PassThrough,    GemmDefault,        1,   256,   256,   128,    16,   1,   1,   32,   32,    4,    2,     S<4, 64, 1>,     S<0, 2, 1>,     S<0, 2, 1>,              1,              4,              1,         0,     S<8, 32, 1>,     S<0, 2, 1>,     S<0, 2, 1>,             1,              4,              1,         0,           1,           1,              S<1, 16, 1, 16>,               4>;

// Fast changing dimension in A/B/C are M/K/N dimensions
using ContractionInstanceMKN = ck::tensor_operation::device::
        //############################| NumDimM| NumDimN| NumDimK| AData| BData| CData| AccData| CShuffle|           A|           B|           C|           GEMM| NumGemmK| Block|  MPer|  NPer|  KPer| AK1| BK1| MPer| NPer| MXdl| NXdl|  ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockTransfer| ABlockLds|  BBlockTransfer| BBlockTransfer| BBlockTransfer| BlockTransfer| BBlockTransfer| BBlockTransfer| BBlockLds|    CShuffle|    CShuffle| CBlockTransferClusterLengths|  CBlockTransfer|
        //############################|        |        |        |  Type|  Type|  Type|    Type| DataType| Elementwise| Elementwise| Elementwise| Spacialization| Prefetch|  Size| Block| Block| Block|    |    |  XDL|  XDL|  Per|  Per|   ThreadCluster|  ThreadCluster| SrcAccessOrder|   SrcVectorDim|      SrcScalar|      DstScalar| AddExtraM|   ThreadCluster|  ThreadCluster| SrcAccessOrder|  SrcVectorDim|      SrcScalar|      DstScalar| AddExtraN| MXdlPerWave| NXdlPerWave|         _MBlock_MWaveMPerXdl| ScalarPerVector|
        //############################|        |        |        |      |      |      |        |         |   Operation|   Operation|   Operation|               |    Stage|      |      |      |      |    |    |     |     | Wave| Wave| Lengths_K0_M_K1|   ArrangeOrder|               |               |      PerVector|   PerVector_K1|          | Lengths_K0_N_K1|   ArrangeOrder|               |              |      PerVector|   PerVector_K1|          |  PerShuffle|  PerShuffle|         _NBlock_NWaveNPerXdl|   _NWaveNPerXdl|
        //############################|        |        |        |      |      |      |        |         |            |            |            |               |         |      |      |      |      |    |    |     |     |     |     |                |               |               |               |               |               |          |                |               |               |              |               |               |          |            |            |                             |                |
        DeviceContraction_Xdl_CShuffle< NumDimM, NumDimN, NumDimK,   F32,   F32,   F32,     F32,      F32, PassThrough, PassThrough, PassThrough,    GemmDefault,        1,   256,   256,   128,    16,   1,   4,   32,   32,    4,    2,     S<4, 64, 1>,     S<0, 2, 1>,     S<0, 2, 1>,              1,              4,              1,         0,     S<4, 64, 1>,     S<1, 0, 2>,     S<1, 0, 2>,             2,              4,              4,         1,           1,           1,               S<1, 16, 1, 16>,              4>;
Chao Liu's avatar
Chao Liu committed
83
84
// clang-format on

Chao Liu's avatar
Chao Liu committed
85
using ContractionInstance = ContractionInstanceKKN;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

template <typename T, typename Range>
void LogRangeToFile(std::ofstream& fs, Range&& range, std::string delim)
{
    bool first = true;
    for(auto&& v : range)
    {
        if(first)
            first = false;
        else
            fs << delim;
        fs << static_cast<T>(v);
    }
    return;
}


Chao Liu's avatar
Chao Liu committed
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// hardcoded for NumDimM == NumDimN == NumDimK == 2
template <ck::index_t NumDimM,
          ck::index_t NumDimN,
          ck::index_t NumDimK,
          typename ADataType,
          typename BDataType,
          typename CDataType,
          typename AccDataType,
          typename AElementwiseOperation,
          typename BElementwiseOperation,
          typename CElementwiseOperation,
          ck::enable_if_t<NumDimM == 2 && NumDimN == 2 && NumDimK == 2, bool> = false>
struct ReferenceContraction_M2_N2_K2 : public ck::tensor_operation::device::BaseOperator
{
    // Argument
    struct Argument : public ck::tensor_operation::device::BaseArgument
    {
        Argument(const Tensor<ADataType>& a_ms_ks,
                 const Tensor<BDataType>& b_ks_ns,
                 Tensor<CDataType>& c_ms_ns,
                 AElementwiseOperation a_element_op,
                 BElementwiseOperation b_element_op,
                 CElementwiseOperation c_element_op)
            : a_ms_ks_{a_ms_ks},
              b_ks_ns_{b_ks_ns},
              c_ms_ns_{c_ms_ns},
              a_element_op_{a_element_op},
              b_element_op_{b_element_op},
              c_element_op_{c_element_op}
        {
        }

        const Tensor<ADataType>& a_ms_ks_;
        const Tensor<BDataType>& b_ks_ns_;
        Tensor<CDataType>& c_ms_ns_;

        AElementwiseOperation a_element_op_;
        BElementwiseOperation b_element_op_;
        CElementwiseOperation c_element_op_;
    };

    // Invoker
    struct Invoker : public ck::tensor_operation::device::BaseInvoker
    {
        using Argument = ReferenceContraction_M2_N2_K2::Argument;

        float Run(const Argument& arg)
        {
            auto f_ms_ns = [&](auto m0, auto m1, auto n0, auto n1) {
                const int K0 = arg.a_ms_ks_.mDesc.GetLengths()[2];
                const int K1 = arg.a_ms_ks_.mDesc.GetLengths()[3];

                AccDataType v_acc = 0;

                for(int k0 = 0; k0 < K0; ++k0)
                {
                    for(int k1 = 0; k1 < K1; ++k1)
                    {
                        AccDataType v_a;
                        AccDataType v_b;

                        arg.a_element_op_(
                            v_a, static_cast<const AccDataType>(arg.a_ms_ks_(m0, m1, k0, k1)));
                        arg.b_element_op_(
                            v_b, static_cast<const AccDataType>(arg.b_ks_ns_(k0, k1, n0, n1)));

                        v_acc += v_a * v_b;
                    }
                }

                AccDataType v_c;

                arg.c_element_op_(v_c, v_acc);

                arg.c_ms_ns_(m0, m1, n0, n1) = v_c;
            };

            make_ParallelTensorFunctor(f_ms_ns,
                                       arg.c_ms_ns_.mDesc.GetLengths()[0],
                                       arg.c_ms_ns_.mDesc.GetLengths()[1],
                                       arg.c_ms_ns_.mDesc.GetLengths()[2],
                                       arg.c_ms_ns_.mDesc.GetLengths()[3])(
                std::thread::hardware_concurrency());

            return 0;
        }

        float Run(const ck::tensor_operation::device::BaseArgument* p_arg,
                  const StreamConfig& /* stream_config */ = StreamConfig{}) override
        {
            return Run(*dynamic_cast<const Argument*>(p_arg));
        }
    };

    static constexpr bool IsValidCompilationParameter()
    {
        // TODO: properly implement this check
        return true;
    }

    bool IsSupportedArgument(const ck::tensor_operation::device::BaseArgument*) override
    {
        return true;
    }

    static auto MakeArgument(const Tensor<ADataType>& a_ms_ks,
                             const Tensor<BDataType>& b_ks_ns,
                             Tensor<CDataType>& c_ms_ns,
                             AElementwiseOperation a_element_op,
                             BElementwiseOperation b_element_op,
                             CElementwiseOperation c_element_op)
    {
        return Argument{a_ms_ks, b_ks_ns, c_ms_ns, a_element_op, b_element_op, c_element_op};
    }

    static auto MakeInvoker() { return Invoker{}; }

    virtual std::unique_ptr<ck::tensor_operation::device::BaseInvoker> MakeInvokerPointer()
    {
        return std::make_unique<Invoker>(Invoker{});
    }

    std::string GetTypeString() const override
    {
        auto str = std::stringstream();

        // clang-format off
        str << "ReferenceContraction_M2_N2_K2"
            << std::endl;
        // clang-format on

        return str.str();
    }
};

using ReferenceOpInstance = ReferenceContraction_M2_N2_K2<NumDimM,
                                                          NumDimN,
                                                          NumDimK,
                                                          ADataType,
                                                          BDataType,
                                                          CDataType,
                                                          AccDataType,
                                                          AElementOp,
                                                          BElementOp,
                                                          CElementOp>;
Chao Liu's avatar
Chao Liu committed
248
249
250
251

int main(int argc, char* argv[])
{
    bool do_verification = true;
252
    int init_method      = 3;
Chao Liu's avatar
Chao Liu committed
253
254
255
256
257
258
259
260
261
262
263
    bool time_kernel     = false;

    if(argc == 4)
    {
        do_verification = std::stoi(argv[1]);
        init_method     = std::stoi(argv[2]);
        time_kernel     = std::stoi(argv[3]);
    }
    else
    {
        printf("arg1: verification (0=no, 1=yes)\n");
264
        printf("arg2: initialization (0=no init, 1=integer value, 2=decimal value, 3=cutensor_style_init)\n");
Chao Liu's avatar
Chao Liu committed
265
        printf("arg3: time kernel (0=no, 1=yes)\n");
Chao Liu's avatar
Chao Liu committed
266
267
268
        exit(0);
    }

269
270
271
272
273
274
275
    std::ofstream tensorA;
    std::ofstream tensorB;
    std::ofstream tensorC;
    std::ofstream tensorC_d;
    std::cout << "RAND_MAX value is " << RAND_MAX << std::endl;

    
Chao Liu's avatar
Chao Liu committed
276
277
278
279
    // Physical layout
    // A[m0, k0, m1, k1]   : leng [5, 6, 3, 4], stride  [108, 20, 16, 1]
    // B[k0, n0, k1, n1]
    // C[m0, m1, n0, n1]
280

Chao Liu's avatar
Chao Liu committed
281
282
283
284
285

    // logic layout
    // A[m0, m1, k0, k1]   : leng [5, 3, 6, 4], stride [108, 16, 20, 1]  K is fast changing
    // C[k0, k1, n0, n1]
#if 1
Chao Liu's avatar
Chao Liu committed
286
    // fast changing dimension: K/K/N
287
    // a[m0, m1, k0, k1]
Chao Liu's avatar
format  
Chao Liu committed
288
    std::vector<ck::index_t> a_ms_ks_lengths{30, 128, 32, 64};
Chao Liu's avatar
Chao Liu committed
289
    std::vector<ck::index_t> a_ms_ks_strides{524288, 4096, 128, 1};
290
    // b[k0, k1, n0, n1]
Chao Liu's avatar
format  
Chao Liu committed
291
    std::vector<ck::index_t> b_ks_ns_lengths{32, 64, 32, 64};
Chao Liu's avatar
Chao Liu committed
292
    std::vector<ck::index_t> b_ks_ns_strides{128, 1, 524288, 4096};
293
    // c[m0, m1, n0, n1]
Chao Liu's avatar
format  
Chao Liu committed
294
    std::vector<ck::index_t> c_ms_ns_lengths{30, 128, 32, 64};
Chao Liu's avatar
Chao Liu committed
295
    std::vector<ck::index_t> c_ms_ns_strides{524288, 4096, 128, 1};
Chao Liu's avatar
Chao Liu committed
296
297
#elif 0
    // fast changing dimension: K/N/N
298
299
    // a[m0, m1, k0, k1]
    std::vector<ck::index_t> a_ms_ks_lengths{5,6,3,4};
Chao Liu's avatar
Chao Liu committed
300
    std::vector<ck::index_t> a_ms_ks_strides{108,20,16,1};
301
302
    // b[k0, k1, n0, n1]
    std::vector<ck::index_t> b_ks_ns_lengths{3,4,3,4};
Chao Liu's avatar
Chao Liu committed
303
    std::vector<ck::index_t> b_ks_ns_strides{48,12,4,1};
304
305
    // c[m0, m1, n0, n1]
    std::vector<ck::index_t> c_ms_ns_lengths{5,6,3,4};
Chao Liu's avatar
Chao Liu committed
306
    std::vector<ck::index_t> c_ms_ns_strides{108,20,16,1};
Chao Liu's avatar
Chao Liu committed
307
#elif 1
Chao Liu's avatar
Chao Liu committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
    // fast changing dimension: K/K/N
    // a[m0, m1, k0, k1]
    std::vector<ck::index_t> a_ms_ks_lengths{5,6,3,4};
    std::vector<ck::index_t> a_ms_ks_strides{108,20,16,1};
    // b[k0, k1, n0, n1]
    std::vector<ck::index_t> b_ks_ns_lengths{3,4,3,4};
    std::vector<ck::index_t> b_ks_ns_strides{16,1,108,20};
    // c[m0, m1, n0, n1]
    std::vector<ck::index_t> c_ms_ns_lengths{5,6,3,4};
    std::vector<ck::index_t> c_ms_ns_strides{108,20,16,1};
#elif 0
    // fast changing dimension: M/N/N
    // a[m0, m1, k0, k1]
    std::vector<ck::index_t> a_ms_ks_lengths{5,6,3,4};
    std::vector<ck::index_t> a_ms_ks_strides{6,1,72,24};
    // b[k0, k1, n0, n1]
    std::vector<ck::index_t> b_ks_ns_lengths{3,4,3,4};
    std::vector<ck::index_t> b_ks_ns_strides{48,12,4,1};
    // c[m0, m1, n0, n1]
    std::vector<ck::index_t> c_ms_ns_lengths{5,6,3,4};
    std::vector<ck::index_t> c_ms_ns_strides{108,20,16,1};
#elif 1
    // fast changing dimension: M/K/N
    // a[m0, m1, k0, k1]
    std::vector<ck::index_t> a_ms_ks_lengths{5,6,3,4};
    std::vector<ck::index_t> a_ms_ks_strides{6,1,72,24};
    // b[k0, k1, n0, n1]
    std::vector<ck::index_t> b_ks_ns_lengths{3,4,3,4};
    std::vector<ck::index_t> b_ks_ns_strides{16,1,108,20};
    // c[m0, m1, n0, n1]
    std::vector<ck::index_t> c_ms_ns_lengths{5,6,3,4};
    std::vector<ck::index_t> c_ms_ns_strides{108,20,16,1};
340
341
#endif

Chao Liu's avatar
Chao Liu committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    Tensor<ADataType> a_ms_ks(
        std::vector<std::size_t>(a_ms_ks_lengths.begin(), a_ms_ks_lengths.end()),
        std::vector<std::size_t>(a_ms_ks_strides.begin(), a_ms_ks_strides.end()));
    Tensor<BDataType> b_ks_ns(
        std::vector<std::size_t>(b_ks_ns_lengths.begin(), b_ks_ns_lengths.end()),
        std::vector<std::size_t>(b_ks_ns_strides.begin(), b_ks_ns_strides.end()));
    Tensor<CDataType> c_ms_ns_host_result(
        std::vector<std::size_t>(c_ms_ns_lengths.begin(), c_ms_ns_lengths.end()),
        std::vector<std::size_t>(c_ms_ns_strides.begin(), c_ms_ns_strides.end()));
    Tensor<CDataType> c_ms_ns_device_result(
        std::vector<std::size_t>(c_ms_ns_lengths.begin(), c_ms_ns_lengths.end()),
        std::vector<std::size_t>(c_ms_ns_strides.begin(), c_ms_ns_strides.end()));

    std::cout << "a_ms_ks: " << a_ms_ks.mDesc << std::endl;
    std::cout << "b_ks_ns: " << b_ks_ns.mDesc << std::endl;
    std::cout << "c_ms_ns: " << c_ms_ns_host_result.mDesc << std::endl;
Chao Liu's avatar
Chao Liu committed
358
359
360
361
362

    switch(init_method)
    {
    case 0: break;
    case 1:
Chao Liu's avatar
Chao Liu committed
363
364
        a_ms_ks.GenerateTensorValue(GeneratorTensor_2<ADataType>{-5, 5});
        b_ks_ns.GenerateTensorValue(GeneratorTensor_2<BDataType>{-5, 5});
Chao Liu's avatar
Chao Liu committed
365
366
        break;
    case 2:
Chao Liu's avatar
Chao Liu committed
367
368
        a_ms_ks.GenerateTensorValue(GeneratorTensor_3<ADataType>{0.0, 1.0});
        b_ks_ns.GenerateTensorValue(GeneratorTensor_3<BDataType>{-0.5, 0.5});
Chao Liu's avatar
Chao Liu committed
369
        break;
370
371
372
373
    case 3:
        a_ms_ks.GenerateTensorValue(GeneratorTensor_cuTensor<ADataType>{});
        b_ks_ns.GenerateTensorValue(GeneratorTensor_cuTensor<BDataType>{});
        break;
Chao Liu's avatar
Chao Liu committed
374
    default:
Chao Liu's avatar
Chao Liu committed
375
376
        a_ms_ks.GenerateTensorValue(GeneratorTensor_Sequential<0>{});
        b_ks_ns.GenerateTensorValue(GeneratorTensor_Sequential<1>{});
Chao Liu's avatar
Chao Liu committed
377
378
    }

Chao Liu's avatar
Chao Liu committed
379
380
381
382
    DeviceMem a_ms_ks_device_buf(sizeof(ADataType) * a_ms_ks.mDesc.GetElementSpace());
    DeviceMem b_ks_ns_device_buf(sizeof(BDataType) * b_ks_ns.mDesc.GetElementSpace());
    DeviceMem c_ms_ns_device_buf(sizeof(CDataType) * c_ms_ns_device_result.mDesc.GetElementSpace());

383
384
385
386
    std::cout << "Tensor A element space: " << a_ms_ks.mDesc.GetElementSpace() << std::endl;
    std::cout << "Tensor B element space: " << b_ks_ns.mDesc.GetElementSpace() << std::endl;
    std::cout << "Tensor C element space: " <<  c_ms_ns_device_result.mDesc.GetElementSpace() << std::endl;

Chao Liu's avatar
Chao Liu committed
387
388
    a_ms_ks_device_buf.ToDevice(a_ms_ks.mData.data());
    b_ks_ns_device_buf.ToDevice(b_ks_ns.mData.data());
Chao Liu's avatar
Chao Liu committed
389

Chao Liu's avatar
Chao Liu committed
390
391
    // set zero
    c_ms_ns_device_buf.SetZero();
Chao Liu's avatar
Chao Liu committed
392
393
394
395
396

    auto a_element_op = AElementOp{};
    auto b_element_op = BElementOp{};
    auto c_element_op = CElementOp{};

Chao Liu's avatar
Chao Liu committed
397
    // device operation
Chao Liu's avatar
Chao Liu committed
398
    auto op       = ContractionInstance{};
Chao Liu's avatar
Chao Liu committed
399
400
401
402
403
    auto invoker  = op.MakeInvoker();
    auto argument = op.MakeArgument(static_cast<ADataType*>(a_ms_ks_device_buf.GetDeviceBuffer()),
                                    static_cast<BDataType*>(b_ks_ns_device_buf.GetDeviceBuffer()),
                                    static_cast<CDataType*>(c_ms_ns_device_buf.GetDeviceBuffer()),
                                    a_ms_ks_lengths,
404
                                    std::vector<ck::index_t>(a_ms_ks.mDesc.mStrides.begin(), a_ms_ks.mDesc.mStrides.end()),
Chao Liu's avatar
Chao Liu committed
405
                                    b_ks_ns_lengths,
406
				    std::vector<ck::index_t>(b_ks_ns.mDesc.mStrides.begin(), b_ks_ns.mDesc.mStrides.end()),
Chao Liu's avatar
Chao Liu committed
407
                                    c_ms_ns_lengths,
408
                                    std::vector<ck::index_t>(c_ms_ns_host_result.mDesc.mStrides.begin(), c_ms_ns_host_result.mDesc.mStrides.end()),
Chao Liu's avatar
Chao Liu committed
409
410
411
412
413
                                    a_element_op,
                                    b_element_op,
                                    c_element_op);

    if(!op.IsSupportedArgument(argument))
Chao Liu's avatar
Chao Liu committed
414
    {
Chao Liu's avatar
Chao Liu committed
415
        std::cout << op.GetTypeString() << " does not support this problem" << std::endl;
Chao Liu's avatar
Chao Liu committed
416
417
418
419
420
421

        return 0;
    }

    float ave_time = invoker.Run(argument, StreamConfig{nullptr, time_kernel});

Chao Liu's avatar
Chao Liu committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
    ck::index_t M = std::accumulate(c_ms_ns_lengths.begin(),
                                    c_ms_ns_lengths.begin() + NumDimM,
                                    ck::index_t{1},
                                    std::multiplies<ck::index_t>{});

    ck::index_t N = std::accumulate(c_ms_ns_lengths.begin() + NumDimM,
                                    c_ms_ns_lengths.begin() + NumDimM + NumDimN,
                                    ck::index_t{1},
                                    std::multiplies<ck::index_t>{});

    ck::index_t K = std::accumulate(a_ms_ks_lengths.begin() + NumDimM,
                                    a_ms_ks_lengths.begin() + NumDimM + NumDimK,
                                    ck::index_t{1},
                                    std::multiplies<ck::index_t>{});

Chao Liu's avatar
Chao Liu committed
437
438
439
440
441
442
443
444
445
    std::size_t flop = std::size_t(2) * M * N * K;
    std::size_t num_btype =
        sizeof(ADataType) * M * K + sizeof(BDataType) * K * N + sizeof(CDataType) * M * N;

    float tflops = static_cast<float>(flop) / 1.E9 / ave_time;

    float gb_per_sec = num_btype / 1.E6 / ave_time;

    std::cout << "Perf: " << ave_time << " ms, " << tflops << " TFlops, " << gb_per_sec << " GB/s, "
Chao Liu's avatar
Chao Liu committed
446
              << op.GetTypeString() << std::endl;
Chao Liu's avatar
Chao Liu committed
447

Chao Liu's avatar
Chao Liu committed
448
    c_ms_ns_device_buf.FromDevice(c_ms_ns_device_result.mData.data());
Chao Liu's avatar
Chao Liu committed
449

Chao Liu's avatar
Chao Liu committed
450
#if 0
451
452
453
454
455
456
457
458
459
460
    tensorA.open("tensor_A.txt");
    LogRangeToFile<ADataType>(tensorA, a_ms_ks.mData, ","); 
    LogRangeAsType<ADataType>(std::cout<<"Tensor A elements:\n", a_ms_ks.mData,",");
    std::cout<<std::endl;
    tensorA.close();
    tensorB.open("tensor_B.txt");
    LogRangeToFile<BDataType>(tensorB, b_ks_ns.mData, ","); 
    LogRangeAsType<BDataType>(std::cout<<"Tensor B elements:\n", b_ks_ns.mData,",");
    std::cout<<std::endl;
    tensorB.close();
Chao Liu's avatar
Chao Liu committed
461
#endif
462

Chao Liu's avatar
Chao Liu committed
463
464
    if(do_verification)
    {
Chao Liu's avatar
Chao Liu committed
465
        auto ref_gemm    = ReferenceOpInstance{};
Chao Liu's avatar
Chao Liu committed
466
467
468
        auto ref_invoker = ref_gemm.MakeInvoker();

        auto ref_argument = ref_gemm.MakeArgument(
Chao Liu's avatar
Chao Liu committed
469
            a_ms_ks, b_ks_ns, c_ms_ns_host_result, a_element_op, b_element_op, c_element_op);
Chao Liu's avatar
Chao Liu committed
470
471
472

        ref_invoker.Run(ref_argument);

Chao Liu's avatar
Chao Liu committed
473
#if 0
474
475
476
477
478
479
480
481
482
483
484
	    tensorC.open("tensor_C_contraction_host_results.txt");
    	LogRangeToFile<CDataType>(tensorC, c_ms_ns_host_result.mData, ","); 
    	LogRangeAsType<CDataType>(std::cout<<"Tensor C_host elements:\n", c_ms_ns_host_result.mData, ",");
    	std::cout<<std::endl;
	    tensorC.close();

	    tensorC.open("tensor_C_contraction_device_results.txt");
    	LogRangeToFile<CDataType>(tensorC_d, c_ms_ns_device_result.mData, ","); 
    	LogRangeAsType<CDataType>(std::cout<<"Tensor C_device elements:\n", c_ms_ns_device_result.mData, ",");
    	std::cout<<std::endl;
	    tensorC.close();
Chao Liu's avatar
Chao Liu committed
485
#endif
486
487


Chao Liu's avatar
Chao Liu committed
488
        return ck::utils::check_err(c_ms_ns_device_result.mData, c_ms_ns_host_result.mData) ? 0 : 1;
Chao Liu's avatar
Chao Liu committed
489
490
491
492
    }

    return 0;
}