avgpool3d_bwd_fp16.cpp 6.41 KB
Newer Older
rocking's avatar
rocking committed
1
2
3
4
5
6
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.

#include <iostream>

#include "ck/ck.hpp"
rocking's avatar
rocking committed
7
#include "ck/tensor_operation/gpu/device/impl/device_avgpool3d_bwd_impl.hpp"
rocking's avatar
rocking committed
8
9
10
11
12
13
#include "ck/tensor_operation/gpu/element/element_wise_operation.hpp"

#include "ck/library/utility/check_err.hpp"
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_tensor.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
rocking's avatar
rocking committed
14
15
#include "ck/library/utility/literals.hpp"
#include "ck/library/reference_tensor_operation/cpu/reference_avgpool_bwd.hpp"
rocking's avatar
rocking committed
16

rocking's avatar
rocking committed
17
18
19
using DOutDataType    = float;
using DInDataType     = float;
using ComputeDataType = float;
rocking's avatar
rocking committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

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

bool pool3d_bwd_test(bool do_verification,
                     bool time_kernel,
                     ck::index_t N,
                     ck::index_t C,
                     ck::index_t Di,
                     ck::index_t Hi,
                     ck::index_t Wi,
                     std::vector<ck::index_t> window_lengths,
                     std::vector<ck::index_t> window_strides,
                     std::vector<ck::index_t> window_dilations,
                     std::vector<ck::index_t> dinput_left_pads,
                     std::vector<ck::index_t> dinput_right_pads)
{
rocking's avatar
rocking committed
36
37
38
39
40
41
42
43
44
45
46
    using DevicePoolBwdInstance =
        ck::tensor_operation::device::DeviceAvgPool3dBwdImpl<DOutDataType,
                                                             DInDataType,
                                                             ComputeDataType, // ComputeDataType
                                                             64,              // BlockSize
                                                             64, // ReduceMThreadClusterSize
                                                             1,  // ReduceKThreadClusterSize
                                                             1,  // ReduceMThreadSliceSize
                                                             1,  // ReduceKThreadSliceSize
                                                             1>; // InSrcOutDstVectorSize

rocking's avatar
rocking committed
47
48
49
50
51
52
53
54
55
56
57
58
    auto OutSpatialLength = [&](auto InSpatialLength, int index) {
        ck::index_t left_pad   = dinput_left_pads[index];
        ck::index_t right_pad  = dinput_right_pads[index];
        ck::index_t window_len = window_lengths[index];
        ck::index_t stride     = window_strides[index];
        return (InSpatialLength + left_pad + right_pad - window_len) / stride + 1;
    };

    ck::index_t Do = OutSpatialLength(Di, 0);
    ck::index_t Ho = OutSpatialLength(Hi, 1);
    ck::index_t Wo = OutSpatialLength(Wi, 1);

rocking's avatar
rocking committed
59
60
61
62
63
64
65
66
67
68
    auto f_host_tensor_descriptor =
        [](std::size_t N_, std::size_t C_, std::size_t D, std::size_t H, std::size_t W) {
            using namespace ck::literals;
            return HostTensorDescriptor({N_, C_, D, H, W},
                                        {D * C_ * H * W, 1_uz, C_ * H * W, W * C_, C_});
        };

    Tensor<DOutDataType> dout(f_host_tensor_descriptor(N, C, Do, Ho, Wo));
    Tensor<DInDataType> din_dev(f_host_tensor_descriptor(N, C, Di, Hi, Wi));
    Tensor<DInDataType> din_host(f_host_tensor_descriptor(N, C, Di, Hi, Wi));
rocking's avatar
rocking committed
69
70
71
72
73
74

    std::cout << "dout: " << dout.mDesc << std::endl;
    std::cout << "din_host: " << din_host.mDesc << std::endl;

    dout.GenerateTensorValue(GeneratorTensor_3<DOutDataType>{0.0, 1.0});

rocking's avatar
rocking committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    DeviceMem dout_device_buf(sizeof(DOutDataType) * dout.mDesc.GetElementSpaceSize());
    DeviceMem din_device_buf(sizeof(DInDataType) * din_dev.mDesc.GetElementSpaceSize());

    dout_device_buf.ToDevice(dout.mData.data());

    auto pool        = DevicePoolBwdInstance{};
    auto invoker_ptr = pool.MakeInvokerPointer();
    auto argument_ptr =
        pool.MakeArgumentPointer(static_cast<DOutDataType*>(dout_device_buf.GetDeviceBuffer()),
                                 static_cast<DInDataType*>(din_device_buf.GetDeviceBuffer()),
                                 {N, C, Do, Ho, Wo},
                                 {N, C, Di, Hi, Wi},
                                 {Do * C * Ho * Wo, 1, C * Ho * Wo, Wo * C, C},
                                 {Di * C * Hi * Wi, 1, C * Hi * Wi, Wi * C, C},
rocking's avatar
rocking committed
89
                                 window_lengths,
rocking's avatar
rocking committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
                                 window_strides,
                                 window_dilations,
                                 dinput_left_pads,
                                 dinput_right_pads);

    if(!pool.IsSupportedArgument(argument_ptr.get()))
    {
        throw std::runtime_error("wrong! device_op with the specified compilation parameters does "
                                 "not support this problem");
    }

    float ave_time = invoker_ptr->Run(argument_ptr.get(), StreamConfig{nullptr, time_kernel});
    std::cout << "Perf: " << ave_time << std::endl;

    bool pass = true;

rocking's avatar
rocking committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    if(do_verification)
    {
        auto ref_pool =
            ck::tensor_operation::host::ReferenceAvgPoolBwd<3, DInDataType, DOutDataType>();

        auto ref_invoker = ref_pool.MakeInvoker();

        auto ref_argument = ref_pool.MakeArgument(din_host,
                                                  dout,
                                                  window_lengths,
                                                  window_strides,
                                                  window_dilations,
                                                  dinput_left_pads,
                                                  dinput_right_pads);

        ref_invoker.Run(ref_argument);
rocking's avatar
rocking committed
122
123
124

        din_device_buf.FromDevice(din_dev.mData.data());
        pass = ck::utils::check_err(din_dev, din_host);
rocking's avatar
rocking committed
125
126
    }

rocking's avatar
rocking committed
127
    return pass;
rocking's avatar
rocking committed
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
}

int main()
{
    std::vector<ck::index_t> window_lengths    = {5, 5, 5};
    std::vector<ck::index_t> window_strides    = {2, 2, 2};
    std::vector<ck::index_t> window_dilations  = {1, 1, 1};
    std::vector<ck::index_t> dinput_left_pads  = {0, 0, 0};
    std::vector<ck::index_t> dinput_right_pads = {0, 0, 0};

    ck::index_t N  = 1;
    ck::index_t C  = 16;
    ck::index_t Di = 40;
    ck::index_t Hi = 40;
    ck::index_t Wi = 40;

    pool3d_bwd_test(true,
                    false,
                    N,
                    C,
                    Di,
                    Hi,
                    Wi,
                    window_lengths,
                    window_strides,
                    window_dilations,
                    dinput_left_pads,
                    dinput_right_pads);
}