blob: 0e1f16e8ffed6d489e7e918b054b15a005186359 [file] [log] [blame]
Ramy Elgammalf800adf2022-12-14 15:39:29 +00001/*
Gunes Bayir59e40602024-02-08 10:50:53 +00002 * Copyright (c) 2023-2024 Arm Limited.
Ramy Elgammalf800adf2022-12-14 15:39:29 +00003 *
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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Ramy Elgammalf800adf2022-12-14 15:39:29 +000026#include "arm_compute/core/Error.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027
Ramy Elgammalf800adf2022-12-14 15:39:29 +000028#include "src/common/utils/Log.h"
29#include "src/core/helpers/AutoConfiguration.h"
30#include "src/dynamic_fusion/sketch/ArgumentPack.h"
Ramy Elgammalf800adf2022-12-14 15:39:29 +000031#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentReshape.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
Ramy Elgammalf800adf2022-12-14 15:39:29 +000033
34namespace arm_compute
35{
36namespace experimental
37{
38namespace dynamic_fusion
39{
40namespace
41{
Gunes Bayircc287732023-01-19 15:56:00 +000042Status is_supported_op_helper(const GpuWorkloadContext &context,
43 const ITensorInfo *src,
44 const ITensorInfo *dst,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045 const ReshapeAttributes &attributes)
Ramy Elgammalf800adf2022-12-14 15:39:29 +000046{
Gunes Bayircc287732023-01-19 15:56:00 +000047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
48
49 TensorInfo dst_info_to_validate;
50 const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate;
51
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 if (dst != nullptr)
Gunes Bayircc287732023-01-19 15:56:00 +000053 {
54 dst_info_to_validate_ptr = dst;
55 }
56
Ramy Elgammalf800adf2022-12-14 15:39:29 +000057 auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(attributes.shape()));
58
59 // Check components
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060 if (context.gpu_language() == GpuLanguage::OpenCL)
Ramy Elgammalf800adf2022-12-14 15:39:29 +000061 {
62 const auto cl_compile_ctx = context.cl_compile_context();
63 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
64
65 // Validate GpuReshape Component
66 ArgumentPack<ITensorInfo> arguments;
67 arguments.add_const_tensor(ACL_SRC_0, src);
Gunes Bayircc287732023-01-19 15:56:00 +000068 arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr);
Ramy Elgammalf800adf2022-12-14 15:39:29 +000069
70 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentReshape::validate(arguments));
71 }
72 else
73 {
74 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
75 }
76
Gunes Bayir59e40602024-02-08 10:50:53 +000077 return Status{ErrorCode::RUNTIME_ERROR, "GpuReshape is not Supported"};
Ramy Elgammalf800adf2022-12-14 15:39:29 +000078}
Gunes Bayircc287732023-01-19 15:56:00 +000079
80GpuOperatorType operator_type = GpuOperatorType::Complex;
81} // namespace
82
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083Status
84GpuReshape::is_supported_op(const GpuWorkloadContext &context, const ITensorInfo *src, const Attributes &attributes)
Gunes Bayircc287732023-01-19 15:56:00 +000085{
86 return is_supported_op_helper(context, src, nullptr, attributes);
87}
88
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089Status GpuReshape::validate_op(const GpuWorkloadSketch &sketch, const ITensorInfo *src, const Attributes &attributes)
Ramy Elgammalf800adf2022-12-14 15:39:29 +000090{
Gunes Bayircc287732023-01-19 15:56:00 +000091 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
92 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
93
94 // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object
95 TensorInfo dst_info_to_validate;
Ramy Elgammalf800adf2022-12-14 15:39:29 +000096
97 // Auto initialize dst tensor info
Ramy Elgammalf800adf2022-12-14 15:39:29 +000098 auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(attributes.shape()));
99
100 // Perform fusion test
101 // Pack tensor infos
102 ArgumentPack<ITensorInfo> tensors;
103 tensors.add_const_tensor(ACL_SRC_0, src);
104 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
105 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
106 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
107 "Operator fusion test failed. This operator cannot be fused into the workload");
108
109 // Check if configuration is supported
Gunes Bayircc287732023-01-19 15:56:00 +0000110 return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate, attributes);
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000111}
112
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100113ITensorInfo *GpuReshape::create_op(GpuWorkloadSketch &sketch, ITensorInfo *src, const Attributes &attributes)
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000114{
Gunes Bayircc287732023-01-19 15:56:00 +0000115 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
116 ARM_COMPUTE_LOG_PARAMS(src, attributes.shape());
117 ARM_COMPUTE_ERROR_THROW_ON(GpuReshape::validate_op(sketch, src, attributes));
118
119 ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
120 ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
121
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000122 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(attributes.shape()));
123
124 // Translate into components and add to component graph
125 auto &comp_graph = sketch.implementation().component_graph();
126 const auto sketch_ctx = sketch.implementation().context();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100127 if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000128 {
129 const auto cl_compile_ctx = sketch_ctx->cl_compile_context();
Omar Al Khatib3c7c1fa2023-03-07 09:57:49 +0000130 ARM_COMPUTE_UNUSED(cl_compile_ctx);
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000131 ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr);
132
133 // Add ElementwiseBinary Component
134 {
135 auto properties = IGpuKernelComponent::Properties();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000137
138 ArgumentPack<ITensorInfo> arguments;
139 arguments.add_const_tensor(ACL_SRC_0, src);
140 arguments.add_const_tensor(ACL_DST_0, dst);
141 comp_graph.add_new_component<ClComponentReshape>(properties, arguments);
142 }
143 }
144 else
145 {
146 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
147 }
148 // Set up fusion test by adding to the Operator Group
149 // Note this has to be performed after all the components have been successfully added to the component graph
150
151 // Pack tensor infos
152 ArgumentPack<ITensorInfo> tensors;
153 tensors.add_const_tensor(ACL_SRC_0, src);
154 tensors.add_tensor(ACL_DST_0, dst);
155
156 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
157 sketch.implementation().operator_group().add_operator(op);
Gunes Bayircc287732023-01-19 15:56:00 +0000158
159 return dst;
Ramy Elgammalf800adf2022-12-14 15:39:29 +0000160}
161} // namespace dynamic_fusion
162} // namespace experimental
Gunes Bayircc287732023-01-19 15:56:00 +0000163} // namespace arm_compute