aboutsummaryrefslogtreecommitdiff
path: root/src/core/experimental/dynamic_fusion/WorkloadImpl/ClFusedKernelGraph.cpp
blob: 4e57d66a1cd28ccf7a707e97d39b0fc9a5ce8ec3 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (c) 2022 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * 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.
 */

#ifdef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION
#include "src/core/experimental/dynamic_fusion/WorkloadImpl/ClFusedKernelGraph.h"

namespace arm_compute
{
namespace experimental
{
namespace dynamic_fusion
{
namespace
{
std::vector<std::pair<ClKernelFusionGroup *, ClKernelFusionGroup *>> get_combinations(const std::vector<ClKernelFusionGroup *> &sorted_fgs)
{
    ARM_COMPUTE_ERROR_ON(sorted_fgs.size() <= 1);
    std::vector<std::pair<ClKernelFusionGroup *, ClKernelFusionGroup *>> combo;
    for(size_t i = 0; i < sorted_fgs.size() - 1; ++i)
    {
        for(size_t j = i + 1; j < sorted_fgs.size(); ++j)
        {
            combo.push_back(std::make_pair(sorted_fgs.at(i), sorted_fgs.at(j)));
        }
    }
    return combo;
}
} // namespace
std::vector<const ClKernel *> traverse(const ClKernelFusionGroup &group)
{
    std::vector<const ClKernel *> kernels;
    const auto                    sorted = group.graph.topological_sort();
    for(const auto &pack : sorted.second)
    {
        kernels.push_back(group.fused_kernels.at(pack.op));
    }
    return kernels;
}

std::vector<const ClKernelFusionGroup *> traverse(const ClFusedKernelGraph &graph)
{
    std::vector<const ClKernelFusionGroup *> kernels;
    const auto                               sorted = graph.fg_dependency.topological_sort();
    for(const auto &pack : sorted.second)
    {
        kernels.push_back(graph.fusion_groups.at(pack.op).get());
    }
    return kernels;
}

std::vector<ClKernelFusionGroup *> traverse(ClFusedKernelGraph &graph)
{
    std::vector<ClKernelFusionGroup *> kernels;
    const auto                         sorted = graph.fg_dependency.topological_sort();
    for(const auto &pack : sorted.second)
    {
        kernels.push_back(graph.fusion_groups.at(pack.op).get());
    }
    return kernels;
}

std::pair<Status, ClFusedKernelGraph> init_fusion_graph(const ClKernelGraph &kernel_graph)
{
    ClFusedKernelGraph fused_kernel_graph{};
    fused_kernel_graph.original_graph = &kernel_graph; // Create a copy of the original kernel graph
    fused_kernel_graph.fg_dependency  = DependencyGraph();
    // Initialize all fusion groups
    for(const auto &kernel : traverse(kernel_graph))
    {
        fused_kernel_graph.add_fusion_group({ kernel });
    }
    return { Status{}, fused_kernel_graph };
}

Status fuse(ClFusedKernelGraph &fused_kernel_graph)
{
    // A naive fusion algorithm that's guaranteed to find optimal pattern if there are no branches
    // If there are branches, the algorithm cannot guanrantee optimality as it doesn't perform any searches

    bool fusion_found = false;
    do
    {
        fusion_found          = false;
        const auto sorted_fgs = traverse(fused_kernel_graph);
        if(sorted_fgs.size() <= 1)
        {
            // Only one or zero fusion group, thus no need to perform fusion
            return Status{};
        }
        auto fgs_combo = get_combinations(sorted_fgs);
        for(auto fgs : fgs_combo)
        {
            auto       fg0 = fgs.first;
            auto       fg1 = fgs.second;
            const auto st  = fused_kernel_graph.can_fuse(*fg0, *fg1);
            if(bool(st))
            {
                const auto st = fused_kernel_graph.fuse(*fg0, *fg1);
                if(!bool(st))
                {
                    return st;
                }
                fusion_found = true;
                break;
            }
        }
    }
    while(fusion_found);
    return Status{};
}
Status generate_store(ClKernelBlueprint &bp, const ClFusedKernelGraph &fused_kernel_graph, const ClKernelFusionGroup &fg)
{
    Status st{};
    for(const auto &dst_t_id : fused_kernel_graph.fg_dependency.dst_tensors(fg.id))
    {
        const auto dst_t = fused_kernel_graph.original_graph->get_tensor(dst_t_id);

        /// NOTE: dst tensor must have already been added to the blueprint at this point
        ArgumentID dst_id;
        st = add_tensor(bp, dst_t->desc, dst_id, dst_t->id);
        if(!bool(st))
        {
            return st;
        }
        /// NOTE: the extra dst tensor is needed as the store kcomp requires 2 tensors. But this is irrelevant to the fused kernel graph
        /// since both tensors share the exact same info and kernel arg descriptor
        ArgumentID dst_dst_id;
        st = add_tensor(bp, dst_t->desc, dst_dst_id);
        if(!bool(st))
        {
            return st;
        }
        /// NOTE: Update the merge point map to link dst_dst_id with dst_t->id instead.
        /// This is required because the get_arguments() returned by the blueprint returns the dst tensor added by the store component
        st = update_merge_point(bp, dst_dst_id, dst_t->id);
        if(!bool(st))
        {
            return st;
        }
        st = add_kcomp_store(bp, fg.get_root_kernel()->config().store_type, dst_id, dst_dst_id);
        if(!bool(st))
        {
            return st;
        }
    }
    return st;
}

Status generate(ClWorkload &workload, const ClWorkloadContext &ctx, const ClFusedKernelGraph &fused_kernel_graph)
{
    workload.context = ctx;
    for(const auto &fg : traverse(fused_kernel_graph))
    {
        ClKernelBlueprint bp{};
        for(const auto &kernel : traverse(*fg))
        {
            const auto st = kernel->generate(bp);
            if(!bool(st))
            {
                return st;
            }
        }
        auto st = set_tile_info(bp, fg->get_root_kernel()->config().tile_desc);
        if(!bool(st))
        {
            return st;
        }
        st = generate_store(bp, fused_kernel_graph, *fg);
        if(!bool(st))
        {
            return st;
        }

        ClKernelCode code{};
        st = build(code, ClCodeBuilderContext{ ctx.gpu_info }, bp);
        if(!bool(st))
        {
            return st;
        }
        const auto bp_graph = get_dependency_graph(bp);

        // Get tensor info
        std::vector<Id> workload_src_tensors{};
        for(const auto &src_t_id : fused_kernel_graph.fg_dependency.src_tensors(fg->id))
        {
            const auto src_t = fused_kernel_graph.original_graph->get_tensor(src_t_id);
            // Get corresponding kernel arg descriptor
            const auto arg_desc    = code.arguments.at(bp_graph.get_merge_points().at(src_t->id));
            const auto kernel_t_id = workload.add_workload_tensor(src_t->desc, src_t->memory_type, src_t->memory_info, arg_desc, src_t->id);
            workload_src_tensors.push_back(kernel_t_id);
        }
        std::vector<Id> workload_dst_tensors{};
        for(const auto &dst_t_id : fused_kernel_graph.fg_dependency.dst_tensors(fg->id))
        {
            const auto dst_t = fused_kernel_graph.original_graph->get_tensor(dst_t_id);
            // Get corresponding kernel arg descriptor
            const auto arg_desc    = code.arguments.at(bp_graph.get_merge_points().at(dst_t->id));
            const auto kernel_t_id = workload.add_workload_tensor(dst_t->desc, dst_t->memory_type, dst_t->memory_info, arg_desc, dst_t->id);
            workload_dst_tensors.push_back(kernel_t_id);
        }

        workload.add_unit_workload(fg->get_root_kernel()->config().stage, code, workload_src_tensors, workload_dst_tensors);
    }

    return Status{};
}

} // namespace dynamic_fusion
} // namespace experimental
} // namespace arm_compute
#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */