miopen.cpp 2.15 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
    p.add_instruction(rtg::activation{"relu"}, conv);
    return p;
}

Paul's avatar
Paul committed
26
// TODO: Move to header
Paul's avatar
Paul committed
27
28
rtg::argument get_tensor_argument_gpu(rtg::shape s)
{
Paul's avatar
Paul committed
29
    auto v = rtg::generate_tensor_data<float>(s);
Paul's avatar
Paul committed
30
    auto p = rtg::share(rtg::miopen::write_to_gpu(v));
Paul's avatar
Paul committed
31
32
33
34
35
36
37
    return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

std::vector<float> cpu()
{
    std::vector<float> result;
    auto p = create_program();
Paul's avatar
Paul committed
38
39
    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
40
    p.compile(rtg::cpu::cpu_target{});
Paul's avatar
Paul committed
41
    auto r      = p.eval({{"x", x}, {"w", w}});
Paul's avatar
Paul committed
42
43
    auto output = r.get<float>();
    result.assign(output.begin(), output.end());
Paul's avatar
Paul committed
44
45
46
47
48
49
50
51
52
53
    return result;
}

std::vector<float> gpu()
{
    std::vector<float> result;
    auto p = create_program();
    auto x = get_tensor_argument_gpu({rtg::shape::float_type, {4, 3, 3, 3}});
    auto w = get_tensor_argument_gpu({rtg::shape::float_type, {4, 3, 3, 3}});
    p.compile(rtg::miopen::miopen_target{});
Paul's avatar
Paul committed
54
    auto y      = get_tensor_argument_gpu(p.get_parameter_shape("output"));
Paul's avatar
Paul committed
55
    auto handle = rtg::miopen::make_obj<rtg::miopen::miopen_handle>(&miopenCreate);
Paul's avatar
Paul committed
56
57
    auto r      = p.eval(
        {{"x", x}, {"w", w}, {"output", y}, {"handle", {rtg::shape::any_type, handle.get()}}});
Paul's avatar
Paul committed
58
    result = rtg::miopen::read_from_gpu<float>(r.data(), r.get_shape().elements());
Paul's avatar
Paul committed
59
60
61
    return result;
}

Paul's avatar
Paul committed
62
void test1()
Paul's avatar
Paul committed
63
64
65
{
    auto x = cpu();
    auto y = gpu();
Paul's avatar
Paul committed
66
    EXPECT(test::verify_range(x, y));
Paul's avatar
Paul committed
67
68
}

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