"vscode:/vscode.git/clone" did not exist on "469543448d23a5eab843570b5e6c31444601f5b3"
perfdb.cpp 5.27 KB
Newer Older
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
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <migraphx/gpu/perfdb.hpp>
#include <migraphx/value.hpp>
#include <migraphx/sqlite.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/permutation.hpp>
#include <fstream>
jungpark-mlir's avatar
jungpark-mlir committed
30
31
#include <mutex>

jungpark-mlir's avatar
jungpark-mlir committed
32
static std::mutex g_db_mutex; // NOLINT
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
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

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

namespace {

std::string get_layout(const shape& s, std::string labels)
{
    auto result = labels;
    auto p      = find_permutation(s);
    std::transform(p.begin(), p.end(), result.begin(), [&](auto i) { return labels[i]; });
    return "'" + result + "'";
}

std::string get_type(const shape& s)
{
    static const std::unordered_map<shape::type_t, std::string> m = {
        {shape::float_type, "'FP32'"},
        {shape::half_type, "'FP16'"},
        {shape::double_type, "'FP64'"},
        {shape::int8_type, "'INT8'"},
        {shape::int32_type, "'INT32'"},
    };
    auto it = m.find(s.type());
    if(it == m.end())
        return "UNKNOWN";
    return it->second;
}

std::string generate_miopen_config(const problem_params& pp)
{
    value v       = pp.op.to_value();
    auto input    = pp.inputs[0].lens();
    auto weights  = pp.inputs[1].lens();
    auto padding  = v["padding"].to_vector<std::size_t>();
    auto stride   = v["stride"].to_vector<std::size_t>();
    auto dilation = v["dilation"].to_vector<std::size_t>();
    if(padding.size() != stride.size())
        padding.erase(padding.begin() + padding.size() / 2, padding.end());
    return to_string_range({std::string{" C.in_channels="},       to_string(input[1]),
                            std::string{" AND C.in_h="},          to_string(input[2]),
                            std::string{" AND C.in_w="},          to_string(input[3]),
                            std::string{" AND C.fil_h="},         to_string(weights[2]),
                            std::string{" AND C.fil_w="},         to_string(weights[3]),
                            std::string{" AND C.out_channels="},  to_string(weights[0]),
                            std::string{" AND C.batchsize="},     to_string(input[0]),
                            std::string{" AND C.pad_h="},         to_string(padding[0]),
                            std::string{" AND C.pad_w="},         to_string(padding[2]),
                            std::string{" AND C.dilation_h="},    to_string(dilation[0]),
                            std::string{" AND C.dilation_w="},    to_string(dilation[1]),
                            std::string{" AND C.conv_stride_h="}, to_string(stride[0]),
                            std::string{" AND C.conv_stride_w="}, to_string(stride[1]),
                            std::string{" AND C.layout="},        get_layout(pp.inputs[0], "NCHW"),
                            std::string{" AND C.data_type="},     get_type(pp.inputs[0]),
                            std::string{" AND C.direction="},     std::string{"'F'"}},
                           " ");
}

auto query_miopen_db(const std::string& query)
{
jungpark-mlir's avatar
jungpark-mlir committed
94
95
    const std::lock_guard<std::mutex> lock(g_db_mutex);

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    // TODO: Store db as a static variable
    const auto dbpath = fs::path{"/opt"} / "rocm" / "share" / "miopen" / "db" / "miopen.db";
    // Check if db file exists.
    std::ifstream dbs(dbpath);
    if(dbs.is_open())
    {
        dbs.close();
    }
    else
    {
        std::vector<std::unordered_map<std::string, std::string>> empty;
        return empty;
    }

    auto db = sqlite::read(dbpath);
    return db.execute(query);
}

} // namespace

116
std::string get_mlir_perf_for_conv(const problem_params& pp, bool xdlops)
117
{
118
119
    std::string solver = xdlops ? "ConvMlirIgemmFwdXdlops" : "ConvMlirIgemmFwd";
    std::string query  = "select P.* \
120
121
                             from perf_db P, config C \
                             where P.config = C.id AND \
122
                             P.solver = '${solver}' AND \
123
124
                             ${config}";

125
126
    auto results = query_miopen_db(
        interpolate_string(query, {{"config", generate_miopen_config(pp)}, {"solver", solver}}));
127
128
129
130
131
132
133
134
    if(results.empty())
        return "";
    return results.front().at("params");
}

} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx