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
As noted in the issue, sharing weights between graphs is a effective way
to enable reuse of device buffers, allowing web developers to manage
weight data in graphs similarly to other input or output buffers.
Resolves#760
- <dfn>descriptor</dfn>: an {{MLOperandDescriptor}}.
1077
+
- <dfn>inputData</dfn>: an {{AllowSharedBufferSource}}. The buffer whose bytes will be written into the tensor.
1078
+
1079
+
**Returns:** {{Promise}}<{{MLConstantTensor}}>.
1080
+
</div>
1081
+
1082
+
<details open algorithm>
1083
+
<summary>
1084
+
The <dfn method for=MLContext>createConstantTensor(|descriptor|, |inputData|)</dfn> method steps are:
1085
+
</summary>
1086
+
1. Let |global| be [=this=]'s [=relevant global object=].
1087
+
1. Let |realm| be [=this=]'s [=relevant realm=].
1088
+
1. If [=this=] [=MLContext/is lost=], then return [=a new promise=] in |realm| [=rejected=] with an "{{InvalidStateError}}" {{DOMException}}.
1089
+
1. If [=MLOperandDescriptor/checking dimensions=] given |descriptor| returns false, then [=exception/throw=] a {{TypeError}}.
1090
+
1. If [=validating buffer with descriptor=] given |inputData| and |descriptor| returns false, then return [=a new promise=] in |realm| [=rejected=] with a {{TypeError}}.
1091
+
1. Let |bytes| be the result of [=getting a copy of the bytes held by the buffer source=] given |inputData|.
1092
+
1. [=Assert=]: |bytes|'s [=byte sequence/length=] is equal to |descriptor|'s [=MLOperandDescriptor/byte length=].
1093
+
1. Let |tensor| be the result of [=creating an MLConstantTensor=] given [=this=], and |descriptor|.
1094
+
1. Let |promise| be [=a new promise=] in |realm|.
1095
+
1. Enqueue the following steps to [=this=].{{MLContext/[[timeline]]}}:
1096
+
1. Run these steps, but [=/abort when=] [=this=] [=MLContext/is lost=]:
1097
+
1. Create |tensor|.{{MLConstantTensor/[[data]]}} given |descriptor| and initialize all bytes to zeros.
1098
+
1. Copy |bytes| to |tensor|.{{MLConstantTensor/[[data]]}}.
1099
+
1. If that fails, then [=queue an ML task=] with |global| to [=reject=] |promise| with an "{{UnknownError}}" {{DOMException}}, and abort these steps.
1100
+
1. Otherwise, [=queue an ML task=] with |global| to [=resolve=] |promise| with |tensor|.
1101
+
1. [=/If aborted=], then [=queue an ML task=] with |global| to [=reject=] |promise| with an "{{InvalidStateError}}" {{DOMException}}.
Reads back the {{MLTensor/[[data]]}} of an {{MLTensor}} from the {{MLContext}}.{{MLContext/[[timeline]]}} to script.
@@ -1627,6 +1664,90 @@ Releases the resources associated with the {{MLTensor}}. This method is idempote
1627
1664
1628
1665
Note: Since no further operations can be enqueued using this tensor, implementations can free any additional resource allocations associated with this tensor once all previously submitted operations using it are complete.
The {{MLConstantTensor}} interface represents a tensor which may be used as a constant to an {{MLGraph}}. The memory backing an {{MLConstantTensor}} should be allocated in an [=implementation-defined=] fashion according to the requirements of the {{MLContext}} and the {{MLOperandDescriptor}} used to create it. Operations involving the {{MLConstantTensor/[[data]]}} of an {{MLConstantTensor}} occur on the {{MLContext/[[timeline]]}} of its associated {{MLContext}}.
1670
+
1671
+
The [=implementation-defined=] requirements of how an {{MLConstantTensor}} is allocated may include constraints such as that the memory is allocated with a particular byte alignment or in a particular memory pool.
1672
+
1673
+
<script type=idl>
1674
+
[SecureContext, Exposed=(Window, Worker)]
1675
+
interface MLConstantTensor : MLTensor {
1676
+
};
1677
+
</script>
1678
+
1679
+
<div class=internal-slots>
1680
+
{{MLConstantTensor}} has the following internal slots:
: <dfn>\[[descriptor]]</dfn> of type {{MLOperandDescriptor}}
1687
+
::
1688
+
The {{MLConstantTensor}}'s descriptor.
1689
+
1690
+
: <dfn>\[[isDestroyed]]</dfn> of type {{boolean}}
1691
+
::
1692
+
Whether the {{MLConstantTensor}}.{{MLConstantTensor/destroy()}} steps have been run. Once destroyed, the {{MLConstantTensor}} can no longer be used.
1693
+
1694
+
: <dfn>\[[data]]</dfn> of an [=implementation-defined=] type
1695
+
::
1696
+
The bytes backing the {{MLConstantTensor}}. This data may only be accessed or modified from the {{MLConstantTensor/[[context]]}}.{{MLContext/[[timeline]]}}.
1697
+
</dl>
1698
+
</div>
1699
+
1700
+
An {{MLConstantTensor}}'s <dfn for=MLConstantTensor>dataType</dfn> is its {{MLConstantTensor/[[descriptor]]}}'s {{MLOperandDescriptor/dataType}}.
1701
+
1702
+
An {{MLConstantTensor}}'s <dfn for=MLConstantTensor>shape</dfn> is its {{MLConstantTensor/[[descriptor]]}}'s {{MLOperandDescriptor/shape}}.
1703
+
1704
+
The <dfn attribute for=MLConstantTensor>dataType</dfn> [=getter steps=] are to return [=this=]'s [=MLConstantTensor/dataType=].
1705
+
1706
+
The <dfn attribute for=MLConstantTensor>shape</dfn> [=getter steps=] are to return [=this=]'s [=MLConstantTensor/shape=].
1707
+
1708
+
The <dfn attribute for=MLConstantTensor>readable</dfn> [=getter steps=] are to return true.
1709
+
1710
+
The <dfn attribute for=MLConstantTensor>writable</dfn> [=getter steps=] are to return false.
1711
+
1712
+
### Creating an {{MLConstantTensor}} ### {#api-mlconstanttensor-create}
1713
+
1714
+
An {{MLConstantTensor}} is created by its associated {{MLContext}}.
1715
+
1716
+
<details open algorithm>
1717
+
<summary>
1718
+
To <dfn>create an MLConstantTensor</dfn> given {{MLContext}} |context|, {{MLOperandDescriptor}} |descriptor|, and {{AllowSharedBufferSource}} |inputData|, run the following steps:
1719
+
</summary>
1720
+
1. Let |realm| be |context|'s [=relevant realm=].
1721
+
1. Let |tensor| be a new {{MLConstantTensor}} in |realm|.
1722
+
1. Set |tensor|.{{MLConstantTensor/[[context]]}} to |context|.
1723
+
1. Set |tensor|.{{MLConstantTensor/[[descriptor]]}} to |descriptor|.
1724
+
1. Set |tensor|.{{MLConstantTensor/[[data]]}} to |inputData|.
1725
+
1. Set |tensor|.{{MLConstantTensor/[[isDestroyed]]}} to false.
1726
+
1. Set |tensor|.{{MLConstantTensor/[[descriptor]]}}.{{MLTensorDescriptor/readable}} to true.
1727
+
1. Set |tensor|.{{MLConstantTensor/[[descriptor]]}}.{{MLTensorDescriptor/writable}} to false.
Note: Since no further operations can be enqueued using this tensor, implementations can free any additional resource allocations associated with this tensor once all previously submitted operations using it are complete.
The {{MLGraphBuilder}} interface defines a set of operations as identified by the [[#usecases]] that can be composed into a computational graph. It also represents the intermediate state of a graph building session.
@@ -1649,6 +1770,9 @@ interface MLGraphBuilder {
1649
1770
// Create a scalar operand from the specified number of the specified type.
0 commit comments