split_single_dyn_dim.cpp 6.89 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
24
25
26
27
28
29
30
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2023 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/split_single_dyn_dim.hpp>
#include <migraphx/module.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/functional.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/ranges.hpp>
31
#include <migraphx/matcher.hpp>
32
33
34
35
36
37
38

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

struct dynamic_dimensions_check
{
    std::string dyn_param_str;
charlie's avatar
charlie committed
39
    shape::dynamic_dimension dd;
40
41
};

charlie's avatar
charlie committed
42
43
44
45
46
47
48
/**
 * Returns value if the parameters contain one non-fixed dynamic_dimension that is the same between
 * all of the dynamic shape parameters.
 * Returns the parameters and the dynamic dimension in a vector of dynamic_dimensions_check objects.
 */
optional<std::vector<dynamic_dimensions_check>>
has_one_unique_dyn_dim(const std::unordered_map<std::string, shape>& param_shapes)
49
{
charlie's avatar
charlie committed
50
51
52
53
    if(param_shapes.empty())
    {
        return std::nullopt;
    }
54
    auto is_dynamic = [](const auto& p) { return p.second.dynamic(); };
charlie's avatar
charlie committed
55
56
57
58
    std::vector<std::decay_t<decltype(param_shapes)>::value_type> dyn_params{};
    std::copy_if(
        param_shapes.begin(), param_shapes.end(), std::back_inserter(dyn_params), is_dynamic);
    if(dyn_params.empty())
59
        return std::nullopt;
charlie's avatar
charlie committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    std::vector<dynamic_dimensions_check> ret{};
    // get non-fixed dynamic_dimension from all parameters
    for(const auto& param : dyn_params)
    {
        const auto& dds   = param.second.dyn_dims();
        int num_non_fixed = 0;
        for(auto dds_it = dds.begin(); dds_it != dds.end(); ++dds_it)
        {
            if(not dds_it->is_fixed())
            {
                num_non_fixed += 1;
                // catch more than one non-fixed dynamic_dimension
                if(num_non_fixed > 1)
                {
                    return std::nullopt;
                }
                ret.push_back(dynamic_dimensions_check{param.first, *dds_it});
            }
        }
    }
    if(ret.empty())
    {
82
        return std::nullopt;
charlie's avatar
charlie committed
83
84
85
86
87
88
89
90
91
92
    }
    // check all the same dynamic_dimension
    bool same_dd =
        std::all_of(ret.begin() + 1, ret.end(), [&](auto ddc) { return ddc.dd == ret.at(0).dd; });
    if(same_dd)
    {
        return ret;
    }
    return std::nullopt;
}
93

charlie's avatar
charlie committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * Check the parameters in std::vector<dynamic_dimensions_check> object to see if any of the
 * parameters outputs to a select_module operator.
 */
bool any_sm_next(module_ref mm, const std::vector<dynamic_dimensions_check>& ddcs)
{
    for(const auto& ddc : ddcs)
    {
        auto p_outputs  = mm->get_parameter(ddc.dyn_param_str)->outputs();
        bool is_sm_next = std::any_of(p_outputs.cbegin(), p_outputs.cend(), [](auto ins) {
            return ins->name() == "select_module";
        });
        if(is_sm_next)
        {
            return true;
        };
    }
    return false;
112
113
114
}

/**
115
116
 * Makes all the shapes in the dynamic_dimension range.  Probably won't work for `if`
 * and `loop` instructions, depending on how the submodules for those
117
 * work. Inserts select_module instruction to the top. Replaces return, bypassing other
118
 * instructions. Skips if the dynamic parameter outputs to a select_module operator.
119
120
121
122
123
124
 */
void split_single_dyn_dim::apply(module_pass_manager& mpm) const
{
    module_ref mm                               = &mpm.get_module();
    auto param_names                            = mm->get_parameter_names();
    auto param_shapes                           = mm->get_parameter_shapes();
charlie's avatar
charlie committed
125
126
127
    optional<std::vector<dynamic_dimensions_check>> dd_check_vec =
        has_one_unique_dyn_dim(param_shapes);
    if(dd_check_vec.has_value() and not any_sm_next(mm, dd_check_vec.value()))
128
    {
charlie's avatar
charlie committed
129
130
        // all dynamic dimension objects should be the same for all parameters in dd_check_vec
        auto dyn_dim = dd_check_vec->at(0).dd;
131
        // create submodules for each dimension size
charlie's avatar
charlie committed
132
133
        std::vector<module_ref> submodules;
        for(size_t dim_size : migraphx::range(dyn_dim.min, dyn_dim.max + 1))
134
135
136
137
        {
            auto* submod = mpm.create_module("dim_" + std::to_string(dim_size));
            // instruction map for new static shaped submodule parameters
            std::unordered_map<instruction_ref, instruction_ref> map_ins;
charlie's avatar
charlie committed
138
139
140
141
142
143
144
145
            for(const auto& dd_check : dd_check_vec.value())
            {
                // create static shape using dim_size
                const auto& dyn_param = mm->get_parameter(dd_check.dyn_param_str);
                auto dyn_param_shape  = mm->get_parameter_shape(dd_check.dyn_param_str);
                auto static_shape     = dyn_param_shape.to_static(dim_size);
                map_ins[dyn_param]    = submod->add_parameter(dd_check.dyn_param_str, static_shape);
            }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
            auto outputs = submod->add_instructions(mm, map_ins);
            submod->add_return({outputs});
            submodules.push_back(submod);
        }
        // redirect to select_module operator and return
        std::vector<instruction_ref> sm_inputs;
        std::transform(param_names.cbegin(),
                       param_names.cend(),
                       std::back_inserter(sm_inputs),
                       [&](auto pn) { return mm->get_parameter(pn); });
        auto output_shapes       = mm->get_output_shapes();
        migraphx::shape out_attr = migraphx::shape{output_shapes};
        auto sm_ins              = mm->add_instruction(
            migraphx::make_op("select_module",
                              {{"output_dyn_shapes", migraphx::to_value(out_attr)}}),
            sm_inputs,
            submodules);
        std::vector<instruction_ref> outputs(output_shapes.size());
        for(size_t i = 0; i < output_shapes.size(); ++i)
        {
            outputs.at(i) =
                mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", i}}), sm_ins);
        }
        mm->replace_return(outputs);
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx