miopen.cpp 1.96 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 test1
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
74
{
    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;
    }
};

int main() { verify_program<test1>(); }