".github/vscode:/vscode.git/clone" did not exist on "57cc5385c0c58c1925ab1da5050aedf987b72cc0"
miopen.cpp 1.95 KB
Newer Older
Paul's avatar
Paul committed
1
2
3

#include <rtg/program.hpp>
#include <rtg/operators.hpp>
Paul's avatar
Paul committed
4
#include <rtg/generate.hpp>
Paul's avatar
Paul committed
5
6
#include <rtg/cpu/cpu_target.hpp>
#include <rtg/miopen/miopen_target.hpp>
Paul's avatar
Paul committed
7
8
#include <rtg/miopen/miopen.hpp>
#include <rtg/miopen/hip.hpp>
Paul's avatar
Paul committed
9
10
11
12
13
#include <rtg/manage_ptr.hpp>

#include <miopen/miopen.h>

#include "test.hpp"
Paul's avatar
Paul committed
14
#include "verify.hpp"
Paul's avatar
Paul committed
15
16
17
18

rtg::program create_program()
{
    rtg::program p;
Paul's avatar
Paul committed
19
    auto input   = p.add_parameter("x", rtg::shape{rtg::shape::float_type, {4, 3, 3, 3}});
Paul's avatar
Paul committed
20
    auto weights = p.add_parameter("w", rtg::shape{rtg::shape::float_type, {4, 3, 3, 3}});
Paul's avatar
Paul committed
21
    auto conv    = p.add_instruction(rtg::convolution{}, input, weights);
Paul's avatar
Paul committed
22
23
24
25
26
27
28
29
    p.add_instruction(rtg::activation{"relu"}, conv);
    return p;
}

std::vector<float> cpu()
{
    std::vector<float> result;
    auto p = create_program();
Paul's avatar
Paul committed
30
31
    auto x = rtg::generate_argument({rtg::shape::float_type, {4, 3, 3, 3}});
    auto w = rtg::generate_argument({rtg::shape::float_type, {4, 3, 3, 3}});
Paul's avatar
Paul committed
32
    p.compile(rtg::cpu::cpu_target{});
Paul's avatar
Paul committed
33
    auto r      = p.eval({{"x", x}, {"w", w}});
Paul's avatar
Paul committed
34
35
    auto output = r.get<float>();
    result.assign(output.begin(), output.end());
Paul's avatar
Paul committed
36
37
38
39
40
41
42
    return result;
}

std::vector<float> gpu()
{
    std::vector<float> result;
    auto p = create_program();
Paul's avatar
Paul committed
43
44
    auto x = rtg::miopen::to_gpu(rtg::generate_argument({rtg::shape::float_type, {4, 3, 3, 3}}));
    auto w = rtg::miopen::to_gpu(rtg::generate_argument({rtg::shape::float_type, {4, 3, 3, 3}}));
Paul's avatar
Paul committed
45
    p.compile(rtg::miopen::miopen_target{});
Paul's avatar
Paul committed
46
    auto y      = rtg::miopen::to_gpu(rtg::generate_argument(p.get_parameter_shape("output")));
Paul's avatar
Paul committed
47
    auto handle = rtg::miopen::make_obj<rtg::miopen::miopen_handle>(&miopenCreate);
Paul's avatar
Paul committed
48
49
    auto r      = p.eval(
        {{"x", x}, {"w", w}, {"output", y}, {"handle", {rtg::shape::any_type, handle.get()}}});
Paul's avatar
Paul committed
50
    result = rtg::miopen::read_from_gpu<float>(r.data(), r.get_shape().elements());
Paul's avatar
Paul committed
51
52
53
    return result;
}

Paul's avatar
Paul committed
54
void test1()
Paul's avatar
Paul committed
55
56
57
{
    auto x = cpu();
    auto y = gpu();
Paul's avatar
Paul committed
58
    EXPECT(test::verify_range(x, y));
Paul's avatar
Paul committed
59
60
}

Paul's avatar
Paul committed
61
int main() { test1(); }