From 306745ec127647b351e0a4bc990445dd5ee1fda7 Mon Sep 17 00:00:00 2001 From: Ningxin Hu Date: Sun, 28 Aug 2022 17:11:34 +0800 Subject: [PATCH] Introduce MLContext.createContextAsync This PR also restricts the sync version of create context to worker. Fix #272 --- index.bs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/index.bs b/index.bs index ff9e38b1..45ae25b6 100644 --- a/index.bs +++ b/index.bs @@ -563,8 +563,13 @@ dictionary MLContextOptions { [SecureContext, Exposed=(Window, DedicatedWorker)] interface ML { + [Exposed=(DedicatedWorker)] MLContext createContext(optional MLContextOptions options = {}); + [Exposed=(DedicatedWorker)] MLContext createContext(GPUDevice gpuDevice); + + Promise createContextAsync(optional MLContextOptions options = {}); + Promise createContextAsync(GPUDevice gpuDevice); }; @@ -583,8 +588,36 @@ The {{ML/createContext()}} method steps are:
Set |context|.{{[[deviceType]]}} to "[=device-type-gpu|gpu=]".
Set |context|.{{[[powerPreference]]}} to "[=power-preference-default|default=]". +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. Else: + 1. Throw an {{NotSupportedError}} {{DOMException}} and stop. 1. Return |context|. +The {{ML/createContextAsync()}} 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 @@ -645,6 +678,9 @@ interface MLContext {}; : \[[powerPreference]] of type [=power preference=] :: The {{MLContext}}'s [=power preference=]. + : \[[implementation]] + :: + The underlying implementation provided by the User Agent.
@@ -2313,7 +2349,7 @@ partial interface MLGraphBuilder { efficient implementation for it, therefore its usage is encouraged from the performance standpoint.
-    return builder.div(x, builder.add(builder.constant(1), build.abs(x)));
+    return builder.div(x, builder.add(builder.constant(1), builder.abs(x)));