Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify destroy() methods and behavior around context loss and error reporting #744

Merged
merged 32 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
aa2a8b8
Specify behavior around context loss and error reporting.
inexorabletash Jul 29, 2024
7456eff
Add link for 'settled'
inexorabletash Jul 29, 2024
01cf420
Add 'is lost' check to build()
inexorabletash Jul 29, 2024
1149c8d
Add 'is not lost' dfn alias
inexorabletash Jul 29, 2024
d2ec3f4
Add "can (not) build" defn, fix throwing vs. rejecting for methods
inexorabletash Jul 29, 2024
057280f
Merge branch 'refs/heads/review' into context-lost
inexorabletash Jul 30, 2024
e4fa318
Merge branch 'refs/heads/review' into context-lost
inexorabletash Jul 30, 2024
68c5b97
Merge branch 'refs/heads/review' into context-lost
inexorabletash Aug 6, 2024
61ff07d
Make build() and compute() explicitly invoke context lost steps
inexorabletash Aug 7, 2024
164d3e4
reword to reduce some indenting
inexorabletash Aug 7, 2024
8f5decf
restore missing algorithm class
inexorabletash Aug 7, 2024
e7a718d
Merge branch 'refs/heads/draft' into context-lost
inexorabletash Nov 5, 2024
62e0c74
Merge branch 'refs/heads/draft' into context-lost
inexorabletash Nov 8, 2024
ae81da2
Merge branch 'refs/heads/draft' into context-lost
inexorabletash Nov 11, 2024
a3ba635
Merge branch 'refs/heads/draft' into context-lost
inexorabletash Nov 15, 2024
c4e0cbe
Merge branch 'refs/heads/draft' into context-lost
inexorabletash Nov 19, 2024
801b892
Merge branch 'refs/heads/draft' into context-lost
inexorabletash Nov 25, 2024
edfadba
Merge branch 'refs/heads/draft' into context-lost
inexorabletash Dec 2, 2024
d150a70
Merge branch 'refs/heads/draft' into context-lost
inexorabletash Dec 3, 2024
a3915c8
Merge branch 'refs/heads/draft' into context-lost
inexorabletash Dec 4, 2024
4f6e751
Specify various destroy() methods
inexorabletash Jan 8, 2025
de1d618
Lint/wording fix
inexorabletash Jan 8, 2025
ecb96d0
whitespace change
inexorabletash Jan 8, 2025
55fc7ed
another whitespace change
inexorabletash Jan 9, 2025
e6d9f27
Move destroy of dependencies to 'lost' steps and other feedback
inexorabletash Jan 10, 2025
cf5a95c
Abort steps / reject promises when context is lost
inexorabletash Jan 10, 2025
a9b8643
Separate out lost/lose steps, make dispatch() use abort-when
inexorabletash Jan 10, 2025
0fc4151
Incorporate more feedback, another lint rule
inexorabletash Jan 13, 2025
95c3b26
Include tensor creation in MLContext/destroy() note
inexorabletash Jan 13, 2025
88e8d58
Move async 'abort when'/'if aborted' into substeps
inexorabletash Jan 13, 2025
5c92d97
Update index.bs
inexorabletash Jan 14, 2025
f59890c
Add suggested wording by @reillyeon for freeing graph resources on co…
inexorabletash Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ The <dfn dfn-for=MLContextOptions dfn-type=dict-member>powerPreference</dfn> opt
1. Set |context|.{{MLContext/[[powerPreference]]}} to {{MLPowerPreference/"default"}}.
1. Otherwise:
1. Set |context|.{{MLContext/[[contextType]]}} to "[=context type/default=]".
1. Set |context|.{{MLContext/[[lost]]}} to [=a new promise=].
1. If |options|["{{MLContextOptions/deviceType}}"] [=map/exists=], then set |context|.{{MLContext/[[deviceType]]}} to |options|["{{MLContextOptions/deviceType}}"]. Otherwise, set |context|.{{MLContext/[[deviceType]]}} to {{MLDeviceType/"cpu"}}.
1. If |options|["{{MLContextOptions/powerPreference}}"] [=map/exists=], then set |context|.{{MLContext/[[powerPreference]]}} to |options|["{{MLContextOptions/powerPreference}}"]. Otherwise, set |context|.{{MLContext/[[powerPreference]]}} to {{MLPowerPreference/"default"}}.
1. If the user agent cannot support |context|.{{MLContext/[[contextType]]}}, |context|.{{MLContext/[[deviceType]]}} and |context|.{{MLContext/[[powerPreference]]}}, return failure.
Expand Down Expand Up @@ -862,10 +863,16 @@ dictionary MLComputeResult {
MLNamedArrayBufferViews outputs;
};

dictionary MLContextLostInfo {
DOMString message;
};

