Skip to content

Commit f9cb8b4

Browse files
committed
Add a simple accelerator selection mechanism.
Signed-off-by: Zoltan Kis <[email protected]>
1 parent 0f6cae2 commit f9cb8b4

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

index.bs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ An {{MLContext}} interface represents a global state of neural network execution
748748
In a situation when a GPU context executes a graph with a constant or an input in the system memory as an {{ArrayBufferView}}, the input content is automatically uploaded from the system memory to the GPU memory, and downloaded back to the system memory of an {{ArrayBufferView}} output buffer at the end of the graph execution. This data upload and download cycles will only occur whenever the execution device requires the data to be copied out of and back into the system memory, such as in the case of the GPU. It doesn't occur when the device is a CPU device. Additionally, the result of the graph execution is in a known layout format. While the execution may be optimized for a native memory access pattern in an intermediate result within the graph, the output of the last operation of the graph must convert the content back to a known layout format at the end of the graph in order to maintain the expected behavior from the caller's perspective.
749749

750750
<div class="note">
751-
When an {{MLContext}} is created with {{MLContextOptions}}, the user agent selects and creates the underlying execution device by taking into account these options, currently only the {{MLPowerPreference}} option.
751+
When an {{MLContext}} is created with {{MLContextOptions}}, the user agent selects and creates the underlying execution device by taking into account these options.
752752

