convolution.cpp 8.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
#include <migraphx/gpu/convolution.hpp>
Paul's avatar
Paul committed
25
26
#include <migraphx/gpu/context.hpp>
#include <migraphx/generate.hpp>
wsttiger's avatar
wsttiger committed
27

Paul's avatar
Paul committed
28
namespace migraphx {
Paul's avatar
Paul committed
29
inline namespace MIGRAPHX_INLINE_NS {
wsttiger's avatar
wsttiger committed
30
31
32
33
namespace gpu {

shape miopen_convolution::compute_shape(const std::vector<shape>& inputs) const
{
Paul's avatar
Paul committed
34
    check_shapes{inputs, *this}.has(4);
kahmed10's avatar
kahmed10 committed
35
    std::vector<shape> conv_inputs(inputs.begin(), inputs.begin() + 2);
Paul's avatar
Format  
Paul committed
36
37
    check_shapes{conv_inputs, *this}.max_ndims(5).packed_layouts(
        {{0, 1, 2}, {0, 1, 2, 3}, {0, 2, 3, 1}, {0, 1, 2, 3, 4}});
kahmed10's avatar
kahmed10 committed
38
    return op.normalize_compute_shape(conv_inputs);
wsttiger's avatar
wsttiger committed
39
}
kahmed10's avatar
kahmed10 committed
40

kahmed10's avatar
kahmed10 committed
41
inline shape reshape_if_1d(const shape& input)
kahmed10's avatar
kahmed10 committed
42
43
44
45
46
47
48
49
50
51
52
53
54
{
    shape new_shape{input};
    auto dims = new_shape.lens();

    if(dims.size() == 3)
    {
        std::vector<size_t> new_dims = dims;
        new_dims.insert(new_dims.begin() + 2, 1);
        new_shape = shape{input.type(), new_dims};
    }
    return new_shape;
}

wsttiger's avatar
wsttiger committed
55
56
57
argument miopen_convolution::compute(context& ctx,
                                     const shape& output_shape,
                                     const std::vector<argument>& args) const
wsttiger's avatar
wsttiger committed
58
{
kahmed10's avatar
kahmed10 committed
59
60
61
    auto x_desc = make_tensor(reshape_if_1d(args[0].get_shape()));
    auto w_desc = make_tensor(reshape_if_1d(args[1].get_shape()));
    auto y_desc = make_tensor(reshape_if_1d(output_shape));
wsttiger's avatar
wsttiger committed
62

63
64
65
    if(solution_id == 0)
        MIGRAPHX_THROW("MIOpen Convolution: invalid solution ID");

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
    // auto status = miopenConvolutionForwardImmediate(ctx.get_stream().get_miopen(),
    //                                                 w_desc.get(),
    //                                                 args[1].implicit(),
    //                                                 x_desc.get(),
    //                                                 args[0].implicit(),
    //                                                 cd.get(),
    //                                                 y_desc.get(),
    //                                                 args[3].implicit(),
    //                                                 args[2].implicit(),
    //                                                 args[2].get_shape().bytes(),
    //                                                 solution_id);

    float alpha = 1;
    float beta  = 0;
    auto status = miopenConvolutionForward(ctx.get_stream().get_miopen(),
                                           &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());
93

Paul's avatar
Paul committed
94
    if(status != miopenStatusSuccess)
95
        MIGRAPHX_THROW("MIOpen Convolution: running convolution failed");
wsttiger's avatar
wsttiger committed
96
97
98
    return args[3];
}

99
shape miopen_convolution::find(context& ctx, const shape& output_shape, std::vector<shape> inputs)
wsttiger's avatar
wsttiger committed
100
101
{
    shape workspace_shape{};
kahmed10's avatar
kahmed10 committed
102

kahmed10's avatar
kahmed10 committed
103
104
105
    auto x_desc = make_tensor(reshape_if_1d(inputs[0]));
    auto w_desc = make_tensor(reshape_if_1d(inputs[1]));
    auto y_desc = make_tensor(reshape_if_1d(output_shape));
wsttiger's avatar
wsttiger committed
106
107

    std::size_t workspace_size = 0;
Paul's avatar
Paul committed
108
109
110
111
112
113
    miopenConvolutionForwardGetWorkSpaceSize(ctx.get_stream().get_miopen(),
                                             w_desc.get(),
                                             x_desc.get(),
                                             cd.get(),
                                             y_desc.get(),
                                             &workspace_size);
wsttiger's avatar
wsttiger committed
114
115
    workspace_shape = shape{shape::int8_type, {workspace_size}};

Paul's avatar
Paul committed
116
117
    auto x         = to_gpu(generate_argument(inputs[0]));
    auto w         = to_gpu(generate_argument(inputs[1]));
Paul's avatar
Paul committed
118
    auto y         = allocate_gpu(output_shape);
wsttiger's avatar
wsttiger committed
119
120
121
122
    auto workspace = allocate_gpu(workspace_shape);

    int algo_count = 1;
    miopenConvAlgoPerf_t perf;
Paul's avatar
Paul committed
123
    auto status = miopenFindConvolutionForwardAlgorithm(ctx.get_stream().get_miopen(),
Paul's avatar
Paul committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
                                                        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)
138
        MIGRAPHX_THROW("MIOpen Convolution: find convolution failed");
139
    algo = perf.fwd_algo;
140
141
142

    size_t solution_count;

143
144
145
146
147
148
    status = miopenConvolutionForwardGetSolutionCount(ctx.get_stream().get_miopen(),
                                                      w_desc.get(),
                                                      x_desc.get(),
                                                      cd.get(),
                                                      y_desc.get(),
                                                      &solution_count);
149
150
151
152
153
    if(status != miopenStatusSuccess)
        MIGRAPHX_THROW("MIOpen Convolution: get solution count failed");

    std::vector<miopenConvSolution_t> solutions(solution_count);

154
    status = miopenConvolutionForwardGetSolution(ctx.get_stream().get_miopen(),
155
156
157
158
159
160
161
162
163
164
165
166
                                                 w_desc.get(),
                                                 x_desc.get(),
                                                 cd.get(),
                                                 y_desc.get(),
                                                 solution_count,
                                                 &solution_count,
                                                 solutions.data());
    if(status != miopenStatusSuccess)
        MIGRAPHX_THROW("MIOpen Convolution: get solution failed");

    solution_id = solutions.front().solution_id;

wsttiger's avatar
wsttiger committed
167
168
169
    return shape{shape::int8_type, {perf.memory}};
}

Paul's avatar
Paul committed
170
171
172
void miopen_convolution::finalize(context& ctx,
                                  const shape& output_shape,
                                  std::vector<shape> inputs)
Paul's avatar
Paul committed
173
{
174
175
    if(cd == nullptr)
        cd = make_conv(op);
176
177
178
179
180
181
182
183
184
185
186
187
188
    if(solution_id == 0)
    {
        // Check that workspace hasn't changed
        auto size = inputs.at(2).bytes();
        auto ws   = find(ctx, output_shape, inputs);
        if(ws.bytes() > size)
            MIGRAPHX_THROW("MIOpen Convolution: workspace has changed during finalization.");
    }

    auto x_desc = make_tensor(reshape_if_1d(inputs[0]));
    auto w_desc = make_tensor(reshape_if_1d(inputs[1]));
    auto y_desc = make_tensor(reshape_if_1d(output_shape));

189
190
191
192
193
194
    auto status = miopenConvolutionForwardCompileSolution(ctx.get_stream().get_miopen(),
                                                          w_desc.get(),
                                                          x_desc.get(),
                                                          cd.get(),
                                                          y_desc.get(),
                                                          solution_id);
195
196
    if(status != miopenStatusSuccess)
        MIGRAPHX_THROW("MIOpen Convolution: compile solution failed");
Paul's avatar
Paul committed
197
198
}

wsttiger's avatar
wsttiger committed
199
} // namespace gpu
Paul's avatar
Paul committed
200
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
201
} // namespace migraphx