-
Notifications
You must be signed in to change notification settings - Fork 18
/
graph.ts
327 lines (300 loc) 路 10.8 KB
/
graph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import '@tensorflow/tfjs-backend-webgl';
import '@tensorflow/tfjs-backend-webgpu';
import '@tensorflow/tfjs-backend-cpu';
import '@tensorflow/tfjs-backend-wasm';
import * as tf from '@tensorflow/tfjs-core';
import {MLNamedOperands} from './graph_builder';
import {ConstantOperand, InputOperand, MLOperand, MLOperandDescriptor, OutputOperand} from './operand';
import {Operation} from './operation';
import {ArrayBufferView} from './types';
import * as utils from './utils';
/**
* [spec](https://webmachinelearning.github.io/webnn/#typedefdef-mlnamedarraybufferviews)
*/
export type MLNamedArrayBufferViews = Record<string, ArrayBufferView>;
/**
* [spec] https://webmachinelearning.github.io/webnn/#dictdef-mlcomputeresult
*/
export interface MLComputeResult {
inputs: MLNamedArrayBufferViews;
outputs: MLNamedArrayBufferViews;
}
/** @internal */
class OperandTensor {
ref: number;
tensor: tf.Tensor;
}
/** @internal */
export class ExecutionContext {
private constantTenosrs_: Map<ConstantOperand, tf.Tensor>;
private inputTensors_: Map<InputOperand, OperandTensor>;
private outputTensors_: Map<OutputOperand, OperandTensor>;
private operandRefs_: Map<MLOperand, number>;
private outputOperands_: Set<OutputOperand>;
constructor(
constantTensors: Map<ConstantOperand, tf.Tensor>,
inputOperands: Map<string, InputOperand>,
inputs: MLNamedArrayBufferViews,
operandRefs: Map<MLOperand, number>) {
this.constantTenosrs_ = constantTensors;
this.operandRefs_ = operandRefs;
this.allocateInputTensors(inputOperands, inputs);
this.outputTensors_ = new Map();
this.outputOperands_ = new Set();
}
private allocateInputTensors(
inputOperands: Map<string, InputOperand>,
inputs: MLNamedArrayBufferViews) {
this.inputTensors_ = new Map();
for (const inputName in inputs) {
const input = inputs[inputName];
const inputOperand = inputOperands.get(inputName);
const desc: MLOperandDescriptor = inputOperand.desc;
const resource = input;
this.inputTensors_.set(inputOperand, {
ref: this.operandRefs_.get(inputOperand),
tensor: utils.createTensor(desc, resource)
});
}
}
compute(outputs: Map<string, OutputOperand>): tf.TensorContainerObject {
for (const output of outputs.values()) {
this.outputOperands_.add(output);
}
const outputTensors: tf.TensorContainerObject = {};
for (const outputName of outputs.keys()) {
outputTensors[outputName] = this.getTensor(outputs.get(outputName));
}
return outputTensors;
}
setOutputTensor(output: OutputOperand, tensor: tf.Tensor): void {
utils.assert(
!this.outputTensors_.has(output), 'MLOutput already has tensor.');
this.outputTensors_.set(
output, {ref: this.operandRefs_.get(output), tensor});
}
releaseTensor(operand: MLOperand): void {
let operandTensorMap: Map<MLOperand, OperandTensor>;
if (operand instanceof InputOperand) {
operandTensorMap = this.inputTensors_;
} else if (operand instanceof OutputOperand) {
if (this.outputOperands_.has(operand)) {
return;
}
operandTensorMap = this.outputTensors_;
} else {
return;
}
const operandTensor: OperandTensor = operandTensorMap.get(operand);
utils.assert(operandTensor !== undefined, 'No tensor found for operand.');
operandTensor.ref--;
if (operandTensor.ref === 0) {
tf.dispose(operandTensor.tensor);
operandTensorMap.delete(operand);
}
}
getTensor(operand: MLOperand): tf.Tensor {
if (operand instanceof ConstantOperand) {
return this.constantTenosrs_.get(operand);
} else if (operand instanceof InputOperand) {
return this.inputTensors_.get(operand).tensor;
} else if (operand instanceof OutputOperand) {
if (this.outputTensors_.has(operand)) {
return this.outputTensors_.get(operand).tensor;
} else {
operand.operation.compute(this);
utils.assert(this.outputTensors_.has(operand), 'No output is set.');
return this.outputTensors_.get(operand).tensor;
}
} else {
throw new Error('The operand is invalid.');
}
}
}
/**
* [spec](https://webmachinelearning.github.io/webnn/#api-mlgraph)
*/
export class MLGraph {
private inputs_: Map<string, InputOperand> = new Map();
private outputs_: Map<string, OutputOperand> = new Map();
private constants_: Set<ConstantOperand> = new Set();
private operandRefs_: Map<MLOperand, number> = new Map();
private constantTensors_: Map<ConstantOperand, tf.Tensor> = new Map();
private validateInputs(inputs: MLNamedArrayBufferViews) {
for (const name in inputs) {
utils.assert(
typeof name === 'string' && this.inputs_.has(name),
'The name of the input is invalid.');
const inputOperand = this.inputs_.get(name);
const resource = inputs[name];
const dimensions = inputOperand.desc.dimensions;
utils.assert(
utils.isTypedArray(resource),
'Only resource of ArrayBufferView type is supported.');
utils.validateTypedArray(
resource, inputOperand.desc.dataType, dimensions);
}
}
private validateAndSetOutputOperands(outputs: MLNamedArrayBufferViews):
Map<string, OutputOperand> {
// Validate and filter the required output operands.
utils.assert(Object.keys(outputs).length !== 0,
'The outputs is invalid.');
const outputOperands = new Map();
for (const outputName in outputs) {
utils.assert(
typeof outputName === 'string' && this.outputs_.has(outputName),
'The name of the output is invalid.');
utils.assert(
utils.isTypedArray(outputs[outputName]),
'Only output of ArrayBufferView type is supported.');
outputOperands.set(outputName, this.outputs_.get(outputName));
}
return outputOperands;
}
private computeOutputTensors(
inputs: MLNamedArrayBufferViews = undefined,
outputs: MLNamedArrayBufferViews = undefined): tf.TensorContainerObject {
if (inputs) {
this.validateInputs(inputs);
} else {
inputs = {};
for (const inputName of this.inputs_.keys()) {
const inputOperand = this.inputs_.get(inputName);
const typedArrayConstructor =
utils.getTypedArray(inputOperand.desc.dataType);
const inputBuffer = new typedArrayConstructor(
utils.sizeFromDimensions(inputOperand.desc.dimensions));
inputs[inputName] = inputBuffer;
}
}
let outputOperands: Map<string, OutputOperand> = this.outputs_;
if (outputs) {
outputOperands = this.validateAndSetOutputOperands(outputs);
}
const outputTensors: tf.TensorContainerObject = tf.tidy(() => {
const context = new ExecutionContext(
this.constantTensors_, this.inputs_, inputs, this.operandRefs_);
// The input and immediate tensors will be cleaned up.
return context.compute(outputOperands);
});
return outputTensors;
}
/** @internal */
async compute(
inputs: MLNamedArrayBufferViews,
outputs: MLNamedArrayBufferViews): Promise<MLComputeResult> {
const outputTensors: tf.TensorContainerObject =
this.computeOutputTensors(inputs, outputs);
// Setup the outputs.
for (const outputName of Object.keys(outputTensors)) {
const tensor = outputTensors[outputName] as tf.Tensor;
const desc = utils.createOperandDescriptorFromTensor(tensor);
const resource = outputs[outputName] ;
utils.validateTypedArray(resource, desc.dataType, desc.dimensions);
resource.set(await tensor.data());
tf.dispose(tensor);
}
return {inputs, outputs};
}
/** @ignore */
constructor(outputs?: MLNamedOperands) {
utils.assert(outputs !== undefined, 'Invalid argument');
for (const name in outputs) {
utils.assert(
typeof name === 'string' && outputs[name] instanceof OutputOperand,
'The outputs parameter is invalid.');
this.outputs_.set(name, outputs[name] as OutputOperand);
}
utils.assert(this.outputs_.size !== 0, 'The outputs is empty');
}
/** @internal */
static async buildAndCompile(outputs?: MLNamedOperands): Promise<MLGraph> {
const graph = new MLGraph(outputs);
graph.build();
await graph.compile();
return graph;
}
private build(): void {
const visitedOps: Set<Operation> = new Set();
for (const output of this.outputs_.values()) {
this.buildOperation(output.operation, visitedOps);
}
}
private buildOperation(operation: Operation, visitedOps: Set<Operation>):
void {
if (visitedOps.has(operation)) {
return;
} else {
visitedOps.add(operation);
}
for (const operand of operation.inputs()) {
if (!this.operandRefs_.has(operand)) {
this.operandRefs_.set(operand, 1);
} else {
let ref = this.operandRefs_.get(operand);
ref++;
this.operandRefs_.set(operand, ref);
}
if (operand instanceof InputOperand) {
if (this.inputs_.has(operand.name)) {
if (this.inputs_.get(operand.name) !== operand) {
throw new Error('The name of this input is existed.');
} else {
continue;
}
}
this.inputs_.set(operand.name, operand);
} else if (operand instanceof ConstantOperand) {
if (!this.constants_.has(operand)) {
this.constants_.add(operand);
}
} else if (operand instanceof OutputOperand) {
this.buildOperation(operand.operation, visitedOps);
}
}
}
private async compile(): Promise<void> {
this.allocateConstants();
await this.computeOnce();
}
private allocateConstants(): void {
for (const constant of this.constants_) {
this.constantTensors_.set(
constant, utils.createTensor(constant.desc, constant.value));
}
}
private async computeOnce(): Promise<void> {
const outputTensors = this.computeOutputTensors();
for (const outputName of Object.keys(outputTensors)) {
const tensor = outputTensors[outputName] as tf.Tensor;
await tensor.data();
tf.dispose(tensor);
}
}
/** @ignore */
// For memory leak testing.
dispose(): void {
for (const tensor of this.constantTensors_.values()) {
tf.dispose(tensor);
}
const visitedOps: Set<Operation> = new Set();
for (const output of this.outputs_.values()) {
this.disposeOperation(output.operation, visitedOps);
}
}
private disposeOperation(operation: Operation, visitedOps: Set<Operation>):
void {
if (visitedOps.has(operation)) {
return;
} else {
operation.dispose();
visitedOps.add(operation);
}
for (const operand of operation.inputs()) {
if (operand instanceof OutputOperand) {
this.disposeOperation(operand.operation, visitedOps);
}
}
}
}