[SecureContext, Exposed=(Window, DedicatedWorker)]
interface MLContext {
Promise<MLComputeResult> compute(
MLGraph graph, MLNamedArrayBufferViews inputs, MLNamedArrayBufferViews outputs);

readonly attribute Promise<MLContextLostInfo> lost;
};
</script>

Expand All @@ -881,6 +888,9 @@ interface MLContext {
: <dfn>\[[powerPreference]]</dfn> of type {{MLPowerPreference}}.
::
The {{MLContext}}'s {{MLPowerPreference}}.
: <dfn>\[[lost]]</dfn> of type {{Promise}}<{{MLContextLostInfo}}>.
::
A {{Promise}} that is resolved when the {{MLContext}}'s underlying execution device is no longer available.
</dl>
</div>

Expand Down Expand Up @@ -978,6 +988,7 @@ Note: Invocations of {{MLContext/compute()}} will fail if any of the {{MLContext
</summary>
1. Let |global| be [=this=]'s [=relevant global object=].
1. Let |realm| be [=this=]'s [=relevant realm=].
1. If [=this=] [=MLContext/is lost=], then [=exception/throw=] an "{{InvalidStateError}}" {{DOMException}}.
1. If |graph|.{{MLGraph/[[context]]}} is not [=this=], then return [=a new promise=] [=rejected=] with a {{TypeError}}.
1. If |graph|.{{MLGraph/[[context]]}}.{{MLContext/[[contextType]]}} is not "[=context type/default=]", then return [=a new promise=] [=rejected=] with an "{{OperationError}}" {{DOMException}}.
1. [=map/For each=] |name| → |descriptor| of |graph|.{{MLGraph/[[inputDescriptors]]}}:
Expand Down Expand Up @@ -1039,6 +1050,31 @@ Note: Invocations of {{MLContext/compute()}} will fail if any of the {{MLContext
</details>
</div>

### Errors ### {#api-mlcontext-errors}

<details open algorithm="context-lost">
<summary>
When an {{MLContext}} |context| is no longer available to fulfill requests, run the following steps:
</summary>
1. Let |global| be |context|'s [=relevant global object=].
1. [=Queue an ML task=] with |global| to run these steps:
1. Let |info| be a new {{MLContextLostInfo}}.
1. Set |info|.{{MLContextLostInfo/message}} to an [=implementation-defined=] value.
1. [=Resolve=] |context|.{{MLContext/[[lost]]}} with |info|.
inexorabletash marked this conversation as resolved.
Show resolved Hide resolved
</details>

<dl dfn-type=dict-member dfn-for=MLContextLostInfo>
: <dfn>message</dfn>
:: An [=implementation-defined=] message providing information about the error that occurred.
</dl>

<div algorithm>
The <dfn attribute for=MLContext>lost</dfn> getter steps are to return [=this=]'s {{MLContext/[[lost]]}} {{Promise}}.
</div>

A {{MLContext}} <dfn for=MLContext>is lost</dfn> if its {{MLContext/[[lost]]}} {{Promise}} is settled.
inexorabletash marked this conversation as resolved.
Show resolved Hide resolved


## {{MLGraph}} interface ## {#api-mlgraph}
The {{MLGraph}} interface represents a compiled computational graph. A compiled graph once constructed is immutable and cannot be subsequently changed.

Expand Down Expand Up @@ -1309,6 +1345,7 @@ The {{MLGraphBuilder}}.{{MLGraphBuilder/build()}} method compiles the graph buil
The [=new=] <dfn constructor for=MLGraphBuilder lt="MLGraphBuilder(context)">MLGraphBuilder(|context|)</dfn> constructor steps are:
</summary>
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}}.
1. If |context| [=MLContext/is lost=], then [=exception/throw=] an "{{InvalidStateError}}" {{DOMException}}.
inexorabletash marked this conversation as resolved.
Show resolved Hide resolved
1. Set [=this=].{{MLGraphBuilder/[[context]]}} to |context|.
1. Set [=this=].{{MLGraphBuilder/[[hasBuilt]]}} to false.
</details>
Expand Down
4 changes: 4 additions & 0 deletions tools/lint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ for (const algorithm of root.querySelectorAll('.algorithm')) {
// Lambda argument declarations
// e.g. "Let validationSteps given MLOperandDescriptor descriptor be..."
seen.add(name);
} else if (new RegExp('^When .* \\b' + name + '\\b').test(text)) {
// Reactive argument declarations
// e.g. "When something bad happens to MLContext context, ..."
seen.add(name);
} else if (!seen.has(name)) {
error(`Uninitialized variable "${name}" in "${algorithm.getAttribute('data-algorithm')}": ${text}`);
seen.add(name);
Expand Down