convolution.cpp 3.41 KB
Newer Older
wsttiger's avatar
wsttiger committed
1
2
3
4
5
6
7
#include <migraph/gpu/convolution.hpp>
#include <migraph/operators.hpp>
#include <migraph/manage_ptr.hpp>
#include <migraph/gpu/miopen.hpp>
#include <utility>

namespace migraph {
8
inline namespace MIGRAPH_INLINE_NS {
wsttiger's avatar
wsttiger committed
9
10
11
12
13
14
15
namespace gpu {

shape miopen_convolution::compute_shape(const std::vector<shape>& inputs) const
{
    check_shapes{inputs, *this}.has(4).standard();
    return op.compute_shape({inputs.at(0), inputs.at(1)});
}
wsttiger's avatar
wsttiger committed
16
17
18
argument miopen_convolution::compute(context& ctx,
                                     const shape& output_shape,
                                     const std::vector<argument>& args) const
wsttiger's avatar
wsttiger committed
19
20
21
22
23
24
{
    auto x_desc = make_tensor(args[0].get_shape());
    auto w_desc = make_tensor(args[1].get_shape());
    auto y_desc = make_tensor(output_shape);

    float alpha = 1, beta = 0;
Paul's avatar
Paul committed
25
    miopenConvolutionForward(ctx.get_stream().get_miopen(),
wsttiger's avatar
wsttiger committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
                             &alpha,
                             x_desc.get(),
                             args[0].implicit(),
                             w_desc.get(),
                             args[1].implicit(),
                             cd.get(),
                             algo,
                             &beta,
                             y_desc.get(),
                             args[3].implicit(),
                             args[2].implicit(),
                             args[2].get_shape().bytes());
    return args[3];
}

wsttiger's avatar
wsttiger committed
41
42
43
shape miopen_convolution::compile(context& ctx,
                                  const shape& output_shape,
                                  std::vector<instruction_ref> inputs)
wsttiger's avatar
wsttiger committed
44
45
46
47
48
49
50
{
    shape workspace_shape{};
    auto x_desc = make_tensor(inputs[0]->get_shape());
    auto w_desc = make_tensor(inputs[1]->get_shape());
    auto y_desc = make_tensor(output_shape);

    std::size_t workspace_size = 0;
Paul's avatar
Paul committed
51
52
53
54
55
56
    miopenConvolutionForwardGetWorkSpaceSize(ctx.get_stream().get_miopen(),
                                             w_desc.get(),
                                             x_desc.get(),
                                             cd.get(),
                                             y_desc.get(),
                                             &workspace_size);
wsttiger's avatar
wsttiger committed
57
58
59
60
    workspace_shape = shape{shape::int8_type, {workspace_size}};

    auto x         = to_gpu(generate_argument(inputs[0]->get_shape()));
    auto w         = to_gpu(generate_argument(inputs[1]->get_shape()));
Paul's avatar
Paul committed
61
    auto y         = allocate_gpu(output_shape);
wsttiger's avatar
wsttiger committed
62
63
64
65
    auto workspace = allocate_gpu(workspace_shape);

    int algo_count = 1;
    miopenConvAlgoPerf_t perf;
Paul's avatar
Paul committed
66
    miopenFindConvolutionForwardAlgorithm(ctx.get_stream().get_miopen(),
wsttiger's avatar
wsttiger committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
                                          x_desc.get(),
                                          x.implicit(),
                                          w_desc.get(),
                                          w.implicit(),
                                          cd.get(),
                                          y_desc.get(),
                                          y.implicit(),
                                          1,
                                          &algo_count,
                                          &perf,
                                          workspace.implicit(),
                                          workspace_size,
                                          false);
    algo = perf.fwd_algo;
    return shape{shape::int8_type, {perf.memory}};
}

} // namespace gpu
Shucai Xiao's avatar
Shucai Xiao committed
85
} // namespace MIGRAPH_INLINE_NS
wsttiger's avatar
wsttiger committed
86
} // namespace migraph