blob: aa45f4c1a58bb5730ccb214960dbc28f85589144 [file] [log] [blame]
Jakub Sujak8ae57142022-12-02 16:09:06 +00001/*
2 * Copyright (c) 2022 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
25#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuResize.h"
26
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
29#include "src/core/helpers/AutoConfiguration.h"
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/ClComponentResize.h"
33
34#include "src/common/utils/Log.h"
35
36namespace arm_compute
37{
38namespace experimental
39{
40namespace dynamic_fusion
41{
42namespace
43{
44void calculate_and_init_dst_if_empty(ITensorInfo *dst, const ITensorInfo *src, const ResizeAttributes &attributes)
45{
46 if(dst->total_size() == 0U)
47 {
48 TensorShape out_shape = src->tensor_shape();
49
50 out_shape.set(1, attributes.output_width());
51 out_shape.set(2, attributes.output_height());
52
53 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(out_shape));
54 }
55}
56
57constexpr GpuOperatorType operator_type = GpuOperatorType::Complex;
58}
59Status GpuResize::is_supported_op(const GpuWorkloadContext &context,
60 const ITensorInfo *src,
61 const ITensorInfo *dst,
62 const Attributes &attributes)
63{
64 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
65
66 // Auto initialize dst tensor info
67 TensorInfo dst_info_to_validate = *dst;
68 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, attributes);
69
70 // Check support level
71 // Data type
72 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
73 // Data layout
74 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
75 // Interpolation policy
76 ARM_COMPUTE_RETURN_ERROR_ON_MSG(attributes.interpolation_policy() != InterpolationPolicy::NEAREST_NEIGHBOR && attributes.interpolation_policy() != InterpolationPolicy::BILINEAR,
77 "Interpolation policy must be NEAREST_NEIGHBOR or BILINEAR");
78
79 // Check components
80 if(context.gpu_language() == GpuLanguage::OpenCL)
81 {
82 const auto cl_compile_ctx = context.cl_compile_context();
83 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
84
85 // Validate Activation Component
86 {
87 const KernelProperties properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
88
89 ArgumentPack<ITensorInfo> arguments;
90 arguments.add_const_tensor(ACL_SRC_0, src);
91 arguments.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
92 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentResize::validate(properties, arguments, attributes));
93 }
94 }
95 else
96 {
97 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
98 }
99
100 return Status{};
101}
102
103Status GpuResize::validate_op(const GpuWorkloadSketch &sketch,
104 const ITensorInfo *src,
105 const ITensorInfo *dst,
106 const GpuResize::Attributes &attributes)
107{
108 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
109 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id() || !dst->has_valid_id());
110
111 // Auto initialize dst tensor info if empty
112 TensorInfo dst_info_to_validate = *dst;
113 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, attributes);
114
115 // Perform fusion test
116 // Pack tensor infos
117 ArgumentPack<ITensorInfo> tensors;
118 tensors.add_const_tensor(ACL_SRC_0, src);
119 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
120 const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
121
122 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
123 "Operator fusion test failed. This operator cannot be fused into the workload");
124
125 // Check if configuration is supported
126 return is_supported_op(*sketch.gpu_context(), src, &dst_info_to_validate, attributes);
127}
128
129void GpuResize::create_op(GpuWorkloadSketch &sketch,
130 ITensorInfo *src,
131 ITensorInfo *dst,
132 const GpuResize::Attributes &attributes)
133{
134 // Assert validation
135 ARM_COMPUTE_ERROR_THROW_ON(GpuResize::validate_op(sketch, src, dst, attributes));
136 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
137 ARM_COMPUTE_LOG_PARAMS(src, dst, attributes);
138
139 // Auto initialize dst tensor info if empty
140 calculate_and_init_dst_if_empty(dst, src, attributes);
141
142 // Translate into components and add to component graph
143 GpuKernelComponentGraph &comp_graph = sketch.implementation().component_graph();
144 const auto *sketch_ctx = sketch.implementation().context();
145
146 if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
147 {
148 ARM_COMPUTE_ERROR_ON_NULLPTR(sketch_ctx->cl_compile_context());
149
150 // Add Resize Component
151 {
152 const auto properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
153
154 ArgumentPack<ITensorInfo> arguments;
155 arguments.add_const_tensor(ACL_SRC_0, src);
156 arguments.add_const_tensor(ACL_DST_0, dst);
157 comp_graph.add_new_component<ClComponentResize>(properties, arguments, attributes);
158 }
159 }
160 else
161 {
162 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
163 }
164
165 // Set up fusion test by adding to the Operator Group
166 // Note this has to be performed after all the components have been successfully added to the component graph
167
168 // Pack tensor infos
169 ArgumentPack<ITensorInfo> tensors;
170 tensors.add_const_tensor(ACL_SRC_0, src);
171 tensors.add_const_tensor(ACL_DST_0, dst);
172
173 const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
174 sketch.implementation().operator_group().add_operator(op);
175}
176
177} // namespace dynamic_fusion
178} // namespace experimental
179} // namespace arm_compute