blob: 38177114e68cb1fadd46841083f3da5e186458bd [file] [log] [blame]
Ramy Elgammal002e6532023-01-11 18:48:04 +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#ifndef TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_SOFTMAXFIXTURE
25#define TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_SOFTMAXFIXTURE
26
27#include "arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h"
28#include "arm_compute/dynamic_fusion/sketch/attributes/SoftmaxAttributes.h"
29#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
30
31#include "tests/SimpleTensor.h"
32#include "tests/framework/Fixture.h"
33#include "tests/framework/Macros.h"
34#include "tests/validation/Validation.h"
35#include "tests/validation/reference/SoftmaxLayer.h"
36
37using namespace arm_compute::experimental::dynamic_fusion;
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46class DynamicFusionSoftmaxValidationGenericFixture : public framework::Fixture
47{
48public:
49 template <typename...>
50 void setup(TensorShape shape, DataType data_type, float beta, size_t axis, bool is_log)
51 {
52 _reference = compute_reference(shape, data_type, beta, axis, is_log);
53 _target = compute_target(shape, data_type, beta, axis, is_log);
54 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor)
59 {
60 if(tensor.data_type() == DataType::F32)
61 {
62 std::uniform_real_distribution<float> distribution(-10.0f, 10.0f);
63 library->fill(tensor, distribution, 0);
64 }
65 else if(tensor.data_type() == DataType::F16)
66 {
67 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -10.0f, 10.0f };
68 library->fill(tensor, distribution, 0);
69 }
70 else if(!is_data_type_quantized(tensor.data_type()))
71 {
72 std::uniform_int_distribution<> distribution(0, 100);
73 library->fill(tensor, distribution, 0);
74 }
75 else
76 {
77 library->fill_tensor_uniform(tensor, 0);
78 }
79 }
80
81 TensorType compute_target(const TensorShape &shape, DataType data_type, float beta, int32_t axis, bool is_log)
82 {
83 // Create a new workload sketch
84 CLCompileContext cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
85 GpuWorkloadContext gpu_ctx = GpuWorkloadContext{ &cl_compile_ctx };
86 GpuWorkloadSketch sketch{ &gpu_ctx };
87
88 SoftmaxAttributes softmax_attr{};
89 softmax_attr.axis(axis).beta(beta).is_log_softmax(is_log);
90 TensorInfo src_info = sketch.create_tensor_info(shape, 1, data_type);
91 TensorInfo dst_info = sketch.create_tensor_info(shape, 1, data_type);
92 FunctionType::create_op(sketch, &src_info, &dst_info, softmax_attr);
93
94 // Configure runtime
95 ClWorkloadRuntime runtime;
96 runtime.configure(sketch);
97
98 // (Important) Allocate auxiliary tensor memory if there are any
99 // Instead of using ACL allocated memory, the user can choose to import memory into the tensors
100 for(auto &data : runtime.get_auxiliary_tensors())
101 {
102 CLTensor *tensor = std::get<0>(data);
103 TensorInfo info = std::get<1>(data);
104 AuxMemoryInfo aux_mem_req = std::get<2>(data);
105 tensor->allocator()->init(info, aux_mem_req.alignment);
106 tensor->allocator()->allocate(); // Use ACL allocated memory
107 }
108 // Construct user tensors
109 TensorType src{};
110 TensorType dst{};
111
112 // Initialize user tensors
113 src.allocator()->init(src_info);
114 dst.allocator()->init(dst_info);
115
116 // Allocate and fill user tensors
117 src.allocator()->allocate();
118 dst.allocator()->allocate();
119 fill(AccessorType(src));
120
121 // Run runtime
122 runtime.run({ &src, &dst });
123
124 return dst;
125 }
126
127 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type, float beta, int32_t axis, bool is_log)
128 {
129 // Create reference
130 SimpleTensor<T> src{ shape, data_type, 1 };
131
132 // Fill reference
133 fill(src);
134
135 return reference::softmax_layer<T>(src, beta, axis, is_log);
136 }
137
138 TensorType _target{};
139 SimpleTensor<T> _reference{};
140};
141
142template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
143class DynamicFusionSoftmaxValidationFixture : public DynamicFusionSoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
144{
145public:
146 template <typename...>
147 void setup(TensorShape shape, DataType data_type, float beta, size_t axis, bool is_log)
148 {
149 DynamicFusionSoftmaxValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
150 data_type,
151 beta,
152 axis,
153 is_log);
154 }
155};
156
157} // namespace validation
158} // namespace test
159} // namespace arm_compute
160
161#endif /* TESTS_VALIDATION_FIXTURES_DYNAMIC_FUSION_OPERATORS_SOFTMAXFIXTURE */