blob: 08796b607b1112df6d10997f91c069671e9b01fa [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
Gunes Bayir3a1e1252023-01-03 21:26:09 +00002 * Copyright (c) 2022-2023 Arm Limited.
SiCong Lif44bbc52022-08-29 18:25:51 +01003 *
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#ifndef SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUWORKLOADSKETCHIMPL
25#define SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUWORKLOADSKETCHIMPL
26
27#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
28#include "src/dynamic_fusion/sketch/gpu/GpuComponentServices.h"
29#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGraph.h"
30#include "src/dynamic_fusion/sketch/gpu/GpuOperatorGroup.h"
31
Gunes Bayir3a1e1252023-01-03 21:26:09 +000032#include <memory>
33#include <vector>
34
SiCong Lif44bbc52022-08-29 18:25:51 +010035namespace arm_compute
36{
37namespace experimental
38{
39namespace dynamic_fusion
40{
41/** Internal implementation of @ref GpuWorkloadSketch */
42class GpuWorkloadSketch::Implementation
43{
44public:
45 /** Constructor
46 *
47 * @param[in] context global workload creation context
48 */
49 explicit Implementation(
50 Context *context)
51 : _context{ context },
52 _comp_services{},
53 _component_graph{ &_comp_services },
Gunes Bayir3a1e1252023-01-03 21:26:09 +000054 _operator_group{},
55 _interm_tensor_info_list{ std::vector<std::unique_ptr<TensorInfo>>() }
SiCong Lif44bbc52022-08-29 18:25:51 +010056 {
57 }
58 /** Prevent instances of this class from being copy constructed */
59 Implementation(const Implementation &impl) = delete;
60 /** Prevent instances of this class from being copied */
61 Implementation &operator=(const Implementation &impl) = delete;
62 /** Allow instances of this class to be move constructed */
63 Implementation(Implementation &&impl) = default;
64 /** Allow instances of this class to be moved */
65 Implementation &operator=(Implementation &&impl) = default;
66 /** Get workload context */
67 const Context *context() const
68 {
69 return _context;
70 }
71 /** Get component graph */
72 const GpuKernelComponentGraph &component_graph() const
73 {
74 return _component_graph;
75 }
76 /** Get component graph */
77 GpuKernelComponentGraph &component_graph()
78 {
79 return _component_graph;
80 }
81 /** Get operator group */
82 const GpuOperatorGroup &operator_group() const
83 {
84 return _operator_group;
85 }
86 /** Get operator group */
87 GpuOperatorGroup &operator_group()
88 {
89 return _operator_group;
90 }
91 ITensorInfo::Id allocate_new_tensor_id()
92 {
93 return ++_next_id;
94 }
95 /** Generate @ref GpuWorkloadSourceCode from the workload sketch
96 * @note The sketch must be valid. Any error encountered during the building of the code will be thrown.
97 *
98 * @return GpuWorkloadSourceCode The generated workload code
99 */
100 GpuWorkloadSourceCode generate_source_code() const
101 {
102 return component_graph().fuse().write_workload_code();
103 }
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000104 /** Create an intermediate tensor info and save it
105 *
106 * @return ITensorInfo The created intermediate tensor info object pointer
107 */
108 ITensorInfo *create_intermediate_tensor()
109 {
110 auto uptr = std::make_unique<TensorInfo>();
111 uptr->set_id(-allocate_new_tensor_id()); // intermediate tensors must have negative id
112 _interm_tensor_info_list.emplace_back(std::move(uptr));
113 return _interm_tensor_info_list.back().get();
114 }
SiCong Lif44bbc52022-08-29 18:25:51 +0100115
116private:
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000117 Context *_context;
118 GpuComponentServices _comp_services;
119 GpuKernelComponentGraph _component_graph;
120 GpuOperatorGroup _operator_group;
121 ITensorInfo::Id _next_id{ ITensorInfo::invalid_tensor_id };
122 std::vector<std::unique_ptr<TensorInfo>> _interm_tensor_info_list;
SiCong Lif44bbc52022-08-29 18:25:51 +0100123};
124} // namespace dynamic_fusion
125} // namespace experimental
126} // namespace arm_compute
127#endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUWORKLOADSKETCHIMPL */