blob: 107a5e5fa7a4a618cd152868f4feef46869925c5 [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
30#include "src/dynamic_fusion/sketch/ArgumentPack.h"
31#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
32#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentStore.h"
Gunes Bayir3a1e1252023-01-03 21:26:09 +000033#include "src/dynamic_fusion/utils/Utils.h"
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000034
35namespace arm_compute
36{
37namespace experimental
38{
39namespace dynamic_fusion
40{
41namespace
42{
43constexpr GpuOperatorType operator_type = GpuOperatorType::Simple;
Gunes Bayircc287732023-01-19 15:56:00 +000044} // namespace
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000045
46Status GpuOutput::is_supported_op(const GpuWorkloadContext &context,
47 const ITensorInfo *src,
48 const ITensorInfo *dst)
49{
50 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
51
52 // Initialize the destination tensor info.
53 TensorInfo dst_to_validate = *dst;
54 auto_init_if_empty(dst_to_validate, *src);
55
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, &dst_to_validate);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, &dst_to_validate);
58
59 ARM_COMPUTE_UNUSED(context);
60 return Status{};
61}
62
63Status GpuOutput::validate_op(const GpuWorkloadSketch &sketch,
64 const ITensorInfo *src,
65 const ITensorInfo *dst)
66{
67 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
68 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
SiCong Li5a2bc012023-01-12 12:54:49 +000069 ARM_COMPUTE_RETURN_ERROR_ON(!is_alloc_tensor(dst));
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000070
71 // Initialize the destination tensor info.
72 TensorInfo dst_to_validate = *dst;
73 auto_init_if_empty(dst_to_validate, *src);
74
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, &dst_to_validate);
76 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, &dst_to_validate);
77
78 // Perform fusion test.
79 ArgumentPack<ITensorInfo> tensors;
80 tensors.add_const_tensor(ACL_SRC_0, src);
81 tensors.add_const_tensor(ACL_DST_0, &dst_to_validate);
82
Gunes Bayir3a1e1252023-01-03 21:26:09 +000083 const auto group = sketch.implementation().operator_group();
84 const auto op = group.new_operator(operator_type, tensors);
Viet-Hoa Do04f46202022-12-14 14:49:56 +000085 const auto success = group.try_add_operator(op, true);
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000086
87 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!success, "This operator cannot be fused into the workload.");
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000088
89 const auto status = is_supported_op(*sketch.gpu_context(), src, dst);
90 return status;
91}
92
93void GpuOutput::create_op(GpuWorkloadSketch &sketch,
94 ITensorInfo *src,
95 ITensorInfo *dst)
96{
97 ARM_COMPUTE_LOG_PARAMS(src, dst);
98 ARM_COMPUTE_ERROR_THROW_ON(GpuOutput::validate_op(sketch, src, dst));
99
100 // Auto initialize dst tensor info if empty
101 auto_init_if_empty(*dst, *src);
102
103 // Translate into components and add to component graph
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000104 auto &comp_graph = sketch.implementation().component_graph();
Viet-Hoa Dob84e2532022-12-13 13:09:10 +0000105 const auto sketch_ctx = sketch.implementation().context();
106
107 if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
108 {
109 ARM_COMPUTE_ERROR_ON(sketch_ctx->cl_compile_context() == nullptr);
110
111 // Add store component
112 {
113 IGpuKernelComponent::Properties properties;
114 properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
115
116 ArgumentPack<ITensorInfo> arguments;
117 arguments.add_const_tensor(ACL_SRC_0, src);
118 arguments.add_const_tensor(ACL_DST_0, dst);
119 comp_graph.add_new_component<ClComponentStore>(properties, arguments);
120 }
121 }
122 else
123 {
124 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
125 }
126
127 // Set up fusion test by adding to the Operator Group
128 // Note this has to be performed after all the components have been successfully added to the component graph
129
130 // Pack tensor infos
131 ArgumentPack<ITensorInfo> tensors;
132 tensors.add_const_tensor(ACL_SRC_0, src);
133 tensors.add_const_tensor(ACL_DST_0, dst);
134
135 const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
Viet-Hoa Do04f46202022-12-14 14:49:56 +0000136 sketch.implementation().operator_group().add_operator(op, true);
Viet-Hoa Dob84e2532022-12-13 13:09:10 +0000137}
138
139} // namespace dynamic_fusion
140} // namespace experimental
141} // namespace arm_compute