aboutsummaryrefslogtreecommitdiff
path: root/tests/validation/NEON/UNIT/RuntimeContext.cpp
blob: f64d380423c23d38d942025696d30604777a014c (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
/*
 * Copyright (c) 2019-2021 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.
 */
#include "arm_compute/runtime/RuntimeContext.h"

#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
#include "arm_compute/runtime/SchedulerFactory.h"
#include "arm_compute/runtime/Tensor.h"
#include "tests/Globals.h"
#include "tests/NEON/Accessor.h"
#include "tests/Utils.h"
#include "tests/framework/Asserts.h"
#include "tests/framework/Macros.h"
#include "tests/validation/Validation.h"
#include "tests/validation/reference/ActivationLayer.h"

#include <memory>
#include <random>
#if !defined(BARE_METAL)
#include <thread>
#endif // !defined(BARE_METAL)

namespace arm_compute
{
namespace test
{
namespace validation
{
TEST_SUITE(NEON)
TEST_SUITE(UNIT)
TEST_SUITE(RuntimeContext)

TEST_CASE(Scheduler, framework::DatasetMode::ALL)
{
    using namespace arm_compute;
    // Create a runtime context object
    RuntimeContext ctx;

    // Check if it's been initialised properly
    ARM_COMPUTE_EXPECT(ctx.scheduler() != nullptr, framework::LogLevel::ERRORS);
    ARM_COMPUTE_EXPECT(ctx.asset_manager() == nullptr, framework::LogLevel::ERRORS);

    // Create a Scheduler
    auto scheduler = SchedulerFactory::create();
    ctx.set_scheduler(scheduler.get());
    // Check if the scheduler has been properly setup
    ARM_COMPUTE_EXPECT(ctx.scheduler() != nullptr, framework::LogLevel::ERRORS);

    // Create a new activation function
    NEActivationLayer act_layer(&ctx);

    Tensor src = create_tensor<Tensor>(TensorShape(32, 32), DataType::F32, 1);
    Tensor dst = create_tensor<Tensor>(TensorShape(32, 32), DataType::F32, 1);

    act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR));

    ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
    ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);

    // Allocate tensors
    src.allocator()->allocate();
    dst.allocator()->allocate();

    ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);

    float min_bound = 0;
    float max_bound = 0;
    std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(ActivationLayerInfo::ActivationFunction::LINEAR, DataType::F32);
    std::uniform_real_distribution<> distribution(min_bound, max_bound);
    library->fill(Accessor(src), distribution, 0);

    // Compute function
    act_layer.run();
}

#if !defined(BARE_METAL)
// This test tries scheduling work concurrently from two independent threads
TEST_CASE(MultipleThreadedScheduller, framework::DatasetMode::ALL)
{
    // Create a runtime context object for thread 1
    RuntimeContext ctx1;

    // Create a runtime context object for thread 2
    RuntimeContext ctx2;

    // Create a new activation function
    NEActivationLayer act_layer_thread0(&ctx1);
    NEActivationLayer act_layer_thread1(&ctx2);

    const TensorShape   tensor_shape(128, 128);
    Tensor              src_t0 = create_tensor<Tensor>(tensor_shape, DataType::F32, 1);
    Tensor              dst_t0 = create_tensor<Tensor>(tensor_shape, DataType::F32, 1);
    Tensor              src_t1 = create_tensor<Tensor>(tensor_shape, DataType::F32, 1);
    Tensor              dst_t1 = create_tensor<Tensor>(tensor_shape, DataType::F32, 1);
    ActivationLayerInfo activation_info(ActivationLayerInfo::ActivationFunction::LINEAR);

    act_layer_thread0.configure(&src_t0, &dst_t0, activation_info);
    act_layer_thread1.configure(&src_t1, &dst_t1, activation_info);

    ARM_COMPUTE_EXPECT(src_t0.info()->is_resizable(), framework::LogLevel::ERRORS);
    ARM_COMPUTE_EXPECT(dst_t0.info()->is_resizable(), framework::LogLevel::ERRORS);
    ARM_COMPUTE_EXPECT(src_t1.info()->is_resizable(), framework::LogLevel::ERRORS);
    ARM_COMPUTE_EXPECT(dst_t1.info()->is_resizable(), framework::LogLevel::ERRORS);

    // Allocate tensors
    src_t0.allocator()->allocate();
    dst_t0.allocator()->allocate();
    src_t1.allocator()->allocate();
    dst_t1.allocator()->allocate();

    ARM_COMPUTE_EXPECT(!src_t0.info()->is_resizable(), framework::LogLevel::ERRORS);
    ARM_COMPUTE_EXPECT(!src_t1.info()->is_resizable(), framework::LogLevel::ERRORS);

    float min_bound = 0;
    float max_bound = 0;
    std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(ActivationLayerInfo::ActivationFunction::LINEAR, DataType::F32);
    std::uniform_real_distribution<> distribution(min_bound, max_bound);
    library->fill(Accessor(src_t0), distribution, 0);
    library->fill(Accessor(src_t1), distribution, 0);

    std::thread neon_thread1([&] { act_layer_thread0.run(); });
    std::thread neon_thread2([&] { act_layer_thread1.run(); });

    neon_thread1.join();
    neon_thread2.join();

    Window window;
    window.use_tensor_dimensions(dst_t0.info()->tensor_shape());
    Iterator t0_it(&dst_t0, window);
    Iterator t1_it(&dst_t1, window);
    execute_window_loop(window, [&](const Coordinates &)
    {
        const bool match = (*reinterpret_cast<float *>(t0_it.ptr()) == *reinterpret_cast<float *>(t1_it.ptr()));
        ARM_COMPUTE_EXPECT(match, framework::LogLevel::ERRORS);
    },
    t0_it, t1_it);
}
#endif // !defined(BARE_METAL)

TEST_SUITE_END() // RuntimeContext
TEST_SUITE_END() // UNIT
TEST_SUITE_END() // Neon
} // namespace validation
} // namespace test
} // namespace arm_compute