eliminate_contiguous.cpp 6.51 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>
Paul's avatar
Paul committed
33
#include <utility>
34

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

38
39
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_ELIMINATE_CONTIGUOUS)

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

        // 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
54
55
        // If the output shape is a standard shape, no need to try its output
        if(new_shape.standard())
56
57
58
59
        {
            return true;
        }

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

66
        auto outputs = ins->outputs();
67
        // If the current instruction has no output, it means it is the last
68
69
        // 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
70
        if(outputs.empty())
71
        {
72
            return false;
73
74
        }

Shucai Xiao's avatar
Shucai Xiao committed
75
        for(auto output : outputs)
76
77
        {
            auto args = output->inputs();
78
79
80
81
            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
82

83
            if(not try_compute_shape(output, input_shapes, mods))
84
85
86
87
            {
                return false;
            }
        }
88
    }
89
90
91
92
93
94
95
96
    catch(const std::exception& e)
    {
        if(enabled(MIGRAPHX_TRACE_ELIMINATE_CONTIGUOUS{}))
        {
            std::cout << "Exception: " << e.what() << std::endl;
        }
        return false;
    }
97
98
    catch(...)
    {
99
100
101
102
        if(enabled(MIGRAPHX_TRACE_ELIMINATE_CONTIGUOUS{}))
        {
            std::cout << "Unknown exception" << std::endl;
        }
103
104
        return false;
    }
105

106
107
108
    return true;
}

109
110
111
static bool try_compute_shape(instruction_ref ins,
                              const std::vector<instruction_ref>& args,
                              const std::vector<module_ref>& mods)
112
113
{
    auto inputs = to_shapes(args);
114
    return try_compute_shape(ins, inputs, mods);
115
116
}

117
118
template <class F>
static void remove_contiguous(const std::string& op_name, module& m, F f)
119
{
120
121
    auto last = std::prev(m.end());
    std::vector<instruction_ref> const_instructions;
122

123
    for(auto ins : iterator_for(m))
124
    {
125
126
127
128
        // return instruction should have inputs with standard shape
        if(ins->name() == "@return")
            continue;

129
130
131
132
133
134
        if(ins != last and ins->outputs().empty())
            continue;

        if(not f(ins))
            continue;

135
        // Make a copy so we can modify it while we iterate
Shucai Xiao's avatar
Shucai Xiao committed
136
137
138
        auto args     = ins->inputs();
        auto new_args = args;
        auto mod_args = ins->module_inputs();
139

Paul's avatar
Paul committed
140
        for(auto arg : ins->inputs())
141
        {
142
143
            if(arg->name() != op_name)
                continue;
144
145
146
147
148
            if(enabled(MIGRAPHX_TRACE_ELIMINATE_CONTIGUOUS{}))
            {
                std::cout << "eliminate_contiguous: ";
                m.debug_print(ins);
            }
149
150
151
152
153
154
155
            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())
156
            {
157
                const_instructions.push_back(arg);
158
159
160
            }
        }
    }
161

Charlie Lin's avatar
Charlie Lin committed
162
    // Perform static contiguous evaluations in parallel
163
164
    std::vector<argument> literals(const_instructions.size());
    par_for(const_instructions.size(), 1, [&](const auto i) {
Charlie Lin's avatar
Charlie Lin committed
165
166
167
168
169
170
171
172
        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);
173
174
    });

Charlie Lin's avatar
Charlie Lin committed
175
    // Replace static contiguous operations with a literal
176
    for(size_t i = 0; i < const_instructions.size(); i++)
177
178
    {
        auto l = m.add_literal(literals[i].get_shape(), literals[i].data());
179
        m.replace_instruction(const_instructions[i], l);
180
    }
181
182
}

183
184
185
186
187
188
189
190
191
192
193
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; });
}

Paul's avatar
Paul committed
194
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
195
} // namespace migraphx