avg_pool3d_bwd.cpp 6.73 KB
Newer Older
rocking's avatar
rocking committed
1
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
2
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
rocking's avatar
rocking committed
3
4
5
6
7
8
9
10
11

#include <iomanip>
#include <vector>
#include <iostream>

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

12
#include "ck/library/tensor_operation_instance/gpu/avg_pool3d_bwd.hpp"
rocking's avatar
rocking committed
13

14
15
using DOutDataType = ck::half_t;
using DInDataType  = ck::half_t;
rocking's avatar
rocking committed
16

17
18
using DOutLayout = ck::tensor_layout::convolution::NDHWC;
using DInLayout  = ck::tensor_layout::convolution::NDHWC;
rocking's avatar
rocking committed
19
20
21
22
23

struct SimpleDeviceMem
{
    SimpleDeviceMem() = delete;

24
    SimpleDeviceMem(std::size_t mem_size) : p_mem_{}, mMemSize_(mem_size)
rocking's avatar
rocking committed
25
26
27
28
29
30
    {
        (void)hipMalloc(static_cast<void**>(&p_mem_), mem_size);
    }

    void* GetDeviceBuffer() { return p_mem_; }

31
32
    void SetZero() const { (void)hipMemset(p_mem_, 0, mMemSize_); }

rocking's avatar
rocking committed
33
34
35
    ~SimpleDeviceMem() { (void)hipFree(p_mem_); }

    void* p_mem_;
36
    std::size_t mMemSize_;
rocking's avatar
rocking committed
37
38
39
40
};

int main(int argc, char* argv[])
{
rocking's avatar
rocking committed
41
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
    ck::index_t N                 = 2;
    ck::index_t C                 = 32;
    ck::index_t Z                 = 2;
    ck::index_t Y                 = 2;
    ck::index_t X                 = 2;
    ck::index_t Di                = 30;
    ck::index_t Hi                = 30;
    ck::index_t Wi                = 30;
    ck::index_t window_stride_d   = 2;
    ck::index_t window_stride_h   = 2;
    ck::index_t window_stride_w   = 2;
    ck::index_t window_dilation_d = 1;
    ck::index_t window_dilation_h = 1;
    ck::index_t window_dilation_w = 1;
    ck::index_t in_left_pad_d     = 1;
    ck::index_t in_left_pad_h     = 1;
    ck::index_t in_left_pad_w     = 1;
    ck::index_t in_right_pad_d    = 1;
    ck::index_t in_right_pad_h    = 1;
    ck::index_t in_right_pad_w    = 1;

    const ck::index_t Zs = (Z - 1) * window_dilation_d + 1;
    const ck::index_t Ys = (Y - 1) * window_dilation_h + 1;
    const ck::index_t Xs = (X - 1) * window_dilation_w + 1;
    ck::index_t Do       = (Di + in_left_pad_d + in_right_pad_d - Zs) / window_stride_d + 1;
    ck::index_t Ho       = (Hi + in_left_pad_h + in_right_pad_h - Ys) / window_stride_h + 1;
    ck::index_t Wo       = (Wi + in_left_pad_w + in_right_pad_w - Xs) / window_stride_w + 1;
rocking's avatar
rocking committed
68
69
70
71
72

    // Pool API only support the order of NCDHW
    std::vector<ck::index_t> in_length              = {N, C, Di, Hi, Wi};
    std::vector<ck::index_t> out_length             = {N, C, Do, Ho, Wo};
    std::vector<ck::index_t> window_spatial_lengths = {Z, Y, X};
rocking's avatar
rocking committed
73
74
75
    std::vector<ck::index_t> window_strides = {window_stride_d, window_stride_h, window_stride_w};
    std::vector<ck::index_t> window_dilations{
        window_dilation_d, window_dilation_h, window_dilation_w};
rocking's avatar
rocking committed
76
77
78
79
80
81
82
83
84
85
    std::vector<ck::index_t> input_left_pads  = {in_left_pad_d, in_left_pad_h, in_left_pad_w};
    std::vector<ck::index_t> input_right_pads = {in_right_pad_d, in_right_pad_h, in_right_pad_w};

    std::size_t in_tensor_size  = N * C * Di * Hi * Wi;
    std::size_t out_tensor_size = N * C * Do * Ho * Wo;

    // tensor layout = NDHWC
    std::vector<ck::index_t> in_tensor_stride  = {Di * C * Hi * Wi, 1, C * Hi * Wi, Wi * C, C};
    std::vector<ck::index_t> out_tensor_stride = {Do * C * Ho * Wo, 1, C * Ho * Wo, Wo * C, C};

86
87
    SimpleDeviceMem dout_device_buf(sizeof(DOutDataType) * out_tensor_size);
    SimpleDeviceMem din_device_buf(sizeof(DInDataType) * in_tensor_size);
rocking's avatar
rocking committed
88

89
90
    using DeviceOp = ck::tensor_operation::device::
        DeviceAvgPoolBwd<3, DOutDataType, DInDataType, DOutLayout, DInLayout>;
rocking's avatar
rocking committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

    // 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;

    std::string best_op_name;
    bool found            = false;
    int best_op_id        = -1;
    float best_ave_time   = std::numeric_limits<float>::max();
    float best_gb_per_sec = 0;

    // profile device operation instances
    std::cout << "Run all instances and do timing" << std::endl;

    for(int i = 0; i < op_ptrs.size(); ++i)
    {
        auto& op_ptr      = op_ptrs[i];
        auto argument_ptr = op_ptr->MakeArgumentPointer(
111
112
            static_cast<DOutDataType*>(dout_device_buf.GetDeviceBuffer()),
            static_cast<DInDataType*>(din_device_buf.GetDeviceBuffer()),
rocking's avatar
rocking committed
113
            out_length,
114
            in_length,
rocking's avatar
rocking committed
115
            out_tensor_stride,
116
117
            in_tensor_stride,
            window_spatial_lengths,
rocking's avatar
rocking committed
118
            window_strides,
rocking's avatar
rocking committed
119
            window_dilations,
rocking's avatar
rocking committed
120
            input_left_pads,
121
            input_right_pads);
rocking's avatar
rocking committed
122
123
124
125
126
127
128

        auto invoker_ptr = op_ptr->MakeInvokerPointer();

        std::string op_name = op_ptr->GetTypeString();

        if(op_ptr->IsSupportedArgument(argument_ptr.get()))
        {
129
130
            din_device_buf.SetZero();

rocking's avatar
rocking committed
131
132
133
            float ave_time = invoker_ptr->Run(argument_ptr.get(), StreamConfig{nullptr, true});

            std::size_t num_bytes =
134
                in_tensor_size * sizeof(DInDataType) + out_tensor_size * sizeof(DOutDataType);
rocking's avatar
rocking committed
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

            float gb_per_sec = num_bytes / 1.E6 / ave_time;

            std::cout << "Perf: " << std::setw(10) << ave_time << " ms, " << gb_per_sec << " GB/s, "
                      << op_name << std::endl;

            if(ave_time < best_ave_time)
            {
                found           = true;
                best_op_id      = i;
                best_op_name    = op_name;
                best_ave_time   = ave_time;
                best_gb_per_sec = gb_per_sec;
            }
        }
        else
        {
            std::cout << op_name << " does not support this problem" << std::endl;
        }
    }

    // run the best intance
    if(found)
    {
        std::cout << "Best Perf: " << best_ave_time << " ms, " << best_gb_per_sec << " GB/s, "
                  << best_op_name << std::endl;

        auto& op_ptr = op_ptrs[best_op_id];
        std::cout << "Run the best instance without timing: " << op_ptr->GetTypeString()
                  << std::endl;

        auto argument_ptr = op_ptr->MakeArgumentPointer(
167
168
            static_cast<DOutDataType*>(dout_device_buf.GetDeviceBuffer()),
            static_cast<DInDataType*>(din_device_buf.GetDeviceBuffer()),
rocking's avatar
rocking committed
169
            out_length,
170
            in_length,
rocking's avatar
rocking committed
171
            out_tensor_stride,
172
173
            in_tensor_stride,
            window_spatial_lengths,
rocking's avatar
rocking committed
174
            window_strides,
rocking's avatar
rocking committed
175
            window_dilations,
rocking's avatar
rocking committed
176
            input_left_pads,
177
            input_right_pads);
rocking's avatar
rocking committed
178
179
180
181
182

        auto invoker_ptr = op_ptr->MakeInvokerPointer();

        if(op_ptr->IsSupportedArgument(argument_ptr.get()))
        {
183
            din_device_buf.SetZero();
rocking's avatar
rocking committed
184
185
186
187
188
189
190
191
            invoker_ptr->Run(argument_ptr.get(), StreamConfig{nullptr, false});
        }

        std::cout << "Done" << std::endl;
    }

    return 0;
}