diff --git a/litert/runtime/CMakeLists.txt b/litert/runtime/CMakeLists.txt index 4891eab2eb..27eaa71bb0 100644 --- a/litert/runtime/CMakeLists.txt +++ b/litert/runtime/CMakeLists.txt @@ -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 diff --git a/litert/runtime/accelerators/BUILD b/litert/runtime/accelerators/BUILD index e2e45fee15..bd389e3837 100644 --- a/litert/runtime/accelerators/BUILD +++ b/litert/runtime/accelerators/BUILD @@ -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", ], ) @@ -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", + ], +) diff --git a/litert/runtime/accelerators/auto_registration.cc b/litert/runtime/accelerators/auto_registration.cc index 536b977958..66e4d555dd 100644 --- a/litert/runtime/accelerators/auto_registration.cc +++ b/litert/runtime/accelerators/auto_registration.cc @@ -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) { @@ -66,34 +54,22 @@ Expected 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); diff --git a/litert/runtime/accelerators/npu_registry.cc b/litert/runtime/accelerators/npu_registry.cc new file mode 100644 index 0000000000..940d467e2a --- /dev/null +++ b/litert/runtime/accelerators/npu_registry.cc @@ -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 diff --git a/litert/runtime/accelerators/npu_registry.h b/litert/runtime/accelerators/npu_registry.h new file mode 100644 index 0000000000..95eda13a41 --- /dev/null +++ b/litert/runtime/accelerators/npu_registry.h @@ -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_ diff --git a/litert/runtime/accelerators/webnn_registry.cc b/litert/runtime/accelerators/webnn_registry.cc new file mode 100644 index 0000000000..923c87724e --- /dev/null +++ b/litert/runtime/accelerators/webnn_registry.cc @@ -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 diff --git a/litert/runtime/accelerators/webnn_registry.h b/litert/runtime/accelerators/webnn_registry.h new file mode 100644 index 0000000000..3c64c3132c --- /dev/null +++ b/litert/runtime/accelerators/webnn_registry.h @@ -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_