aboutsummaryrefslogtreecommitdiff
path: root/tests/validation/CL/UNIT/dynamic_fusion/ArbitraryElementwiseFusion.cpp
blob: 1b1e8aa761e57faf3529e94645bc6fed9e391c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * Copyright (c) 2022 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifdef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION

#include "src/core/experimental/dynamic_fusion/ClKernelBuildingAPI.h"
#include "src/core/utils/helpers/float_ops.h"
#include "tests/CL/CLAccessor.h"
#include "tests/framework/Macros.h"
#include "tests/validation/Validation.h"
#include "tests/validation/reference/ConvolutionLayer.h"
#include "tests/validation/reference/ElementwiseOperations.h"
#include "tests/validation/reference/Permute.h"

#include "arm_compute/runtime/experimental/ClCompositeOperator.h"
#include "tests/validation/reference/Floor.h"

#include "arm_compute/core/ITensor.h"
#include "arm_compute/runtime/CL/CLTensor.h"
#include "tests/validation/CL/UNIT/dynamic_fusion/Utils.h"

using namespace arm_compute::experimental::dynamic_fusion;
using namespace arm_compute::test::validation::utils;

namespace arm_compute
{
namespace test
{
namespace validation
{
TEST_SUITE(CL)
TEST_SUITE(UNIT)
TEST_SUITE(DYNAMIC_FUSION)
TEST_SUITE(ArbitraryFusion)

TEST_CASE(ElementwiseBroadcasting, framework::DatasetMode::ALL)
{
    // Test elementwise broadcasting
    const auto data_type   = DataType::F32;
    const auto data_layout = DataLayout::NHWC;

    const auto input_shape = TensorShape(7, 9, 5);
    const auto rhs_shape   = TensorShape(7, 1, 1);
    const auto dst_shape   = TensorShape(7, 9, 5);

    // Tensor Info
    auto input_info  = TensorInfo(input_shape, 1, data_type, data_layout);
    auto addend_info = TensorInfo(rhs_shape, 1, data_type, data_layout);
    auto dst_info    = TensorInfo();

    ElementwiseDescriptor add_desc{ ArithmeticOperation::ADD };

    CLScheduler::get().default_reinit();
    const auto    cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
    OperatorGraph op_graph;

    const auto op_input  = add_tensor(op_graph, input_info);
    const auto op_addend = add_tensor(op_graph, addend_info);
    const auto op_dst    = add_tensor(op_graph, dst_info);

    add_op_elementwise_op(op_graph, add_desc, op_input, op_addend, op_dst);

    const ClWorkloadContext workload_ctx{ GpuInfo{ CLScheduler::get().target() } };
    ClWorkload              workload;
    build(workload, op_graph, workload_ctx);

    ClCompositeOperator op;
    op.configure(cl_compile_ctx, workload);

    // Construct tensors
    CLTensor t_input{};
    CLTensor t_addend{};
    CLTensor t_dst{};

    // Init tensors
    t_input.allocator()->init(input_info);
    t_addend.allocator()->init(addend_info);
    t_dst.allocator()->init(dst_info);

    // Allocate and fill tensors
    t_input.allocator()->allocate();
    t_addend.allocator()->allocate();
    t_dst.allocator()->allocate();

    // Fill
    fill<float>(CLAccessor(t_input), 0, library.get());
    fill<float>(CLAccessor(t_addend), 1, library.get());

    // Pack tensors
    OpTensorBinding bp_tensors({ { op_input, &t_input },
        { op_addend, &t_addend },
        { op_dst, &t_dst }
    });

    // Populate prepare and run pack-maps (including allocating aux tensors)
    ClAuxTensorData aux_tensor_data{};
    TensorPackMap   prepare_pack_map{};
    TensorPackMap   run_pack_map{};
    bind_tensors(aux_tensor_data, prepare_pack_map, run_pack_map, workload, bp_tensors);

    op.prepare(prepare_pack_map);
    op.run(run_pack_map);

    // Create reference
    SimpleTensor<float> ref_input{ input_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
    SimpleTensor<float> ref_addend{ rhs_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };

    // Fill reference
    fill<float>(ref_input, 0, library.get());
    fill<float>(ref_addend, 1, library.get());

    auto ref_input_nchw  = reference::permute(ref_input, PermutationVector(1U, 2U, 0U));
    auto ref_addend_nchw = reference::permute(ref_addend, PermutationVector(1U, 2U, 0U));

    auto dst_shape_nchw = dst_shape;
    permute(dst_shape_nchw, PermutationVector(1U, 2U, 0U));

    auto ref_t_dst_nchw = reference::arithmetic_operation(
                              ArithmeticOperation::ADD,
                              ref_input_nchw,
                              ref_addend_nchw,
                              data_type,
                              ConvertPolicy{});

    const auto ref_t_dst = reference::permute(ref_t_dst_nchw, PermutationVector(2U, 0U, 1U));

    RelativeTolerance<float> tolerance_f32(0.001f);
    validate(CLAccessor(t_dst), ref_t_dst_nchw, tolerance_f32);
}
TEST_CASE(DivFloor, framework::DatasetMode::ALL)
{
    // x = floor(div(input, input2))
    const auto data_type    = DataType::F32;
    const auto eltwise_info = ElementwiseDescriptor{ ArithmeticOperation::DIV };

    // Tensor Values
    const auto width  = 7U;
    const auto height = 6U;

    // Shapes
    const auto input1_shape = TensorShape(width, height);
    const auto input2_shape = TensorShape(width, height);
    const auto dst_shape    = TensorShape(width, height);

    // Create reference
    SimpleTensor<float> ref_src_nhwc{ input1_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
    SimpleTensor<float> ref_src2_nhwc{ input2_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };

    // Fill reference
    fill<float>(ref_src_nhwc, 0, library.get());
    fill<float>(ref_src2_nhwc, 1, library.get());

    auto ref_src  = reference::permute(ref_src_nhwc, PermutationVector(1U, 2U, 0U));
    auto ref_src2 = reference::permute(ref_src2_nhwc, PermutationVector(1U, 2U, 0U));

    TensorShape dst_shape_nchw{ dst_shape };
    permute(dst_shape_nchw, PermutationVector(1U, 2U, 0U));

    const auto ref_dst_nchw = reference::floor_layer(reference::arithmetic_operation(
                                                         ArithmeticOperation::DIV,
                                                         ref_src,
                                                         ref_src2,
                                                         data_type,
                                                         ConvertPolicy::SATURATE));

    const auto ref_t_dst = reference::permute(ref_dst_nchw, PermutationVector(2U, 0U, 1U));

    // Tensor Info
    auto input1_info = TensorInfo(input1_shape, 1, data_type, DataLayout::NHWC);
    auto input2_info = TensorInfo(input2_shape, 1, data_type, DataLayout::NHWC);
    auto dst_info    = TensorInfo();
    auto acc_info    = TensorInfo(); // Intermediate tensor for division

    // Initialise Scheduler
    CLScheduler::get().default_reinit();
    const auto    cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
    OperatorGraph op_graph;

    // add tensors
    auto op_input1 = add_tensor(op_graph, input1_info);
    auto op_input2 = add_tensor(op_graph, input2_info);
    auto op_acc    = add_tensor(op_graph, acc_info);
    auto op_dst    = add_tensor(op_graph, dst_info);

    add_op_elementwise_op(op_graph, eltwise_info, op_input1, op_input2, op_acc);
    add_op_floor(op_graph, FloorDescriptor(), op_acc, op_dst);

    const ClWorkloadContext workload_ctx{ GpuInfo{ CLScheduler::get().target() } };
    ClWorkload              workload;
    build(workload, op_graph, workload_ctx);

    ClCompositeOperator op;
    op.configure(cl_compile_ctx, workload);

    // Configure and add tensors.
    CLTensor t_input1{};
    CLTensor t_input2{};
    CLTensor t_dst{};

    // Init Tensors
    t_input1.allocator()->init(input1_info);
    t_input2.allocator()->init(input2_info);
    t_dst.allocator()->init(dst_info);

    // Allocate and fill tensors
    t_input1.allocator()->allocate();
    t_input2.allocator()->allocate();
    t_dst.allocator()->allocate();

    fill<float>(CLAccessor(t_input1), 0, library.get());
    fill<float>(CLAccessor(t_input2), 1, library.get());

    // "Pack" tensors
    OpTensorBinding bp_tensors({ { op_input1, &t_input1 },
        { op_input2, &t_input2 },
        { op_dst, &t_dst }
    });

    // Populate prepare and run pack-maps (including allocating aux tensors)
    ClAuxTensorData aux_tensor_data{};
    TensorPackMap   prepare_pack_map{};
    TensorPackMap   run_pack_map{};
    bind_tensors(aux_tensor_data, prepare_pack_map, run_pack_map, workload, bp_tensors);

    op.prepare(prepare_pack_map);
    op.run(run_pack_map);

    RelativeTolerance<float> tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for floating point data types */
    validate(CLAccessor(t_dst), ref_dst_nchw, tolerance_f32);
}
TEST_CASE(Dconv2dAddDiv, framework::DatasetMode::ALL)
{
    // output = div(divend, add(addend, conv2d1x1(direct_conv)(input, weights, bias)))
    const auto data_type   = DataType::F32;
    const auto data_layout = DataLayout::NHWC;

    const auto input_shape  = TensorShape(384, 12, 12);
    const auto weight_shape = TensorShape(384, 1, 1, 16);
    const auto dst_shape    = TensorShape(16, 12, 12);

    // Tensor Info
    auto input_info  = TensorInfo(input_shape, 1, data_type, data_layout);
    auto weight_info = TensorInfo(weight_shape, 1, data_type, data_layout);
    auto addend_info = TensorInfo(dst_shape, 1, data_type, data_layout);
    auto divend_info = TensorInfo(dst_shape, 1, data_type, data_layout);
    auto acc_info    = TensorInfo(); // Intermediate tensor for conv
    auto acc_1_info  = TensorInfo();
    auto dst_info    = TensorInfo();

    Conv2dDescriptor      conv2d_desc{};
    ElementwiseDescriptor add_desc{ ArithmeticOperation::ADD };
    ElementwiseDescriptor div_desc{ ArithmeticOperation::DIV };

    CLScheduler::get().default_reinit();
    const auto    cl_compile_ctx = CLKernelLibrary::get().get_compile_context();
    OperatorGraph op_graph;

    const auto op_input  = add_tensor(op_graph, input_info);
    const auto op_weight = add_tensor(op_graph, weight_info);
    const auto op_addend = add_tensor(op_graph, addend_info);
    const auto op_divend = add_tensor(op_graph, divend_info);
    const auto op_acc    = add_tensor(op_graph, acc_info);   // temp accumulator; TensorInfo to be inferred
    const auto op_acc_1  = add_tensor(op_graph, acc_1_info); // temp accumulator; TensorInfo to be inferred
    const auto op_dst    = add_tensor(op_graph, dst_info);

    auto conv2d = add_op_conv2d(op_graph, conv2d_desc, op_input, op_weight, op_acc);
    force_conv2d_method(op_graph, conv2d, ConvolutionMethod::DIRECT);
    add_op_elementwise_op(op_graph, add_desc, op_acc, op_addend, op_acc_1);
    add_op_elementwise_op(op_graph, div_desc, op_acc_1, op_divend, op_dst);

    const ClWorkloadContext workload_ctx{ GpuInfo{ CLScheduler::get().target() } };
    ClWorkload              workload;
    build(workload, op_graph, workload_ctx);

    ClCompositeOperator op;
    op.configure(cl_compile_ctx, workload);

    // Construct tensors
    CLTensor t_input{};
    CLTensor t_weight{};
    CLTensor t_addend{};
    CLTensor t_divend{};
    CLTensor t_dst{};

    // Init tensors
    t_input.allocator()->init(input_info);
    t_weight.allocator()->init(weight_info);
    t_divend.allocator()->init(divend_info);
    t_addend.allocator()->init(addend_info);
    t_dst.allocator()->init(dst_info);

    // Allocate and fill tensors
    t_input.allocator()->allocate();
    t_weight.allocator()->allocate();
    t_divend.allocator()->allocate();
    t_addend.allocator()->allocate();
    t_dst.allocator()->allocate();

    // Fill
    fill<float>(CLAccessor(t_input), 0, library.get());
    fill<float>(CLAccessor(t_weight), 1, library.get());
    fill<float>(CLAccessor(t_addend), 2, library.get());
    fill<float>(CLAccessor(t_divend), 3, library.get());

    // Pack tensors
    OpTensorBinding bp_tensors({ { op_input, &t_input },
        { op_weight, &t_weight },
        { op_addend, &t_addend },
        { op_divend, &t_divend },
        { op_dst, &t_dst }
    });

    // Populate prepare and run pack-maps (including allocating aux tensors)
    ClAuxTensorData aux_tensor_data{};
    TensorPackMap   prepare_pack_map{};
    TensorPackMap   run_pack_map{};
    bind_tensors(aux_tensor_data, prepare_pack_map, run_pack_map, workload, bp_tensors);

    op.prepare(prepare_pack_map);
    op.run(run_pack_map);

    // Create reference
    SimpleTensor<float> ref_input{ input_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
    SimpleTensor<float> ref_weight{ weight_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
    SimpleTensor<float> ref_bias_placeholder{ dst_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
    SimpleTensor<float> ref_addend{ dst_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
    SimpleTensor<float> ref_divend{ dst_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };

    // Fill reference
    fill<float>(ref_input, 0, library.get());
    fill<float>(ref_weight, 1, library.get());
    fill<float>(ref_addend, 2, library.get());
    fill<float>(ref_divend, 3, library.get());

    auto ref_input_nchw            = reference::permute(ref_input, PermutationVector(1U, 2U, 0U));
    auto ref_weight_nchw           = reference::permute(ref_weight, PermutationVector(1U, 2U, 0U));
    auto ref_bias_placeholder_nchw = reference::permute(ref_bias_placeholder, PermutationVector(1U, 2U, 0U));
    auto ref_addend_nchw           = reference::permute(ref_addend, PermutationVector(1U, 2U, 0U));
    auto ref_divend_nchw           = reference::permute(ref_divend, PermutationVector(1U, 2U, 0U));

    auto dst_shape_nchw = dst_shape;
    permute(dst_shape_nchw, PermutationVector(1U, 2U, 0U));

    PadStrideInfo legacy_pad_stride(conv2d_desc.stride.x(), conv2d_desc.stride.y(), conv2d_desc.pad.left, conv2d_desc.pad.right, conv2d_desc.pad.top, conv2d_desc.pad.bottom, DimensionRoundingType{});
    auto          ref_acc_nchw = reference::arithmetic_operation(
                                     ArithmeticOperation::ADD,
                                     ref_addend_nchw,
                                     reference::convolution_layer(ref_input_nchw, ref_weight_nchw, ref_bias_placeholder_nchw, dst_shape_nchw, legacy_pad_stride, conv2d_desc.dilation),
                                     data_type,
                                     ConvertPolicy{});

    auto ref_t_dst_nchw = reference::arithmetic_operation(
                              ArithmeticOperation::DIV,
                              ref_acc_nchw,
                              ref_divend_nchw,
                              data_type,
                              ConvertPolicy{});

    const auto ref_t_dst = reference::permute(ref_t_dst_nchw, PermutationVector(2U, 0U, 1U));

    RelativeTolerance<float> tolerance_f32(0.001f);
    validate(CLAccessor(t_dst), ref_t_dst_nchw, tolerance_f32);
}

TEST_SUITE_END() // ArbitraryFusion
TEST_SUITE_END() // DYNAMIC_FUSION
TEST_SUITE_END() // UNIT
TEST_SUITE_END() // CL

} // namespace validation
} // namespace test
} // namespace arm_compute

#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */