quant_convolution.cpp 8.28 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.
 */
24
25
26
27
28
29
30
31
32
33
#include <migraphx/gpu/quant_convolution.hpp>
#include <migraphx/gpu/context.hpp>
#include <migraphx/generate.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

shape miopen_quant_convolution::compute_shape(const std::vector<shape>& inputs) const
{
Shucai Xiao's avatar
Shucai Xiao committed
34
    check_shapes{inputs, *this}.has(4).standard();
kahmed10's avatar
kahmed10 committed
35
    return op.normalize_compute_shape({inputs.at(0), inputs.at(1)});
36
37
}
argument miopen_quant_convolution::compute(context& ctx,
Shucai Xiao's avatar
Shucai Xiao committed
38
39
                                           const shape& output_shape,
                                           const std::vector<argument>& args) const
40
{
41
42
    auto x_desc = make_tensor(args[0].get_shape(), int8_x4_format);
    auto w_desc = make_tensor(args[1].get_shape(), int8_x4_format);
Shucai Xiao's avatar
Shucai Xiao committed
43
    auto y_desc = make_tensor(output_shape);
44

45
46
47
    float alpha = 1;
    float beta  = 0;

48
    auto status = miopenConvolutionForward(ctx.get_stream().get_miopen(),
Shucai Xiao's avatar
Shucai Xiao committed
49
50
51
52
53
54
55
56
57
58
59
60
                                           &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());
Shucai Xiao's avatar
Shucai Xiao committed
61
    if(status != miopenStatusSuccess)
62
63
64
    {
        MIGRAPHX_THROW("QUANT_CONVOLUTION: run convolution forward failed");
    }
65

Shucai Xiao's avatar
Shucai Xiao committed
66
    return args[3];
67
68
}

Paul Fultz II's avatar
Paul Fultz II committed
69
70
71
shape miopen_quant_convolution::find(context& ctx,
                                     const shape& output_shape,
                                     std::vector<shape> inputs)
72
73
{
    shape workspace_shape{};
74
75
    auto x_desc = make_tensor(inputs[0], int8_x4_format);
    auto w_desc = make_tensor(inputs[1], int8_x4_format);
Shucai Xiao's avatar
Shucai Xiao committed
76
    auto y_desc = make_tensor(output_shape);
77
78
79
80
81
82
83
84
85
86

    std::size_t workspace_size = 0;
    miopenConvolutionForwardGetWorkSpaceSize(ctx.get_stream().get_miopen(),
                                             w_desc.get(),
                                             x_desc.get(),
                                             cd.get(),
                                             y_desc.get(),
                                             &workspace_size);
    workspace_shape = shape{shape::int8_type, {workspace_size}};

87
88
89
90
91
92
93
    auto x_shape = inputs[0];
    auto w_shape = inputs[1];
    if(int8_x4_format)
    {
        x_shape = pack_int8_shape(x_shape);
        w_shape = pack_int8_shape(w_shape);
    }
Paul Fultz II's avatar
Paul Fultz II committed
94
95
96
97
    auto x         = to_gpu(generate_argument(x_shape));
    auto w         = to_gpu(generate_argument(w_shape));
    auto y         = allocate_gpu(output_shape);
    auto workspace = allocate_gpu(workspace_shape);
98
99
100
101
102

    int algo_count = 1;
    miopenConvAlgoPerf_t perf;
    auto status = miopenFindConvolutionForwardAlgorithm(ctx.get_stream().get_miopen(),
                                                        x_desc.get(),
Paul Fultz II's avatar
Paul Fultz II committed
103
                                                        x.implicit(),
104
                                                        w_desc.get(),
Paul Fultz II's avatar
Paul Fultz II committed
105
                                                        w.implicit(),
106
107
108
109
110
111
112
113
114
115
                                                        cd.get(),
                                                        y_desc.get(),
                                                        y.implicit(),
                                                        1,
                                                        &algo_count,
                                                        &perf,
                                                        workspace.implicit(),
                                                        workspace_size,
                                                        false);
    if(status != miopenStatusSuccess)
Paul Fultz II's avatar
Paul Fultz II committed
116
117
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
        MIGRAPHX_THROW("MIOpen Quant Convolution: find convolution failed");
    algo = perf.fwd_algo;

    size_t solution_count;

    status = miopenConvolutionForwardGetSolutionCount(ctx.get_stream().get_miopen(),
                                                      w_desc.get(),
                                                      x_desc.get(),
                                                      cd.get(),
                                                      y_desc.get(),
                                                      &solution_count);
    if(status != miopenStatusSuccess)
        MIGRAPHX_THROW("MIOpen Quant Convolution: get solution count failed");

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

    status = miopenConvolutionForwardGetSolution(ctx.get_stream().get_miopen(),
                                                 w_desc.get(),
                                                 x_desc.get(),
                                                 cd.get(),
                                                 y_desc.get(),
                                                 solution_count,
                                                 &solution_count,
                                                 solutions.data());
    if(status != miopenStatusSuccess)
        MIGRAPHX_THROW("MIOpen Quant Convolution: get solution failed");

    solution_id = solutions.front().solution_id;

145
146
147
148
    return shape{shape::int8_type, {perf.memory}};
}

void miopen_quant_convolution::finalize(context& ctx,
Shucai Xiao's avatar
Shucai Xiao committed
149
150
                                        const shape& output_shape,
                                        std::vector<shape> inputs)
151
{
Paul Fultz II's avatar
Paul Fultz II committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    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 Quant Convolution: workspace has changed during finalization.");
    }

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

    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 Quant Convolution: compile solution failed");
175
176
}

177
shape miopen_quant_convolution::pack_int8_shape(const shape& s) const
178
{
Shucai Xiao's avatar
Shucai Xiao committed
179
    if(s.type() != shape::int8_type)
180
181
182
183
    {
        MIGRAPHX_THROW("PACK_INT8_SHAPE: only process int8_type");
    }

Shucai Xiao's avatar
Shucai Xiao committed
184
    auto lens    = s.lens();
185
    auto strides = s.strides();
Shucai Xiao's avatar
Shucai Xiao committed
186
187
    lens[1]      = (lens[1] + 3) / 4 * 4;
    strides[0]   = strides[1] * lens[1];
188
189
190
191

    return {s.type(), lens, strides};
}

192
193
194
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx