"data/__init__.py" did not exist on "fd31a4693ff3935e668136720345e4e7042543e4"
perf.cpp 2.88 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.
 */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <migraphx/gpu/driver/perf.hpp>
#include <migraphx/context.hpp>
#include <migraphx/generate.hpp>
#include <migraphx/time.hpp>
#include <migraphx/gpu/hip.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {
namespace driver {

std::vector<argument> generate_arguments(const std::vector<shape>& shapes, unsigned long seed = 0)
{
    std::vector<argument> args;
    std::transform(shapes.begin(), shapes.end(), std::back_inserter(args), [&](auto& s) {
        return to_gpu(generate_argument(s, seed++));
    });
    return args;
}

using milliseconds = std::chrono::duration<double, std::milli>;
45
46
std::pair<double, double>
time_op(context& ictx, operation op, const std::vector<shape>& inputs, int n)
47
{
48

49
    // TODO: Use std::ref
50
51
52
53
    migraphx::context ctx = ictx;
    auto& gctx            = any_cast<migraphx::gpu::context>(ctx);
    auto output           = op.compute_shape(inputs);
    op.finalize(ctx, output, inputs);
54
55
    auto args = generate_arguments(inputs);
    auto run  = [&] {
56
57
        op.compute(ctx, output, args);
        ctx.finish();
58
59
    };
    run();
Paul's avatar
Paul committed
60
    // Measure host time
Paul's avatar
Format  
Paul committed
61
    double host_time = 0.0;
62
63
64
65
    for(auto i : range(n))
    {
        (void)i;
        host_time += time<milliseconds>(run);
Paul's avatar
Paul committed
66
    }
Paul's avatar
Paul committed
67
    // Measure device time only for code_object ops which support it
Paul's avatar
Paul committed
68
    double device_time = 0.0;
Paul's avatar
Format  
Paul committed
69
    if(op.name() == "gpu::code_object")
Paul's avatar
Paul committed
70
    {
Paul's avatar
Paul committed
71
        gctx.enable_perf_measurement();
Paul's avatar
Paul committed
72
        run();
Paul's avatar
Paul committed
73
74
75
76
77
78
        for(auto i : range(n))
        {
            (void)i;
            run();
            device_time += gctx.get_elapsed_ms();
        }
79
80
    }
    return std::make_pair(host_time / n, device_time / n);
81
82
83
84
85
86
}

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