You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: index.bs
+25-165
Original file line number
Diff line number
Diff line change
@@ -725,24 +725,9 @@ The implementation may use views, as above, for intermediate values.
725
725
726
726
Before the execution, the computation graph that is used to compute one or more specified outputs needs to be compiled and optimized. The key purpose of the compilation step is to enable optimizations that span two or more operations, such as operation or loop fusion.
727
727
728
-
There are multiple ways by which the graph may be compiled. The {{MLGraphBuilder}}.{{MLGraphBuilder/build()}} method compiles the graph in the background without blocking the calling thread, and returns a {{Promise}} that resolves to an {{MLGraph}}. The {{MLGraphBuilder}}.{{MLGraphBuilder/buildSync()}} method compiles the graph immediately on the calling thread, which must be a worker thread running on CPU or GPU device, and returns an {{MLGraph}}. Both compilation methods produce an {{MLGraph}} that represents a compiled graph for optimal execution.
728
+
The {{MLGraphBuilder}}.{{MLGraphBuilder/build()}} method compiles the graph in the background without blocking the calling thread, and returns a {{Promise}} that resolves to an {{MLGraph}}. The compilation step produces an {{MLGraph}} that represents a compiled graph for optimal execution.
729
729
730
-
Once the {{MLGraph}} is constructed, there are multiple ways by which the graph may be executed. The
731
-
{{MLContext}}.{{MLContext/computeSync()}} method represents a way the execution of the graph is carried out immediately
732
-
on the calling thread, which must also be a worker thread, either on a CPU or GPU device. The execution
733
-
produces the results of the computation from all the inputs bound to the graph.
734
-
735
-
The {{MLContext}}.{{MLContext/compute()}} method represents a way the execution of the graph is performed asynchronously
736
-
either on a parallel timeline in a separate worker thread for the CPU execution or on a GPU timeline in a GPU
737
-
command queue. This method returns immediately without blocking the calling thread while the actual execution is
738
-
offloaded to a different timeline. This type of execution is appropriate when the responsiveness of the calling
739
-
thread is critical to good user experience. The computation results will be placed at the bound outputs at the
740
-
time the operation is successfully completed on the offloaded timeline at which time the calling thread is
741
-
signaled. This type of execution supports both the CPU and GPU device.
742
-
743
-
In both the {{MLContext}}.{{MLContext/compute()}} and {{MLContext}}.{{MLContext/computeSync()}} execution methods, the caller supplies
744
-
the input values using {{MLNamedArrayBufferViews}}, binding the input {{MLOperand}}s to their values. The caller
745
-
then supplies pre-allocated buffers for output {{MLOperand}}s using {{MLNamedArrayBufferViews}}.
730
+
Once the {{MLGraph}} is constructed, the {{MLContext}}.{{MLContext/compute()}} method performs the execution of the graph asynchronously either on a parallel timeline in a separate worker thread for the CPU execution or on a GPU timeline in a GPU command queue. This method returns immediately without blocking the calling thread while the actual execution is offloaded to a different timeline. The caller supplies the input values using {{MLNamedArrayBufferViews}}, binding the input {{MLOperand}}s to their values. The caller then supplies pre-allocated buffers for output {{MLOperand}}s using {{MLNamedArrayBufferViews}}. The execution produces the results of the computation from all the inputs bound to the graph. The computation results will be placed at the bound outputs at the time the operation is successfully completed on the offloaded timeline at which time the calling thread is signaled. This type of execution supports both the CPU and GPU device.
746
731
747
732
The {{MLCommandEncoder}} interface created by the {{MLContext}}.{{MLContext/createCommandEncoder()}} method supports
748
733
a graph execution method that provides the maximum flexibility to callers that also utilize WebGPU in their
The <dfn method for=ML>createContextSync(|options|)</dfn> method steps are:
876
-
</summary>
877
-
<div class=algorithm-steps>
878
-
1. If [=this=]'s [=relevant global object=]'s [=associated Document=] is not [=allowed to use=] the [=webnn-feature|webnn=] feature, then [=exception/throw=] a "{{SecurityError}}" {{DOMException}}.
879
-
1. Let |context| be the result of [=creating a context=] with |options|. If that returns failure, then [=exception/throw=] a "{{NotSupportedError}}" {{DOMException}}.
880
-
1. Return |context|.
881
-
</div>
882
-
</details>
883
-
884
-
<details open algorithm>
885
-
<summary>
886
-
The <dfn method for=ML>createContextSync(|gpuDevice|)</dfn> method steps are:
887
-
</summary>
888
-
<div class=algorithm-steps>
889
-
1. If [=this=]'s [=relevant global object=]'s [=associated Document=] is not [=allowed to use=] the [=webnn-feature|webnn=] feature, then [=exception/throw=] a "{{SecurityError}}" {{DOMException}}.
890
-
1. Let |context| be the result of [=creating a context=] with |gpuDevice|. If that returns failure, then [=exception/throw=] a "{{NotSupportedError}}" {{DOMException}}.
Objects implementing the {{MLActivation}} interface represent activation function types.
@@ -1132,40 +1088,6 @@ interface MLContext {};
1132
1088
When the {{[[contextType]]}} is set to [=context type/default=] with the {{MLContextOptions}}.{{deviceType}} set to [=device type/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.
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 [=exception/throws=] an "{{OperationError}}" {{DOMException}}.
- *graph*: an {{MLGraph}}. The compiled graph to be executed.
1149
-
- *inputs*: an {{MLNamedArrayBufferViews}}. The resources of inputs.
1150
-
- *outputs*: an {{MLNamedArrayBufferViews}}. The pre-allocated resources of required outputs.
1151
-
1152
-
**Returns:** {{undefined}}.
1153
-
</div>
1154
-
1155
-
<details open algorithm>
1156
-
<summary>
1157
-
The <dfn method for=MLContext>computeSync(|graph|, |inputs|, |outputs|)</dfn> method steps are:
1158
-
</summary>
1159
-
<div class=algorithm-steps>
1160
-
1. If |graph|.{{MLGraph/[[context]]}}.{{MLContext/[[contextType]]}} is not "[=context type/default=]", [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
1161
-
1. If [=validating graph resources=] given |inputs| and |graph|.{{MLGraph/[[inputDescriptors]]}} returns false, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
1162
-
1. If [=validating graph resources=] given |outputs| and |graph|.{{MLGraph/[[outputDescriptors]]}} returns false, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
1163
-
1. Invoke [=execute graph=] given |graph|, |inputs| and |outputs|.
1164
-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
1165
-
1. Return {{undefined}}.
1166
-
</div>
1167
-
</details>
1168
-
1169
1091
<details open algorithm>
1170
1092
<summary>
1171
1093
To <dfn>validate graph resources</dfn>, given |resources| and |descriptors|, run the following steps:
// Compile the graph up to the specified output operands asynchronously.
1428
1310
Promise<MLGraph> build(MLNamedOperands outputs);
1429
-
1430
-
// Compile the graph up to the specified output operands synchronously.
1431
-
[Exposed=(DedicatedWorker)]
1432
-
MLGraph buildSync(MLNamedOperands outputs);
1433
1311
};
1434
1312
</script>
1435
1313
1436
1314
<div class="note">
1437
-
Both{{MLGraphBuilder}}.{{MLGraphBuilder/build()}}and {{MLGraphBuilder}}.{{MLGraphBuilder/buildSync()}} methods compile the graph builder state up to the specified output operands into a compiled graph according to the type of {{MLContext}} that creates it. Since this operation can be costly in some machine configurations, the calling thread of the {{MLGraphBuilder}}.{{MLGraphBuilder/buildSync()}} method must only be a worker thread to avoid potential disruption of the user experience. When the {{[[contextType]]}} of the {{MLContext}} is set to "[=context type/default=]", the compiled graph is initialized right before the {{MLGraph}} is returned. This graph initialization stage is important for optimal performance of the subsequent graph executions. See [[#api-mlcommandencoder-graph-initialization]] for more detail.
1315
+
The{{MLGraphBuilder}}.{{MLGraphBuilder/build()}}method compiles the graph builder state up to the specified output operands into a compiled graph according to the type of {{MLContext}} that creates it. When the {{[[contextType]]}} of the {{MLContext}} is set to "[=context type/default=]", the compiled graph is initialized right before the {{MLGraph}} is returned. This graph initialization stage is important for optimal performance of the subsequent graph executions. See [[#api-mlcommandencoder-graph-initialization]] for more detail.
1438
1316
</div>
1439
1317
1440
1318
{{MLBufferResourceView}} has the following members:
0 commit comments