0

[ios blink] Make webnn work on the device

tflite as a fallback works correctly on iOS in the GPU process.
CoreML model requires file system access to compile a model
and the GPU process is restricted from having file access. We've
asked Apple to provide an update how CoreML can be used in the
sandboxed rendering process.

Bug: 40254930
Change-Id: I72598db882db38fa43d5d04f4c6055cbafb33a87
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6543648
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Reviewed-by: Joe Mason <joenotcharles@google.com>
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1460093}
This commit is contained in:
Dave Tapuska
2025-05-14 07:57:56 -07:00
committed by Chromium LUCI CQ
parent 3321e4b183
commit cd5897004c
5 changed files with 28 additions and 8 deletions

@ -106,7 +106,7 @@ component("webnn_service") {
libs = [ "dxgi.lib" ]
}
if (is_mac) {
if (is_apple) {
sources += [
"coreml/buffer_content_coreml.h",
"coreml/buffer_content_coreml.mm",

@ -29,6 +29,7 @@
#include "base/task/bind_post_task.h"
#include "base/task/thread_pool.h"
#include "base/types/expected_macros.h"
#include "build/build_config.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "mojo/public/cpp/bindings/self_owned_associated_receiver.h"
#include "services/webnn/coreml/buffer_content_coreml.h"
@ -79,6 +80,7 @@ namespace {
// compilation process.
struct ScopedModelPaths {
~ScopedModelPaths() {
#if BUILDFLAG(IS_MAC)
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kWebNNCoreMlDumpModel)) {
const auto dump_directory =
@ -100,6 +102,7 @@ struct ScopedModelPaths {
}
}
}
#endif
// Though the destructors of ScopedTempDir will delete these directories.
// Explicitly delete them here to check for success.
if (model_file_dir.IsValid()) {

@ -5,9 +5,11 @@
import("//build/config/features.gni")
import("//services/on_device_model/on_device_model.gni")
assert(use_blink)
declare_args() {
# TFLite is used as a fallback option on macOS and Windows.
webnn_use_tflite = is_android || is_chromeos || is_linux || is_mac || is_win
# TFLite is used as a fallback option on Apple and Windows.
webnn_use_tflite = is_android || is_chromeos || is_linux || is_apple || is_win
# Enable the GPU delegate provided by the Optimization Guide library.
webnn_use_chrome_ml_api = enable_ml_internal

@ -18,10 +18,19 @@ feature kExperimentalWebMachineLearningNeuralNetwork {
};
// Enables the Core ML backend for WebNN.
[EnableIf=is_mac]
[EnableIf=is_apple]
feature kWebNNCoreML {
const string name = "WebNNCoreML";
// macOS default state is enabled.
[EnableIf=is_macos]
const bool default_state = true;
// iOS default state is disabled since there are sandbox issues
// with using CoreML in the GPU process, we are waiting to hear back
// from Apple. https://crbug.com/417496826
[EnableIf=is_ios]
const bool default_state = false;
};
// Enables the DirectML backend for WebNN.

@ -24,6 +24,9 @@
#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#endif
#if BUILDFLAG(IS_APPLE)
#include "services/webnn/coreml/context_impl_coreml.h"
#endif
@ -187,15 +190,18 @@ void WebNNContextProviderImpl::CreateWebNNContext(
}
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_MAC)
#if BUILDFLAG(IS_APPLE)
if (__builtin_available(macOS 14, *)) {
if (base::FeatureList::IsEnabled(mojom::features::kWebNNCoreML) &&
base::mac::GetCPUType() == base::mac::CPUType::kArm) {
if (base::FeatureList::IsEnabled(mojom::features::kWebNNCoreML)
#if BUILDFLAG(IS_MAC)
&& base::mac::GetCPUType() == base::mac::CPUType::kArm
#endif // BUILDFLAG(IS_MAC)
) {
context_impl = std::make_unique<coreml::ContextImplCoreml>(
std::move(receiver), this, std::move(options));
}
}
#endif // BUILDFLAG(IS_MAC)
#endif // BUILDFLAG(IS_APPLE)
#if BUILDFLAG(WEBNN_USE_TFLITE)
if (!context_impl) {