Skip to content

Commit bcc5dc2

Browse files
authored
Merge pull request #790 from huningxin/constant_supports_shared
Use `AllowSharedBufferSource` for `MLGraphBuilder.constant()`
2 parents dd5cbdc + 0bd2d0a commit bcc5dc2

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

index.bs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,8 @@ interface MLGraphBuilder {
16891689
MLOperand input(USVString name, MLOperandDescriptor descriptor);
16901690

16911691
// Create an operand for a graph constant.
1692-
MLOperand constant(MLOperandDescriptor descriptor, ArrayBufferView bufferView);
1692+
MLOperand constant(MLOperandDescriptor descriptor,
1693+
AllowSharedBufferSource buffer);
16931694

16941695
// Create a scalar operand from the specified number of the specified type.
16951696
MLOperand constant(MLOperandDataType type, MLNumber value);
@@ -1700,7 +1701,7 @@ interface MLGraphBuilder {
17001701
</script>
17011702

17021703
<div class="note">
1703-
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)|constant()}} method as constant operands during graph construction time.
1704+
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, buffer)|constant()}} method as constant operands during graph construction time.
17041705
</div>
17051706

17061707
<div class=internal-slots>
@@ -1764,26 +1765,26 @@ Create a named {{MLOperand}} based on a descriptor, that can be used as an input
17641765
### constant operands ### {#api-mlgraphbuilder-constant}
17651766
Create a constant {{MLOperand}} that can be used in {{MLGraphBuilder}} methods.
17661767

1767-
#### {{MLGraphBuilder/constant(descriptor, bufferView)}} #### {#api-mlgraphbuilder-constant-bufferview}
1768+
#### {{MLGraphBuilder/constant(descriptor, buffer)}} #### {#api-mlgraphbuilder-constant-buffer}
17681769
Create a constant {{MLOperand}} of the specified data type and shape that contains the initializing data.
17691770

1770-
<div dfn-for="MLGraphBuilder/constant(descriptor, bufferView)" dfn-type=argument>
1771+
<div dfn-for="MLGraphBuilder/constant(descriptor, buffer)" dfn-type=argument>
17711772
**Arguments:**
17721773
- <dfn>descriptor</dfn>: an {{MLOperandDescriptor}}. The descriptor of the output tensor.
1773-
- <dfn>bufferView</dfn>: an {{ArrayBufferView}}. The view of the buffer containing the initializing data.
1774+
- <dfn>buffer</dfn>: an {{AllowSharedBufferSource}}. The buffer containing the initializing data.
17741775
**Returns:** an {{MLOperand}}. The constant output tensor.
17751776
</div>
17761777

17771778
<details open algorithm>
17781779
<summary>
1779-
The <dfn method for=MLGraphBuilder>constant(|descriptor|, |bufferView|)</dfn> method steps are:
1780+
The <dfn method for=MLGraphBuilder>constant(|descriptor|, |buffer|)</dfn> method steps are:
17801781
</summary>
17811782
1. If [=this=].{{MLGraphBuilder/[[hasBuilt]]}} is true, then [=exception/throw=] an "{{InvalidStateError}}" {{DOMException}}.
17821783
1. If [=MLOperandDescriptor/checking dimensions=] given |descriptor| returns false, then [=exception/throw=] a {{TypeError}}.
1783-
1. If [=validating buffer with descriptor=] given |bufferView| and |descriptor| returns false, then [=exception/throw=] a {{TypeError}}.
1784+
1. If [=validating buffer with descriptor=] given |buffer| and |descriptor| returns false, then [=exception/throw=] a {{TypeError}}.
17841785
1. *Make graph connections:*
17851786
1. Let |operand| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
1786-
1. Let |bytes| be the result of [=getting a copy of the bytes held by the buffer source=] given |bufferView|.
1787+
1. Let |bytes| be the result of [=getting a copy of the bytes held by the buffer source=] given |buffer|.
17871788
1. Add |operand| to [=this=]'s [=MLGraphBuilder/graph=]'s [=computational graph/constants=] with |bytes| as value.
17881789
1. Return |operand|.
17891790
</details>
@@ -1851,9 +1852,6 @@ Build a composed graph up to a given output operand into a computational graph a
18511852
1. Set |graph|.{{MLGraph/[[context]]}} to [=this=].{{MLGraphBuilder/[[context]]}}.
18521853
1. [=set/For each=] |operand| in |inputs|:
18531854
1. Set |graph|.{{MLGraph/[[inputDescriptors]]}}[|operand|.{{MLOperand/[[name]]}}] to |operand|.{{MLOperand/[[descriptor]]}}.
1854-
1855-
Issue(566): If {{MLGraphBuilder/constant(descriptor, bufferView)|constants'}} {{ArrayBuffer}}s are not [=ArrayBuffer/transferred=], make copies for [=MLGraphBuilder/graph=]'s [=computational graph/constants=] here.
1856-
18571855
1. [=map/For each=] |name| → |operand| of |outputs|:
18581856
1. Set |graph|.{{MLGraph/[[outputDescriptors]]}}[|name|] to |operand|.{{MLOperand/[[descriptor]]}}.
18591857
1. Let |promise| be [=a new promise=].
@@ -2380,8 +2378,7 @@ partial dictionary MLOpSupportLimits {
23802378
input, builder.constant(input.dataType, options.minValue));
23812379
} else {
23822380
return builder.min(
2383-
builder.max(
2384-
input, builder.constant(input.dataType, options.minValue)),
2381+
builder.max(input, builder.constant(input.dataType, options.minValue)),
23852382
builder.constant(input.dataType, options.maxValue));
23862383
}
23872384
}
@@ -3834,8 +3831,8 @@ partial dictionary MLOpSupportLimits {
38343831
{shape: [4, 3]},
38353832
new Float32Array([0, 1, 2, 10, 11, 12, 20, 21, 22, 30, 31, 32]));
38363833

