eliminate_contiguous.cpp 6.31 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
25
26
27
28
29
#include <migraphx/eliminate_contiguous.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
30
31
#include <migraphx/op/contiguous.hpp>
#include <migraphx/op/identity.hpp>
32
#include <migraphx/par_for.hpp>
33
#include <type_traits>
Paul's avatar
Paul committed
34
#include <utility>
35

Paul's avatar
Paul committed
36
namespace migraphx {
Paul's avatar
Paul committed
37
inline namespace MIGRAPHX_INLINE_NS {
38

39
40
41
static bool try_compute_shape(instruction_ref ins,
                              const std::vector<shape>& inputs,
                              const std::vector<module_ref>& mods)
42
43
44
{
    try
    {
45
        shape new_shape = ins->get_operator().compute_shape(inputs, mods);
Charlie Lin's avatar
Charlie Lin committed
46
47
48
49
50
51
52

        // Cannot tell if a dynamic shape will need to be made contiguous
        if(new_shape.dynamic())
        {
            return false;
        }

Shucai Xiao's avatar
Shucai Xiao committed
53
54
        // If the output shape is a standard shape, no need to try its output
        if(new_shape.standard())
55
56
57
58
        {
            return true;
        }

59
        // if no changes for the shape, the contiguous can also be removed
Shucai Xiao's avatar
Shucai Xiao committed
60
        if(new_shape == ins->get_shape())
61
62
63
64
        {
            return true;
        }

65
        auto outputs = ins->outputs();
66
        // If the current instruction has no output, it means it is the last
67
68
        // instruction and generates a non-standard output shape, and the last
        // output shape is different from the case with the contiguous operator
Shucai Xiao's avatar
Shucai Xiao committed
69
        if(outputs.empty())
70
        {
71
            return false;
72
73
        }

Shucai Xiao's avatar
Shucai Xiao committed
74
        for(auto output : outputs)
75
76
        {
            auto args = output->inputs();
77
78
79
80
            std::vector<shape> input_shapes(args.size());
            std::transform(args.begin(), args.end(), input_shapes.begin(), [&](auto& arg) {
                return (arg == ins) ? new_shape : arg->get_shape();
            });
Shucai Xiao's avatar
Shucai Xiao committed
81

82
            if(not try_compute_shape(output, input_shapes, mods))
83
84
85
86
            {
                return false;
            }
        }
87
88
89
90
91
    }
    catch(...)
    {
        return false;
    }
92

93
94
95
    return true;
}

96
97
98
static bool try_compute_shape(instruction_ref ins,
                              const std::vector<instruction_ref>& args,
                              const std::vector<module_ref>& mods)
99
100
{
    auto inputs = to_shapes(args);
101
    return try_compute_shape(ins, inputs, mods);
102
103
}

104
105
template <class F>
static void remove_contiguous(const std::string& op_name, module& m, F f)
106
{
107
108
    auto last = std::prev(m.end());
    std::vector<instruction_ref> const_instructions;
109

110
    for(auto ins : iterator_for(m))
111
    {
112
113
114
115
        // return instruction should have inputs with standard shape
        if(ins->name() == "@return")
            continue;

116
117
118
119
120
121
        if(ins != last and ins->outputs().empty())
            continue;

        if(not f(ins))
            continue;

122
        // Make a copy so we can modify it while we iterate
Shucai Xiao's avatar
Shucai Xiao committed
123
124
125
        auto args     = ins->inputs();
        auto new_args = args;
        auto mod_args = ins->module_inputs();
126

Paul's avatar
Paul committed
127
        for(auto arg : ins->inputs())
128
        {
129
130
131
132
133
134
135
136
137
            if(arg->name() != op_name)
                continue;
            auto prev = arg->inputs().front();
            replace(new_args, arg, prev);
            if(try_compute_shape(ins, new_args, mod_args))
            {
                instruction::replace_argument(ins, arg, prev);
            }
            else if(prev->can_eval())
138
            {
139
                const_instructions.push_back(arg);
140
141
142
            }
        }
    }
143

Charlie Lin's avatar
Charlie Lin committed
144
    // Perform static contiguous evaluations in parallel
145
146
    std::vector<argument> literals(const_instructions.size());
    par_for(const_instructions.size(), 1, [&](const auto i) {
Charlie Lin's avatar
Charlie Lin committed
147
148
149
150
151
152
153
154
        auto c    = op::contiguous{};
        auto prev = const_instructions[i]->inputs().front();
        // compute the output contiguous shape from the previous instruction shape
        shape computed_shape                   = c.compute_shape({prev->get_shape()});
        const std::vector<argument>& prev_eval = {prev->eval()};
        // prev_eval should not be used in make_compute_output_shape() as computed_shape is static
        auto co_shape = make_compute_output_shape(pack(c, computed_shape, prev_eval));
        literals[i]   = c.compute(co_shape, prev_eval);
155
156
    });

Charlie Lin's avatar
Charlie Lin committed
157
    // Replace static contiguous operations with a literal
158
    for(size_t i = 0; i < const_instructions.size(); i++)
159
160
    {
        auto l = m.add_literal(literals[i].get_shape(), literals[i].data());
161
        m.replace_instruction(const_instructions[i], l);
162
    }
163
164
}

165
166
167
168
169
170
171
172
173
174
175
176
static void remove_contiguous_noops(const std::string& op_name, module& m)
{
    for(auto ins : iterator_for(m))
    {
        if (ins->name() != op_name)
            continue;
        if(ins->inputs().front()->get_shape() != ins->get_shape())
            continue;
        m.replace_instruction(ins, ins->inputs().front());
    }
}

177
178
179
180
181
182
183
184
185
void eliminate_contiguous::apply(module& m) const
{
    // Skip contiguous from splits first
    remove_contiguous(op_name, m, [](auto ins) {
        if(ins->name() != "slice")
            return true;
        return (ins->inputs().front()->outputs().size() == 1);
    });
    remove_contiguous(op_name, m, [](auto) { return true; });
186
    remove_contiguous_noops(op_name, m);
187
188
}

Paul's avatar
Paul committed
189
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
190
} // namespace migraphx