"host/host_tensor/include/device.hpp" did not exist on "5c7cec11159d22636dd4c1119e7e430d156a8df7"
profile_groupnorm.cpp 3.25 KB
Newer Older
rocking's avatar
rocking committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

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

#include "profiler/include/profile_groupnorm_impl.hpp"

using ck::index_t;
using ck::profiler::ElementwiseOpEnum;

struct GroupnormArgParser
{
    std::unordered_map<std::string, std::vector<int>> long_opts = {{"length", {}}};

    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_groupnorm()
{
    std::cout << "arg1: tensor operation (groupnorm: Group normalization)\n"
              << "arg2: data type (0: fp16; 1: fp32)\n"
              << "arg3: verification (0: no; 1: yes)\n"
              << "arg4: initialization (0: no init; 1: integer value; 2: decimal value)\n"
              << "arg5: print tensor value (0: no; 1: yes)\n"
              << "arg6: time kernel (0=n0, 1=yes)\n"
              << "arg7: out elementwise op (0=passthrough, 1=sigmoid)\n"
55
              << "--length: tensor extents (e.g, --length 1 16 16 32 40) \n"
rocking's avatar
rocking committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
              << std::endl;
}

int profile_groupnorm(int argc, char* argv[])
{
    ck::DataTypeEnum data_type         = ck::DataTypeEnum::Half;
    bool do_verification               = false;
    int init_method                    = 0;
    bool do_log                        = 0;
    bool time_kernel                   = 1;
    ElementwiseOpEnum outElementwiseOp = ElementwiseOpEnum::eSigmoid;
    std::vector<index_t> length        = {1, 16, 16, 32, 40};

    if(argc != 1 && argc != 14)
    {
        print_help_groupnorm();
        return 0;
    }

    if(argc == 14)
    {
        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]);
        outElementwiseOp = static_cast<ElementwiseOpEnum>(std::stoi(argv[7]));

        // parse the long options
        GroupnormArgParser arg_parser;
        arg_parser(argc, argv);
        length = arg_parser.long_opts["length"];
    }

    using F16 = ck::half_t;
    using F32 = float;

    if(data_type == ck::DataTypeEnum::Half && outElementwiseOp == ElementwiseOpEnum::eSigmoid)
    {
        ck::profiler::profile_groupnorm_impl<F16, F16, F16, F32, F16>(
            do_verification, init_method, do_log, time_kernel, length, outElementwiseOp);
    }
    else
    {
        throw std::runtime_error("not implemented yet");
    }

    return 0;
}

// hijack main() for quick debugging
// int main(int argc, char* argv[])
// {
//     profile_groupnorm(argc, argv);
//     return 0;
// }