-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathlitert_options.cc
More file actions
171 lines (151 loc) · 6.08 KB
/
litert_options.cc
File metadata and controls
171 lines (151 loc) · 6.08 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
// Copyright 2025 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.
#include "litert/cc/litert_options.h"
#include <optional>
#include <utility>
#include "litert/c/litert_common.h"
#include "litert/c/litert_opaque_options.h"
#include "litert/c/litert_options.h"
#include "litert/c/options/litert_compiler_options.h"
#include "litert/c/options/litert_cpu_options.h"
#include "litert/c/options/litert_google_tensor_options.h"
#include "litert/c/options/litert_gpu_options.h"
#include "litert/c/options/litert_intel_openvino_options.h"
#include "litert/c/options/litert_mediatek_options.h"
#include "litert/c/options/litert_qualcomm_options.h"
#include "litert/c/options/litert_runtime_options.h"
#include "litert/cc/internal/scoped_file.h"
#include "litert/cc/litert_expected.h"
#include "litert/cc/litert_macros.h"
#include "litert/cc/litert_opaque_options.h"
#include "litert/cc/options/litert_compiler_options.h"
#include "litert/cc/options/litert_cpu_options.h"
#include "litert/cc/options/litert_google_tensor_options.h"
#include "litert/cc/options/litert_gpu_options.h"
#include "litert/cc/options/litert_intel_openvino_options.h"
#include "litert/cc/options/litert_mediatek_options.h"
#include "litert/cc/options/litert_qualcomm_options.h"
#include "litert/cc/options/litert_runtime_options.h"
#include "litert/core/options.h"
namespace litert {
namespace {
template <typename OptionType>
Expected<OptionType&> EnsureOption(std::optional<OptionType>& slot) {
if (!slot) {
LITERT_ASSIGN_OR_RETURN(auto option, OptionType::Create());
slot.emplace(std::move(option));
}
return slot.value();
}
template <typename OptionType>
LiteRtStatus AppendAndReset(LiteRtOptions options,
std::optional<OptionType>& slot) {
if (!slot) {
return kLiteRtStatusOk;
}
LiteRtOpaqueOptions opaque = slot->Release();
slot.reset();
return LiteRtAddOpaqueOptions(options, opaque);
}
template <typename OptionType, typename GetDataFunc>
LiteRtStatus AppendAndResetOpaqueData(LiteRtOptions options,
std::optional<OptionType>& slot,
GetDataFunc get_data_func) {
if (!slot) {
return kLiteRtStatusOk;
}
const char* identifier;
void* payload = nullptr;
void (*payload_deleter)(void*) = nullptr;
LITERT_RETURN_IF_ERROR(
get_data_func(slot->Get(), &identifier, &payload, &payload_deleter));
LiteRtOpaqueOptions opaque_opts = nullptr;
LITERT_RETURN_IF_ERROR(LiteRtCreateOpaqueOptions(
identifier, payload, payload_deleter, &opaque_opts));
LITERT_RETURN_IF_ERROR(LiteRtAddOpaqueOptions(options, opaque_opts));
slot.reset();
return kLiteRtStatusOk;
}
} // namespace
Expected<GpuOptions&> Options::GetGpuOptions() {
return EnsureOption(gpu_options_);
}
Expected<CpuOptions&> Options::GetCpuOptions() {
return EnsureOption(cpu_options_);
}
Expected<qualcomm::QualcommOptions&> Options::GetQualcommOptions() {
return EnsureOption(qualcomm_options_);
}
Expected<mediatek::MediatekOptions&> Options::GetMediatekOptions() {
return EnsureOption(mediatek_options_);
}
Expected<google_tensor::GoogleTensorOptions&>
Options::GetGoogleTensorOptions() {
return EnsureOption(google_tensor_options_);
}
Expected<intel_openvino::IntelOpenVinoOptions&>
Options::GetIntelOpenVinoOptions() {
return EnsureOption(intel_openvino_options_);
}
Expected<RuntimeOptions&> Options::GetRuntimeOptions() {
return EnsureOption(runtime_options_);
}
Expected<CompilerOptions&> Options::GetCompilerOptions() {
return EnsureOption(compiler_options_);
}
Expected<void> Options::Build() {
LITERT_RETURN_IF_ERROR(AppendAndResetOpaqueData(Get(), gpu_options_,
LrtGetOpaqueGpuOptionsData));
LITERT_RETURN_IF_ERROR(AppendAndResetOpaqueData(Get(), cpu_options_,
LrtGetOpaqueCpuOptionsData));
LITERT_RETURN_IF_ERROR(AppendAndResetOpaqueData(
Get(), qualcomm_options_, LrtGetOpaqueQualcommOptionsData));
LITERT_RETURN_IF_ERROR(AppendAndResetOpaqueData(
Get(), mediatek_options_, LrtGetOpaqueMediatekOptionsData));
LITERT_RETURN_IF_ERROR(AppendAndResetOpaqueData(
Get(), google_tensor_options_, LrtGetOpaqueGoogleTensorOptionsData));
LITERT_RETURN_IF_ERROR(AppendAndResetOpaqueData(
Get(), intel_openvino_options_, LrtGetOpaqueIntelOpenVinoOptionsData));
LITERT_RETURN_IF_ERROR(AppendAndResetOpaqueData(
Get(), runtime_options_, LrtGetOpaqueRuntimeOptionsData));
LITERT_RETURN_IF_ERROR(AppendAndResetOpaqueData(
Get(), compiler_options_, LrtGetOpaqueCompilerOptionsData));
return {};
}
Expected<void> Options::SetExternalWeightScopedFile(
ScopedFile& scoped_file, ScopedWeightSectionMap sections) {
if (!scoped_file.IsValid()) {
return Unexpected(kLiteRtStatusErrorInvalidArgument,
"Scoped file handle must be valid");
}
if (sections.empty()) {
return Unexpected(kLiteRtStatusErrorInvalidArgument,
"At least one external buffer group must be provided");
}
for (const auto& [name, section] : sections) {
if (section.length == 0) {
return Unexpected(kLiteRtStatusErrorInvalidArgument,
"Section length must be positive for group " + name);
}
}
auto* options_impl = reinterpret_cast<LiteRtOptionsT*>(Get());
if (!options_impl) {
return Unexpected(kLiteRtStatusErrorRuntimeFailure,
"Options handle must not be null");
}
options_impl->scoped_weight_source = std::make_unique<ScopedWeightSource>(
std::move(scoped_file), std::move(sections));
return {};
}
} // namespace litert