convolution.cpp 12 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>
27
#include <miopen/miopen.h>
wsttiger's avatar
wsttiger committed
28

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

shape miopen_convolution::compute_shape(const std::vector<shape>& inputs) const
{
Paul's avatar
Paul committed
35
    check_shapes{inputs, *this}.has(4);
kahmed10's avatar
kahmed10 committed
36
    std::vector<shape> conv_inputs(inputs.begin(), inputs.begin() + 2);
Paul's avatar
Format  
Paul committed
37
38
    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
39
    return op.normalize_compute_shape(conv_inputs);
wsttiger's avatar
wsttiger committed
40
}
kahmed10's avatar
kahmed10 committed
41

kahmed10's avatar
kahmed10 committed
42
inline shape reshape_if_1d(const shape& input)
kahmed10's avatar
kahmed10 committed
43
44
45
46
47
48
49
50
51
52
53
54
55
{
    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
56
57
58
argument miopen_convolution::compute(context& ctx,
                                     const shape& output_shape,
                                     const std::vector<argument>& args) const
wsttiger's avatar
wsttiger committed
59
{
60
61
62
63
64
    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));
    auto* miopen_stream_handle = ctx.get_stream().get_miopen();
    auto workspace_size        = args[2].get_shape().bytes();
wsttiger's avatar
wsttiger committed
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
#ifdef MIGRAPHX_HAS_FIND_2_API
    {
        const miopenTensorArgument_t tensor_args[3] = {
            {miopenTensorConvolutionX, nullptr, args[0].implicit()},
            {miopenTensorConvolutionW, nullptr, args[1].implicit()},
            {miopenTensorConvolutionY, nullptr, args[3].implicit()},
        };

        if(solution_ptr.get() == nullptr)
            MIGRAPHX_THROW("MIOpen Convolution : Load MIOpen Solution before running it");

        auto status = miopenRunSolution(miopen_stream_handle,
                                        solution_ptr.get(),
                                        3,
                                        tensor_args,
                                        args[2].implicit(),
                                        workspace_size);
        if(status != miopenStatusSuccess)
            MIGRAPHX_THROW("MIOpen Convolution: running convolution using find_2.0 failed");

        return args[3];
    }
#else
    // else use immediate mode
90
91
92
    if(solution_id == 0)
        MIGRAPHX_THROW("MIOpen Convolution: invalid solution ID");

93
    auto status = miopenConvolutionForwardImmediate(miopen_stream_handle,
94
95
96
97
98
99
100
101
                                                    w_desc.get(),
                                                    args[1].implicit(),
                                                    x_desc.get(),
                                                    args[0].implicit(),
                                                    cd.get(),
                                                    y_desc.get(),
                                                    args[3].implicit(),
                                                    args[2].implicit(),
102
                                                    workspace_size,
103
104
                                                    solution_id);

Paul's avatar
Paul committed
105
    if(status != miopenStatusSuccess)
106
        MIGRAPHX_THROW("MIOpen Convolution: running convolution failed");
wsttiger's avatar
wsttiger committed
107
    return args[3];
108
#endif
wsttiger's avatar
wsttiger committed
109
110
}

111
shape miopen_convolution::find(context& ctx, const shape& output_shape, std::vector<shape> inputs)
wsttiger's avatar
wsttiger committed
112
113
{
    shape workspace_shape{};
114
115
116
117
    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));
    std::size_t workspace_size = 0;
kahmed10's avatar
kahmed10 committed
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifdef MIGRAPHX_HAS_FIND_2_API
    {
        auto conv_problem = make_obj<miopen_problem>(
            &miopenCreateConvProblem, cd.get(), miopenProblemDirectionForward);

        set_tensor_descriptor(miopenTensorConvolutionX, x_desc, conv_problem);
        set_tensor_descriptor(miopenTensorConvolutionW, w_desc, conv_problem);
        set_tensor_descriptor(miopenTensorConvolutionY, y_desc, conv_problem);

        auto* miopen_stream_handle = ctx.get_stream().get_miopen();

        solution_ptr = find_solution(miopen_stream_handle, conv_problem.get());

        auto status = miopenGetSolutionWorkspaceSize(solution_ptr.get(), &workspace_size);
        if(status != miopenStatusSuccess)
            MIGRAPHX_THROW("MIOpen Convolution : failed to get solution's workspace size");

        std::size_t solution_size;
        status = miopenGetSolutionSize(solution_ptr.get(), &solution_size);
        if(status != miopenStatusSuccess)
            MIGRAPHX_THROW("MIOpen Convolution: Failed to fetch solution size");

        auto solution_binary = std::vector<char>{};
        solution_binary.resize(solution_size);

        status = miopenSaveSolution(solution_ptr.get(), solution_binary.data());
        if(status != miopenStatusSuccess)
            MIGRAPHX_THROW("MIOpen Convolution: Saving solution failed");
        solution_object = value::binary{solution_binary.data(), solution_size};

        return shape{shape::int8_type, {workspace_size}};
    }
