-
Notifications
You must be signed in to change notification settings - Fork 969
Expand file tree
/
Copy pathbackend_init_context_test.cpp
More file actions
241 lines (190 loc) · 7.96 KB
/
backend_init_context_test.cpp
File metadata and controls
241 lines (190 loc) · 7.96 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/runtime/backend/backend_init_context.h>
#include <executorch/runtime/backend/options.h>
#include <executorch/runtime/platform/runtime.h>
#include <gtest/gtest.h>
using namespace ::testing;
using executorch::runtime::BackendInitContext;
using executorch::runtime::BackendOption;
using executorch::runtime::BackendOptions;
using executorch::runtime::Error;
using executorch::runtime::Span;
class BackendInitContextTest : public ::testing::Test {
protected:
void SetUp() override {
executorch::runtime::runtime_init();
}
};
// Test default constructor without runtime specs
TEST_F(BackendInitContextTest, DefaultConstructorNoRuntimeSpecs) {
BackendInitContext context(nullptr);
auto specs = context.runtime_specs();
EXPECT_EQ(specs.size(), 0);
}
// Test constructor with runtime specs
TEST_F(BackendInitContextTest, ConstructorWithRuntimeSpecs) {
BackendOptions<4> opts;
opts.set_option("compute_unit", "cpu_and_gpu");
opts.set_option("num_threads", 4);
opts.set_option("enable_profiling", true);
// Create a const span from the mutable view
auto view = opts.view();
Span<const BackendOption> const_span(view.data(), view.size());
BackendInitContext context(
nullptr, // runtime_allocator
nullptr, // event_tracer
"forward", // method_name
nullptr, // named_data_map
const_span // runtime_specs
);
auto specs = context.runtime_specs();
EXPECT_EQ(specs.size(), 3);
}
// Test get_runtime_spec<bool> with valid key
TEST_F(BackendInitContextTest, GetRuntimeSpecBoolValid) {
BackendOptions<2> opts;
opts.set_option("enable_profiling", true);
opts.set_option("debug_mode", false);
auto view = opts.view();
Span<const BackendOption> const_span(view.data(), view.size());
BackendInitContext context(nullptr, nullptr, nullptr, nullptr, const_span);
auto result1 = context.get_runtime_spec<bool>("enable_profiling");
EXPECT_TRUE(result1.ok());
EXPECT_TRUE(result1.get());
auto result2 = context.get_runtime_spec<bool>("debug_mode");
EXPECT_TRUE(result2.ok());
EXPECT_FALSE(result2.get());
}
// Test get_runtime_spec<int> with valid key
TEST_F(BackendInitContextTest, GetRuntimeSpecIntValid) {
BackendOptions<2> opts;
opts.set_option("num_threads", 8);
opts.set_option("batch_size", 32);
auto view = opts.view();
Span<const BackendOption> const_span(view.data(), view.size());
BackendInitContext context(nullptr, nullptr, nullptr, nullptr, const_span);
auto result1 = context.get_runtime_spec<int>("num_threads");
EXPECT_TRUE(result1.ok());
EXPECT_EQ(result1.get(), 8);
auto result2 = context.get_runtime_spec<int>("batch_size");
EXPECT_TRUE(result2.ok());
EXPECT_EQ(result2.get(), 32);
}
// Test get_runtime_spec<int64_t> with valid key (covers pointer-sized handle
// use case, e.g., CUDA driver handles like CUgreenCtx).
TEST_F(BackendInitContextTest, GetRuntimeSpecInt64Valid) {
BackendOptions<2> opts;
// Use a value with the top bit clear so the literal is well-defined as a
// signed int64_t across toolchains.
constexpr int64_t kHandle = static_cast<int64_t>(0x123456789ABCDEF0LL);
constexpr int64_t kLargeOffset = static_cast<int64_t>(0x7FFFFFFFFFFFFFFFLL);
opts.set_option("opaque_handle", kHandle);
opts.set_option("large_offset", kLargeOffset);
auto view = opts.view();
Span<const BackendOption> const_span(view.data(), view.size());
BackendInitContext context(nullptr, nullptr, nullptr, nullptr, const_span);
auto result1 = context.get_runtime_spec<int64_t>("opaque_handle");
EXPECT_TRUE(result1.ok());
EXPECT_EQ(result1.get(), kHandle);
auto result2 = context.get_runtime_spec<int64_t>("large_offset");
EXPECT_TRUE(result2.ok());
EXPECT_EQ(result2.get(), kLargeOffset);
}
// Test get_runtime_spec<const char*> with valid key
TEST_F(BackendInitContextTest, GetRuntimeSpecStringValid) {
BackendOptions<2> opts;
opts.set_option("compute_unit", "cpu_and_gpu");
opts.set_option("cache_dir", "/tmp/cache");
auto view = opts.view();
Span<const BackendOption> const_span(view.data(), view.size());
BackendInitContext context(nullptr, nullptr, nullptr, nullptr, const_span);
auto result1 = context.get_runtime_spec<const char*>("compute_unit");
EXPECT_TRUE(result1.ok());
EXPECT_STREQ(result1.get(), "cpu_and_gpu");
auto result2 = context.get_runtime_spec<const char*>("cache_dir");
EXPECT_TRUE(result2.ok());
EXPECT_STREQ(result2.get(), "/tmp/cache");
}
// Test get_runtime_spec<T> with non-existent key returns NotFound
TEST_F(BackendInitContextTest, GetRuntimeSpecNotFound) {
BackendOptions<1> opts;
opts.set_option("key", "value");
auto view = opts.view();
Span<const BackendOption> const_span(view.data(), view.size());
BackendInitContext context(nullptr, nullptr, nullptr, nullptr, const_span);
auto bool_result = context.get_runtime_spec<bool>("nonexistent");
EXPECT_FALSE(bool_result.ok());
EXPECT_EQ(bool_result.error(), Error::NotFound);
auto int_result = context.get_runtime_spec<int>("nonexistent");
EXPECT_FALSE(int_result.ok());
EXPECT_EQ(int_result.error(), Error::NotFound);
auto string_result = context.get_runtime_spec<const char*>("nonexistent");
EXPECT_FALSE(string_result.ok());
EXPECT_EQ(string_result.error(), Error::NotFound);
}
// Test get_runtime_spec<T> with wrong type returns InvalidArgument
TEST_F(BackendInitContextTest, GetRuntimeSpecTypeMismatch) {
BackendOptions<4> opts;
opts.set_option("bool_opt", true);
opts.set_option("int_opt", 42);
opts.set_option("int64_opt", static_cast<int64_t>(0x123456789ABCDEF0LL));
opts.set_option("string_opt", "hello");
auto view = opts.view();
Span<const BackendOption> const_span(view.data(), view.size());
BackendInitContext context(nullptr, nullptr, nullptr, nullptr, const_span);
// Try to get bool as int
auto result1 = context.get_runtime_spec<int>("bool_opt");
EXPECT_FALSE(result1.ok());
EXPECT_EQ(result1.error(), Error::InvalidArgument);
// Try to get int as string
auto result2 = context.get_runtime_spec<const char*>("int_opt");
EXPECT_FALSE(result2.ok());
EXPECT_EQ(result2.error(), Error::InvalidArgument);
// Try to get string as bool
auto result3 = context.get_runtime_spec<bool>("string_opt");
EXPECT_FALSE(result3.ok());
EXPECT_EQ(result3.error(), Error::InvalidArgument);
// int and int64_t are distinct variant arms; reading an int as int64_t
// (and vice versa) must fail with InvalidArgument rather than implicit
// narrowing/widening.
auto result4 = context.get_runtime_spec<int64_t>("int_opt");
EXPECT_FALSE(result4.ok());
EXPECT_EQ(result4.error(), Error::InvalidArgument);
auto result5 = context.get_runtime_spec<int>("int64_opt");
EXPECT_FALSE(result5.ok());
EXPECT_EQ(result5.error(), Error::InvalidArgument);
}
// Test empty runtime specs
TEST_F(BackendInitContextTest, EmptyRuntimeSpecs) {
Span<const BackendOption> empty_span;
BackendInitContext context(nullptr, nullptr, nullptr, nullptr, empty_span);
EXPECT_EQ(context.runtime_specs().size(), 0);
// All lookups should return NotFound
auto bool_result = context.get_runtime_spec<bool>("any_key");
EXPECT_FALSE(bool_result.ok());
EXPECT_EQ(bool_result.error(), Error::NotFound);
}
// Test that other context fields still work
TEST_F(BackendInitContextTest, OtherFieldsStillWork) {
BackendOptions<1> opts;
opts.set_option("key", "value");
auto view = opts.view();
Span<const BackendOption> const_span(view.data(), view.size());
BackendInitContext context(
nullptr, // runtime_allocator
nullptr, // event_tracer
"forward", // method_name
nullptr, // named_data_map
const_span // runtime_specs
);
EXPECT_EQ(context.get_runtime_allocator(), nullptr);
EXPECT_EQ(context.event_tracer(), nullptr);
EXPECT_STREQ(context.get_method_name(), "forward");
EXPECT_EQ(context.get_named_data_map(), nullptr);
}