profile_layernorm_fwd.cpp 3.69 KB
Newer Older
rocking5566's avatar
rocking5566 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.
rocking5566's avatar
rocking5566 committed
3
4
5
6
7

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

8
#include "profiler/data_type_enum.hpp"
rocking's avatar
rocking committed
9
#include "profiler/profile_layernorm_fwd_impl.hpp"
10
#include "profiler_operation_registry.hpp"
rocking5566's avatar
rocking5566 committed
11
12
13
14
15

using ck::index_t;

struct LayernormArgParser
{
16
    std::unordered_map<std::string, std::vector<int>> long_opts = {{"length", {}}};
rocking5566's avatar
rocking5566 committed
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

    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_layernorm()
{
    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"
rocking5566's avatar
rocking5566 committed
53
              << "arg5: time kernel (0=no, 1=yes)\n"
rocking5566's avatar
rocking5566 committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
              << "--length: tensor extents (e.g, --length 1024 1024) \n"
              << std::endl;
}

int profile_layernorm(int argc, char* argv[])
{
    if(argc <= 2)
    {
        print_help_layernorm();
        return 0;
    }

    LayernormArgParser arg_parser;

    // short unnamed options
    const ck::DataTypeEnum data_type = static_cast<ck::DataTypeEnum>(std::stoi(argv[2]));
    const bool do_verification       = std::stoi(argv[3]);
    const int init_method            = std::stoi(argv[4]);
    const bool do_log                = std::stoi(argv[5]);
    const bool time_kernel           = std::stoi(argv[6]);

    // parse the long options
    arg_parser(argc, argv);
77
    const std::vector<index_t> length = arg_parser.long_opts["length"];
rocking5566's avatar
rocking5566 committed
78

rocking's avatar
rocking committed
79
80
    using F16 = ck::half_t;
    using F32 = float;
rocking5566's avatar
rocking5566 committed
81

rocking's avatar
rocking committed
82
    if(length.size() == 2)
rocking5566's avatar
rocking5566 committed
83
    {
rocking's avatar
rocking committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        constexpr int rank = 2;

        if(data_type == ck::DataTypeEnum::Half)
        {
            ck::profiler::profile_layernorm_impl<F16, F16, F16, F32, F16, F32, false, rank>(
                do_verification, init_method, do_log, time_kernel, length);
        }
        else if(data_type == ck::DataTypeEnum::Float)
        {
            ck::profiler::profile_layernorm_impl<F32, F32, F32, F32, F32, F32, false, rank>(
                do_verification, init_method, do_log, time_kernel, length);
        }
        else
        {
            throw std::runtime_error("not implemented yet");
        }
rocking5566's avatar
rocking5566 committed
100
    }
rocking's avatar
rocking committed
101
    else if(length.size() == 4)
rocking5566's avatar
rocking5566 committed
102
    {
rocking's avatar
rocking committed
103
104
105
106
        constexpr int rank = 4;

        if(data_type == ck::DataTypeEnum::Half)
        {
107
            ck::profiler::profile_layernorm_impl<F16, F16, F16, F32, F16, F16, false, rank>(
rocking's avatar
rocking committed
108
109
110
111
112
113
114
115
116
117
118
                do_verification, init_method, do_log, time_kernel, length);
        }
        else if(data_type == ck::DataTypeEnum::Float)
        {
            ck::profiler::profile_layernorm_impl<F32, F32, F32, F32, F32, F32, false, rank>(
                do_verification, init_method, do_log, time_kernel, length);
        }
        else
        {
            throw std::runtime_error("not implemented yet");
        }
rocking5566's avatar
rocking5566 committed
119
120
121
122
123
124
125
126
    }
    else
    {
        throw std::runtime_error("not implemented yet");
    }

    return 0;
}
127
128

REGISTER_PROFILER_OPERATION("layernorm", "Layer Normalization", profile_layernorm);