753753
Depending on the underlying platform, the user agent <span class=allow-2119>may</span> select different combinations of CPU, NPU and GPU devices.
754754
</div>
@@ -978,6 +978,7 @@ enum MLPowerPreference {
978978

979979
dictionary MLContextOptions {
980980
MLPowerPreference powerPreference = "default";
981+
boolean accelerated = true;
981982
};
982983

983984
[SecureContext, Exposed=(Window, Worker)]
@@ -1001,6 +1002,8 @@ The <dfn dfn-for=MLContextOptions dfn-type=dict-member>powerPreference</dfn> opt
10011002
<dd>Prioritizes power consumption over other considerations such as execution speed.</dd>
10021003
</dl>
10031004

1005+
The <dfn dfn-for=MLContextOptions dfn-type=dict-member>accelerated</dfn> option indicates the application's preference as related to massively parallel acceleration. When set to `true` (by default), the underlying platform will attempt to use the available massively parallel accelerators, such as GPU or NPU, also depending on the {{MLContextOptions/powerPreference}}. When set to `false`, the application hints to prefer CPU inference.
1006+
10041007
### {{ML/createContext()}} ### {#api-ml-createcontext}
10051008

10061009
<div dfn-for="ML/createContext(options), ML/createContext(gpuDevice)" dfn-type=argument>
@@ -1018,11 +1021,16 @@ The <dfn dfn-for=MLContextOptions dfn-type=dict-member>powerPreference</dfn> opt
10181021
1. If |options| is a {{GPUDevice}} object, then:
10191022
1. Set |context|.{{MLContext/[[contextType]]}} to "[=context type/webgpu=]".
10201023
1. Set |context|.{{MLContext/[[powerPreference]]}} to {{MLPowerPreference/"default"}}.
1024+
1. Set |context|.{{MLContext/[[accelerated]]}} to `true`.
1025+
1. Set |context|.{{MLContext/[[cpuFallbackActive]]}} to `false`.
10211026
1. Otherwise:
10221027
1. Set |context|.{{MLContext/[[contextType]]}} to "[=context type/default=]".
10231028
1. Set |context|.{{MLContext/[[lost]]}} to [=a new promise=] in |realm|.
10241029
1. If |options|["{{MLContextOptions/powerPreference}}"] [=map/exists=], then set |context|.{{MLContext/[[powerPreference]]}} to |options|["{{MLContextOptions/powerPreference}}"].
10251030
1. Otherwise, set |context|.{{MLContext/[[powerPreference]]}} to {{MLPowerPreference/"default"}}.
1031+
1. If |options|["{{MLContextOptions/accelerated}}"] [=map/exists=], then set |context|.{{MLContext/[[accelerated]]}} to |options|["{{MLContextOptions/accelerated}}"].
1032+
1. Otherwise, set |context|.{{MLContext/[[accelerated]]}} to `true`.
1033+
1. Set |context|.{{MLContext/[[cpuFallbackActive]]}} to `false`.
10261034
1. If the user agent cannot support |context|.{{MLContext/[[contextType]]}}, then return failure.
10271035
1. Return |context|.
10281036
</details>
@@ -1082,6 +1090,8 @@ interface MLContext {
10821090

10831091
undefined destroy();
10841092

1093+
readonly attribute boolean accelerated;
1094+
readonly attribute boolean cpuFallbackActive;
10851095
readonly attribute Promise<MLContextLostInfo> lost;
10861096
};
10871097
</script>
@@ -1095,6 +1105,12 @@ interface MLContext {
10951105
: <dfn>\[[powerPreference]]</dfn> of type {{MLPowerPreference}}.
10961106
::
10971107
The {{MLContext}}'s {{MLPowerPreference}}.
1108+
: <dfn>\[[accelerated]]</dfn> of type {{boolean}}.
1109+
::
1110+
The {{MLContext}}'s processing type (CPU or massively parallel processing).
1111+
: <dfn>\[[cpuFallbackActive]]</dfn> of type {{boolean}}.
1112+
::
1113+
The {{MLContext}}'s status for CPU fallback type (CPU or massively parallel processing).
10981114
: <dfn>\[[lost]]</dfn> of type {{Promise}}<{{MLContextLostInfo}}>.
10991115
::
11001116
A {{Promise}} that is resolved when the {{MLContext}}'s underlying execution device is no longer available.
@@ -1178,7 +1194,8 @@ Note: `dispatch()` itself provides no signal that graph execution has completed.
11781194
1. If [=validating tensors with descriptors=] given |outputs| and |graph|.{{MLGraph/[[outputDescriptors]]}} returns false, then [=exception/throw=] a {{TypeError}}.
11791195
1. Enqueue the following steps to |graph|.{{MLGraph/[[context]]}}.{{MLContext/[[timeline]]}}:
11801196
1. Run these steps, but [=/abort when=] [=this=] [=MLContext/is lost=]:
1181-
1. Issue a compute request to |graph|.{{MLGraph/[[implementation]]}} given |inputs| and |outputs|.
1197+
1. Issue a compute request to |graph|.{{MLGraph/[[implementation]]}} given |inputs| and |outputs|, as well as |graph|.{{MLGraph/[[context]]}}.{{MLContext/[[powerPreference]]}} and |graph|.{{MLGraph/[[context]]}}.{{MLContext/[[accelerated]]}}.
1198+
1. If |graph|.{{MLGraph/[[context]]}}.{{MLContext/[[accelerated]]}} is `true` and the underlying platform can only do CPU inference at the moment, then set |graph|.{{MLGraph/[[context]]}}.{{MLContext/[[cpuFallbackActive]]}} to `true`, otherwise set it to `false`.
11821199

11831200
Issue(778): Add a mechanism for reporting errors during graph execution.
11841201

@@ -1730,7 +1747,7 @@ typedef (bigint or unrestricted double) MLNumber;
17301747
: <dfn>\[[operator]]</dfn> of type [=operator=]
17311748
::
17321749
Reference to {{MLOperand}}'s corresponding [=operator=].
1733-
1750+
17341751
: <dfn>\[[constantTensor]]</dfn> of type {{MLTensor}}
17351752
::
17361753
The {{MLOperand}}'s tensor (only for constant operands).
@@ -2151,7 +2168,7 @@ Build a composed graph up to a given output operand into a computational graph a
21512168
1. If |name| is empty, then return [=a new promise=] in |realm| [=rejected=] with a {{TypeError}}.
21522169
1. If [=MLGraphBuilder/validating operand=] given [=this=] and |operand| returns false, then return [=a new promise=] in |realm| [=rejected=] with a {{TypeError}}.
21532170
1. If |operand| is in [=this=]'s [=MLGraphBuilder/graph=]'s [=computational graph/inputs=] or [=computational graph/constants=], then return [=a new promise=] in |realm| [=rejected=] with a {{TypeError}}.
2154-
1. If |operand|.{{MLOperand/[[constantTensor]]}} exists and |operand|.{{MLOperand/[[constantTensor]]}}.{{MLTensor/[[isDestroyed]]}} is true, then return [=a new promise=] in |realm| [=rejected=] with a {{TypeError}}.
2171+
1. If |operand|.{{MLOperand/[[constantTensor]]}} exists and |operand|.{{MLOperand/[[constantTensor]]}}.{{MLTensor/[[isDestroyed]]}} is true, then return [=a new promise=] in |realm| [=rejected=] with a {{TypeError}}.
21552172
1. Let |operands| be a new empty [=/set=].
21562173
1. Let |operators| be a new empty [=/set=].
21572174
1. Let |inputs| be a new empty [=/set=].

0 commit comments

Comments
 (0)