"example/17_convnd_bwd_data/README.md" did not exist on "0536f2b3123f6ad0a0c3598f97459a70a0a55fb0"
profile_conv_bwd_data_impl.hpp 10.4 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

4
#pragma once
Chao Liu's avatar
Chao Liu committed
5
6
7
8
9
10

#include "ck/ck.hpp"
#include "ck/tensor_operation/gpu/device/tensor_layout.hpp"
#include "ck/tensor_operation/gpu/device/device_conv_bwd_data.hpp"
#include "ck/tensor_operation/gpu/element/element_wise_operation.hpp"

Chao Liu's avatar
Chao Liu committed
11
12
#include "ck/library/tensor_operation_instance/gpu/convolution_backward_data.hpp"

13
#include "ck/library/utility/check_err.hpp"
Chao Liu's avatar
Chao Liu committed
14
15
16
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_tensor.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
Chao Liu's avatar
Chao Liu committed
17
#include "ck/library/utility/convolution_parameter.hpp"
Chao Liu's avatar
clean  
Chao Liu committed
18
#include "ck/library/utility/convolution_host_tensor_descriptor_helper.hpp"
Chao Liu's avatar
Chao Liu committed
19
#include "ck/library/reference_tensor_operation/cpu/reference_conv_bwd_data.hpp"
20
21
22
23
24
25
26
27

namespace ck {
namespace profiler {

template <typename DataType>
void show_data_nhwc_layout(Tensor<DataType>& nhwc)
{
    std::cout << "[";
28
    for(int n = 0; n < ck::type_convert<int>(nhwc.mDesc.GetLengths()[0]); n++)
29
30
    {
        std::cout << "[";
31
        for(int hi = 0; hi < ck::type_convert<int>(nhwc.mDesc.GetLengths()[2]); hi++)
32
33
        {
            std::cout << "[";
34
            for(int wi = 0; wi < ck::type_convert<int>(nhwc.mDesc.GetLengths()[3]); wi++)
35
36
            {
                std::cout << "[";
37
                for(int c = 0; c < ck::type_convert<int>(nhwc.mDesc.GetLengths()[1]); c++)
38
39
40
41
42
43
44
45
46
47
48
49
                {
                    std::cout << static_cast<float>(nhwc(n, c, hi, wi)) << "  ";
                }
                std::cout << "]";
            }
            std::cout << "]";
        }
        std::cout << "]";
    }
    std::cout << "]";
}

Chao Liu's avatar
Chao Liu committed
50
template <ck::index_t NDimSpatial,
51
52
          typename InLayout,
          typename WeiLayout,
53
54
55
56
57
58
59
60
          typename OutLayout,
          typename InDataType,
          typename WeiDataType,
          typename OutDataType>
bool profile_conv_bwd_data_impl(int do_verification,
                                int init_method,
                                bool do_log,
                                bool time_kernel,
Chao Liu's avatar
clean  
Chao Liu committed
61
                                const ck::utils::conv::ConvParam& conv_param)
62
63
64
65
66
67
68
69
70
{
    using InElementOp  = ck::tensor_operation::element_wise::PassThrough;
    using WeiElementOp = ck::tensor_operation::element_wise::PassThrough;
    using OutElementOp = ck::tensor_operation::element_wise::PassThrough;

    const auto in_element_op  = InElementOp{};
    const auto wei_element_op = WeiElementOp{};
    const auto out_element_op = OutElementOp{};

Chao Liu's avatar
clean  
Chao Liu committed
71
72
73
    const auto in_desc  = ck::utils::conv::get_input_host_tensor_descriptor<InLayout>(conv_param);
    const auto wei_desc = ck::utils::conv::get_weight_host_tensor_descriptor<WeiLayout>(conv_param);
    const auto out_desc = ck::utils::conv::get_output_host_tensor_descriptor<OutLayout>(conv_param);
74
75
76
77
78

    Tensor<InDataType> input_host_result(in_desc);
    Tensor<InDataType> input_device_result(in_desc);
    Tensor<WeiDataType> weight(wei_desc);
    Tensor<OutDataType> output(out_desc);
79

ltqin's avatar
ltqin committed
80
    std::cout << "input: " << input_host_result.mDesc << std::endl;
81
    std::cout << "weight: " << weight.mDesc << std::endl;
ltqin's avatar
ltqin committed
82
    std::cout << "output: " << output.mDesc << std::endl;
83
84
85
86
87

    switch(init_method)
    {
    case 0: break;
    case 1:
ltqin's avatar
ltqin committed
88
        output.GenerateTensorValue(GeneratorTensor_2<OutDataType>{-5, 5});
89
        weight.GenerateTensorValue(GeneratorTensor_2<WeiDataType>{-5, 5});
90
91
        break;
    default:
Chao Liu's avatar
Chao Liu committed
92
93
        output.GenerateTensorValue(GeneratorTensor_3<OutDataType>{0.0, 1.0});
        weight.GenerateTensorValue(GeneratorTensor_3<WeiDataType>{-0.5, 0.5});
94
95
    }

ltqin's avatar
ltqin committed
96
    DeviceMem in_device_buf(sizeof(InDataType) * input_device_result.mDesc.GetElementSpace());
97
    DeviceMem wei_device_buf(sizeof(WeiDataType) * weight.mDesc.GetElementSpace());
ltqin's avatar
ltqin committed
98
    DeviceMem out_device_buf(sizeof(OutDataType) * output.mDesc.GetElementSpace());
99

ltqin's avatar
ltqin committed
100
    out_device_buf.ToDevice(output.mData.data());
101
    wei_device_buf.ToDevice(weight.mData.data());
102
103
104

    if(do_verification)
    {
105
106
107
108
109
        auto ref_conv = ck::tensor_operation::host::ReferenceConvBwdData<NDimSpatial,
                                                                         InLayout,
                                                                         WeiLayout,
                                                                         OutLayout,
                                                                         InDataType,
ltqin's avatar
ltqin committed
110
111
112
113
                                                                         WeiDataType,
                                                                         OutDataType,
                                                                         InElementOp,
                                                                         WeiElementOp,
114
115
116
117
118
119
120
                                                                         OutElementOp>{};

        auto ref_invoker = ref_conv.MakeInvoker();

        auto ref_argument = ref_conv.MakeArgument(input_host_result,
                                                  weight,
                                                  output,
Chao Liu's avatar
clean  
Chao Liu committed
121
122
123
124
                                                  conv_param.conv_filter_strides_,
                                                  conv_param.conv_filter_dilations_,
                                                  conv_param.input_left_pads_,
                                                  conv_param.input_right_pads_,
125
126
127
128
                                                  InElementOp{},
                                                  WeiElementOp{},
                                                  OutElementOp{});
        ref_invoker.Run(ref_argument);
129
130
    }

Chao Liu's avatar
Chao Liu committed
131
132
133
134
135
136
137
138
139
140
    using DeviceOp = ck::tensor_operation::device::DeviceConvBwdData<NDimSpatial,
                                                                     InLayout,
                                                                     WeiLayout,
                                                                     OutLayout,
                                                                     InDataType,
                                                                     WeiDataType,
                                                                     OutDataType,
                                                                     InElementOp,
                                                                     WeiElementOp,
                                                                     OutElementOp>;
141

Chao Liu's avatar
Chao Liu committed
142
143
144
145
146
    // get device op instances
    const auto op_ptrs = ck::tensor_operation::device::instance::DeviceOperationInstanceFactory<
        DeviceOp>::GetInstances();

    std::cout << "found " << op_ptrs.size() << " instances" << std::endl;
147

148
149
    std::string best_op_name;
    float best_avg_time   = 0;
150
151
152
153
    float best_tflops     = 0;
    float best_gb_per_sec = 0;

    // profile device Conv instances
154
155
    bool pass = true;

Chao Liu's avatar
Chao Liu committed
156
    for(auto& op_ptr : op_ptrs)
157
    {
Chao Liu's avatar
Chao Liu committed
158
159
160
161
        auto argument_ptr =
            op_ptr->MakeArgumentPointer(static_cast<InDataType*>(in_device_buf.GetDeviceBuffer()),
                                        static_cast<WeiDataType*>(wei_device_buf.GetDeviceBuffer()),
                                        static_cast<OutDataType*>(out_device_buf.GetDeviceBuffer()),
Chao Liu's avatar
clean  
Chao Liu committed
162
163
164
165
166
167
168
169
170
171
                                        conv_param.N_,
                                        conv_param.K_,
                                        conv_param.C_,
                                        conv_param.input_spatial_lengths_,
                                        conv_param.filter_spatial_lengths_,
                                        conv_param.output_spatial_lengths_,
                                        conv_param.conv_filter_strides_,
                                        conv_param.conv_filter_dilations_,
                                        conv_param.input_left_pads_,
                                        conv_param.input_right_pads_,
Chao Liu's avatar
Chao Liu committed
172
173
174
175
176
                                        in_element_op,
                                        wei_element_op,
                                        out_element_op);

        if(op_ptr->IsSupportedArgument(argument_ptr.get()))
177
        {
Chao Liu's avatar
Chao Liu committed
178
179
            // for conv bwd data, some input tensor element are zero, but not written by kernel,
            // need to set zero
180
181
            in_device_buf.SetZero();

Chao Liu's avatar
Chao Liu committed
182
183
184
            std::string op_name = op_ptr->GetTypeString();

            auto invoker_ptr = op_ptr->MakeInvokerPointer();
185

186
            float avg_time =
JD's avatar
JD committed
187
                invoker_ptr->Run(argument_ptr.get(), StreamConfig{nullptr, time_kernel});
188

Chao Liu's avatar
clean  
Chao Liu committed
189
190
            std::size_t flop      = conv_param.GetFlops();
            std::size_t num_btype = conv_param.GetByte<InDataType, WeiDataType, OutDataType>();
191

192
193
            float tflops     = static_cast<float>(flop) / 1.E9 / avg_time;
            float gb_per_sec = num_btype / 1.E6 / avg_time;
194

195
            std::cout << "Perf: " << avg_time << " ms, " << tflops << " TFlops, " << gb_per_sec
196
197
198
199
                      << " GB/s" << std::endl;

            if(tflops > best_tflops)
            {
200
                best_op_name    = op_name;
201
                best_tflops     = tflops;
202
                best_avg_time   = avg_time;
203
204
205
206
207
                best_gb_per_sec = gb_per_sec;
            }

            if(do_verification)
            {
ltqin's avatar
ltqin committed
208
                in_device_buf.FromDevice(input_device_result.mData.data());
209

210
211
                pass =
                    pass & ck::utils::check_err(input_device_result.mData, input_host_result.mData);
212
213
214
215

                if(do_log)
                {
                    std::cout << "in : ";
ltqin's avatar
ltqin committed
216
                    show_data_nhwc_layout(output);
217
218
219
                    std::cout << std::endl;

                    std::cout << "wei: ";
220
                    show_data_nhwc_layout(weight);
221
222
223
                    std::cout << std::endl;

                    std::cout << "out_host  : ";
ltqin's avatar
ltqin committed
224
                    show_data_nhwc_layout(input_host_result);
225
226
227
                    std::cout << std::endl;

                    std::cout << "out_device: ";
ltqin's avatar
ltqin committed
228
                    show_data_nhwc_layout(input_device_result);
229
230
231
232
                    std::cout << std::endl;
                }
            }
        }
Chao Liu's avatar
Chao Liu committed
233
234
235
236
        else
        {
            std::cout << op_ptr->GetTypeString() << " does not support this problem" << std::endl;
        }
237
238
    }

Chao Liu's avatar
Chao Liu committed
239
240
241
    std::cout << "Best configuration parameters:"
              << "\nname: " << best_op_name << "\navg_time: " << best_avg_time
              << "\ntflops: " << best_tflops << "\nGB/s: " << best_gb_per_sec << std::endl;
242
243

    return pass;
244
245
246
247
}

} // namespace profiler
} // namespace ck