miopen.cpp 2.81 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

Paul's avatar
Paul committed
16
template <class V>
Paul's avatar
Paul committed
17
rtg::argument run_cpu()
Paul's avatar
Paul committed
18
{
Paul's avatar
Paul committed
19
20
    V v;
    auto p = v.create_program();
Paul's avatar
Paul committed
21
    p.compile(rtg::cpu::cpu_target{});
Paul's avatar
Paul committed
22
    return p.eval(v.create_params());
Paul's avatar
Paul committed
23
24
}

Paul's avatar
Paul committed
25
template <class V>
Paul's avatar
Paul committed
26
rtg::argument run_gpu()
Paul's avatar
Paul committed
27
{
Paul's avatar
Paul committed
28
29
    V v;
    auto p = v.create_program();
Paul's avatar
Paul committed
30
    p.compile(rtg::miopen::miopen_target{});
Paul's avatar
Paul committed
31
32

    auto m = v.create_params();
Paul's avatar
Paul committed
33
    for(auto&& e : m)
Paul's avatar
Paul committed
34
35
36
37
    {
        e.second = rtg::miopen::to_gpu(e.second);
    }

Paul's avatar
Paul committed
38
    m["output"] = rtg::miopen::to_gpu(rtg::generate_argument(p.get_parameter_shape("output")));
Paul's avatar
Paul committed
39
    auto handle = rtg::miopen::make_obj<rtg::miopen::miopen_handle>(&miopenCreate);
Paul's avatar
Paul committed
40
41
42
    m["handle"] = {rtg::shape::any_type, handle.get()};

    return rtg::miopen::from_gpu(p.eval(m));
Paul's avatar
Paul committed
43
44
}

Paul's avatar
Paul committed
45
template <class V>
Paul's avatar
Paul committed
46
void verify_program()
Paul's avatar
Paul committed
47
{
Paul's avatar
Paul committed
48
49
    auto cpu_arg = run_cpu<V>();
    auto gpu_arg = run_gpu<V>();
Paul's avatar
Paul committed
50
    visit_all(cpu_arg, gpu_arg)([](auto cpu, auto gpu) { EXPECT(test::verify_range(cpu, gpu)); });
Paul's avatar
Paul committed
51
52
}

Paul's avatar
Paul committed
53
struct test_conv_relu
Paul's avatar
Paul committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{
    rtg::program create_program() const
    {
        rtg::program p;
        auto input   = p.add_parameter("x", rtg::shape{rtg::shape::float_type, {4, 3, 3, 3}});
        auto weights = p.add_parameter("w", rtg::shape{rtg::shape::float_type, {4, 3, 3, 3}});
        auto conv    = p.add_instruction(rtg::convolution{}, input, weights);
        p.add_instruction(rtg::activation{"relu"}, conv);
        return p;
    }

    rtg::program::parameter_map create_params() const
    {
        rtg::program::parameter_map m;
        m["x"] = rtg::generate_argument({rtg::shape::float_type, {4, 3, 3, 3}});
        m["w"] = rtg::generate_argument({rtg::shape::float_type, {4, 3, 3, 3}});
        return m;
    }
};

Paul's avatar
Paul committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
struct test_conv_pooling
{
    rtg::program create_program() const
    {
        rtg::program p;
        auto input   = p.add_parameter("x", rtg::shape{rtg::shape::float_type, {4, 3, 32, 32}});
        auto weights = p.add_parameter("w", rtg::shape{rtg::shape::float_type, {4, 3, 3, 3}});
        auto conv    = p.add_instruction(rtg::convolution{}, input, weights);
        auto pooling = p.add_instruction(rtg::pooling{"max"}, conv);
        p.add_instruction(rtg::activation{"relu"}, pooling);
        return p;
    }

    rtg::program::parameter_map create_params() const
    {
        rtg::program::parameter_map m;
        m["x"] = rtg::generate_argument({rtg::shape::float_type, {4, 3, 32, 32}});
        m["w"] = rtg::generate_argument({rtg::shape::float_type, {4, 3, 3, 3}});
        return m;
    }
};

Paul's avatar
Paul committed
96
97
98
99
int main()
{
    verify_program<test_conv_relu>();
    verify_program<test_conv_pooling>();
Paul's avatar
Paul committed
100
}