-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathlitert_environment.h
More file actions
305 lines (271 loc) · 11.1 KB
/
litert_environment.h
File metadata and controls
305 lines (271 loc) · 11.1 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
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
// Copyright 2024 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ODML_LITERT_LITERT_CC_LITERT_ENVIRONMENT_H_
#define ODML_LITERT_LITERT_CC_LITERT_ENVIRONMENT_H_
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
#include "absl/types/span.h" // from @com_google_absl
#include "litert/c/litert_any.h"
#include "litert/c/litert_common.h"
#include "litert/c/litert_environment_options.h"
#include "litert/cc/internal/litert_handle.h"
#include "litert/cc/internal/litert_runtime_proxy.h"
#include "litert/cc/litert_any.h"
#include "litert/cc/litert_environment_options.h"
#include "litert/cc/litert_expected.h"
#include "litert/cc/litert_macros.h"
struct LiteRtRuntimeCApiStruct;
namespace litert {
using internal::RuntimeProxy;
namespace internal {
/// @internal
/// @brief A holder for an environment and its associated runtime.
/// TODO(b/478306820): Mark fields as const after the bug is fixed.
struct EnvironmentHolder {
/// @brief The runtime that the environment is associated with.
RuntimeProxy* runtime;
/// @brief The environment handle.
LiteRtEnvironment handle;
bool operator==(const EnvironmentHolder& other) const noexcept {
return runtime == other.runtime && handle == other.handle;
}
bool operator!=(const EnvironmentHolder& other) const noexcept {
return !(*this == other);
}
};
} // namespace internal
/// @brief The environment works like a context that holds the runtime states.
///
/// To create a `CompiledModel` or `TensorBuffer`, an Environment is required.
/// In a case of having multiple CompiledModels, it is recommended to share
/// the same Environment.
class Environment {
public:
enum class [[deprecated("Use EnvironmentOptions::Tag instead.")]] OptionTag {
CompilerPluginLibraryDir = kLiteRtEnvOptionTagCompilerPluginLibraryDir,
DispatchLibraryDir = kLiteRtEnvOptionTagDispatchLibraryDir,
ClDeviceId = kLiteRtEnvOptionTagOpenClDeviceId,
ClPlatformId = kLiteRtEnvOptionTagOpenClPlatformId,
ClContext = kLiteRtEnvOptionTagOpenClContext,
ClCommandQueue = kLiteRtEnvOptionTagOpenClCommandQueue,
EglContext = kLiteRtEnvOptionTagEglContext,
EglDisplay = kLiteRtEnvOptionTagEglDisplay,
WebGpuDevice = kLiteRtEnvOptionTagWebGpuDevice,
WebGpuQueue = kLiteRtEnvOptionTagWebGpuQueue,
MetalDevice = kLiteRtEnvOptionTagMetalDevice,
MetalCommandQueue = kLiteRtEnvOptionTagMetalCommandQueue,
/// @warning Vulkan support is experimental.
VulkanEnvironment = kLiteRtEnvOptionTagVulkanEnvironment,
CallbackOnGpuEnvDestroy = kLiteRtEnvOptionTagCallbackOnGpuEnvDestroy,
CallbackUserDataOnGpuEnvDestry =
kLiteRtEnvOptionTagCallbackUserDataOnGpuEnvDestroy,
MagicNumberConfigs = kLiteRtEnvOptionTagMagicNumberConfigs,
MagicNumberVerifications = kLiteRtEnvOptionTagMagicNumberVerifications,
CompilerCacheDir = kLiteRtEnvOptionTagCompilerCacheDir,
WebGpuInstance = kLiteRtEnvOptionTagWebGpuInstance,
WebGpuProcs = kLiteRtEnvOptionTagWebGpuProcs,
RuntimeLibraryDir = kLiteRtEnvOptionTagRuntimeLibraryDir,
AutoRegisterAccelerators = kLiteRtEnvOptionTagAutoRegisterAccelerators,
};
struct [[deprecated("Use EnvironmentOptions::Option instead.")]] Option {
OptionTag tag;
LiteRtVariant value;
};
Expected<EnvironmentOptions> GetOptions() const {
LiteRtEnvironmentOptions options;
LITERT_RETURN_IF_ERROR(
runtime_->GetEnvironmentOptions(handle_.get(), &options));
return FromCOptions(options);
}
static Expected<Environment> Create(absl::Span<const Option> options) {
std::vector<EnvironmentOptions::Option> env_options;
env_options.reserve(options.size());
for (const auto& option : options) {
env_options.push_back(
{static_cast<EnvironmentOptions::Tag>(option.tag), option.value});
}
auto env_options_obj = EnvironmentOptions(env_options);
return Create(env_options_obj);
}
static Expected<Environment> Create(const EnvironmentOptions& options) {
auto c_options = ToCOptions(options.GetOptions());
if (!c_options) {
return c_options.Error();
}
const struct LiteRtRuntimeCApiStruct* handle = nullptr;
Expected<const LiteRtVariant> system_runtime_handle =
options.GetOption(EnvironmentOptions::Tag::kSystemRuntimeHandle);
if (system_runtime_handle &&
std::holds_alternative<const void*>(*system_runtime_handle)) {
handle = reinterpret_cast<const struct LiteRtRuntimeCApiStruct*>(
std::get<const void*>(*system_runtime_handle));
}
auto runtime = CreateRuntime(handle);
LiteRtEnvironment env;
if (auto status = runtime->CreateEnvironment(c_options->size(),
c_options->data(), &env);
status != kLiteRtStatusOk) {
return Error(status);
} else {
return Environment(env, std::move(runtime));
}
}
/// @brief Returns whether the environment supports CL/GL interop.
bool SupportsClGlInterop() const {
bool is_supported = false;
if (auto status = runtime_->EnvironmentSupportsClGlInterop(handle_.get(),
&is_supported);
status != kLiteRtStatusOk) {
return false;
}
return is_supported;
}
/// @brief Returns whether the environment supports AHWB/CL interop.
bool SupportsAhwbClInterop() const {
bool is_supported = false;
if (auto status = runtime_->EnvironmentSupportsAhwbClInterop(handle_.get(),
&is_supported);
status != kLiteRtStatusOk) {
return false;
}
return is_supported;
}
/// @brief Returns whether the environment supports AHWB/GL interop.
bool SupportsAhwbGlInterop() const {
bool is_supported = false;
if (auto status = runtime_->EnvironmentSupportsAhwbGlInterop(handle_.get(),
&is_supported);
status != kLiteRtStatusOk) {
return false;
}
return is_supported;
}
/// @internal
/// @brief Returns the underlying environment handle.
[[deprecated("Use GetHolder() instead.")]]
LiteRtEnvironment Get() const noexcept {
return handle_.get();
}
/// @internal
/// @brief Returns the underlying environment handle and runtime.
internal::EnvironmentHolder GetHolder() const noexcept {
return {runtime_.get(), handle_.get()};
}
/// @internal
/// @brief Releases ownership of the environment handle.
///
/// After this call, `GetHolder()` returns a null handle.
internal::EnvironmentHolder Release() noexcept {
return {runtime_.release(), handle_.release()};
}
/// @brief Returns `true` if the underlying LiteRT handle is valid.
explicit operator bool() const noexcept { return static_cast<bool>(handle_); }
bool operator==(const Environment& other) const noexcept {
return GetHolder() == other.GetHolder();
}
bool operator!=(const Environment& other) const noexcept {
return GetHolder() != other.GetHolder();
}
/// @internal
/// @brief Wraps a `LiteRtEnvironment` C object in an `Environment` C++
/// object, with the builtin runtime.
/// @warning This is for internal use only.
static Environment WrapCObject(LiteRtEnvironment env, OwnHandle owned) {
auto runtime = CreateRuntime();
return Environment(env, std::move(runtime), owned);
}
/// @internal
/// @brief Wraps a `EnvironmentHolder` in an `Environment` C++ object.
/// @warning This is for internal use only.
static Environment WrapCObject(const internal::EnvironmentHolder& env,
OwnHandle owned) {
auto runtime =
std::unique_ptr<RuntimeProxy, std::function<void(RuntimeProxy*)>>(
env.runtime, [](RuntimeProxy*) {});
return Environment(env.handle, std::move(runtime), owned);
}
private:
explicit Environment(
LiteRtEnvironment env,
std::unique_ptr<RuntimeProxy, std::function<void(RuntimeProxy*)>> runtime,
OwnHandle owned = OwnHandle::kYes)
: runtime_(std::move(runtime)) {
std::function<void(LiteRtEnvironment)> handle_deleter;
auto runtime_ptr = runtime_.get();
if (owned == OwnHandle::kYes && runtime_ptr != nullptr) {
handle_deleter = [runtime_ptr](LiteRtEnvironment env_) {
runtime_ptr->DestroyEnvironment(env_);
};
} else {
handle_deleter = [](LiteRtEnvironment) {};
}
handle_ = std::unique_ptr<std::remove_pointer_t<LiteRtEnvironment>,
std::function<void(LiteRtEnvironment)>>(
env, handle_deleter);
}
std::unique_ptr<RuntimeProxy, std::function<void(RuntimeProxy*)>> runtime_;
// handle_ needs to be declared after runtime_ to ensure that they will be
// destroyed in the reverse order when the Environment is destroyed. This is
// because the deleter function may access the member runtime_.
std::unique_ptr<std::remove_pointer_t<LiteRtEnvironment>,
std::function<void(LiteRtEnvironment)>>
handle_;
static Expected<std::vector<LiteRtEnvOption>> ToCOptions(
absl::Span<const EnvironmentOptions::Option> options) {
std::vector<LiteRtEnvOption> c_options;
c_options.reserve(options.size());
for (auto& option : options) {
auto litert_any = ToLiteRtAny(option.value);
if (!litert_any) {
return litert_any.Error();
}
LiteRtEnvOption c_option = {
/*.tag=*/static_cast<LiteRtEnvOptionTag>(option.tag),
/*.value=*/*litert_any,
};
c_options.push_back(c_option);
}
return c_options;
}
Expected<EnvironmentOptions> FromCOptions(
LiteRtEnvironmentOptions options) const {
std::vector<EnvironmentOptions::Option> env_options;
for (int i = 0; i <= kLiteRtEnvOptionTagAutoRegisterAccelerators; ++i) {
LiteRtAny value;
if (runtime_->GetEnvironmentOptionsValue(
options, static_cast<LiteRtEnvOptionTag>(i), &value) ==
kLiteRtStatusOk) {
EnvironmentOptions::Option option(
static_cast<EnvironmentOptions::Tag>(i), ToStdAny(value));
env_options.push_back(std::move(option));
}
}
return EnvironmentOptions(env_options);
}
/// @brief Creates a runtime proxy with the externally provided system runtime
/// handle.
///
/// If the system runtime handle is not provided, the builtin runtime will be
/// used.
static std::unique_ptr<RuntimeProxy> CreateRuntime(
const struct LiteRtRuntimeCApiStruct* system_runtime_handle = nullptr) {
return std::make_unique<RuntimeProxy>(system_runtime_handle);
}
};
} // namespace litert
#endif // ODML_LITERT_LITERT_CC_LITERT_ENVIRONMENT_H_