8000 #litert Create the NPU accelerator. · linux-on-ibm-z/tensorflow@0f1a45d · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f1a45d

Browse files
qukhantensorflower-gardener
authored andcommitted
#litert Create the NPU accelerator.
The accelerator is not yet automatically registered to the LiteRT environment. PiperOrigin-RevId: 730924856
1 parent 57859a1 commit 0f1a45d

File tree

5 files changed

+321
-2
lines changed

5 files changed

+321
-2
lines changed

tensorflow/lite/experimental/litert/c/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ cc_library(
385385
":litert_accelerator",
386386
":litert_accelerator_options",
387387
":litert_common",
388-
":litert_compiled_model",
389388
"//tensorflow/lite/experimental/litert/cc:litert_expected",
390389
"//tensorflow/lite/experimental/litert/core:accelerator",
391390
"//tensorflow/lite/experimental/litert/core:environment",

tensorflow/lite/experimental/litert/c/litert_accelerator_registration.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "tensorflow/lite/experimental/litert/c/litert_accelerator.h"
2121
#include "tensorflow/lite/experimental/litert/c/litert_accelerator_options.h"
2222
#include "tensorflow/lite/experimental/litert/c/litert_common.h"
23-
#include "tensorflow/lite/experimental/litert/c/litert_compiled_model.h"
2423
#include "tensorflow/lite/experimental/litert/c/litert_environment.h"
2524
#include "tensorflow/lite/experimental/litert/cc/litert_expected.h"
2625
#include "tensorflow/lite/experimental/litert/core/accelerator.h"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2025 Google LLC.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
package(
16+
# copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
17+
default_visibility = [
18+
"//tensorflow/lite/experimental/litert:__subpackages__",
19+
],
20+
)
21+
22+
cc_library(
23+
name = "dispatch_accelerator",
24+
srcs = ["dispatch_accelerator.cc"],
25+
hdrs = ["dispatch_accelerator.h"],
26+
deps = [
27+
"//tensorflow/lite/c:c_api_types",
28+
"//tensorflow/lite/experimental/litert/c:litert_accelerator",
29+
"//tensorflow/lite/experimental/litert/c:litert_accelerator_options",
30+
"//tensorflow/lite/experimental/litert/c:litert_accelerator_registration",
31+
"//tensorflow/lite/experimental/litert/c:litert_common",
32+
"//tensorflow/lite/experimental/litert/c:litert_dispatch_delegate",
33+
"//tensorflow/lite/experimental/litert/cc:litert_any",
34+
"//tensorflow/lite/experimental/litert/cc:litert_dispatch_delegate",
35+
"//tensorflow/lite/experimental/litert/cc:litert_expected",
36+
"//tensorflow/lite/experimental/litert/cc:litert_macros",
37+
"//tensorflow/lite/experimental/litert/core:accelerator_model_compilation_data",
38+
"//tensorflow/lite/experimental/litert/core:environment",
39+
"@com_google_absl//absl/strings:string_view",
40+
],
41+
)
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
// Copyright 2025 Google LLC.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "tensorflow/lite/experimental/litert/runtime/accelerators/dispatch/dispatch_accelerator.h"
16+
17+
#include <memory>
18+
#include <string>
19+
#include <utility>
20+
21+
#include "absl/strings/string_view.h"
22+
#include "tensorflow/lite/c/c_api_types.h"
23+
#include "tensorflow/lite/experimental/litert/c/litert_accelerator.h"
24+
#include "tensorflow/lite/experimental/litert/c/litert_accelerator_options.h"
25+
#include "tensorflow/lite/experimental/litert/c/litert_accelerator_registration.h"
26+
#include "tensorflow/lite/experimental/litert/c/litert_common.h"
27+
#include "tensorflow/lite/experimental/litert/c/litert_dispatch_delegate.h"
28+
#include "tensorflow/lite/experimental/litert/c/litert_environment.h"
29+
#include "tensorflow/lite/experimental/litert/cc/litert_any.h"
30+
#include "tensorflow/lite/experimental/litert/cc/litert_dispatch_delegate.h"
31+
#include "tensorflow/lite/experimental/litert/cc/litert_expected.h"
32+
#include "tensorflow/lite/experimental/litert/cc/litert_macros.h"
33+
#include "tensorflow/lite/experimental/litert/core/accelerator_model_compilation_data.h"
34+
#include "tensorflow/lite/experimental/litert/core/environment.h"
35+
36+
namespace litert {
37+
38+
class NpuAccelerator final {
39+
constexpr static const absl::string_view kName = "NpuAccelerator";
40+
// Warning: this should be incremented every time the code of this accelerator
41+
// is updated according to semanting versioning.
42+
constexpr static const LiteRtApiVersion kVersion{1, 0, 0};
43+
constexpr static const LiteRtHwAcceleratorSet kHwSupport =
44+
kLiteRtHwAcceleratorNpu;
45+
46+
public:
47+
explicit NpuAccelerator(std::string library_folder)
48+
: library_folder_(std::move(library_folder)) {}
49+
50+
struct Deleter {
51+
void operator()(NpuAccelerator* npu_accelerator) { delete npu_accelerator; }
52+
};
53+
using Ptr = std::unique_ptr<NpuAccelerator, Deleter>;
54+
55+
static Expected<Ptr> Create(std::string library_folder) {
56+
LITERT_RETURN_IF_ERROR(
57+
!library_folder.empty(),
58+
Error(kLiteRtStatusErrorInvalidArgument,
59+
"Dispatch API implementation library folder was not specified."));
60+
return Ptr(new NpuAccelerator(std::move(library_folder)));
61+
}
62+
63+
// C API
64+
65+
// Deletes the accelerator data.
66+
static void Destroy(void* npu_accelerator) {
67+
Deleter()(reinterpret_cast<NpuAccelerator*>(npu_accelerator));
68+
}
69+
70+
// Stores the accelerator's name in `name`.
71+
static LiteRtStatus GetName(LiteRtAccelerator accelerator,
72+
const char** name) {
73+
LITERT_ENSURE(accelerator != nullptr, kLiteRtStatusErrorInvalidArgument,
74+
"Accelerator handle is invalid.");
75+
LITERT_ENSURE(name != nullptr, kLiteRtStatusErrorInvalidArgument,
76+
"Name pointer is null.");
77+
*name = kName.data();
78+
return kLiteRtStatusOk;
79+
}
80+
81+
// Stores the accelerator's version in `version`.
82+
static LiteRtStatus GetVersion(LiteRtAccelerator accelerator,
83+
LiteRtApiVersion* version) {
84+
LITERT_ENSURE(accelerator != nullptr, kLiteRtStatusErrorInvalidArgument,
85+
"Accelerator handle is invalid.");
86+
LITERT_ENSURE(version != nullptr, kLiteRtStatusErrorInvalidArgument,
87+
"Version pointer is null.");
88+
*version = kVersion;
89+
return kLiteRtStatusOk;
90+
}
91+
92+
// Stores the accelerator's hardware support in `hw_set`.
93+
static LiteRtStatus GetHardwareSupport(LiteRtAccelerator accelerator,
94+
LiteRtHwAcceleratorSet* hw_set) {
95+
LITERT_ENSURE(accelerator != nullptr, kLiteRtStatusErrorInvalidArgument,
96+
"Accelerator handle is invalid.");
97+
LITERT_ENSURE(hw_set != nullptr, kLiteRtStatusErrorInvalidArgument,
98+
"Harware support pointer is null.");
99+
*hw_set = kHwSupport;
100+
return kLiteRtStatusOk;
101+
}
102+
103+
// Goes through the options in the linked list and returns the model
104+
// compilation data if it exists.
105+
static Expected<const litert::ModelCompilationData*> GetModelCompilationData(
106+
LiteRtAcceleratorCompilationOptions options) {
107+
while (options) {
108+
if (options->identifier == litert::ModelCompilationData::kIdentifier) {
109+
return reinterpret_cast<litert::ModelCompilationData*>(options);
110+
}
111+
LiteRtGetNextAcceleratorCompilationOptions(&options);
112+
}
113+
return Unexpected(kLiteRtStatusErrorNotFound,
114+
"Could not retrieve mode compilation data.");
115+
}
116+
117+
// Creates a Dispatch delegate instance.
118+
static LiteRtStatus CreateDelegate(
119+
LiteRtAccelerator accelerator,
120+
LiteRtAcceleratorCompilationOptions options, void** delegate) {
121+
LITERT_ENSURE(delegate != nullptr, kLiteRtStatusErrorInvalidArgument,
122+
"Delegate pointer is null.");
123+
LITERT_ENSURE(accelerator != nullptr, kLiteRtStatusErrorInvalidArgument,
124+
"Accelerator handle is invalid.");
125+
LITERT_ENSURE(accelerator->env != nullptr,
126+
kLiteRtStatusErrorInvalidArgument,
127+
"Accelerator is not registered to an environment.");
128+
129+
LITERT_ASSIGN_OR_RETURN(
130+
const litert::ModelCompilationData* compilation_data,
131+
GetModelCompilationData(options));
132+
const char* allocation_base = compilation_data->allocation_base;
133+
134+
LITERT_ENSURE(allocation_base != nullptr, kLiteRtStatusErrorRuntimeFailure,
135+
"No model allocation was passed by the runtime.");
136+
137+
auto dispatch_delegate_options =
138+
litert::CreateDispatchDelegateOptionsPtr(*accelerator->env);
139+
LITERT_ENSURE(dispatch_delegate_options != nullptr,
140+
kLiteRtStatusErrorRuntimeFailure,
141+
"Dispatch delegate options failed to be created.");
142+
143+
LITERT_ENSURE(
144+
LiteRtDispatchDelegateAddAllocBaseOption(
145+
dispatch_delegate_options.get(), allocation_base) == kTfLiteOk,
146+
kLiteRtStatusErrorRuntimeFailure,
147+
"Could not add allocation base to dispatch delegate options.");
148+
149+
auto dispatch_delegate = litert::CreateDispatchDelegatePtr(
150+
*accelerator->env, std::move(dispatch_delegate_options));
151+
LITERT_ENSURE(dispatch_delegate != nullptr,
152+
kLiteRtStatusErrorRuntimeFailure,
153+
"Dispatch delegate failed to be created.");
154+
155+
*delegate = dispatch_delegate.release();
156+
return kLiteRtStatusOk;
157+
}
158+
159+
// Destroys a Dispatch delegate instance.
160+
static void DestroyDelegate(void* delegate) {
161+
LiteRtDestroyDispatchDelegate(
162+
reinterpret_cast<TfLiteOpaqueDelegate*>(delegate));
163+
}
164+
165+
private:
166+
// Note: we do not directly use the option structure because we want to copy
167+
// and own all the option data.
168+
169+
// Folder to the Dispatch API implementation shared library.
170+
std::string library_folder_;
171+
};
172+
173+
namespace {
174+
175+
struct AcceleratorDestructor {
176+
void operator()(LiteRtAccelerator accelerator) {
177+
LiteRtDestroyAccelerator(accelerator);
178+
}
179+
};
180+
181+
using AcceleratorGuard =
182+
std::unique_ptr<std::pointer_traits<LiteRtAccelerator>::element_type,
183+
AcceleratorDestructor>;
184+
185+
} // namespace
186+
} // namespace litert
187+
188+
extern "C" {
189+
190+
LiteRtStatus LiteRtRegisterNpuAccelerator(
191+
LiteRtEnvironmentT* environment, LiteRtNpuAcceleratorOptions* options) {
192+
LITERT_ENSURE(environment != nullptr, kLiteRtStatusErrorInvalidArgument,
193+
"accelerator handle is invalid");
194+
LiteRtAccelerator accelerator_handle;
195+
LITERT_RETURN_IF_ERROR(LiteRtCreateAccelerator(&accelerator_handle));
196+
litert::AcceleratorGuard accelerator(accelerator_handle);
197+
198+
LiteRtSetAcceleratorGetName(accelerator.get(),
199+
litert::NpuAccelerator::GetName);
200+
LiteRtSetAcceleratorGetVersion(accelerator.get(),
201+
litert::NpuAccelerator::GetVersion);
202+
LiteRtSetAcceleratorGetHardwareSupport(
203+
accelerator.get(), litert::NpuAccelerator::GetHardwareSupport);
204+
205+
LiteRtSetDelegateFunction(accelerator.get(),
206+
litert::NpuAccelerator::CreateDelegate,
207+
litert::NpuAccelerator::DestroyDelegate);
208+
209+
std::string library_folder;
210+
if (options && options->library_folder) {
211+
library_folder = options->library_folder;
212+
}
213+
// Check the environment options if the library folder wasn't set in the
214+
// options.
215+
if (library_folder.empty()) {
216+
if (auto env_library_folder =
217+
environment->GetOption(kLiteRtEnvOptionTagDispatchLibraryDir);
218+
env_library_folder.has_value()) {
219+
LITERT_ASSIGN_OR_RETURN(
220+
library_folder, litert::Get<std::string>(env_library_folder.value()));
221+
}
222+
}
223+
224+
LITERT_ASSIGN_OR_RETURN(
225+
auto accelerator_impl,
226+
litert::NpuAccelerator::Create(std::move(library_folder)));
227+
228+
LITERT_RETURN_IF_ERROR(LiteRtRegisterAccelerator(
229+
environment, accelerator.release(), accelerator_impl.release(),
230+
litert::NpuAccelerator::Destroy));
231+
return kLiteRtStatusOk;
232+
}
233+
234+
} // extern "C"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2025 Google LLC.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef TENSORFLOW_LITE_EXPERIMENTAL_LITERT_RUNTIME_ACCELERATORS_DISPATCH_DISPATCH_ACCELERATOR_H_
16+
#define TENSORFLOW_LITE_EXPERIMENTAL_LITERT_RUNTIME_ACCELERATORS_DISPATCH_DISPATCH_ACCELERATOR_H_
17+
18+
#include "tensorflow/lite/experimental/litert/c/litert_common.h"
19+
#include "tensorflow/lite/experimental/litert/c/litert_environment.h"
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
struct LiteRtNpuAcceleratorOptions {
26+
const char* library_folder;
27+
};
28+
29+
// Registers the NPU accelerator to the given environment.
30+
//
31+
// `options` may be null, in which case the accelerator is registered with
32+
// a default configuration.
33+
//
34+
// If `options.library_folder` is not specified, the library folder is replaced
35+
// with the `LiteRtEnvOptionTagDispatchLibraryDir` environment option (that was
36+
// passed upon creation).
37+
//
38+
// Once this function has returned, options may be freed or reused.
39+
LiteRtStatus LiteRtRegisterNpuAccelerator(LiteRtEnvironment environment,
40+
LiteRtNpuAcceleratorOptions* options);
41+
42+
#ifdef __cplusplus
43+
} // extern "C"
44+
#endif
45+
46+
#endif // TENSORFLOW_LITE_EXPERIMENTAL_LITERT_RUNTIME_ACCELERATORS_DISPATCH_DISPATCH_ACCELERATOR_H_

0 commit comments

Comments
 (0)
0