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
+27-167
Original file line number
Diff line number
Diff line change
@@ -723,24 +723,9 @@ The implementation may use views, as above, for intermediate values.
723
723
724
724
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.
725
725
726
-
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.
726
+
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.
727
727
728
-
Once the {{MLGraph}} is constructed, there are multiple ways by which the graph may be executed. The
729
-
{{MLContext}}.{{MLContext/computeSync()}} method represents a way the execution of the graph is carried out immediately
730
-
on the calling thread, which must also be a worker thread, either on a CPU or GPU device. The execution
731
-
produces the results of the computation from all the inputs bound to the graph.
732
-
733
-
The {{MLContext}}.{{MLContext/compute()}} method represents a way the execution of the graph is performed asynchronously
734
-
either on a parallel timeline in a separate worker thread for the CPU execution or on a GPU timeline in a GPU
735
-
command queue. This method returns immediately without blocking the calling thread while the actual execution is
736
-
offloaded to a different timeline. This type of execution is appropriate when the responsiveness of the calling
737
-
thread is critical to good user experience. The computation results will be placed at the bound outputs at the
738
-
time the operation is successfully completed on the offloaded timeline at which time the calling thread is
739
-
signaled. This type of execution supports both the CPU and GPU device.
740
-
741
-
In both the {{MLContext}}.{{MLContext/compute()}} and {{MLContext}}.{{MLContext/computeSync()}} execution methods, the caller supplies
742
-
the input values using {{MLNamedArrayBufferViews}}, binding the input {{MLOperand}}s to their values. The caller
743
-
then supplies pre-allocated buffers for output {{MLOperand}}s using {{MLNamedArrayBufferViews}}.
728
+
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.
The <dfn method for=ML>createContextSync(|options|)</dfn> method steps are:
867
-
</summary>
868
-
<div class=algorithm-steps>
869
-
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}}.
870
-
1. Let |context| be the result of [=creating a context=] with |options|. If that returns failure, then [=exception/throw=] a "{{NotSupportedError}}" {{DOMException}}.
871
-
1. Return |context|.
872
-
</div>
873
-
</details>
874
-
875
-
<details open algorithm>
876
-
<summary>
877
-
The <dfn method for=ML>createContextSync(|gpuDevice|)</dfn> method steps are:
878
-
</summary>
879
-
<div class=algorithm-steps>
880
-
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}}.
881
-
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.
@@ -994,40 +950,6 @@ interface MLContext {};
994
950
When the {{MLContext/[[contextType]]}} is set to [=context type/default=] with the {{MLContextOptions}}.{{deviceType}} set to {{MLDeviceType/"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.
1011
-
- *inputs*: an {{MLNamedArrayBufferViews}}. The resources of inputs.
1012
-
- *outputs*: an {{MLNamedArrayBufferViews}}. The pre-allocated resources of required outputs.
1013
-
1014
-
**Returns:** {{undefined}}.
1015
-
</div>
1016
-
1017
-
<details open algorithm>
1018
-
<summary>
1019
-
The <dfn method for=MLContext>computeSync(|graph|, |inputs|, |outputs|)</dfn> method steps are:
1020
-
</summary>
1021
-
<div class=algorithm-steps>
1022
-
1. If |graph|.{{MLGraph/[[context]]}}.{{MLContext/[[contextType]]}} is not "[=context type/default=]", [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
1023
-
1. If [=validating graph resources=] given |inputs| and |graph|.{{MLGraph/[[inputDescriptors]]}} returns false, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
1024
-
1. If [=validating graph resources=] given |outputs| and |graph|.{{MLGraph/[[outputDescriptors]]}} returns false, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
1025
-
1. Invoke [=execute graph=] given |graph|, |inputs| and |outputs|.
1026
-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
1027
-
1. Return {{undefined}}.
1028
-
</div>
1029
-
</details>
1030
-
1031
953
<details open algorithm>
1032
954
<summary>
1033
955
To <dfn>validate graph resources</dfn>, given {{MLNamedArrayBufferViews}} |resources| and [=ordered map=] |descriptors|, run the following steps:
// Compile the graph up to the specified output operands asynchronously.
1277
1159
Promise<MLGraph> build(MLNamedOperands outputs);
1278
-
1279
-
// Compile the graph up to the specified output operands synchronously.
1280
-
[Exposed=(DedicatedWorker)]
1281
-
MLGraph buildSync(MLNamedOperands outputs);
1282
1160
};
1283
1161
</script>
1284
1162
1285
1163
<div class="note">
1286
-
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 {{MLContext/[[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. It typically involves a process known as "weight preprocessing" where all the constant inputs to the graph are preprocessed and cached at the operating system level for subsequent graph execution calls. The initializing inputs are typically the constant weight data specified through the {{MLGraphBuilder/constant(descriptor, bufferView)|MLGraphBuilder/constant(value, type)}} method as constant operands during graph construction time.
1164
+
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 {{MLContext/[[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. It typically involves a process known as "weight preprocessing" where all the constant inputs to the graph are preprocessed and cached at the operating system level for subsequent graph execution calls. The initializing inputs are typically the constant weight data specified through the {{MLGraphBuilder/constant(descriptor, bufferView)|MLGraphBuilder/constant(value, type)}} method as constant operands during graph construction time.
1287
1165
1288
1166
Issue(552): Decide how to specify graph initialization.
0 commit comments