"src/eliminate_fp8.cpp" did not exist on "7f9d85cc0323bff32b92ea5b3751ac8c2e6fb2f3"
convolution.cpp 3.97 KB
Newer Older
Paul's avatar
Paul committed
1
#include <migraphx/gpu/convolution.hpp>
Paul's avatar
Paul committed
2
3
#include <migraphx/gpu/context.hpp>
#include <migraphx/generate.hpp>
wsttiger's avatar
wsttiger committed
4

Paul's avatar
Paul committed
5
namespace migraphx {
Paul's avatar
Paul committed
6
inline namespace MIGRAPHX_INLINE_NS {
wsttiger's avatar
wsttiger committed
7
8
9
10
11
12
13
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
14
15
16
argument miopen_convolution::compute(context& ctx,
                                     const shape& output_shape,
                                     const std::vector<argument>& args) const
wsttiger's avatar
wsttiger committed
17
18
19
20
21
{
    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);

Paul's avatar
Paul committed
22
    float alpha = 1;
Paul's avatar
Paul committed
23
    float beta  = 0;
Paul's avatar
Paul committed
24
    miopenConvolutionForward(ctx.get_stream().get_miopen(),
wsttiger's avatar
wsttiger committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
                             &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
40
41
shape miopen_convolution::compile(context& ctx,
                                  const shape& output_shape,
Paul's avatar
Paul committed
42
                                  std::vector<shape> inputs)
wsttiger's avatar
wsttiger committed
43
44
{
    shape workspace_shape{};
Paul's avatar
Paul committed
45
46
    auto x_desc = make_tensor(inputs[0]);
    auto w_desc = make_tensor(inputs[1]);
wsttiger's avatar
wsttiger committed
47
48
49
    auto y_desc = make_tensor(output_shape);

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

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

    int algo_count = 1;
    miopenConvAlgoPerf_t perf;
Paul's avatar
Paul committed
65
    auto status = miopenFindConvolutionForwardAlgorithm(ctx.get_stream().get_miopen(),
Paul's avatar
Paul committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
                                                        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);
    if(status != miopenStatusSuccess)
        MIGRAPHX_THROW("Find convolution failed");
Paul's avatar
Paul committed
81
    handle = ctx.get_stream().get_miopen();
Paul's avatar
Paul committed
82
    algo   = perf.fwd_algo;
wsttiger's avatar
wsttiger committed
83
84
85
    return shape{shape::int8_type, {perf.memory}};
}

Paul's avatar
Paul committed
86
87
88
void miopen_convolution::finalize(context& ctx,
                                  const shape& output_shape,
                                  std::vector<shape> inputs)
Paul's avatar
Paul committed
89
{
Paul's avatar
Paul committed
90
    if(handle == ctx.get_stream().get_miopen())
Paul's avatar
Paul committed
91
92
        return;
    // TODO: Check that workspace hasn't changed
Paul's avatar
Paul committed
93
    compile(ctx, output_shape, std::move(inputs));
Paul's avatar
Paul committed
94
95
}

wsttiger's avatar
wsttiger committed
96
} // namespace gpu
Paul's avatar
Paul committed
97
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
98
} // namespace migraphx