perf.cpp 4.13 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
/*
 * 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.
 */
Paul's avatar
Paul committed
24
25
26
#include "perf.hpp"

#include <migraphx/generate.hpp>
27
28
#include <migraphx/instruction.hpp>
#include <migraphx/instruction_ref.hpp>
29
#include <migraphx/register_target.hpp>
Paul's avatar
Paul committed
30
31
32
33
34
35
36
37
#ifdef HAVE_GPU
#include <migraphx/gpu/hip.hpp>
#endif

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

38
39
40
41
42
43
template <class T>
auto get_hash(const T& x)
{
    return std::hash<T>{}(x);
}

44
45
46
47
parameter_map fill_param_map(parameter_map& m,
                             const std::unordered_map<std::string, shape>& param_shapes,
                             const target& t,
                             bool offload)
48
{
49
    for(auto&& x : param_shapes)
50
51
52
    {
        argument& arg = m[x.first];
        if(arg.empty())
53
54
        {
            assert(not x.second.dynamic());
55
            arg = generate_argument(x.second, get_hash(x.first));
56
        }
57
58
59
60
61
62
        if(not offload)
            arg = t.copy_to(arg);
    }
    return m;
}

63
parameter_map create_param_map(const program& p, const target& t, bool offload)
64
{
65
    parameter_map m;
66
67
68
69
70
71
72
73
74
75
76
    for(auto&& x : p.get_parameter_shapes())
    {
        auto arg = generate_argument(x.second, get_hash(x.first));
        if(offload)
            m[x.first] = arg;
        else
            m[x.first] = t.copy_to(arg);
    }
    return m;
}

77
parameter_map create_param_map(const program& p, bool gpu)
Paul's avatar
Paul committed
78
{
79
    parameter_map m;
Paul's avatar
Paul committed
80
81
82
83
    for(auto&& x : p.get_parameter_shapes())
    {
#ifdef HAVE_GPU
        if(gpu)
84
            m[x.first] = gpu::to_gpu(generate_argument(x.second, get_hash(x.first)));
Paul's avatar
Paul committed
85
        else
Paul's avatar
Paul committed
86
#else
Paul's avatar
Paul committed
87
        (void)gpu;
Paul's avatar
Paul committed
88
#endif
89
            m[x.first] = generate_argument(x.second, get_hash(x.first));
Paul's avatar
Paul committed
90
91
92
93
    }
    return m;
}

94
95
96
target get_target(bool gpu)
{
    if(gpu)
97
        return make_target("gpu");
98
    else
99
        return make_target("cpu");
100
101
}

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
bool is_offload_copy_set(const program& p)
{
    assert(p.is_compiled());
    const module* mm                     = p.get_main_module();
    std::vector<std::string> param_names = mm->get_parameter_names();
    std::unordered_set<instruction_ref> param_ins;
    std::transform(param_names.begin(),
                   param_names.end(),
                   std::inserter(param_ins, param_ins.begin()),
                   [&](const auto& i) { return mm->get_parameter(i); });
    for(const auto& i : *mm)
    {
        if(i.name() == "hip::copy_to_gpu")
        {
            auto copy_arg = instruction::get_output_alias(i.inputs().front(), true);
            param_ins.erase(copy_arg);
        }
        else if(i.name() == "@return")
        {
            auto return_args = i.inputs();
            for(const auto& j : return_args)
            {
                auto alias_ins = instruction::get_output_alias(j, true);
                if((alias_ins->name() == "@param" && param_ins.erase(alias_ins) == 0) or
                   (alias_ins->name() != "hip::copy_from_gpu"))
                    return false;
            }
        }
    }
    return param_ins.empty();
}

134
} // namespace  MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
135
136
} // namespace driver
} // namespace migraphx