3837-
const indices1 = builder.constant(
3838-
{dataType: 'uint32', shape: [2]}, new Uint32Array([3, 1]));
3834+
const indices1 =
3835+
builder.constant({dataType: 'uint32', shape: [2]}, new Uint32Array([3, 1]));
38393836

38403837
const indices2 = builder.constant(
38413838
{dataType: 'uint32', shape: [3]}, new Uint32Array([2, 1, 1]));
@@ -4370,10 +4367,7 @@ partial dictionary MLOpSupportLimits {
43704367
let hiddenState = options.initialHiddenState;
43714368

43724369
if (!hiddenState) {
4373-
const desc = {
4374-
dataType: 'float32',
4375-
shape: [numDirections, 1, hiddenSize]
4376-
};
4370+
const desc = {dataType: 'float32', shape: [numDirections, 1, hiddenSize]};
43774371
const totalSize = numDirections * hiddenSize;
43784372
hiddenState = builder.constant(desc, new Float32Array(totalSize).fill(0));
43794373
}
@@ -5072,8 +5066,7 @@ partial dictionary MLOpSupportLimits {
50725066
const reduceOptions = {axes: [2, 3], keepDimensions: true};
50735067
const mean = builder.reduceMean(input, reduceOptions);
50745068
const variance = builder.reduceMean(
5075-
builder.pow(
5076-
builder.sub(input, mean), builder.constant(input.dataType, 2)),
5069+
builder.pow(builder.sub(input, mean), builder.constant(input.dataType, 2)),
50775070
reduceOptions);
50785071

50795072
// The scale and bias values are applied per input feature
@@ -5223,8 +5216,7 @@ partial dictionary MLOpSupportLimits {
52235216
const reduceOptions = {axes: [1, 2, 3], keepDimensions: true};
52245217
const mean = builder.reduceMean(input, reduceOptions);
52255218
const variance = builder.reduceMean(
5226-
builder.pow(
5227-
builder.sub(input, mean), builder.constant(input.dataType, 2)),
5219+
builder.pow(builder.sub(input, mean), builder.constant(input.dataType, 2)),
52285220
reduceOptions);
52295221

52305222
// The scale and bias tensors are of the shape of the input
@@ -5705,19 +5697,13 @@ partial dictionary MLOpSupportLimits {
57055697
let cellState = options.initialCellState;
57065698

57075699
if (!hiddenState) {
5708-
const desc = {
5709-
dataType: 'float32',
5710-
shape: [numDirections, 1, hiddenSize]
5711-
};
5700+
const desc = {dataType: 'float32', shape: [numDirections, 1, hiddenSize]};
57125701
const totalSize = numDirections * hiddenSize;
57135702
hiddenState = builder.constant(desc, new Float32Array(totalSize).fill(0));
57145703
}
57155704

57165705
if (!cellState) {
5717-
const desc = {
5718-
dataType: 'float32',
5719-
shape: [numDirections, 1, hiddenSize]
5720-
};
5706+
const desc = {dataType: 'float32', shape: [numDirections, 1, hiddenSize]};
57215707
const totalSize = numDirections * hiddenSize;
57225708
cellState = builder.constant(desc, new Float32Array(totalSize).fill(0));
57235709
}
@@ -6381,8 +6367,7 @@ partial dictionary MLOpSupportLimits {
63816367
<pre highlight="js">
63826368
// input: [[1,2,3], [4,5,6]]
63836369
const input = builder.constant(
6384-
{dataType: 'float32', shape: [2, 3]},
6385-
new Float32Array([1, 2, 3, 4, 5, 6]));
6370+
{dataType: 'float32', shape: [2, 3]}, new Float32Array([1, 2, 3, 4, 5, 6]));
63866371

63876372
const beginningPadding = [1, 2];
63886373
const endingPadding = [1, 2];

0 commit comments

Comments
 (0)