quant_convolution.cpp 4.99 KB
Newer Older
1
#include <migraphx/gpu/quant_convolution.hpp>
2
#include <migraphx/gpu/device/convert.hpp>
3
4
5
6
7
8
9
10
11
#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
12
    check_shapes{inputs, *this}.has(4).standard();
13
14
15
    return op.compute_shape({inputs.at(0), inputs.at(1)});
}
argument miopen_quant_convolution::compute(context& ctx,
Shucai Xiao's avatar
Shucai Xiao committed
16
17
                                           const shape& output_shape,
                                           const std::vector<argument>& args) const
18
{
Shucai Xiao's avatar
Shucai Xiao committed
19
20
21
    auto x_desc = make_tensor(args[0].get_shape(), true);
    auto w_desc = make_tensor(args[1].get_shape(), true);
    auto y_desc = make_tensor(output_shape);
22

23
24
25
    float alpha = 1;
    float beta  = 0;

26
    auto status = miopenConvolutionForward(ctx.get_stream().get_miopen(),
Shucai Xiao's avatar
Shucai Xiao committed
27
28
29
30
31
32
33
34
35
36
37
38
                                           &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
39
    if(status != miopenStatusSuccess)
40
41
42
    {
        MIGRAPHX_THROW("QUANT_CONVOLUTION: run convolution forward failed");
    }
43

Shucai Xiao's avatar
Shucai Xiao committed
44
    return args[3];
45
46
47
}

shape miopen_quant_convolution::compile(context& ctx,
Shucai Xiao's avatar
Shucai Xiao committed
48
49
                                        const shape& output_shape,
                                        std::vector<shape> inputs)
50
51
{
    shape workspace_shape{};
52
53
    auto x_desc = make_tensor(inputs[0], true);
    auto w_desc = make_tensor(inputs[1], true);
Shucai Xiao's avatar
Shucai Xiao committed
54
    auto y_desc = make_tensor(output_shape);
55
56
57
58
59
60
61
62
63
64

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

Shucai Xiao's avatar
Shucai Xiao committed
65
66
67
68
    auto arg_vec4_x = to_gpu(generate_argument(pack_int8_shape(inputs[0])));
    auto arg_vec4_w = to_gpu(generate_argument(pack_int8_shape(inputs[1])));
    auto y          = allocate_gpu(output_shape);
    auto workspace  = allocate_gpu(workspace_shape);
69
70
71
72
73

    int algo_count = 1;
    miopenConvAlgoPerf_t perf;
    auto status = miopenFindConvolutionForwardAlgorithm(ctx.get_stream().get_miopen(),
                                                        x_desc.get(),
74
                                                        arg_vec4_x.implicit(),
75
                                                        w_desc.get(),
76
                                                        arg_vec4_w.implicit(),
77
78
79
80
81
82
83
84
85
86
                                                        cd.get(),
                                                        y_desc.get(),
                                                        y.implicit(),
                                                        1,
                                                        &algo_count,
                                                        &perf,
                                                        workspace.implicit(),
                                                        workspace_size,
                                                        false);
    if(status != miopenStatusSuccess)
87
88
89
    {
        MIGRAPHX_THROW("QUANT_CONVOLUTION: find convolution failed");
    }
90
91
92
93
94
95
    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
96
97
                                        const shape& output_shape,
                                        std::vector<shape> inputs)
98
99
100
101
102
103
104
105
106
107
{
    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.");
}

108
shape miopen_quant_convolution::pack_int8_shape(const shape& s)
109
{
Shucai Xiao's avatar
Shucai Xiao committed
110
    if(s.type() != shape::int8_type)
111
112
113
114
    {
        MIGRAPHX_THROW("PACK_INT8_SHAPE: only process int8_type");
    }

Shucai Xiao's avatar
Shucai Xiao committed
115
    auto lens    = s.lens();
116
    auto strides = s.strides();
Shucai Xiao's avatar
Shucai Xiao committed
117
118
    lens[1]      = (lens[1] + 3) / 4 * 4;
    strides[0]   = strides[1] * lens[1];
119
120
121
122

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

123
124
125
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx