"src/vscode:/vscode.git/clone" did not exist on "3ef186fd7e4b1f10db9f74b13a21640a3e587010"
profile_avg_pool2d_fwd.cpp 4.36 KB
Newer Older
rocking's avatar
rocking committed
1
2
3
4
5
6
7
8
9
10
11
12
13
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

#include <iostream>
#include <vector>
#include <unordered_map>

#include "profiler/data_type_enum.hpp"
#include "profiler/profile_pool2d_fwd_impl.hpp"
#include "profiler_operation_registry.hpp"

using ck::index_t;

rocking's avatar
rocking committed
14
struct avgPoolFwdArgParser
rocking's avatar
rocking committed
15
16
{
    std::unordered_map<std::string, std::vector<int>> long_opts = {
rocking's avatar
rocking committed
17
        {"length", {}}, {"wsize", {}}, {"wstride", {}}, {"pad1", {}}, {"pad2", {}}};
rocking's avatar
rocking committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

    bool parse_opt(int argc, char* argv[], const std::string& key, int i)
    {
        if(std::string("--") + key == argv[i])
        {
            int pos = i;
            while(++i < argc && argv[i][0] != '-') {}
            int end = i;
            for(int j = pos + 1; j < end; j++)
            {
                long_opts[key].push_back(std::stoi(argv[j]));
            }
            return true;
        }
        return false;
    }

    void operator()(int argc, char* argv[])
    {
        for(auto& kv : long_opts)
        {
            for(int i = 1; i < argc; i++)
            {
                if(parse_opt(argc, argv, kv.first, i))
                    break;
            }
        }
    }
};

void print_help_avg_pool2d_fwd()
{
    std::cout << "arg1: data type (0: fp16; 1: fp32)\n"
              << "arg2: verification (0: no; 1: yes)\n"
              << "arg3: initialization (0: no init; 1: integer value; 2: decimal value)\n"
              << "arg4: print tensor value (0: no; 1: yes)\n"
              << "arg5: time kernel (0=no, 1=yes)\n"
              << "--length: input tensor length for NDHW(e.g, --length 2 32 30 30) \n"
              << "--wsize: window size for YX (e.g, --wsize 2 2) \n"
              << "--wstride: window stride for HW (e.g, --wstride 2 2) \n"
rocking's avatar
rocking committed
58
59
60
61
              << "--pad1: left side of padding in HW (e.g, --pad1 1 1) \n"
              << "--pad2: right side of padding in HW (e.g, --pad2 1 1) \n"
              << "eg: ckProfiler avg_pool2d_fwd 0 1 2 0 1 0 --length 2 32 30 30 --wsize 2 2 "
                 "--wstride 2 2 --pad1 1 1 --pad2 1 1"
rocking's avatar
rocking committed
62
63
64
65
66
67
68
69
              << std::endl;
}

int profile_avg_pool2d_fwd(int argc, char* argv[])
{
    ck::DataTypeEnum data_type = ck::DataTypeEnum::Half;
    bool do_verification       = true;
    int init_method            = 0;
rocking's avatar
rocking committed
70
    bool do_log                = false;
rocking's avatar
rocking committed
71
72
73
74
75
    bool time_kernel           = true;

    std::vector<index_t> in_length = {2, 32, 30, 30};
    std::vector<index_t> wsize     = {2, 2};
    std::vector<index_t> wstride   = {2, 2};
rocking's avatar
rocking committed
76
77
    std::vector<index_t> pad1      = {1, 1};
    std::vector<index_t> pad2      = {1, 1};
rocking's avatar
rocking committed
78

rocking's avatar
rocking committed
79
    if(argc != 2 && argc != 25)
rocking's avatar
rocking committed
80
81
82
83
    {
        print_help_avg_pool2d_fwd();
        return 0;
    }
rocking's avatar
rocking committed
84
    else if(argc == 25)
rocking's avatar
rocking committed
85
86
87
88
89
90
91
92
    {
        data_type       = static_cast<ck::DataTypeEnum>(std::stoi(argv[2]));
        do_verification = std::stoi(argv[3]);
        init_method     = std::stoi(argv[4]);
        do_log          = std::stoi(argv[5]);
        time_kernel     = std::stoi(argv[6]);

        // parse the long options
rocking's avatar
rocking committed
93
        avgPoolFwdArgParser arg_parser;
rocking's avatar
rocking committed
94
        arg_parser(argc, argv);
rocking's avatar
rocking committed
95
96
97
        in_length = arg_parser.long_opts["length"];
        wsize     = arg_parser.long_opts["wsize"];
        wstride   = arg_parser.long_opts["wstride"];
rocking's avatar
rocking committed
98
99
        pad1      = arg_parser.long_opts["pad1"];
        pad2      = arg_parser.long_opts["pad2"];
rocking's avatar
rocking committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    }

    using F16                 = ck::half_t;
    using F32                 = float;
    using I32                 = int32_t;
    constexpr auto ReduceOpId = ck::ReduceTensorOp::AVG;

    if(data_type == ck::DataTypeEnum::Half)
    {
        ck::profiler::profile_pool2d_fwd_impl<F16, F16, F32, I32, ReduceOpId, false, false>(
            do_verification,
            init_method,
            do_log,
            time_kernel,
            in_length,
            wsize,
            wstride,
rocking's avatar
rocking committed
117
118
            pad1,
            pad2);
rocking's avatar
rocking committed
119
120
121
122
123
124
125
126
127
128
129
    }
    else if(data_type == ck::DataTypeEnum::Float)
    {
        ck::profiler::profile_pool2d_fwd_impl<F32, F32, F32, I32, ReduceOpId, false, false>(
            do_verification,
            init_method,
            do_log,
            time_kernel,
            in_length,
            wsize,
            wstride,
rocking's avatar
rocking committed
130
131
            pad1,
            pad2);
rocking's avatar
rocking committed
132
133
134
135
136
137
138
139
140
141
    }
    else
    {
        throw std::runtime_error("not implemented yet");
    }

    return 0;
}

REGISTER_PROFILER_OPERATION("avg_pool2d_fwd", "avg_pool2d fwd", profile_avg_pool2d_fwd);