diff --git a/index.bs b/index.bs index 2f2c6f3b..9b25c343 100644 --- a/index.bs +++ b/index.bs @@ -583,36 +583,40 @@ interface ML { }; -The {{ML/createContext()}} method steps are: -1. If [=this=]'s [=relevant global object=]'s [=associated Document=] is not [=allowed to use=] the [=webnn-feature|webnn=] feature, then throw a "{{SecurityError!!exception}}" {{DOMException}} and abort these steps. -1. Let |promise| be [=a new promise=]. -1. Let |context| be a new {{MLContext}} object. -1. Switch on the method's first argument: -
-
{{MLContextOptions}} -
Set |context|.{{[[contextType]]}} to [=default-context|default=]. -
Set |context|.{{[[deviceType]]}} to the value of {{MLContextOptions}}'s {{deviceType}}. -
Set |context|.{{[[powerPreference]]}} to the value of {{MLContextOptions}}'s {{powerPreference}}. - -
{{GPUDevice}} -
Set |context|.{{[[contextType]]}} to [=webgpu-context|webgpu=]. -
Set |context|.{{[[deviceType]]}} to "[=device-type-gpu|gpu=]". -
Set |context|.{{[[powerPreference]]}} to "[=power-preference-default|default=]". -
-1. Issue the following steps to a separate timeline: - 1. If the User Agent can support the |context|.{{[[contextType]]}}, |context|.{{[[deviceType]]}} and |context|.{{[[powerPreference]]}}, then: - 1. Set |context|.{{MLContext/[[implementation]]}} to an implementation supporting |context|.{{[[contextType]]}}, |context|.{{[[deviceType]]}} and |context|.{{[[powerPreference]]}}. - 1. [=Resolve=] |promise| with |context|. - 1. Else: - 1. [=Resolve=] |promise| with a new {{NotSupportedError}}. -1. Return |promise|. - ### Permissions Policy Integration ### {#permissions-policy-integration} This specification defines a policy-controlled feature identified by the string "webnn". Its default allowlist is 'self'. +### The {{ML/createContext()}} method ### {#api-ml-createcontext} +The {{ML/createContext()}} method steps are: +1. If [=this=]'s [=relevant global object=]'s [=associated Document=] is not [=allowed to use=] the [=webnn-feature|webnn=] feature, return [=a new promise=] [=rejected=] with a "{{SecurityError}}" {{DOMException}} and abort these steps. + +1. Let |promise| be [=a new promise=]. +1. Return |promise| and run the following steps [=in parallel=]. +1. Let |options| be the first argument. +1. Run the create context steps given |options|: + 1. Let |context| be a new {{MLContext}} object. + 1. If |options| is a {{GPUDevice}} object, + 1. Set |context|.{{[[contextType]]}} to "[=webgpu-context|webgpu=]". + 1. Set |context|.{{[[deviceType]]}} to "[=device-type-gpu|gpu=]". + 1. Set |context|.{{[[powerPreference]]}} to "[=power-preference-default|default=]". + 1. Otherwise, + 1. Set |context|.{{[[contextType]]}} to "[=default-context|default=]". + 1. If |options|["{{deviceType}}"] [=map/exists=], then set |context|.{{[[deviceType]]}} to |options|["{{deviceType}}"]. Otherwise, set |context|.{{[[deviceType]]}} to "[=device-type-cpu|cpu=]". + 1. If |options|["{{powerPreference}}"] [=map/exists=], then set |context|.{{[[powerPreference]]}} to |options|["{{powerPreference}}"]. Otherwise, set |context|.{{[[powerPreference]]}} to "[=power-preference-default|default=]". +1. If the validate MLContext steps given |context| return `false`, [=reject=] |promise| with a "{{NotSupportedError}}" {{DOMException}} and abort these steps. +1. [=Resolve=] |promise| with |context|. + +### The {{ML/createContextSync()}} method ### {#api-ml-createcontextsync} +The {{ML/createContextSync()}} method steps are: +1. If [=this=]'s [=relevant global object=]'s [=associated Document=] is not [=allowed to use=] the [=webnn-feature|webnn=] feature, throw a "{{SecurityError}}" and abort these steps. +1. Let |options| be the first argument. +1. Let |context| be the result of running the create context steps given |options|. +1. If the validate MLContext steps given |context| return `false`, throw a "{{NotSupportedError}}" {{DOMException}} and abort these steps. +1. Return |context|. + ## The MLContext interface ## {#api-mlcontext} The {{MLContext}} interface represents a global state of neural network compute workload and execution processes. Each {{MLContext}} object has associated [=context type=], [=device type=] and [=power preference=]. @@ -661,15 +665,20 @@ interface MLContext {}; : \[[powerPreference]] of type [=power preference=] :: The {{MLContext}}'s [=power preference=]. - : \[[implementation]] - :: - The underlying implementation provided by the User Agent.
When the {{[[contextType]]}} is set to [=default-context|default=] with the {{MLContextOptions}}.{{deviceType}} set to [=device-type-gpu|gpu=], the user agent is responsible for creating an internal GPU device that operates within the context and is capable of ML workload submission on behalf of the calling application. In this setting however, only {{ArrayBufferView}} inputs and outputs are allowed in and out of the graph execution since the application has no way to know what type of internal GPU device is being created on their behalf. In this case, the user agent is responsible for automatic uploads and downloads of the inputs and outputs to and from the GPU memory using this said internal device.
+### The {{MLContext}} validation algorithm ### {#api-mlcontext-validate} +To validate {{MLContext}}, given |context|, run these steps: +1. If |context|.{{[[contextType]]}} is not "[=webgpu-context|webgpu=]" or "[=default-context|default=], return `false`. +1. If |context|.{{[[deviceType]]}} is not "[=device-type-cpu|cpu=]" or "[=device-type-gpu|gpu=]", return `false`. +1. If |context|.{{[[powerPreference]]}} is not "[=power-preference-default|default=]" or "[=power-preference-high-performance|high-performance=]" or "[=power-preference-low-power|low-power=]", return `false`. +1. If the user agent cannot support |context|.{{[[contextType]]}}, |context|.{{[[deviceType]]}} and |context|.{{[[powerPreference]]}}, return `false`. +1. Return `true`; + ### Synchronous Execution ### {#api-mlcontext-sync-execution} Synchronously carries out the computational workload of a compiled graph {{MLGraph}} on the calling thread, which must be a worker thread, to produce results as defined by the operations in the graph. This method of execution requires an {{MLContext}} created with {{MLContextOptions}}. Otherwise, it throws an {{OperationError}} exception.