#else
    // else use immediate find mode
    auto status = miopenConvolutionForwardGetWorkSpaceSize(ctx.get_stream().get_miopen(),
                                                           w_desc.get(),
                                                           x_desc.get(),
                                                           cd.get(),
                                                           y_desc.get(),
                                                           &workspace_size);
    if(status != miopenStatusSuccess)
        MIGRAPHX_THROW("MIOpen Convolution: Failed to get forward workspace size");
wsttiger's avatar
wsttiger committed
161
162
163

    workspace_shape = shape{shape::int8_type, {workspace_size}};

Paul's avatar
Paul committed
164
165
    auto x         = to_gpu(generate_argument(inputs[0]));
    auto w         = to_gpu(generate_argument(inputs[1]));
Paul's avatar
Paul committed
166
    auto y         = allocate_gpu(output_shape);
wsttiger's avatar
wsttiger committed
167
168
169
170
    auto workspace = allocate_gpu(workspace_shape);

    int algo_count = 1;
    miopenConvAlgoPerf_t perf;
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    status = miopenFindConvolutionForwardAlgorithm(ctx.get_stream().get_miopen(),
                                                   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);
Paul's avatar
Paul committed
185
    if(status != miopenStatusSuccess)
186
        MIGRAPHX_THROW("MIOpen Convolution: find convolution failed");
187
    algo = perf.fwd_algo;
188
189
190

    size_t solution_count;

191
192
193
194
195
196
    status = miopenConvolutionForwardGetSolutionCount(ctx.get_stream().get_miopen(),
                                                      w_desc.get(),
                                                      x_desc.get(),
                                                      cd.get(),
                                                      y_desc.get(),
                                                      &solution_count);
197
198
199
200
201
    if(status != miopenStatusSuccess)
        MIGRAPHX_THROW("MIOpen Convolution: get solution count failed");

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

202
    status = miopenConvolutionForwardGetSolution(ctx.get_stream().get_miopen(),
203
204
205
206
207
208
209
210
211
212
213
214
                                                 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
215
    return shape{shape::int8_type, {perf.memory}};
216
#endif
wsttiger's avatar
wsttiger committed
217
218
}

Paul's avatar
Format  
Paul committed
219
220
221
value miopen_convolution::compile(context& ctx,
                                  const shape& output,
                                  const std::vector<shape>& input)
Paul's avatar
Paul committed
222
223
224
225
226
227
228
{
    if(cd == nullptr)
        cd = make_conv(op);
    auto ws = find(ctx, output, input);
    return {{"workspace", ws.bytes()}};
}

Paul's avatar
Paul committed
229
230
void miopen_convolution::finalize(context& ctx,
                                  const shape& output_shape,
231
                                  const std::vector<shape>& inputs)
Paul's avatar
Paul committed
232
{
233
#ifdef MIGRAPHX_HAS_FIND_2_API
234
    {
235
236
237
238
239
240
241
242
243
244
245
246
247
248
        (void)(ctx); // avoid warnings
        (void)(output_shape);
        (void)(inputs);
        // load solution
        if(solution_ptr == nullptr)
        {
            miopenSolution_t ptr;
            auto status  = miopenLoadSolution(&ptr,
                                             reinterpret_cast<const char*>(solution_object.data()),
                                             solution_object.size());
            solution_ptr = miopen_solution{ptr};
            if(status != miopenStatusSuccess)
                MIGRAPHX_THROW("MIOpen Convolution: loading convolution solution failed");
        }
249
    }
250
251
252
253
254
255
256
257
258
259
260
261
262
#else
    // Use immediate mode API
    {
        if(cd == nullptr)
            cd = make_conv(op);
        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.");
        }
263

264
265
266
        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));
267

268
269
270
271
272
273
274
275
276
277
        auto status = miopenConvolutionForwardCompileSolution(ctx.get_stream().get_miopen(),
                                                              w_desc.get(),
                                                              x_desc.get(),
                                                              cd.get(),
                                                              y_desc.get(),
                                                              solution_id);
        if(status != miopenStatusSuccess)
            MIGRAPHX_THROW("MIOpen Convolution: compile solution failed");
    }
#endif
Paul's avatar
Paul committed
278
279
}

wsttiger's avatar
wsttiger committed
280
} // namespace gpu
Paul's avatar
Paul committed
281
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
282
} // namespace migraphx