miopen.cpp 3.42 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
11
12

#include <rtg/program.hpp>
#include <rtg/operators.hpp>
#include <rtg/cpu/cpu_target.hpp>
#include <rtg/miopen/miopen_target.hpp>
#include <rtg/manage_ptr.hpp>

#include <miopen/miopen.h>

#include <random>

#include "test.hpp"
Paul's avatar
Paul committed
13
#include "verify.hpp"
Paul's avatar
Paul committed
14

Paul's avatar
Paul committed
15
16
using hip_ptr       = RTG_MANAGE_PTR(void, hipFree);
using miopen_handle = RTG_MANAGE_PTR(miopenHandle_t, miopenDestroy);
Paul's avatar
Paul committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

template <class Result, class F, class... Ts>
Result make_obj(F f, Ts... xs)
{
    typename Result::pointer x = nullptr;
    auto status                = f(&x, xs...);
    Result r{x};
    if(status != miopenStatusSuccess)
        RTG_THROW("MIOpen call failed");
    return r;
}

hip_ptr hip_allocate(std::size_t sz)
{
    void* result;
    // TODO: Check status
    hipMalloc(&result, sz);
    return hip_ptr{result};
}

Paul's avatar
Paul committed
37
template <class T>
Paul's avatar
Paul committed
38
39
hip_ptr write(const T& x)
{
Paul's avatar
Paul committed
40
41
    using type  = typename T::value_type;
    auto size   = x.size() * sizeof(type);
Paul's avatar
Paul committed
42
43
44
45
46
47
    auto result = hip_allocate(size);
    // TODO: Check status
    hipMemcpy(result.get(), x.data(), size, hipMemcpyHostToDevice);
    return result;
}

Paul's avatar
Paul committed
48
template <class T>
Paul's avatar
Paul committed
49
std::vector<T> read(const void* x, std::size_t sz)
Paul's avatar
Paul committed
50
51
52
{
    std::vector<T> result(sz);
    // TODO: Check status
Paul's avatar
Paul committed
53
    hipMemcpy(result.data(), x, sz * sizeof(T), hipMemcpyDeviceToHost);
Paul's avatar
Paul committed
54
55
56
57
58
59
    return result;
}

rtg::program create_program()
{
    rtg::program p;
Paul's avatar
Paul committed
60
    auto input   = p.add_parameter("x", rtg::shape{rtg::shape::float_type, {4, 3, 3, 3}});
Paul's avatar
Paul committed
61
    auto weights = p.add_parameter("w", rtg::shape{rtg::shape::float_type, {4, 3, 3, 3}});
Paul's avatar
Paul committed
62
    auto conv    = p.add_instruction(rtg::convolution{}, input, weights);
Paul's avatar
Paul committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    p.add_instruction(rtg::activation{"relu"}, conv);
    return p;
}

std::vector<float> get_tensor_data(rtg::shape s)
{
    std::vector<float> result(s.elements());
    std::mt19937 engine{0};
    std::uniform_real_distribution<> dist;
    std::generate(result.begin(), result.end(), [&] { return dist(engine); });
    return result;
}

rtg::argument get_tensor_argument_cpu(rtg::shape s)
{
    auto v = get_tensor_data(s);
    return {s, [v]() mutable { return reinterpret_cast<char*>(v.data()); }};
}

rtg::argument get_tensor_argument_gpu(rtg::shape s)
{
    auto v = get_tensor_data(s);
    auto p = rtg::share(write(v));
    return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

std::vector<float> cpu()
{
    std::vector<float> result;
    auto p = create_program();
    auto x = get_tensor_argument_cpu({rtg::shape::float_type, {4, 3, 3, 3}});
    auto w = get_tensor_argument_cpu({rtg::shape::float_type, {4, 3, 3, 3}});
    p.compile(rtg::cpu::cpu_target{});
Paul's avatar
Paul committed
96
    auto r = p.eval({{"x", x}, {"w", w}});
Paul's avatar
Paul committed
97
98
99
100
101
102
103
104
105
106
107
    r.visit([&](auto output) { result.assign(output.begin(), output.end()); });
    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
108
    auto y      = get_tensor_argument_gpu(p.get_parameter_shape("output"));
Paul's avatar
Paul committed
109
    auto handle = make_obj<miopen_handle>(&miopenCreate);
Paul's avatar
Paul committed
110
111
    auto r      = p.eval(
        {{"x", x}, {"w", w}, {"output", y}, {"handle", {rtg::shape::any_type, handle.get()}}});
Paul's avatar
Paul committed
112
    result = read<float>(r.data(), r.get_shape().elements());
Paul's avatar
Paul committed
113
114
115
    return result;
}

Paul's avatar
Paul committed
116
void test1()
Paul's avatar
Paul committed
117
118
119
{
    auto x = cpu();
    auto y = gpu();
Paul's avatar
Paul committed
120
    EXPECT(test::verify_range(x, y));
Paul's avatar
Paul committed
121
122
}

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