Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 Arm Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |
| 25 | |
| 26 | #ifndef ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_IMPL_COMMON_H |
| 27 | #define ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_IMPL_COMMON_H |
| 28 | |
| 29 | #include "arm_compute/core/CL/CLCompileContext.h" |
| 30 | #include "arm_compute/core/Error.h" |
| 31 | #include "arm_compute/core/GPUTarget.h" |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame] | 32 | #include "src/core/common/Macros.h" |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 33 | |
| 34 | #include "src/core/experimental/dynamic_fusion/ClKernelBuildingAPI.h" |
| 35 | |
| 36 | #include <queue> |
| 37 | #include <stack> |
| 38 | #include <string> |
| 39 | #include <unordered_set> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace experimental |
| 44 | { |
| 45 | namespace dynamic_fusion |
| 46 | { |
| 47 | /** We introduce the concept of *Shared Variables* in the context of kernel building. |
| 48 | * They are variables that can be accessed / shared among all the kernel components within a single kernel. |
| 49 | * For now we consider 2 groups of shared variables: |
| 50 | * Argument: The argument variables (parameters) of a kernel |
| 51 | * Automatic: The automatic variables declared inside a kernel |
| 52 | * All Shared Variables have the same kernel scope, and are thus visible to all kernel components |
| 53 | */ |
| 54 | |
| 55 | enum class SharedVarIO |
| 56 | { |
| 57 | Input, |
| 58 | Output |
| 59 | }; |
| 60 | |
| 61 | enum class SharedVarGroup |
| 62 | { |
| 63 | Argument, // Parameters to a kernel function |
| 64 | Automatic // Automatic variables declared within the kernel body |
| 65 | }; |
| 66 | |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame] | 67 | /** Specifies a shared variable link for a component. |
| 68 | * It describes all the information that's available when a component is constructed / added: |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 69 | * e.g. its linkage (via ArgumentID and io) and its group |
| 70 | * This is not shared variable on its own, but is used for instantiating a SharedVar when building the code |
| 71 | */ |
| 72 | struct SharedVarLink |
| 73 | { |
| 74 | ArgumentID arg_id{ g_arg_placeholder }; |
| 75 | SharedVarIO io{ SharedVarIO::Input }; |
| 76 | SharedVarGroup group{ SharedVarGroup::Argument }; |
| 77 | bool is_empty() const |
| 78 | { |
| 79 | return arg_id == g_arg_placeholder; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | /** A table of all the variables used in the kernel / blueprint |
| 84 | * NOTE: the order they appear in the table is the order of their "declaration" in the component code, and is also their ID |
| 85 | * NOTE: the variables all have the scope of the full kernel function |
| 86 | */ |
| 87 | class SharedVarTable |
| 88 | { |
| 89 | public: |
| 90 | struct SharedVar |
| 91 | { |
| 92 | SharedVarGroup group; |
| 93 | std::string uniq_name; // Unique name, also the final variable name used in the built code |
| 94 | ClKernelArgRuntimeDescriptor desc; // Automatic variables can and should still be described using this struct |
| 95 | }; |
| 96 | |
| 97 | using Arguments = std::vector<SharedVar>; |
| 98 | |
| 99 | /** @note: The order of insertion is important. There is one precondition: |
| 100 | * PRECOND: The components have been sorted topologically / is being traversed in topological order |
| 101 | * This ensures that all the consumer var links (Output, Automatic Links) can consume (return) the producer var links when they're referred |
| 102 | */ |
| 103 | SharedVar add(SharedVarLink var_link, ClKernelArgRuntimeDescriptor runtime_desc, const std::string &name = "unnamed") |
| 104 | { |
| 105 | ARM_COMPUTE_ERROR_ON_MSG(var_link.is_empty(), "Non-empty SharedVarLink expected"); |
| 106 | auto var_id = _num_var; |
| 107 | std::stringstream ss; |
| 108 | ss << name << "_" << var_id; |
| 109 | const auto uniq_name = ss.str(); |
| 110 | SharedVar var{ var_link.group, uniq_name, runtime_desc }; |
| 111 | |
| 112 | if(var_link.group == SharedVarGroup::Argument) |
| 113 | { |
| 114 | _arguments.emplace(var_id, var); |
| 115 | _num_var++; |
| 116 | _var_id_lut[var_link.arg_id] = var_id; |
| 117 | } |
| 118 | else if(var_link.group == SharedVarGroup::Automatic) |
| 119 | { |
| 120 | if(var_link.io == SharedVarIO::Output) |
| 121 | { |
| 122 | _global_vars.emplace(var_id, var); |
| 123 | _num_var++; |
| 124 | _var_id_lut[var_link.arg_id] = var_id; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | // For the input link, the var (and thus its arg_id) will always have been added by the time we get here if we traverse components in topological order |
| 129 | var = get_var(var_link.arg_id); |
| 130 | } |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | ARM_COMPUTE_ERROR("Unrecognised SharedVarGroup"); |
| 135 | } |
| 136 | return var; |
| 137 | } |
| 138 | |
| 139 | SharedVar get_var(ArgumentID arg_id) const |
| 140 | { |
| 141 | const auto var_id = _var_id_lut.at(arg_id); // arg_id has to exist in lut to begin with |
| 142 | auto it = _global_vars.find(var_id); |
| 143 | if(it != _global_vars.end()) |
| 144 | { |
| 145 | return it->second; |
| 146 | } |
| 147 | it = _arguments.find(var_id); |
| 148 | if(it != _arguments.end()) |
| 149 | { |
| 150 | return it->second; |
| 151 | } |
| 152 | ARM_COMPUTE_ERROR("Cannot find component variable"); |
| 153 | } |
| 154 | |
| 155 | /** @note The arguments are returned in the order they are added |
| 156 | */ |
| 157 | Arguments get_kernel_arguments() const |
| 158 | { |
| 159 | Arguments args{}; |
| 160 | for(const auto &a : _arguments) |
| 161 | { |
| 162 | args.push_back(a.second); |
| 163 | } |
| 164 | return args; |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | using VarID = int32_t; |
| 169 | |
| 170 | private: |
| 171 | std::map<VarID, SharedVar> _global_vars{}; |
| 172 | std::map<VarID, SharedVar> _arguments{}; |
| 173 | std::unordered_map<ArgumentID, VarID> _var_id_lut{}; |
| 174 | VarID _num_var{ 0 }; |
| 175 | }; |
| 176 | |
| 177 | enum class ComponentType |
| 178 | { |
| 179 | Simple, |
| 180 | Complex, |
| 181 | Store |
| 182 | }; |
| 183 | |
| 184 | using ComponentID = int32_t; |
| 185 | using ComponentList = std::vector<ComponentID>; |
| 186 | class IClKernelComponent |
| 187 | { |
| 188 | public: |
| 189 | using Link = SharedVarLink; |
| 190 | using Tag = std::string; |
| 191 | struct TagVal |
| 192 | { |
| 193 | TagVal() = default; |
| 194 | TagVal(SharedVarTable::SharedVar var) |
| 195 | : value{ var.uniq_name } |
| 196 | { |
| 197 | } |
| 198 | |
| 199 | TagVal(ComponentID id) |
| 200 | : value{ std::to_string(id) } |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | std::string value{}; |
| 205 | }; |
| 206 | using TagLUT = std::unordered_map<Tag, TagVal>; // Used to instantiating a code template / replacing tags |
| 207 | public: |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame] | 208 | IClKernelComponent(const ClKernelBlueprint *blueprint) |
| 209 | : _blueprint(blueprint) |
| 210 | { |
| 211 | } |
| 212 | |
| 213 | ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(IClKernelComponent); |
| 214 | |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 215 | virtual ~IClKernelComponent() = default; |
| 216 | virtual ComponentType get_component_type() const = 0; |
| 217 | virtual std::vector<Link> get_links() const = 0; |
| 218 | virtual std::string name() const = 0; |
| 219 | |
| 220 | static std::string replace_tags(const std::string &code_template, const TagLUT &tags) |
| 221 | { |
| 222 | std::string replaced_code = ""; |
| 223 | std::unordered_set<std::string> used_tags{}; |
| 224 | bool scanning_pattern = false; |
| 225 | std::string pattern_found = ""; |
| 226 | for(size_t i = 0; i < code_template.size() - 1; ++i) |
| 227 | { |
| 228 | if(!scanning_pattern) |
| 229 | { |
| 230 | if(code_template[i] == '{' && code_template[i + 1] == '{') |
| 231 | { |
| 232 | i += 1; |
| 233 | scanning_pattern = true; |
| 234 | pattern_found = ""; |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | replaced_code += code_template[i]; |
| 239 | } |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | if(code_template[i] == '}' && code_template[i + 1] == '}') |
| 244 | { |
| 245 | i += 1; |
| 246 | scanning_pattern = false; |
| 247 | std::string err = "Pattern " + pattern_found + " not found in tags"; |
| 248 | ARM_COMPUTE_ERROR_ON_MSG(tags.find(pattern_found) == tags.end(), err.c_str()); |
| 249 | replaced_code += tags.find(pattern_found)->second.value; |
| 250 | used_tags.insert(pattern_found); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | pattern_found += code_template[i]; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | // Check for unused tags |
| 259 | for(const auto &tag : tags) |
| 260 | { |
| 261 | ARM_COMPUTE_UNUSED(tag); |
| 262 | ARM_COMPUTE_ERROR_ON_MSG(used_tags.find(tag.first) == used_tags.end(), "Warning: unused tags"); |
| 263 | } |
| 264 | return replaced_code; |
| 265 | } |
| 266 | ComponentID id() const |
| 267 | { |
| 268 | return _id; |
| 269 | } |
| 270 | void set_id(ComponentID id) |
| 271 | { |
| 272 | _id = id; |
| 273 | } |
| 274 | |
| 275 | virtual std::set<std::string> get_headers_list() const |
| 276 | { |
| 277 | return std::set<std::string> {}; |
| 278 | } |
| 279 | |
| 280 | virtual std::string get_additional_macros() const |
| 281 | { |
| 282 | return ""; |
| 283 | } |
| 284 | |
| 285 | virtual std::string get_component_code() const |
| 286 | { |
| 287 | return ""; |
| 288 | } |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame] | 289 | |
| 290 | virtual Window get_window() const |
| 291 | { |
| 292 | return Window{}; |
| 293 | } |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 294 | /** "Allocate" all shared variables used in a component to the @p vtable, and generate a TagLUT used to instantiate the component code |
| 295 | * |
| 296 | * @param vtable |
| 297 | * @return TagLUT |
| 298 | */ |
| 299 | virtual TagLUT allocate_vars(SharedVarTable &vtable) const = 0; |
| 300 | |
| 301 | virtual std::string get_dst_addr_calculation() const |
| 302 | { |
| 303 | return ""; |
| 304 | } |
| 305 | |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame] | 306 | protected: |
| 307 | const ClKernelBlueprint *_blueprint; |
| 308 | |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 309 | private: |
| 310 | ComponentID _id{}; |
| 311 | }; |
| 312 | |
| 313 | using ComponentUniquePtr = std::unique_ptr<IClKernelComponent>; |
| 314 | |
| 315 | /** Intermediate representation of the final, complete kernel source. |
| 316 | */ |
| 317 | struct ClKernelBlueprint::Implementation |
| 318 | { |
| 319 | public: |
| 320 | Implementation() = default; |
| 321 | ~Implementation() = default; |
| 322 | |
| 323 | public: |
| 324 | ArgumentID add_kernel_argument(const ClTensorDescriptor &tensor_desc) |
| 325 | { |
| 326 | _kernel_arguments.insert(std::make_pair(_num_args, tensor_desc)); |
| 327 | _shared_var_group_lut[_num_args] = SharedVarGroup::Argument; |
| 328 | return _num_args++; |
| 329 | } |
| 330 | |
| 331 | ArgumentID add_intermediate_tensor() |
| 332 | { |
| 333 | _intermediate_tensors.insert(_num_args); |
| 334 | _shared_var_group_lut[_num_args] = SharedVarGroup::Automatic; |
| 335 | return _num_args++; |
| 336 | } |
| 337 | |
| 338 | void set_tile_info(const TileDescriptor &tile_info) |
| 339 | { |
| 340 | _tile_info = tile_info; |
| 341 | } |
| 342 | |
| 343 | SharedVarGroup group(ArgumentID arg_id) const |
| 344 | { |
| 345 | if(arg_id == g_arg_placeholder) |
| 346 | { |
| 347 | // In case of placeholder, don't care what we return; |
| 348 | return SharedVarGroup::Argument; |
| 349 | } |
| 350 | return _shared_var_group_lut.at(arg_id); |
| 351 | } |
| 352 | |
| 353 | void validate_arg_ids(std::initializer_list<ArgumentID> args) const |
| 354 | { |
| 355 | for(const auto arg_id : args) |
| 356 | { |
| 357 | ARM_COMPUTE_UNUSED(arg_id); |
| 358 | ARM_COMPUTE_ERROR_ON_MSG(_kernel_arguments.find(arg_id) == _kernel_arguments.end() && _intermediate_tensors.find(arg_id) == _intermediate_tensors.end() && arg_id != g_arg_placeholder, |
| 359 | "Trying to use an argument that hasn't been added to the blueprint"); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void add_component(ComponentUniquePtr component) |
| 364 | { |
| 365 | if(component->get_component_type() == ComponentType::Complex) |
| 366 | { |
| 367 | ++_num_complex_components; |
| 368 | ARM_COMPUTE_ERROR_ON_MSG(_num_complex_components > 1, "Only one complex component per blueprint is supported."); |
| 369 | } |
| 370 | |
| 371 | // This flag specifies if the current component is the root of the component graph |
| 372 | // If the root is set to -1, it means that a root hasn't been added yet |
| 373 | bool is_graph_root = true; |
| 374 | |
| 375 | // Get an unique ID for the component that's being added |
| 376 | const ComponentID component_id = _num_components++; |
| 377 | component->set_id(component_id); |
| 378 | |
| 379 | // Add this component to the component graph. Don't connect it to anything yet |
| 380 | _component_graph.emplace(component_id, ComponentList{}); |
| 381 | |
| 382 | int32_t positional_arg = 0; |
| 383 | |
| 384 | // For every { arg_id, arg_io } passed along with this component... |
| 385 | for(const auto &link : component->get_links()) |
| 386 | { |
| 387 | const ArgumentID &arg_id = link.arg_id; |
| 388 | const SharedVarIO &arg_io = link.io; |
| 389 | |
| 390 | // A component is considered root only if all its input arguments are kernel arguments (or placeholders, which means nullptr) |
| 391 | // This performs a check on every argument, and if one of them doesn't respect the condition, the component is not considered root |
| 392 | is_graph_root &= (_kernel_arguments.find(arg_id) != _kernel_arguments.end()) || (arg_io == SharedVarIO::Output) || (arg_id == g_arg_placeholder); |
| 393 | |
| 394 | // Add the arg_id to the map describing the input/output relationship between an argument and the components that use it, if it doesn't yet exist there |
| 395 | if(_outgoing_components.find(arg_id) == _outgoing_components.end()) |
| 396 | { |
| 397 | _outgoing_components.emplace(arg_id, ComponentList{}); |
| 398 | _incoming_components.emplace(arg_id, ComponentList{}); |
| 399 | } |
| 400 | |
| 401 | // If it's an input argument, connect any other component that has it as output with this component |
| 402 | // Additionally, set this component as one that treats this argument as "Input" (append to index 0) |
| 403 | // This is used so that we keep track of whether two components use the same argument, one as input and one as output |
| 404 | if(arg_io == SharedVarIO::Input) |
| 405 | { |
| 406 | for(const auto &prev_component : _incoming_components[arg_id]) |
| 407 | { |
| 408 | _component_graph[prev_component].push_back(component_id); |
| 409 | } |
| 410 | |
| 411 | _outgoing_components[arg_id].push_back(component_id); |
| 412 | } |
| 413 | // If it's an output argument, connect this component with any other component that has it as input |
| 414 | // Additionally, set this component as one that treats this argument as "Output" (append to index 1) |
| 415 | else |
| 416 | { |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame] | 417 | if(component->get_component_type() == ComponentType::Store) |
| 418 | { |
| 419 | ARM_COMPUTE_ERROR_ON_MSG(_dst_id >= 0, "Trying to add more than one dst argument to the graph"); |
| 420 | _dst_id = arg_id; |
| 421 | } |
| 422 | |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 423 | for(const auto &subseq_component : _outgoing_components[arg_id]) |
| 424 | { |
| 425 | _component_graph[component_id].push_back(subseq_component); |
| 426 | } |
| 427 | |
| 428 | _incoming_components[arg_id].push_back(component_id); |
| 429 | } |
| 430 | |
| 431 | ++positional_arg; |
| 432 | } |
| 433 | |
| 434 | if(is_graph_root) |
| 435 | { |
| 436 | ARM_COMPUTE_ERROR_ON_MSG(_graph_root >= 0, "Trying to add more than one root to the graph"); |
| 437 | _graph_root = component_id; |
| 438 | } |
| 439 | |
| 440 | // Finally, add this component to the dictionary of components |
| 441 | _components.insert(std::make_pair(component_id, std::move(component))); |
| 442 | } |
| 443 | |
| 444 | std::string build_kernel_name() const |
| 445 | { |
| 446 | std::string name = ""; |
| 447 | |
| 448 | auto stack = topological_sort(); |
| 449 | while(!stack.empty()) |
| 450 | { |
| 451 | name += _components.find(stack.top())->second->name() + (stack.size() > 2 ? "___" : ""); |
| 452 | stack.pop(); |
| 453 | } |
| 454 | |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 455 | return name; |
| 456 | } |
| 457 | |
| 458 | std::string build_code() |
| 459 | { |
| 460 | ARM_COMPUTE_ERROR_ON_MSG(_graph_root < 0, "No root found in the component graph"); |
| 461 | |
| 462 | // These data structures will hold the data from all the components in the blueprint |
| 463 | std::set<std::string> headers_list{}; |
| 464 | std::set<std::string> additional_macros{}; |
| 465 | std::vector<std::string> component_codes{}; // vector because order matters |
| 466 | |
| 467 | // Go through the components graph (topological sort) and fill the data structures above |
| 468 | auto stack = topological_sort(); |
| 469 | while(!stack.empty()) |
| 470 | { |
| 471 | auto curr_component_id = stack.top(); |
| 472 | auto &curr_component = _components.find(curr_component_id)->second; |
| 473 | |
| 474 | auto curr_headers_list = curr_component->get_headers_list(); |
| 475 | auto curr_additional_macros = curr_component->get_additional_macros(); |
| 476 | auto curr_component_code = curr_component->get_component_code(); |
| 477 | const auto var_lut = curr_component->allocate_vars(_vtable); // Ideally can be merged with get_component_code once we have finer-grained code generation technique |
| 478 | component_codes.push_back(IClKernelComponent::replace_tags(curr_component_code, var_lut)); |
| 479 | |
| 480 | headers_list.insert(curr_headers_list.begin(), curr_headers_list.end()); |
| 481 | if(!curr_additional_macros.empty()) // Some components might not have any |
| 482 | { |
| 483 | additional_macros.insert(curr_additional_macros); |
| 484 | } |
| 485 | |
| 486 | stack.pop(); |
| 487 | } |
| 488 | |
| 489 | // This section assembles the data gathered by traversing the graph into the string "code" |
| 490 | std::string code = ""; |
| 491 | |
| 492 | for(auto &header : headers_list) |
| 493 | { |
| 494 | code += "#include \"" + header + "\"\n"; |
| 495 | } |
| 496 | |
| 497 | for(auto ¯os : additional_macros) |
| 498 | { |
| 499 | code += macros; |
| 500 | } |
| 501 | |
| 502 | code += generate_kernel_signature(_vtable.get_kernel_arguments()); |
| 503 | |
| 504 | code += "\n{\n\n"; |
| 505 | |
| 506 | code += " //------------------ START KERNEL_BUILDER_COORDINATE ---------------------\n\n"; |
| 507 | code += generate_global_section(); |
| 508 | code += " //------------------ END KERNEL_BUILDER_COORDINATE ---------------------\n"; |
| 509 | |
| 510 | for(auto &component_code : component_codes) |
| 511 | { |
| 512 | code += component_code; |
| 513 | } |
| 514 | |
| 515 | code += "}\n"; |
| 516 | |
| 517 | return code; |
| 518 | } |
| 519 | |
| 520 | std::string build_config_id() const |
| 521 | { |
| 522 | return ""; |
| 523 | } |
| 524 | |
| 525 | CLBuildOptions build_options() const |
| 526 | { |
| 527 | return CLBuildOptions{}; |
| 528 | } |
| 529 | |
| 530 | Window get_execution_window() const |
| 531 | { |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame] | 532 | ARM_COMPUTE_ERROR_ON_MSG(_graph_root < 0, "No root found in the component graph"); |
| 533 | ARM_COMPUTE_ERROR_ON_MSG(_dst_id == -1, "Destination Tensor Id should be ready before calling get_execution_window()"); |
| 534 | |
| 535 | return _components.find(_graph_root)->second->get_window(); |
| 536 | } |
| 537 | |
| 538 | ArgumentID get_dst_id() const |
| 539 | { |
| 540 | return _dst_id; |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | ClKernelArgList get_arguments() const |
| 544 | { |
| 545 | ClKernelArgList arg_list{}; |
| 546 | for(const auto &arg_var : _vtable.get_kernel_arguments()) |
| 547 | { |
| 548 | arg_list.push_back(arg_var.desc); |
| 549 | } |
| 550 | return arg_list; |
| 551 | } |
| 552 | |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame] | 553 | const ClTensorDescriptor *get_kernel_argument(const ArgumentID id) const |
| 554 | { |
| 555 | auto it = _kernel_arguments.find(id); |
| 556 | if(it != _kernel_arguments.end()) |
| 557 | { |
| 558 | return &_kernel_arguments.find(id)->second; |
| 559 | } |
| 560 | return nullptr; |
| 561 | } |
| 562 | |
| 563 | ITensorInfo *get_kernel_argument_info(const ArgumentID id) const |
| 564 | { |
| 565 | const ClTensorDescriptor *arg_desc = get_kernel_argument(id); |
| 566 | if(arg_desc != nullptr) |
| 567 | { |
| 568 | return arg_desc->tensor_info; |
| 569 | } |
| 570 | return nullptr; |
| 571 | } |
| 572 | |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 573 | private: |
| 574 | void topological_sort_utility(ComponentID component_id, std::unordered_set<ComponentID> &visited, std::stack<ComponentID> &stack) const |
| 575 | { |
| 576 | visited.insert(component_id); |
| 577 | |
| 578 | for(auto connected_component : _component_graph.find(component_id)->second) |
| 579 | { |
| 580 | if(visited.find(connected_component) == visited.end()) |
| 581 | { |
| 582 | topological_sort_utility(connected_component, visited, stack); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | stack.push(component_id); |
| 587 | } |
| 588 | |
| 589 | std::stack<ComponentID> topological_sort() const |
| 590 | { |
| 591 | std::stack<ComponentID> stack{}; |
| 592 | std::unordered_set<ComponentID> visited{}; |
| 593 | |
| 594 | topological_sort_utility(_graph_root, visited, stack); |
| 595 | |
| 596 | return stack; |
| 597 | } |
| 598 | |
| 599 | std::string generate_argument_declaration(const SharedVarTable::SharedVar &var) const |
| 600 | { |
| 601 | ARM_COMPUTE_ERROR_ON_MSG(var.group != SharedVarGroup::Argument, "An argument declaration can only be generated from a kernel argument"); |
| 602 | std::string code; |
| 603 | switch(var.desc.tensor_arg_type) |
| 604 | { |
| 605 | case TensorArgType::Image: |
| 606 | { |
| 607 | code += "IMAGE_DECLARATION(" + var.uniq_name + ")"; |
| 608 | break; |
| 609 | } |
| 610 | case TensorArgType::Image_3D: |
| 611 | { |
| 612 | code += "IMAGE_DECLARATION(" + var.uniq_name + "),\n"; |
| 613 | code += "uint " + var.uniq_name + "_stride_z"; |
| 614 | break; |
| 615 | } |
| 616 | case TensorArgType::Image_3D_Export_To_ClImage2D: |
| 617 | { |
| 618 | code += "__read_only image2d_t " + var.uniq_name + "_img,\n"; |
| 619 | code += "uint " + var.uniq_name + "_stride_z,\n"; |
| 620 | break; |
| 621 | } |
| 622 | default: |
| 623 | { |
| 624 | ARM_COMPUTE_ERROR("Unsupported declaration generation for TensorArgType"); |
| 625 | } |
| 626 | } |
| 627 | return code; |
| 628 | } |
| 629 | |
| 630 | std::string generate_kernel_signature(const SharedVarTable::Arguments &argument_list) const |
| 631 | { |
| 632 | std::string code = "\n__kernel void " + build_kernel_name() + "("; |
| 633 | |
| 634 | for(const auto &arg : argument_list) |
| 635 | { |
| 636 | code += "\n " + generate_argument_declaration(arg) + ","; |
| 637 | } |
| 638 | |
| 639 | code[code.length() - 1] = ')'; |
| 640 | |
| 641 | return code; |
| 642 | } |
| 643 | |
| 644 | std::string generate_global_section() const |
| 645 | { |
| 646 | std::string code = " uint g_x = get_global_id(0);\n"; |
| 647 | code += " uint g_y = get_global_id(1);\n"; |
| 648 | code += " uint g_z = get_global_id(2);\n\n"; |
| 649 | |
| 650 | size_t tile_dim_x = _tile_info.empty() ? 1 : _tile_info.tile_dims.x(); |
| 651 | size_t tile_dim_y = _tile_info.empty() ? 1 : _tile_info.tile_dims.y(); |
| 652 | |
| 653 | switch(_tile_info.clipping) |
| 654 | { |
| 655 | case ClippingStrategy::TOP_LEFT: |
| 656 | code += " const bool g_cond_x = (g_x == 0);\n"; |
| 657 | code += " const bool g_cond_y = (g_y == 0);\n"; |
| 658 | break; |
| 659 | case ClippingStrategy::TOP_RIGHT: |
| 660 | code += " const bool g_cond_x = ((g_x + 1) * " + std::to_string(tile_dim_x) + " >= " + std::to_string(_tile_info.boundaries.x()) + ");\n"; |
| 661 | code += " const bool g_cond_y = (g_y == 0);\n"; |
| 662 | break; |
| 663 | case ClippingStrategy::BOTTOM_LEFT: |
| 664 | code += " const bool g_cond_x = (g_x == 0);\n"; |
| 665 | code += " const bool g_cond_y = ((g_y + 1) * " + std::to_string(tile_dim_y) + " >= " + std::to_string(_tile_info.boundaries.y()) + ");\n"; |
| 666 | break; |
| 667 | case ClippingStrategy::BOTTOM_RIGHT: |
| 668 | code += " const bool g_cond_x = ((g_x + 1) * " + std::to_string(tile_dim_x) + " >= " + std::to_string(_tile_info.boundaries.x()) + ");\n"; |
| 669 | code += " const bool g_cond_y = ((g_y + 1) * " + std::to_string(tile_dim_y) + " >= " + std::to_string(_tile_info.boundaries.y()) + ");\n"; |
| 670 | break; |
| 671 | default: |
| 672 | ARM_COMPUTE_ERROR("Unsupported clipping strategy"); |
| 673 | } |
| 674 | |
| 675 | code += "\n REPEAT_VAR_INIT_TO_CONST(M0, uint, g_zout, 0);\n"; |
| 676 | code += " REPEAT_VAR_INIT_TO_CONST(16, uint, g_zero, 0);\n\n"; |
| 677 | |
| 678 | return code; |
| 679 | } |
| 680 | |
| 681 | TileDescriptor _tile_info{}; |
| 682 | |
| 683 | int32_t _num_args{}; |
| 684 | int32_t _num_components{}; |
| 685 | int32_t _num_complex_components{}; |
| 686 | |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame] | 687 | ArgumentID _dst_id{ -1 }; |
| 688 | |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 689 | // Argument, components and intermediate tensors IDs with corresponding ptrs (except intermediate) |
| 690 | std::unordered_map<ComponentID, ComponentUniquePtr> _components{}; |
| 691 | std::unordered_map<ArgumentID, ClTensorDescriptor> _kernel_arguments{}; |
| 692 | std::unordered_set<ArgumentID> _intermediate_tensors{}; |
| 693 | // Argument group lookup. Can be replaced by extending the ArgumentID type to include group info |
| 694 | std::unordered_map<ArgumentID, SharedVarGroup> _shared_var_group_lut{}; |
| 695 | |
| 696 | // Tracks all variables (e.g.: kernel arguments, kernel "global variables") |
| 697 | SharedVarTable _vtable{}; |
| 698 | |
| 699 | // Component directed graph (represented by an adjecency list of Component IDs) |
| 700 | // This is used to understand the ordering and bindings between components when generating the kernel |
| 701 | // It's initially set to -1 which means the graph has no root yet, since node IDs are positive numbers |
| 702 | ComponentID _graph_root{ -1 }; |
| 703 | std::unordered_map<ComponentID, ComponentList> _component_graph{}; |
| 704 | |
| 705 | // Additional data structures used to define the relationships between components and arguments |
| 706 | // For each argument, it contains the list of components that consider it as an incoming or an outgoing argument |
| 707 | // E.g. tensor0 -> component0 -> tensor1 |
| 708 | // _outgoing_components[tensor0] == {component0} (component0 is the outgoing component of tensor0. Component0 treats tensor0 as an input tensor) |
| 709 | // _incoming_components[tensor1] == {component0} (component0 is the incoming component of tensor1. Component1 treats tensor1 as an output tensor) |
| 710 | std::unordered_map<ArgumentID, ComponentList> _outgoing_components{}; |
| 711 | std::unordered_map<ArgumentID, ComponentList> _incoming_components{}; |
| 712 | }; |
| 713 | |
| 714 | } // namespace dynamic_fusion |
| 715 | } // namespace experimental |
| 716 | } // namespace arm_compute |
| 717 | #endif //ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_IMPL_COMMON_H |
| 718 | |
| 719 | #endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |