split_single_dyn_dim.cpp 5.58 KB
Newer Older
charlie's avatar
charlie committed
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
31
32
33
34
35
36
37
38
/*
 * 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/functional.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/make_op.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

bool has_one_dyn_dim(std::unordered_map<std::string, shape> param_shapes,
                     std::string& dyn_param_str,
                     int& dyn_index,
                     int& min_dim,
                     int& max_dim)
{
charlie's avatar
charlie committed
39
40
    // true if parameters contain exactly one dynamic shape with exactly one non-fixed
    // dynamic_dimension
charlie's avatar
charlie committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    if(std::none_of(
           param_shapes.cbegin(), param_shapes.cend(), [](auto ps) { return ps.second.dynamic(); }))
        return false;
    int num_dynamic = 0;
    std::string out_str;
    int tmp_min = -1;
    int tmp_max = -1;
    int tmp_ind = -1;
    for(auto ps : param_shapes)
    {
        if(ps.second.dynamic())
        {
            num_dynamic += 1;
            if(num_dynamic > 1)
            {
                return false;
            }
            int num_nf = 0;
            auto dds   = ps.second.dyn_dims();
            for(int i = 0; i < dds.size(); ++i)
            {
                const auto& dd = dds.at(i);
                if(not dd.is_fixed())
                {
                    num_nf += 1;
                    tmp_min = dd.min;
                    tmp_max = dd.max;
                    tmp_ind = i;
                }
            }
            if(num_nf == 1)
            {
                out_str = ps.first;
            }
            else
            {
                return false;
            }
        }
    }
    min_dim       = tmp_min;
    max_dim       = tmp_max;
    dyn_index     = tmp_ind;
    dyn_param_str = out_str;
    return true;
}

/**
charlie's avatar
charlie committed
89
 * Make all the batch sizes in the range for now.
charlie's avatar
charlie committed
90
91
92
93
94
 * Probably won't work for `if` and `loop` instructions, depending on how the submodules for those
 * work create additional submodules for optimal values if not already done insert select_module
 * instruction to the top, replace return bypassing other instructions. Unused instructions should
 * be removed by dead_code_elimination
 */
charlie's avatar
charlie committed
95
void split_single_dyn_dim::apply(module_pass_manager& mpm) const
charlie's avatar
charlie committed
96
{
charlie's avatar
charlie committed
97
    module_ref mm     = &mpm.get_module();
charlie's avatar
charlie committed
98
99
100
101
102
103
104
105
    auto param_names  = mm->get_parameter_names();
    auto param_shapes = mm->get_parameter_shapes();
    std::string dyn_param_name;
    int dyn_index;
    int min_dim;
    int max_dim;
    if(has_one_dyn_dim(param_shapes, dyn_param_name, dyn_index, min_dim, max_dim))
    {
charlie's avatar
charlie committed
106
107
        const auto& dyn_param = mm->get_parameter(dyn_param_name);
        auto dyn_param_shape  = mm->get_parameter_shape(dyn_param_name);
charlie's avatar
charlie committed
108
        std::vector<module_ref> submodules;
charlie's avatar
charlie committed
109
        // create submodules for each dimension size
charlie's avatar
charlie committed
110
111
112
        for(int dim_size = min_dim; dim_size <= max_dim; ++dim_size)
        {
            auto submod = mpm.create_module("batch_" + std::to_string(dim_size));
charlie's avatar
charlie committed
113
            // instruction map for new static submodule parameters
charlie's avatar
charlie committed
114
            std::unordered_map<instruction_ref, instruction_ref> map_ins;
charlie's avatar
charlie committed
115
116
            // create static shape using dim_size
            auto static_lens          = dyn_param_shape.max_lens();
charlie's avatar
charlie committed
117
            static_lens.at(dyn_index) = dim_size;
charlie's avatar
charlie committed
118
119
120
121
            auto static_param         = submod->add_parameter(
                dyn_param_name, migraphx::shape{dyn_param_shape.type(), static_lens});
            map_ins[dyn_param] = static_param;
            auto outputs       = submod->add_instructions(mm, map_ins);
charlie's avatar
charlie committed
122
            submod->add_return({outputs});
charlie's avatar
charlie committed
123
124
            submodules.push_back(submod);
        }
charlie's avatar
charlie committed
125
        // redirect to select_module operator and return
charlie's avatar
charlie committed
126
127
128
129
130
        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); });
charlie's avatar
charlie committed
131
132
133
        auto output_shapes = mm->get_output_shapes();
        migraphx::shape out_attr = migraphx::shape{output_shapes};
        auto ret              = mm->add_instruction(
charlie's avatar
charlie committed
134
            migraphx::make_op("select_module",
charlie's avatar
charlie committed
135
                              {{"output_dyn_shapes", migraphx::to_value(out_attr)}}),
charlie's avatar
charlie committed
136
137
            sm_inputs,
            submodules);
charlie's avatar
charlie committed
138
139
140
141
142
        if (output_shapes.size() == 1)
        {
            ret = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), ret);
        }
        mm->replace_return({ret});
charlie's avatar
charlie committed
143
144
145
146
147
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx