blob: 0f43a578dff0d7e5b1e129e408561ec15f3e18ca [file] [log] [blame]
Ramy Elgammalf800adf2022-12-14 15:39:29 +00001/*
2 * Copyright (c) 2023 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#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuReshape.h"
25#include "arm_compute/core/Error.h"
26#include "src/common/utils/Log.h"
27#include "src/core/helpers/AutoConfiguration.h"
28#include "src/dynamic_fusion/sketch/ArgumentPack.h"
29#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
30#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentReshape.h"
31
32namespace arm_compute
33{
34namespace experimental
35{
36namespace dynamic_fusion
37{
38namespace
39{
Gunes Bayircc287732023-01-19 15:56:00 +000040Status is_supported_op_helper(const GpuWorkloadContext &context,
41 const ITensorInfo *src,
42 const ITensorInfo *dst,
43 const ReshapeAttributes &attributes)
Ramy Elgammalf800adf2022-12-14 15:39:29 +000044{
Gunes Bayircc287732023-01-19 15:56:00 +000045 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
46
47 TensorInfo dst_info_to_validate;
48 const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate;
49
50 if(dst != nullptr)
51 {
52 dst_info_to_validate_ptr = dst;
53 }
54
Ramy Elgammalf800adf2022-12-14 15:39:29 +000055 auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(attributes.shape()));
56
57 // Check components
58 if(context.gpu_language() == GpuLanguage::OpenCL)
59 {
60 const auto cl_compile_ctx = context.cl_compile_context();
61 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
62
63 // Validate GpuReshape Component
64 ArgumentPack<ITensorInfo> arguments;
65 arguments.add_const_tensor(ACL_SRC_0, src);
Gunes Bayircc287732023-01-19 15:56:00 +000066 arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr);
Ramy Elgammalf800adf2022-12-14 15:39:29 +000067
68 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentReshape::validate(arguments));
69 }
70 else
71 {
72 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
73 }
74
75 return Status{};
76}
Gunes Bayircc287732023-01-19 15:56:00 +000077
78GpuOperatorType operator_type = GpuOperatorType::Complex;
79} // namespace
80
81Status GpuReshape::is_supported_op(const GpuWorkloadContext &context,
82 const ITensorInfo *src,
83 const Attributes &attributes)
84{
85 return is_supported_op_helper(context, src, nullptr, attributes);
86}
87
Ramy Elgammalf800adf2022-12-14 15:39:29 +000088Status GpuReshape::validate_op(const GpuWorkloadSketch &sketch,
89 const ITensorInfo *src,
Ramy Elgammalf800adf2022-12-14 15:39:29 +000090 const Attributes &attributes)
91{
Gunes Bayircc287732023-01-19 15:56:00 +000092 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
93 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
94
95 // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object
96 TensorInfo dst_info_to_validate;
Ramy Elgammalf800adf2022-12-14 15:39:29 +000097
98 // Auto initialize dst tensor info
Ramy Elgammalf800adf2022-12-14 15:39:29 +000099 auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(attributes.shape()));
100
101 // Perform fusion test
102 // Pack tensor infos
103 ArgumentPack<ITensorInfo> tensors;
104 tensors.add_const_tensor(ACL_SRC_0, src);
105 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
106 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
107 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
108 "Operator fusion test failed. This operator cannot be fused into the workload");
109
110 // Check if configuration is supported
Gunes Bayircc287732023-01-19 15:56:00 +0000111 return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate, attributes);
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000112}
113
Gunes Bayircc287732023-01-19 15:56:00 +0000114ITensorInfo *GpuReshape::create_op(GpuWorkloadSketch &sketch,
115 ITensorInfo *src,
116 const Attributes &attributes)
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000117{
Gunes Bayircc287732023-01-19 15:56:00 +0000118 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
119 ARM_COMPUTE_LOG_PARAMS(src, attributes.shape());
120 ARM_COMPUTE_ERROR_THROW_ON(GpuReshape::validate_op(sketch, src, attributes));
121
122 ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
123 ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
124
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000125 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(attributes.shape()));
126
127 // Translate into components and add to component graph
128 auto &comp_graph = sketch.implementation().component_graph();
129 const auto sketch_ctx = sketch.implementation().context();
130 if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
131 {
132 const auto cl_compile_ctx = sketch_ctx->cl_compile_context();
Omar Al Khatib3c7c1fa2023-03-07 09:57:49 +0000133 ARM_COMPUTE_UNUSED(cl_compile_ctx);
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000134 ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr);
135
136 // Add ElementwiseBinary Component
137 {
138 auto properties = IGpuKernelComponent::Properties();
139 properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
140
141 ArgumentPack<ITensorInfo> arguments;
142 arguments.add_const_tensor(ACL_SRC_0, src);
143 arguments.add_const_tensor(ACL_DST_0, dst);
144 comp_graph.add_new_component<ClComponentReshape>(properties, arguments);
145 }
146 }
147 else
148 {
149 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
150 }
151 // Set up fusion test by adding to the Operator Group
152 // Note this has to be performed after all the components have been successfully added to the component graph
153
154 // Pack tensor infos
155 ArgumentPack<ITensorInfo> tensors;
156 tensors.add_const_tensor(ACL_SRC_0, src);
157 tensors.add_tensor(ACL_DST_0, dst);
158
159 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
160 sketch.implementation().operator_group().add_operator(op);
Gunes Bayircc287732023-01-19 15:56:00 +0000161
162 return dst;
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000163}
164} // namespace dynamic_fusion
165} // namespace experimental
Gunes Bayircc287732023-01-19 15:56:00 +0000166} // namespace arm_compute