Closed Bug 2051970 Opened 4 months ago Closed 3 months ago

Introduce a dedicated process for (potentially) hardware accelerated inference

Categories

(Core :: Machine Learning: On Device, enhancement, P2)

enhancement

Tracking

()

RESOLVED FIXED
156 Branch
Tracking Status
firefox156 --- fixed

People

(Reporter: padenot, Assigned: padenot)

References

(Blocks 4 open bugs)

Details

Attachments

(9 external files, 2 obsolete files)

Splitting this off from bug 1940906 for clarity, this is about introducing the HWInference utility process. The name might change, the concept stays.

Blocks: 2051973

This is essentially a copy-paste of the GPU process policy, trimmed down
to removed everything that wasn't needed. One thing that has been added
is read/write permissions to the /plugincontainer directory so shader
compilation caching works.

We have discussed changing the executable name to relinquish allowing
executable page, this hasn't been implemented.

This also include just enough code so that it compiles and run on Linux,
but that side needs work.

Assignee: nobody → padenot
Status: NEW → ASSIGNED

The HWInference process is a new type of process, that is created from a
content process (e.g. to implement a Web API), but can also talk to the
main process (e.g. to interact with ModelHub and acquire ML models).

It doesn't run script. It has a dedicated sandbox policy (in a
subsequent patch), that is roughly similar to the GPU process policy,
with everything related to graphics rendering removed (connection to the
display server, access to fonts, misc system calls and privileges,
etc.), as it is just for hardware accelerated computations.

This patch only contains the scaffolding, and the first actor
(PSpeechRecognition) will be introduced in another patch.

This allows calling into ModelHub from native code. This also adds a
couple testing facilities, and a little bit of formatting fixes.

The testing facilities allow almost end-to-end testing of some
Web Speech API feature, testing all the IPC, but sidestepping the very
last part about having an actual model.

Only the facilities SpeechRecognition needs are exposed via this small
XPCOM component: check availability, download a mode, with progress
callbacks, and getting an fd for a model file stored in ModelHub.

Attachment #9603479 - Attachment description: Bug 2051970 - Sandbox policy for the HWInference utility process. r?haik → WIP: Bug 2051970 - Sandbox policy for the HWInference utility process. r?haik

This is be later used when initializing the speech recognition backend.
The files range from a hundreds megabyte to multiple gigabytes
(depending on the quality and how many languages they can handle), so we
really need to avoid a copy here.

Summary: Introduce a dedicated process for (potentially) hardware accelerate inference → Introduce a dedicated process for (potentially) hardware accelerated inference
Duplicate of this bug: 2046974
Blocks: 2052544
Attachment #9603479 - Attachment description: WIP: Bug 2051970 - Sandbox policy for the HWInference utility process. r?haik → Bug 2051970 - Sandbox policy for the HWInference utility process. r?haik

The Windows sandbox for HW_INFERENCE inherited UtilitySandboxProps'
defaults, which blocked loading our vendored, non-Microsoft-signed
mozinference.dll in two ways:

  • Code Integrity Guard (MITIGATION_FORCE_MS_SIGNED_BINS) rejects
    loading any binary that isn't Microsoft-signed.
  • Even with CIG disabled, the sandbox broker's file-access policy is
    default-deny: nothing granted HW_INFERENCE read access to its own
    installation directory, so the LoadLibrary call still failed.

Content processes hit neither problem: they never enable CIG at all,
and SetSecurityLevelForContentProcess() explicitly grants read access
to the installation directory. This makes HWInferenceSandboxProps do
the same two things - disable CIG and grant that same read-only
directory rule - while leaving the job/token/integrity levels at their
strict defaults, since content processes prove those aren't the
blocker.

Verified with MOZ_LOG=LlamaRuntimeLinker:5 on try: mozinference.dll now
loads and links successfully in the sandboxed process, and
mochitest-speech-recognition is green on Windows, Linux, and macOS.

