Skip to content

Commit 0a3af2c

Browse files
niuchlcopybara-github
authored andcommitted
Re-structure NPU/WebNN accelerator registration.
LiteRT-PiperOrigin-RevId: 893744799
1 parent e136113 commit 0a3af2c

6 files changed

Lines changed: 181 additions & 44 deletions

File tree

litert/runtime/accelerators/BUILD

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,14 @@ cc_library(
2626
deps = [
2727
":cpu_registry",
2828
":gpu_registry",
29-
":registration_helper",
29+
":npu_registry",
30+
":webnn_registry",
3031
"//litert/c:litert_any",
3132
"//litert/c:litert_common",
3233
"//litert/c:litert_environment_options",
33-
"//litert/c/internal:litert_accelerator_def",
34-
"//litert/c/internal:litert_accelerator_registration",
3534
"//litert/c/internal:litert_logging",
36-
"//litert/c/internal:litert_tensor_buffer_registry_header",
3735
"//litert/cc:litert_expected",
38-
"//litert/cc:litert_macros",
3936
"//litert/core:environment",
40-
"//litert/runtime/accelerators/dispatch:dispatch_accelerator",
41-
"@com_google_absl//absl/strings",
4237
],
4338
)
4439

@@ -102,3 +97,25 @@ cc_library(
10297
"@com_google_absl//absl/strings",
10398
],
10499
)
100+
101+
cc_library(
102+
name = "npu_registry",
103+
srcs = ["npu_registry.cc"],
104+
hdrs = ["npu_registry.h"],
105+
deps = [
106+
"//litert/c:litert_common",
107+
"//litert/c/internal:litert_logging",
108+
"//litert/runtime/accelerators/dispatch:dispatch_accelerator",
109+
],
110+
)
111+
112+
cc_library(
113+
name = "webnn_registry",
114+
srcs = ["webnn_registry.cc"],
115+
hdrs = ["webnn_registry.h"],
116+
deps = [
117+
"//litert/c:litert_common",
118+
"//litert/c/internal:litert_logging",
119+
"//litert/core:environment",
120+
],
121+
)

litert/runtime/accelerators/auto_registration.cc

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,15 @@
2222
#include "litert/core/environment.h"
2323
#include "litert/runtime/accelerators/cpu_registry.h"
2424
#include "litert/runtime/accelerators/gpu_registry.h"
25-
#if !defined(LITERT_DISABLE_NPU)
26-
#include "litert/runtime/accelerators/dispatch/dispatch_accelerator.h"
27-
#endif // !defined(LITERT_DISABLE_NPU)
28-
29-
extern "C" {
30-
31-
// Define a function pointer for the WebNN accelerator.
32-
LiteRtStatus (*LiteRtRegisterStaticLinkedAcceleratorWebNn)(
33-
LiteRtEnvironmentT& environment) = nullptr;
34-
35-
} // extern "C"
25+
#include "litert/runtime/accelerators/npu_registry.h"
26+
#include "litert/runtime/accelerators/webnn_registry.h"
3627

3728
namespace litert {
3829
namespace {
3930

4031
constexpr LiteRtHwAcceleratorSet kDefaultAutoRegisterAccelerators =
41-
kLiteRtHwAcceleratorCpu | kLiteRtHwAcceleratorGpu | kLiteRtHwAcceleratorNpu
42-
#if defined(__EMSCRIPTEN__)
43-
| kLiteRtHwAcceleratorWebNn
44-
#endif // defined(__EMSCRIPTEN__)
45-
;
32+
kLiteRtHwAcceleratorCpu | kLiteRtHwAcceleratorGpu |
33+
kLiteRtHwAcceleratorNpu | litert::internal::kLiteRtHwAcceleratorWebNnAlias;
4634

4735
LiteRtHwAcceleratorSet GetAutoRegisterAccelerators(
4836
const LiteRtEnvironmentT& environment) {
@@ -66,34 +54,22 @@ Expected<void> TriggerAcceleratorAutomaticRegistration(
6654
LiteRtEnvironmentT& environment) {
6755
const LiteRtHwAcceleratorSet auto_register_accelerators =
6856
GetAutoRegisterAccelerators(environment);
69-
// Register the NPU accelerator.
70-
#if !defined(LITERT_DISABLE_NPU)
57+
7158
if (auto_register_accelerators & kLiteRtHwAcceleratorNpu) {
72-
if (auto npu_registration = LiteRtRegisterNpuAccelerator(&environment);
73-
npu_registration == kLiteRtStatusOk) {
74-
LITERT_LOG(LITERT_INFO, "NPU accelerator registered.");
75-
} else {
76-
LITERT_LOG(LITERT_WARNING,
77-
"NPU accelerator could not be loaded and registered: %s.",
78-
LiteRtGetStatusString(npu_registration));
79-
}
59+
litert::internal::LiteRtRegisterNpuAccelerator(&environment);
8060
} else {
8161
LITERT_LOG(LITERT_VERBOSE,
8262
"NPU accelerator registration skipped by environment options.");
8363
}
84-
#else
85-
LITERT_LOG(LITERT_VERBOSE, "NPU accelerator accelerator is disabled.");
86-
#endif
8764

88-
// Register the WebNN accelerator if statically linked.
89-
#if defined(__EMSCRIPTEN__)
90-
if ((auto_register_accelerators & kLiteRtHwAcceleratorWebNn) &&
91-
LiteRtRegisterStaticLinkedAcceleratorWebNn != nullptr &&
92-
LiteRtRegisterStaticLinkedAcceleratorWebNn(environment) ==
93-
kLiteRtStatusOk) {
94-
LITERT_LOG(LITERT_INFO, "Statically linked WebNN accelerator registered.");
65+
if (auto_register_accelerators &
66+
litert::internal::kLiteRtHwAcceleratorWebNnAlias) {
67+
litert::internal::LiteRtRegisterWebNnAccelerator(&environment);
68+
} else {
69+
LITERT_LOG(
70+
LITERT_VERBOSE,
71+
"WebNN accelerator registration skipped by environment options.");
9572
}
96-
#endif // defined(__EMSCRIPTEN__)
9773

9874
if (auto_register_accelerators & kLiteRtHwAcceleratorGpu) {
9975
litert::internal::LiteRtRegisterGpuAccelerator(&environment);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2026 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 "litert/runtime/accelerators/npu_registry.h"
16+
17+
#include "litert/c/internal/litert_logging.h"
18+
#include "litert/c/litert_common.h"
19+
20+
#if !defined(LITERT_DISABLE_NPU)
21+
#include "litert/runtime/accelerators/dispatch/dispatch_accelerator.h"
22+
#endif // !defined(LITERT_DISABLE_NPU)
23+
24+
namespace litert::internal {
25+
26+
LiteRtStatus LiteRtRegisterNpuAccelerator(LiteRtEnvironment environment) {
27+
#if !defined(LITERT_DISABLE_NPU)
28+
auto npu_registration = ::LiteRtRegisterNpuAccelerator(environment);
29+
if (npu_registration == kLiteRtStatusOk) {
30+
LITERT_LOG(LITERT_INFO, "NPU accelerator registered.");
31+
} else {
32+
LITERT_LOG(LITERT_WARNING,
33+
"NPU accelerator could not be loaded and registered: %s.",
34+
LiteRtGetStatusString(npu_registration));
35+
}
36+
return npu_registration;
37+
#else
38+
LITERT_LOG(LITERT_VERBOSE, "NPU accelerator is disabled.");
39+
return kLiteRtStatusErrorUnsupported;
40+
#endif
41+
}
42+
43+
} // namespace litert::internal
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2026 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 THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_NPU_REGISTRY_H_
16+
#define THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_NPU_REGISTRY_H_
17+
18+
#include "litert/c/litert_common.h"
19+
20+
namespace litert::internal {
21+
22+
LiteRtStatus LiteRtRegisterNpuAccelerator(LiteRtEnvironment environment);
23+
24+
} // namespace litert::internal
25+
26+
#endif // THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_NPU_REGISTRY_H_
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2026 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 "litert/runtime/accelerators/webnn_registry.h"
16+
17+
#include "litert/c/internal/litert_logging.h"
18+
#include "litert/c/litert_common.h"
19+
#include "litert/core/environment.h"
20+
21+
extern "C" {
22+
23+
// Define a function pointer for the WebNN accelerator.
24+
LiteRtStatus (*LiteRtRegisterStaticLinkedAcceleratorWebNn)(
25+
LiteRtEnvironmentT& environment) = nullptr;
26+
27+
} // extern "C"
28+
29+
namespace litert::internal {
30+
31+
LiteRtStatus LiteRtRegisterWebNnAccelerator(LiteRtEnvironment environment) {
32+
if (LiteRtRegisterStaticLinkedAcceleratorWebNn != nullptr &&
33+
LiteRtRegisterStaticLinkedAcceleratorWebNn(*environment) ==
34+
kLiteRtStatusOk) {
35+
LITERT_LOG(LITERT_INFO, "Statically linked WebNN accelerator registered.");
36+
return kLiteRtStatusOk;
37+
}
38+
39+
LITERT_LOG(LITERT_VERBOSE, "WebNN accelerator is disabled.");
40+
return kLiteRtStatusErrorUnsupported;
41+
}
42+
43+
} // namespace litert::internal
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2026 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 THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_WEBNN_REGISTRY_H_
16+
#define THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_WEBNN_REGISTRY_H_
17+
18+
#include "litert/c/litert_common.h"
19+
20+
namespace litert::internal {
21+
22+
#if defined(__EMSCRIPTEN__)
23+
inline constexpr int kLiteRtHwAcceleratorWebNnAlias = kLiteRtHwAcceleratorWebNn;
24+
#else
25+
inline constexpr int kLiteRtHwAcceleratorWebNnAlias = 0;
26+
#endif // defined(__EMSCRIPTEN__)
27+
28+
LiteRtStatus LiteRtRegisterWebNnAccelerator(LiteRtEnvironment environment);
29+
30+
} // namespace litert::internal
31+
32+
#endif // THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_WEBNN_REGISTRY_H_

0 commit comments

Comments
 (0)