profile_permute_scale.cpp 5.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2024, Advanced Micro Devices, Inc. All rights reserved.

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

#include "profiler/profile_permute_scale_impl.hpp"
#include "profiler_operation_registry.hpp"

namespace {

enum struct DataType
{
    F32_F32, // 0
    F16_F16  // 1
};

#define OP_NAME "permute_scale"
#define OP_DESC "Permute Scale"

static void print_helper_msg()
{
    std::cout
        // clang-format off
        << "arg1: tensor operation (" OP_NAME ": " OP_DESC ")\n"
        << "arg2: data type (0: Input fp32, Output fp32\n"
        << "                 1: Input fp16, Output fp16\n"
        << "arg4: verification (0: no, 1: yes)\n"
        << "arg5: initialization (0: no init, 1: integer value, 2: decimal value)\n"
        << "arg6: print tensor value (0: no; 1: yes)\n"
        << "arg7: time kernel (0: no, 1: yes)\n"
        << "from arg8: tensor lengths\n"
        << "           input strides\n"
        << "           output strides\n" << std::endl;
    // clang-format on
}

40
41
42
43
44
45
46
47
48
49
50
51
52
53
void init_strides(const std::vector<ck::index_t>& lengths,
                  const std::vector<ck::index_t>& dims_order,
                  std::vector<ck::index_t>& strides)
{

    ck::index_t stride = 1;
    for(ck::index_t d = lengths.size() - 1; d >= 0; d--)
    {
        ck::index_t dim = dims_order[d];
        strides[dim]    = stride;
        stride *= lengths[dim];
    }
}

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
} // namespace

int profile_permute_scale(int argc, char* argv[])
{
    constexpr int control_argc = 7;
    const int dims_argc        = argc - control_argc;
    // Number of lenghs, input strides and outputs strides must be equal
    if(argc < control_argc && dims_argc % 3 != 0)
    {
        print_helper_msg();
        return 1;
    }

    const auto data_type       = static_cast<DataType>(std::stoi(argv[2]));
    const bool do_verification = std::stoi(argv[3]);
    const int init_method      = std::stoi(argv[4]);
    const bool do_log          = std::stoi(argv[5]);
    const bool time_kernel     = std::stoi(argv[6]);
    const int num_dims         = dims_argc / 3;

    std::vector<ck::index_t> lengths(num_dims);
75
76
    std::vector<ck::index_t> input_dims_order(num_dims);
    std::vector<ck::index_t> output_dims_order(num_dims);
77
78
79

    for(int i = 0; i < num_dims; i++)
    {
80
81
82
        lengths[i]           = std::stoi(argv[control_argc + i]);
        input_dims_order[i]  = std::stoi(argv[control_argc + num_dims + i]);
        output_dims_order[i] = std::stoi(argv[control_argc + 2 * num_dims + i]);
83
84
    }

85
86
87
88
89
    std::vector<ck::index_t> input_strides(num_dims);
    std::vector<ck::index_t> output_strides(num_dims);
    init_strides(lengths, input_dims_order, input_strides);
    init_strides(lengths, output_dims_order, output_strides);

90
91
92
93
94
95
96
97
98
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
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
    using F32 = float;
    using F16 = ck::half_t;

    constexpr auto I1 = ck::Number<1>{};
    constexpr auto I2 = ck::Number<2>{};
    constexpr auto I3 = ck::Number<3>{};
    constexpr auto I4 = ck::Number<4>{};
    constexpr auto I5 = ck::Number<5>{};
    constexpr auto I6 = ck::Number<6>{};

    auto profile = [&](auto num_dim_tmp, auto in_type, auto out_type) {
        constexpr ck::index_t NDim = num_dim_tmp.value;

        using InDataType  = decltype(in_type);
        using OutDataType = decltype(out_type);

        bool pass =
            ck::profiler::profile_permute_scale_impl<InDataType, OutDataType, NDim>(do_verification,
                                                                                    init_method,
                                                                                    do_log,
                                                                                    time_kernel,
                                                                                    lengths,
                                                                                    input_strides,
                                                                                    output_strides);

        return pass ? 0 : 1;
    };

    if(num_dims == 1)
    {
        if(data_type == DataType::F32_F32)
        {
            return profile(I1, F32{}, F32{});
        }
        else if(data_type == DataType::F16_F16)
        {
            return profile(I1, F16{}, F16{});
        }
    }
    else if(num_dims == 2)
    {
        if(data_type == DataType::F32_F32)
        {
            return profile(I2, F32{}, F32{});
        }
        else if(data_type == DataType::F16_F16)
        {
            return profile(I2, F16{}, F16{});
        }
    }
    else if(num_dims == 3)
    {
        if(data_type == DataType::F32_F32)
        {
            return profile(I3, F32{}, F32{});
        }
        else if(data_type == DataType::F16_F16)
        {
            return profile(I3, F16{}, F16{});
        }
    }
    else if(num_dims == 4)
    {
        if(data_type == DataType::F32_F32)
        {
            return profile(I4, F32{}, F32{});
        }
        else if(data_type == DataType::F16_F16)
        {
            return profile(I4, F16{}, F16{});
        }
    }
    else if(num_dims == 5)
    {
        if(data_type == DataType::F32_F32)
        {
            return profile(I5, F32{}, F32{});
        }
        else if(data_type == DataType::F16_F16)
        {
            return profile(I5, F16{}, F16{});
        }
    }
    else if(num_dims == 6)
    {
        if(data_type == DataType::F32_F32)
        {
            return profile(I6, F32{}, F32{});
        }
        else if(data_type == DataType::F16_F16)
        {
            return profile(I6, F16{}, F16{});
        }
    }

    std::cout << "this data_type & layout is not implemented" << std::endl;
    return 1;
}

REGISTER_PROFILER_OPERATION(OP_NAME, OP_DESC, profile_permute_scale);