Attachment #9603480 - Attachment description: WIP: Bug 2051970 - Introduce the HWInference utility process. r?#ipc-reviewers,gerard-majax → Bug 2051970 - Introduce the HWInference utility process. r?#ipc-reviewers,gerard-majax
Attachment #9603481 - Attachment description: WIP: Bug 2051970 - Add an XPCOM interface to ModelHub.sys.mjs, add available() to ModelHub. r?tarek,#xpcom-reviewers → Bug 2051970 - Add an XPCOM interface to ModelHub.sys.mjs, add available() to ModelHub. r?tarek,#xpcom-reviewers
Attachment #9603482 - Attachment description: WIP: Bug 2051970 - Add generic IsModelAvailable IPC message to PHWInference. r?#ipc-reviewers → Bug 2051970 - Add generic IsModelAvailable IPC message to PHWInference. r?#ipc-reviewers
Attachment #9603483 - Attachment description: WIP: Bug 2051970 - Add generic InstallModel IPC message to PHWInference. r?#ipc-reviewers → Bug 2051970 - Add generic InstallModel IPC message to PHWInference. r?#ipc-reviewers
Attachment #9603484 - Attachment description: WIP: Bug 2051970 - Allow getting a Blob of a model file in the HWInference process, cross-process, without copies. r?#ipc-reviewers → Bug 2051970 - Allow getting a Blob of a model file in the HWInference process, cross-process, without copies. r?#ipc-reviewers

The severity field is not set for this bug.
:gregtatum, could you have a look please?

For more information, please visit BugBot documentation.

Flags: needinfo?(gtatum)
Type: defect → enhancement
Priority: -- → P2
Flags: needinfo?(gtatum)
Attachment #9603481 - Attachment description: Bug 2051970 - Add an XPCOM interface to ModelHub.sys.mjs, add available() to ModelHub. r?tarek,#xpcom-reviewers → Bug 2051970 - Add an XPCOM interface to ModelHub.sys.mjs, add isModelAvailable() to ModelHub. r?tarek,#xpcom-reviewers
Attachment #9603479 - Attachment description: Bug 2051970 - Sandbox policy for the HWInference utility process. r?haik → Bug 2051970 - Sandbox policy for the HWInference utility process. r?haik,#ai-platform-reviewers,valentinp
Attachment #9603480 - Attachment description: Bug 2051970 - Introduce the HWInference utility process. r?#ipc-reviewers,gerard-majax → Bug 2051970 - Introduce the HWInference utility process. r?#ipc-reviewers,gerard-majax,#ai-platform-reviewers
Attachment #9603481 - Attachment description: Bug 2051970 - Add an XPCOM interface to ModelHub.sys.mjs, add isModelAvailable() to ModelHub. r?tarek,#xpcom-reviewers → Bug 2051970 - Add an XPCOM interface to ModelHub.sys.mjs, add isModelAvailable() to ModelHub. r?#xpcom-reviewers,#ai-platform-reviewers
Attachment #9603482 - Attachment description: Bug 2051970 - Add generic IsModelAvailable IPC message to PHWInference. r?#ipc-reviewers → Bug 2051970 - Add generic IsModelAvailable IPC message to PHWInference. r?#ipc-reviewers,#ai-platform-reviewers
Attachment #9603483 - Attachment description: Bug 2051970 - Add generic InstallModel IPC message to PHWInference. r?#ipc-reviewers → Bug 2051970 - Add generic InstallModel/IsModelInstalled IPC messages to PHWInference. r?#ipc-reviewers,#ai-platform-reviewers
Attachment #9603484 - Attachment description: Bug 2051970 - Allow getting a Blob of a model file in the HWInference process, cross-process, without copies. r?#ipc-reviewers → Bug 2051970 - Allow getting a Blob of a model file in the HWInference process, cross-process, without copies. r?#ipc-reviewers,#ai-platform-reviewers

The HWInference process, once launched, only ever died at browser shutdown.
UtilityProcessManager now keeps a keep-alive count per process instance key
(AcquireHWInferenceKeepAlive/ReleaseHWInferenceKeepAlive) and shuts the
instance down with CleanShutdown as soon as the last one is released. It is a
plain refcount with no policy of its own; any 'keep it warm' behaviour belongs
to the consumer holding the keep-alive.

The count lives in the main process because that is where
UtilityProcessManager is: HWInferenceManagerParent, which represents 'a
content process is connected', lives in the utility process and cannot shut
its own process down. This mirrors what the WINDOWS_FILE_DIALOG utility
process does with sOpenDialogActors.

Content processes go through PContent: RequestHWInferenceConnection acquires
a keep-alive on success and the new ReleaseHWInferenceConnection drops it.
HWInferenceManagerChild sends exactly one release per successful request, from
ActorDestroy for the endpoint it adopted and immediately for one it had to
discard, since two concurrent requests can race and only the first is bound.
ContentParent counts its own process' share so ActorDestroy can release the
remainder, keeping a crashed content process from pinning the utility process
forever. Parent-process consumers call Acquire/Release directly.

TO MOVE: this is its own patch, to sit immediately after 'Introduce the
HWInference utility process' -- not squashed into it.

Blocks: 2058461
Blocks: 2058464
Attached file WIP: Bug 2051970 - HWInference (obsolete) —

Based on D268403, NEVER LAND

As noted in the original patch that used this (D275653), this touches many patches of the
entire series, and I've have liked to see it in one separate diff. I can also fold it
into the patches it touches, now or prior to landing.

A managed actor is bound to its manager thread, so PSpeechRecognition managed by
PHWInferenceManager meant the first consumer to connect picked the thread for
all the others. PHWInferenceManager now creates the connection instead of
managing it: CreateSpeechRecognition() returns an Endpoint<PSpeechRecognitionChild>
the consumer binds where it wants. The manager moves to the main thread in both
processes; speech keeps its dedicated thread for the session actor.

The shared connection also had no owner: speech closed it when its own users hit
zero. It is refcounted by HWInferenceConnectionGuard now, and speech holds one
guard. Per-feature users and idle policy stay with the feature, since those
genuinely differ.

The utility now attaches the ContentParentId from the parent to
SpeechRecognitionParent at creation, since the managed relationship no longer
carries it.

Teardown is an explicit Close(), and session creation costs one round trip, so
Start() and the transient-session path resolve a promise instead of returning an
actor synchronously. This is perfectly fine.

Pushed by padenot@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/bcc7c27b3e5c https://hg.mozilla.org/integration/autoland/rev/56742e925f81 Sandbox policy for the HWInference utility process. r=haik,sandbox-reviewers,ai-platform-reviewers,ipc-reviewers,nika https://github.com/mozilla-firefox/firefox/commit/fb63707bd614 https://hg.mozilla.org/integration/autoland/rev/fa3f3a4aaea0 Allow the HWInference utility process to load mozinference.dll on Windows. r=bobowen,sandbox-reviewers,ipc-reviewers https://github.com/mozilla-firefox/firefox/commit/3c981492fc46 https://hg.mozilla.org/integration/autoland/rev/c5c2c38183e2 Introduce the HWInference utility process. r=gerard-majax,ipc-reviewers,fluent-reviewers,nika,ai-platform-reviewers,flod,sandbox-reviewers,gregtatum https://github.com/mozilla-firefox/firefox/commit/8cdea25f49e5 https://hg.mozilla.org/integration/autoland/rev/c8ea8bf093c2 Let consumers decide the HWInference process lifetime. r=ipc-reviewers,ai-platform-reviewers,valentinp,nika https://github.com/mozilla-firefox/firefox/commit/ad16c36ee2d0 https://hg.mozilla.org/integration/autoland/rev/4b1566881bb7 Add an XPCOM interface to ModelHub.sys.mjs, add isModelAvailable() to ModelHub. r=beth,ai-platform-reviewers,gregtatum https://github.com/mozilla-firefox/firefox/commit/65ee90ff8f2a https://hg.mozilla.org/integration/autoland/rev/a624029f63d0 Add generic IsModelAvailable IPC message to PHWInference. r=ipc-reviewers,ai-platform-reviewers,nika,gregtatum https://github.com/mozilla-firefox/firefox/commit/4fbbf84dfd83 https://hg.mozilla.org/integration/autoland/rev/4738cfcf61a6 Add generic InstallModel/IsModelInstalled IPC messages to PHWInference. r=ipc-reviewers,ai-platform-reviewers,nika,gregtatum https://github.com/mozilla-firefox/firefox/commit/27eff32504fe https://hg.mozilla.org/integration/autoland/rev/b6c1f3457ae2 Allow getting a Blob of a model file in the HWInference process, cross-process, without copies. r=ipc-reviewers,nika,ai-platform-reviewers,gregtatum https://github.com/mozilla-firefox/firefox/commit/52c6c6d4773d https://hg.mozilla.org/integration/autoland/rev/7a4e3a181bf1 Add HWInference architecture and security documentation. r=ai-platform-reviewers,valentinp
QA Whiteboard: [qa-triage-done-c157/b156]
Attachment #9625319 - Attachment is obsolete: true
Attachment #9621318 - Attachment is obsolete: true
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: