profile_normalization_impl.hpp 10.1 KB
Newer Older
1
2
3
4
5
6
7
8
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

#pragma once

#include <iomanip>

#include "ck/ck.hpp"
9
10
11
12
13
#include "ck/utility/data_type.hpp"
#include "ck/tensor_operation/gpu/device/device_softmax.hpp"
#include "ck/tensor_operation/gpu/element/element_wise_operation.hpp"

#include "ck/library/reference_tensor_operation/cpu/reference_softmax.hpp"
14
#include "ck/library/utility/check_err.hpp"
15
16
17
18
#include "ck/library/utility/convolution_parameter.hpp"
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_tensor.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
19
20
21
22

namespace ck {
namespace tensor_operation {
namespace device {
23
namespace instance {
24

Adam Osewski's avatar
Adam Osewski committed
25
26
27
28
29
namespace {
using F16         = ck::half_t;
using F32         = float;
using PassThrough = ck::tensor_operation::element_wise::PassThrough;
} // namespace
30

Adam Osewski's avatar
Adam Osewski committed
31
32
33
34
35
36
37
38
39
void add_device_softmax_f16_f16_rank3_instances(
    std::vector<DeviceSoftmaxPtr<F16, F32, F16, PassThrough, PassThrough, 3>>&);
void add_device_softmax_f16_f16_rank4_instances(
    std::vector<DeviceSoftmaxPtr<F16, F32, F16, PassThrough, PassThrough, 4>>&);

void add_device_softmax_f32_f32_rank3_instances(
    std::vector<DeviceSoftmaxPtr<F32, F32, F32, PassThrough, PassThrough, 3>>&);
void add_device_softmax_f32_f32_rank4_instances(
    std::vector<DeviceSoftmaxPtr<F32, F32, F32, PassThrough, PassThrough, 4>>&);
40

41
} // namespace instance
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
} // namespace device
} // namespace tensor_operation
} // namespace ck

namespace ck {
namespace profiler {

enum struct NormType
{
    BATCHNORM,
    SOFTMAX,
};

enum struct NormDataType
{
    F32_F32, // in, out
    F16_F16,
    BF16_BF16,
    INT8_INT8,
};

// clang-format off
template <typename NormDataType> std::string type_to_string();
template <> std::string type_to_string<float>()   { return "f32"; }
template <> std::string type_to_string<half_t>()  { return "f16"; }
template <> std::string type_to_string<bhalf_t>() { return "bf16"; }
template <> std::string type_to_string<int8_t>()  { return "int8"; }
template <> std::string type_to_string<int32_t>() { return "int32"; }
// clang-format on

Adam Osewski's avatar
Adam Osewski committed
72
template <typename InDataType, typename AccDataType, typename OutDataType, index_t Rank>
73
74
75
76
77
78
79
80
81
82
83
void profile_normalization_impl(int do_verification,
                                int init_method,
                                bool do_log,
                                bool time_kernel,
                                std::vector<index_t> in_length,
                                std::vector<index_t> in_strides,
                                std::vector<index_t> reduce_dims,
                                AccDataType alpha,
                                AccDataType beta,
                                NormType norm_type)
{
Adam Osewski's avatar
Adam Osewski committed
84
85
86
87
88
    if(Rank != in_length.size())
    {
        throw std::runtime_error("Input tensor rank is different from template argument Rank!");
    }

89
90
    Tensor<InDataType> in = in_strides.empty() ? Tensor<InDataType>(in_length)
                                               : Tensor<InDataType>(in_length, in_strides);
91
    Tensor<OutDataType> out(in.GetDesc());
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

    switch(init_method)
    {
    // case 0: break;
    case 0:
        in.GenerateTensorValue(GeneratorTensor_1<InDataType>{});
        out.GenerateTensorValue(GeneratorTensor_1<OutDataType>{});
        break;
    case 1:
        in.GenerateTensorValue(GeneratorTensor_2<InDataType>{-5, 5});
        out.GenerateTensorValue(GeneratorTensor_2<OutDataType>{-5, 5});
        break;
    default:
        in.GenerateTensorValue(GeneratorTensor_3<InDataType>{0.0, 1.0});
        out.GenerateTensorValue(GeneratorTensor_3<OutDataType>{-0.5, 0.5});
    }

    Tensor<OutDataType> out_ref(out);

111
112
113
114
    DeviceMem in_dev(in.GetMemorySize());
    DeviceMem out_dev(out.GetMemorySize());
    in_dev.ToDevice(in.data());
    out_dev.ToDevice(out.data());
115

116
117
    std::vector<index_t> i_in_lengths(in.GetLengths().begin(), in.GetLengths().end());
    std::vector<index_t> i_in_strides(in.GetStrides().begin(), in.GetStrides().end());
118

Adam Osewski's avatar
Adam Osewski committed
119
120
121
122
123
    // add device softmax instances
    using PassThrough = ck::tensor_operation::element_wise::PassThrough;
    using DeviceOpPtr = tensor_operation::device::
        DeviceSoftmaxPtr<InDataType, AccDataType, OutDataType, PassThrough, PassThrough, Rank>;
    std::vector<DeviceOpPtr> instances;
124
125
126
127
128
129

    if(norm_type == NormType::SOFTMAX)
    {
        if constexpr(is_same<InDataType, half_t>::value && is_same<OutDataType, half_t>::value &&
                     is_same<AccDataType, float>::value)
        {
Adam Osewski's avatar
Adam Osewski committed
130
            if constexpr(Rank == 3)
131
132
                tensor_operation::device::instance::add_device_softmax_f16_f16_rank3_instances(
                    instances);
Adam Osewski's avatar
Adam Osewski committed
133
            else if constexpr(Rank == 4)
134
135
                tensor_operation::device::instance::add_device_softmax_f16_f16_rank4_instances(
                    instances);
136
137
138
139
        }
        else if constexpr(is_same<InDataType, float>::value && is_same<OutDataType, float>::value &&
                          is_same<AccDataType, float>::value)
        {
Adam Osewski's avatar
Adam Osewski committed
140
            if constexpr(Rank == 3)
141
142
                tensor_operation::device::instance::add_device_softmax_f32_f32_rank3_instances(
                    instances);
Adam Osewski's avatar
Adam Osewski committed
143
            else if constexpr(Rank == 4)
144
145
                tensor_operation::device::instance::add_device_softmax_f32_f32_rank4_instances(
                    instances);
146
147
148
149
150
151
152
153
154
155
156
157
        }
    }

    if(instances.size() <= 0)
    {
        throw std::runtime_error("wrong! no device normalization instance found");
    }

    std::string best_instance_name;
    float best_avg_time   = std::numeric_limits<float>::max();
    float best_gb_per_sec = 0;

Adam Osewski's avatar
Adam Osewski committed
158
159
    using PassThrough = ck::tensor_operation::element_wise::PassThrough;

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    for(auto& inst_ptr : instances)
    {
        // Is this user's responsibility to check if problem mismatches kernel instance (ie. rank 3
        // problem to rank 4 kernel) other than invoking IsSupportedArgument()?
        if(!(inst_ptr->GetRank() == static_cast<index_t>(i_in_lengths.size()) &&
             inst_ptr->GetNumReduceDim() == static_cast<index_t>(reduce_dims.size())))
        {
            continue;
        }

        auto argument_ptr = inst_ptr->MakeArgumentPointer(i_in_lengths,
                                                          i_in_strides,
                                                          reduce_dims,
                                                          &alpha,
                                                          &beta,
                                                          in_dev.GetDeviceBuffer(),
Adam Osewski's avatar
Adam Osewski committed
176
177
178
                                                          out_dev.GetDeviceBuffer(),
                                                          PassThrough{},
                                                          PassThrough{});
179
180
181
182
183
184
185
186
187
188
189
190
191
192

        if(!inst_ptr->IsSupportedArgument(argument_ptr.get()))
        {
            std::cout << inst_ptr->GetTypeString() << " skipped due to unsupported argument: ";
            LogRange(std::cout << "input lengths = [", in_length, ", ")
                << "], "
                << "scaler = [" << alpha << ", " << beta << "]." << std::endl;
            return;
        }

        auto invoker_ptr = inst_ptr->MakeInvokerPointer();

        float avg_time = invoker_ptr->Run(argument_ptr.get(), StreamConfig{nullptr, time_kernel});

193
194
        std::size_t num_bytes = in.GetElementSize() * sizeof(InDataType) +
                                (beta == 0.0f ? 1 : 2) * out.GetElementSize() * sizeof(OutDataType);
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

        float gb_per_sec = num_bytes / 1.E6 / avg_time;

        std::cout << "Perf: " << std::setw(10) << avg_time << " ms, " << gb_per_sec << " GB/s, "
                  << inst_ptr->GetTypeString() << std::endl;

        if(avg_time < best_avg_time)
        {
            best_instance_name = inst_ptr->GetTypeString();
            best_avg_time      = avg_time;
            best_gb_per_sec    = gb_per_sec;
        }

        if(do_verification)
        {
            // TODO: factory method to dynamically switch between different reference normalizations
            using ReferenceFactory =
                tensor_operation::host::ReferenceSoftmax<InDataType, OutDataType, AccDataType>;

            ReferenceFactory{}.MakeInvoker().Run({in, out_ref, alpha, beta, reduce_dims});

216
            out_dev.FromDevice(out.data());
217
218

            bool pass;
219
            if constexpr(std::is_same_v<InDataType, int8_t>)
220
            {
221
                pass = ck::utils::check_err(out, out_ref, "Error: Incorrect results!", 0, 1);
222
223
                if(do_log)
                {
224
225
226
                    LogRangeAsType<int>(std::cout << "in  : ", in, ",") << std::endl;
                    LogRangeAsType<int>(std::cout << "out_ref  : ", out_ref, ",") << std::endl;
                    LogRangeAsType<int>(std::cout << "out  : ", out, ",") << std::endl;
227
228
229
230
                }
            }
            else
            {
231
                pass = ck::utils::check_err(out, out_ref);
232
233
                if(do_log)
                {
234
235
236
                    LogRangeAsType<float>(std::cout << "in  : ", in, ",") << std::endl;
                    LogRangeAsType<float>(std::cout << "out_ref  : ", out_ref, ",") << std::endl;
                    LogRangeAsType<float>(std::cout << "out  : ", out, ",") << std::endl;
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
                }
            }

            if(!pass)
            {
                std::cout << inst_ptr->GetTypeString() << " failed verification: ";
                LogRange(std::cout << "input lengths = [", in_length, ", ")
                    << "], "
                    << "scaler = [" << alpha << ", " << beta << "]." << std::endl;
            }
        }
    }
    std::cout << "Best Perf for datatype = " << type_to_string<InDataType>() << "_"
              << type_to_string<OutDataType>() << ", ";
    LogRange(std::cout << "length = ", i_in_lengths, ",") << ", ";
    LogRange(std::cout << "stride = ", i_in_strides, ",") << ", ";
    LogRange(std::cout << "reduce dims ", reduce_dims, ",") << ", ";
    std::cout << "alpha = " << alpha << ", "
              << "beta = " << beta << ", " << best_avg_time << " ms, " << best_gb_per_sec
              << " GB/s, " << best_instance_name << std::endl;
}

} // namespace profiler
} // namespace ck