"vscode:/vscode.git/clone" did not exist on "8c07cf4118a55346265a321d90059dff76436847"
split_dynamic_batch.cpp 3.83 KB
Newer Older
charlie's avatar
initial  
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
39
40
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * 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_dynamic_batch.hpp>
#include <migraphx/functional.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,
                     unsigned int& max_batches)
{
    // true if exactly one dynamic shape with exactly one non-fixed dynamic_dimension
    // dyn_param_str is updated to the parameter string with the dynamic_dimension
    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_batches;
    for(auto ps : param_shapes)
    {
        if(ps.second.dynamic())
        {
            num_dynamic += 1;
            if(num_dynamic > 1)
            {
                return false;
            }
            int num_nf = 0;
            for(auto dd : ps.second.dyn_dims())
            {
                if(not dd.is_fixed())
                {
                    num_nf += 1;
                    tmp_batches = dd.max;
                }
            }
            if(num_nf == 1)
            {
                out_str = ps.first;
            }
            else
            {
                return false;
            }
        }
    }
    dyn_param_str = out_str;
    max_batches   = tmp_batches;
    return true;
}

// don't use program
// use module_pass_manager
void split_dynamic_batch::apply(program& p) const
{
    auto param_shapes = p.get_parameter_shapes();
    std::string dyn_param_str;
    unsigned int max_batches;
    if(has_one_dyn_dim(param_shapes, dyn_param_str, max_batches))
    {
        // floor(log_2(max_batches))
        unsigned int mask = 1U << 31;
        while(not(max_batches & mask))
        {
            mask >>= 1;
        }
        // create submodules based on binary base
        while(mask)
        {
            auto mod =
                p.copy_module(p.get_main_module()->name(), "batch_size_" + std::to_string(mask));
            // change dynamic input parameter shape to static
            // propagate the shape change through the model
            // create new parameter_list
            // use module.add_instructions() with map between old and new static parameter
            // add a return to the submodule
            mask >>= 1;
        }
        // create additional submodules for optimal values if not already done
        //
        // delete stuff in main module, insert instruction to the top, replace return bypassing
        // other instructions unused instructions should be removed by dead_code_elimination
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx