blob: f0d368d75718b3973f4dc62ee8304bd29016fb97 [file] [log] [blame]
Viet-Hoa Dob84e2532022-12-13 13:09:10 +00001/*
Gunes Bayir3a1e1252023-01-03 21:26:09 +00002 * Copyright (c) 2022-2023 Arm Limited.
Viet-Hoa Dob84e2532022-12-13 13:09:10 +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
25#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h"
26
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000027#include "src/common/utils/Log.h"
Gunes Bayir3a1e1252023-01-03 21:26:09 +000028#include "src/core/helpers/AutoConfiguration.h"
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000029#include "src/dynamic_fusion/sketch/ArgumentPack.h"
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000030#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentStore.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
Gunes Bayir3a1e1252023-01-03 21:26:09 +000032#include "src/dynamic_fusion/utils/Utils.h"
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000033
34namespace arm_compute
35{
36namespace experimental
37{
38namespace dynamic_fusion
39{
40namespace
41{
42constexpr GpuOperatorType operator_type = GpuOperatorType::Simple;
Gunes Bayircc287732023-01-19 15:56:00 +000043} // namespace
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000044
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045Status GpuOutput::is_supported_op(const GpuWorkloadContext &context, const ITensorInfo *src, const ITensorInfo *dst)
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000046{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
48
49 // Initialize the destination tensor info.
50 TensorInfo dst_to_validate = *dst;
51 auto_init_if_empty(dst_to_validate, *src);
52
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, &dst_to_validate);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, &dst_to_validate);
55
56 ARM_COMPUTE_UNUSED(context);
57 return Status{};
58}
59
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060Status GpuOutput::validate_op(const GpuWorkloadSketch &sketch, const ITensorInfo *src, const ITensorInfo *dst)
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000061{
62 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
63 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
SiCong Li5a2bc012023-01-12 12:54:49 +000064 ARM_COMPUTE_RETURN_ERROR_ON(!is_alloc_tensor(dst));
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000065
66 // Initialize the destination tensor info.
67 TensorInfo dst_to_validate = *dst;
68 auto_init_if_empty(dst_to_validate, *src);
69
70 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, &dst_to_validate);
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, &dst_to_validate);
72
73 // Perform fusion test.
74 ArgumentPack<ITensorInfo> tensors;
75 tensors.add_const_tensor(ACL_SRC_0, src);
76 tensors.add_const_tensor(ACL_DST_0, &dst_to_validate);
77
Gunes Bayir3a1e1252023-01-03 21:26:09 +000078 const auto group = sketch.implementation().operator_group();
79 const auto op = group.new_operator(operator_type, tensors);
Viet-Hoa Do04f46202022-12-14 14:49:56 +000080 const auto success = group.try_add_operator(op, true);
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000081
82 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!success, "This operator cannot be fused into the workload.");
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000083
84 const auto status = is_supported_op(*sketch.gpu_context(), src, dst);
85 return status;
86}
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088void GpuOutput::create_op(GpuWorkloadSketch &sketch, ITensorInfo *src, ITensorInfo *dst)
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000089{
90 ARM_COMPUTE_LOG_PARAMS(src, dst);
91 ARM_COMPUTE_ERROR_THROW_ON(GpuOutput::validate_op(sketch, src, dst));
92
93 // Auto initialize dst tensor info if empty
94 auto_init_if_empty(*dst, *src);
95
96 // Translate into components and add to component graph
Gunes Bayir3a1e1252023-01-03 21:26:09 +000097 auto &comp_graph = sketch.implementation().component_graph();
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000098 const auto sketch_ctx = sketch.implementation().context();
99
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
Viet-Hoa Dob84e2532022-12-13 13:09:10 +0000101 {
102 ARM_COMPUTE_ERROR_ON(sketch_ctx->cl_compile_context() == nullptr);
103
104 // Add store component
105 {
106 IGpuKernelComponent::Properties properties;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
Viet-Hoa Dob84e2532022-12-13 13:09:10 +0000108
109 ArgumentPack<ITensorInfo> arguments;
110 arguments.add_const_tensor(ACL_SRC_0, src);
111 arguments.add_const_tensor(ACL_DST_0, dst);
112 comp_graph.add_new_component<ClComponentStore>(properties, arguments);
113 }
114 }
115 else
116 {
117 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
118 }
119
120 // Set up fusion test by adding to the Operator Group
121 // Note this has to be performed after all the components have been successfully added to the component graph
122
123 // Pack tensor infos
124 ArgumentPack<ITensorInfo> tensors;
125 tensors.add_const_tensor(ACL_SRC_0, src);
126 tensors.add_const_tensor(ACL_DST_0, dst);
127
128 const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
Viet-Hoa Do04f46202022-12-14 14:49:56 +0000129 sketch.implementation().operator_group().add_operator(op, true);
Viet-Hoa Dob84e2532022-12-13 13:09:10 +0000130}
131
132} // namespace dynamic_fusion
133} // namespace experimental
134} // namespace arm_compute