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

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

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

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

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

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

92
93
94
    return true;
}

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

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

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

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

        if(not f(ins))
            continue;

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

Paul's avatar
Paul committed
126
        for(auto arg : ins->inputs())
127
        {
128
129
130
131
132
133
134
135
136
            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())
137
            {
138
                const_instructions.push_back(arg);
139
140
141
            }
        }
    }
142

Charlie Lin's avatar
Charlie Lin committed
143
    // Perform static contiguous evaluations in parallel
144
145
    std::vector<argument> literals(const_instructions.size());
    par_for(const_instructions.size(), 1, [&](const auto i) {
Charlie Lin's avatar
Charlie Lin committed
146
147
148
149
150
151
152
153
        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);
154
155
    });

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

164
165
166
167
168
169
170
171
172
173
174
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
175
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
176
} // namespace migraphx