eliminate_contiguous.cpp 5.32 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);
Shucai Xiao's avatar
Shucai Xiao committed
45
46
        // If the output shape is a standard shape, no need to try its output
        if(new_shape.standard())
47
48
49
50
        {
            return true;
        }

51
        // if no changes for the shape, the contiguous can also be removed
Shucai Xiao's avatar
Shucai Xiao committed
52
        if(new_shape == ins->get_shape())
53
54
55
56
        {
            return true;
        }

57
        auto outputs = ins->outputs();
58
        // If the current instruction has no output, it means it is the last
59
60
        // 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
61
        if(outputs.empty())
62
        {
63
            return false;
64
65
        }

Shucai Xiao's avatar
Shucai Xiao committed
66
        for(auto output : outputs)
67
68
        {
            auto args = output->inputs();
69
70
71
72
            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
73

74
            if(not try_compute_shape(output, input_shapes, mods))
75
76
77
78
            {
                return false;
            }
        }
79
80
81
82
83
    }
    catch(...)
    {
        return false;
    }
84

85
86
87
    return true;
}

88
89
90
static bool try_compute_shape(instruction_ref ins,
                              const std::vector<instruction_ref>& args,
                              const std::vector<module_ref>& mods)
91
92
{
    auto inputs = to_shapes(args);
93
    return try_compute_shape(ins, inputs, mods);
94
95
}

96
97
template <class F>
static void remove_contiguous(const std::string& op_name, module& m, F f)
98
{
99
100
    auto last = std::prev(m.end());
    std::vector<instruction_ref> const_instructions;
101

102
    for(auto ins : iterator_for(m))
103
    {
104
105
106
107
        // return instruction should have inputs with standard shape
        if(ins->name() == "@return")
            continue;

108
109
110
111
112
113
        if(ins != last and ins->outputs().empty())
            continue;

        if(not f(ins))
            continue;

114
        // Make a copy so we can modify it while we iterate
Shucai Xiao's avatar
Shucai Xiao committed
115
116
117
        auto args     = ins->inputs();
        auto new_args = args;
        auto mod_args = ins->module_inputs();
118

Paul's avatar
Paul committed
119
        for(auto arg : ins->inputs())
120
        {
121
122
123
124
125
126
127
128
129
            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())
130
            {
131
                const_instructions.push_back(arg);
132
133
134
            }
        }
    }
135
136

    // Perform evaluations in parallel
137
138
    std::vector<argument> literals(const_instructions.size());
    par_for(const_instructions.size(), 1, [&](const auto i) {
139
        auto c      = op::contiguous{};
140
        auto prev   = const_instructions[i]->inputs().front();
141
142
143
        literals[i] = c.compute(c.compute_shape({prev->get_shape()}), {prev->eval()});
    });

144
    for(size_t i = 0; i < const_instructions.size(); i++)
145
146
    {
        auto l = m.add_literal(literals[i].get_shape(), literals[i].data());
147
        m.replace_instruction(const_instructions[i], l);
148
    }
149
150
}

151
152
153
154
155
156
157
158
159
160
161
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
162
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
163
} // namespace migraphx