Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions litert/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ set(LITERT_RUNTIME_SOURCES
accelerators/auto_registration.cc
accelerators/cpu_registry.cc
accelerators/gpu_registry.cc
accelerators/npu_registry.cc
accelerators/webnn_registry.cc
accelerators/registration_helper.cc
accelerators/dispatch/dispatch_accelerator.cc
accelerators/xnnpack/xnnpack_accelerator.cc
Expand Down
31 changes: 24 additions & 7 deletions litert/runtime/accelerators/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@ cc_library(
deps = [
":cpu_registry",
":gpu_registry",
":registration_helper",
":npu_registry",
":webnn_registry",
"//litert/c:litert_any",
"//litert/c:litert_common",
"//litert/c:litert_environment_options",
"//litert/c/internal:litert_accelerator_def",
"//litert/c/internal:litert_accelerator_registration",
"//litert/c/internal:litert_logging",
"//litert/c/internal:litert_tensor_buffer_registry_header",
"//litert/cc:litert_expected",
"//litert/cc:litert_macros",
"//litert/core:environment",
"//litert/runtime/accelerators/dispatch:dispatch_accelerator",
"@com_google_absl//absl/strings",
],
)

Expand Down Expand Up @@ -102,3 +97,25 @@ cc_library(
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "npu_registry",
srcs = ["npu_registry.cc"],
hdrs = ["npu_registry.h"],
deps = [
"//litert/c:litert_common",
"//litert/c/internal:litert_logging",
"//litert/runtime/accelerators/dispatch:dispatch_accelerator",
],
)

cc_library(
name = "webnn_registry",
srcs = ["webnn_registry.cc"],
hdrs = ["webnn_registry.h"],
deps = [
"//litert/c:litert_common",
"//litert/c/internal:litert_logging",
"//litert/core:environment",
],
)
50 changes: 13 additions & 37 deletions litert/runtime/accelerators/auto_registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,15 @@
#include "litert/core/environment.h"
#include "litert/runtime/accelerators/cpu_registry.h"
#include "litert/runtime/accelerators/gpu_registry.h"
#if !defined(LITERT_DISABLE_NPU)
#include "litert/runtime/accelerators/dispatch/dispatch_accelerator.h"
#endif // !defined(LITERT_DISABLE_NPU)

extern "C" {

// Define a function pointer for the WebNN accelerator.
LiteRtStatus (*LiteRtRegisterStaticLinkedAcceleratorWebNn)(
LiteRtEnvironmentT& environment) = nullptr;

} // extern "C"
#include "litert/runtime/accelerators/npu_registry.h"
#include "litert/runtime/accelerators/webnn_registry.h"

namespace litert {
namespace {

constexpr LiteRtHwAcceleratorSet kDefaultAutoRegisterAccelerators =
kLiteRtHwAcceleratorCpu | kLiteRtHwAcceleratorGpu | kLiteRtHwAcceleratorNpu
#if defined(__EMSCRIPTEN__)
| kLiteRtHwAcceleratorWebNn
#endif // defined(__EMSCRIPTEN__)
;
kLiteRtHwAcceleratorCpu | kLiteRtHwAcceleratorGpu |
kLiteRtHwAcceleratorNpu | litert::internal::kLiteRtHwAcceleratorWebNnAlias;

LiteRtHwAcceleratorSet GetAutoRegisterAccelerators(
const LiteRtEnvironmentT& environment) {
Expand All @@ -66,34 +54,22 @@ Expected<void> TriggerAcceleratorAutomaticRegistration(
LiteRtEnvironmentT& environment) {
const LiteRtHwAcceleratorSet auto_register_accelerators =
GetAutoRegisterAccelerators(environment);
// Register the NPU accelerator.
#if !defined(LITERT_DISABLE_NPU)

if (auto_register_accelerators & kLiteRtHwAcceleratorNpu) {
if (auto npu_registration = LiteRtRegisterNpuAccelerator(&environment);
npu_registration == kLiteRtStatusOk) {
LITERT_LOG(LITERT_INFO, "NPU accelerator registered.");
} else {
LITERT_LOG(LITERT_WARNING,
"NPU accelerator could not be loaded and registered: %s.",
LiteRtGetStatusString(npu_registration));
}
litert::internal::LiteRtRegisterNpuAccelerator(&environment);
} else {
LITERT_LOG(LITERT_VERBOSE,
"NPU accelerator registration skipped by environment options.");
}
#else
LITERT_LOG(LITERT_VERBOSE, "NPU accelerator accelerator is disabled.");
#endif

// Register the WebNN accelerator if statically linked.
#if defined(__EMSCRIPTEN__)
if ((auto_register_accelerators & kLiteRtHwAcceleratorWebNn) &&
LiteRtRegisterStaticLinkedAcceleratorWebNn != nullptr &&
LiteRtRegisterStaticLinkedAcceleratorWebNn(environment) ==
kLiteRtStatusOk) {
LITERT_LOG(LITERT_INFO, "Statically linked WebNN accelerator registered.");
if (auto_register_accelerators &
litert::internal::kLiteRtHwAcceleratorWebNnAlias) {
litert::internal::LiteRtRegisterWebNnAccelerator(&environment);
} else {
LITERT_LOG(
LITERT_VERBOSE,
"WebNN accelerator registration skipped by environment options.");
}
#endif // defined(__EMSCRIPTEN__)

if (auto_register_accelerators & kLiteRtHwAcceleratorGpu) {
litert::internal::LiteRtRegisterGpuAccelerator(&environment);
Expand Down
43 changes: 43 additions & 0 deletions litert/runtime/accelerators/npu_registry.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2026 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/runtime/accelerators/npu_registry.h"

#include "litert/c/internal/litert_logging.h"
#include "litert/c/litert_common.h"

#if !defined(LITERT_DISABLE_NPU)
#include "litert/runtime/accelerators/dispatch/dispatch_accelerator.h"
#endif // !defined(LITERT_DISABLE_NPU)

namespace litert::internal {

LiteRtStatus LiteRtRegisterNpuAccelerator(LiteRtEnvironment environment) {
#if !defined(LITERT_DISABLE_NPU)
auto npu_registration = ::LiteRtRegisterNpuAccelerator(environment);
if (npu_registration == kLiteRtStatusOk) {
LITERT_LOG(LITERT_INFO, "NPU accelerator registered.");
} else {
LITERT_LOG(LITERT_WARNING,
"NPU accelerator could not be loaded and registered: %s.",
LiteRtGetStatusString(npu_registration));
}
return npu_registration;
#else
LITERT_LOG(LITERT_VERBOSE, "NPU accelerator is disabled.");
return kLiteRtStatusErrorUnsupported;
#endif
}

} // namespace litert::internal
26 changes: 26 additions & 0 deletions litert/runtime/accelerators/npu_registry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2026 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 THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_NPU_REGISTRY_H_
#define THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_NPU_REGISTRY_H_

#include "litert/c/litert_common.h"

namespace litert::internal {

LiteRtStatus LiteRtRegisterNpuAccelerator(LiteRtEnvironment environment);

} // namespace litert::internal

#endif // THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_NPU_REGISTRY_H_
43 changes: 43 additions & 0 deletions litert/runtime/accelerators/webnn_registry.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2026 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/runtime/accelerators/webnn_registry.h"

#include "litert/c/internal/litert_logging.h"
#include "litert/c/litert_common.h"
#include "litert/core/environment.h"

extern "C" {

// Define a function pointer for the WebNN accelerator.
LiteRtStatus (*LiteRtRegisterStaticLinkedAcceleratorWebNn)(
LiteRtEnvironmentT& environment) = nullptr;

} // extern "C"

namespace litert::internal {

LiteRtStatus LiteRtRegisterWebNnAccelerator(LiteRtEnvironment environment) {
if (LiteRtRegisterStaticLinkedAcceleratorWebNn != nullptr &&
LiteRtRegisterStaticLinkedAcceleratorWebNn(*environment) ==
kLiteRtStatusOk) {
LITERT_LOG(LITERT_INFO, "Statically linked WebNN accelerator registered.");
return kLiteRtStatusOk;
}

LITERT_LOG(LITERT_VERBOSE, "WebNN accelerator is disabled.");
return kLiteRtStatusErrorUnsupported;
}

} // namespace litert::internal
32 changes: 32 additions & 0 deletions litert/runtime/accelerators/webnn_registry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2026 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 THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_WEBNN_REGISTRY_H_
#define THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_WEBNN_REGISTRY_H_

#include "litert/c/litert_common.h"

namespace litert::internal {

#if defined(__EMSCRIPTEN__)
inline constexpr int kLiteRtHwAcceleratorWebNnAlias = kLiteRtHwAcceleratorWebNn;
#else
inline constexpr int kLiteRtHwAcceleratorWebNnAlias = 0;
#endif // defined(__EMSCRIPTEN__)

LiteRtStatus LiteRtRegisterWebNnAccelerator(LiteRtEnvironment environment);

} // namespace litert::internal

#endif // THIRD_PARTY_ODML_LITERT_LITERT_RUNTIME_ACCELERATORS_WEBNN_REGISTRY_H_
Loading