quant_convolution.cpp 6.35 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
#include <migraphx/gpu/quant_convolution.hpp>
25
#include <migraphx/gpu/device/convert.hpp>
26
27
28
29
30
31
32
33
34
#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
35
    check_shapes{inputs, *this}.has(4).standard();
kahmed10's avatar
kahmed10 committed
36
    return op.normalize_compute_shape({inputs.at(0), inputs.at(1)});
37
38
}
argument miopen_quant_convolution::compute(context& ctx,
Shucai Xiao's avatar
Shucai Xiao committed
39
40
                                           const shape& output_shape,
                                           const std::vector<argument>& args) const
41
{
42
43
    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
44
    auto y_desc = make_tensor(output_shape);
45

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

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

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

shape miopen_quant_convolution::compile(context& ctx,
Shucai Xiao's avatar
Shucai Xiao committed
71
72
                                        const shape& output_shape,
                                        std::vector<shape> inputs)
73
74
{
    shape workspace_shape{};
75
76
    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
77
    auto y_desc = make_tensor(output_shape);
78
79
80
81
82
83
84
85
86
87

    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}};

88
89
90
91
92
93
94
95
96
    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);
    }
    auto arg_vec4_x = to_gpu(generate_argument(x_shape));
    auto arg_vec4_w = to_gpu(generate_argument(w_shape));
Shucai Xiao's avatar
Shucai Xiao committed
97
98
    auto y          = allocate_gpu(output_shape);
    auto workspace  = allocate_gpu(workspace_shape);
99
100
101
102
103

    int algo_count = 1;
    miopenConvAlgoPerf_t perf;
    auto status = miopenFindConvolutionForwardAlgorithm(ctx.get_stream().get_miopen(),
                                                        x_desc.get(),
104
                                                        arg_vec4_x.implicit(),
105
                                                        w_desc.get(),
106
                                                        arg_vec4_w.implicit(),
107
108
109
110
111
112
113
114
115
116
                                                        cd.get(),
                                                        y_desc.get(),
                                                        y.implicit(),
                                                        1,
                                                        &algo_count,
                                                        &perf,
                                                        workspace.implicit(),
                                                        workspace_size,
                                                        false);
    if(status != miopenStatusSuccess)
117
118
119
    {
        MIGRAPHX_THROW("QUANT_CONVOLUTION: find convolution failed");
    }
120
121
122
123
124
125
    handle = ctx.get_stream().get_miopen();
    algo   = perf.fwd_algo;
    return shape{shape::int8_type, {perf.memory}};
}

void miopen_quant_convolution::finalize(context& ctx,
Shucai Xiao's avatar
Shucai Xiao committed
126
127
                                        const shape& output_shape,
                                        std::vector<shape> inputs)
128
129
130
131
132
133
134
135
136
137
{
    if(handle == ctx.get_stream().get_miopen())
        return;
    // Check that workspace hasn't changed
    auto size = inputs.at(2).bytes();
    auto ws   = compile(ctx, output_shape, std::move(inputs));
    if(ws.bytes() > size)
        MIGRAPHX_THROW("Workspace has changed during finalization.");
}

138
shape miopen_quant_convolution::pack_int8_shape(const shape& s) const
139
{
Shucai Xiao's avatar
Shucai Xiao committed
140
    if(s.type() != shape::int8_type)
141
142
143
144
    {
        MIGRAPHX_THROW("PACK_INT8_SHAPE: only process int8_type");
    }

Shucai Xiao's avatar
Shucai Xiao committed
145
    auto lens    = s.lens();
146
    auto strides = s.strides();
Shucai Xiao's avatar
Shucai Xiao committed
147
148
    lens[1]      = (lens[1] + 3) / 4 * 4;
    strides[0]   = strides[1] * lens[1];
149
150
151
152

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

153
154
155
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx