compile_miopen.cpp 3.41 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 * 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.
 */
#include <migraphx/gpu/compile_miopen.hpp>
#include <migraphx/gpu/context.hpp>
#include <migraphx/module.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/register_op.hpp>
#include <migraphx/op/identity.hpp>
Paul's avatar
Paul committed
32
#include <migraphx/gpu/rocblas.hpp>
Paul's avatar
Paul committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

struct miopen_op
{
    operation op = op::identity{};

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.op, "op"));
    }

    std::string name() const { return "gpu::miopen_op"; }

    shape compute_shape(std::vector<shape> inputs) const
    {
        inputs.push_back(inputs.back());
        return op.compute_shape(inputs);
    }

    std::ptrdiff_t output_alias(const std::vector<shape>& shapes) const
    {
        return shapes.size() - 1;
    }
};
MIGRAPHX_REGISTER_OP(miopen_op);

Paul's avatar
Paul committed
63
64
65
std::size_t compile_miopen::compile(operation& op, instruction_ref ins, bool format) const
{
    op.from_value({{"int8_x4_format", format}});
Paul's avatar
Format  
Paul committed
66
    auto v = op.compile(*ctx, ins->get_shape(), to_shapes(ins->inputs()));
Paul's avatar
Paul committed
67
68
69
    return v.get("workspace", 0);
}

Paul's avatar
Paul committed
70
71
void compile_miopen::apply(module& m) const
{
Paul's avatar
Merge  
Paul committed
72
    assert(ctx);
Paul's avatar
Paul committed
73
    const bool int8_x4_format = get_int8_x4_format(any_cast<migraphx::gpu::context>(*ctx));
Paul's avatar
Format  
Paul committed
74
    for(auto ins : iterator_for(m))
Paul's avatar
Paul committed
75
    {
Paul's avatar
Format  
Paul committed
76
        if(ins->name() != "gpu::miopen_op")
Paul's avatar
Paul committed
77
            continue;
Paul's avatar
Format  
Paul committed
78
        auto op        = any_cast<miopen_op>(ins->get_operator()).op;
Paul's avatar
Paul committed
79
80
81
82
83
84
85
86
87
88
89
        std::size_t ws = 0;
        try
        {
            // for the regular convolution and deconvolution, this try would always succeed
            ws = compile(op, ins, int8_x4_format);
        }
        catch(migraphx::exception&)
        {
            // In case no solver supports the default format, retry using the other format.
            ws = compile(op, ins, not int8_x4_format);
        }
Paul's avatar
Format  
Paul committed
90
91
        auto inputs = ins->inputs();
        auto alloc  = m.insert_instruction(
Paul's avatar
Format  
Paul committed
92
            ins, make_op("allocate", {{"shape", to_value(shape{shape::int8_type, {ws}})}}));
Paul's avatar
Paul committed
93
94
95
96
97
98
99
100
101
        inputs.insert(std::prev(inputs.end()), alloc);

        m.replace_instruction(ins, op, inputs);
    }
}

} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx