File: ml_graph_builder.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (562 lines) | stat: -rw-r--r-- 23,132 bytes parent folder | download | duplicates (5)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_ML_WEBNN_ML_GRAPH_BUILDER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_ML_WEBNN_ML_GRAPH_BUILDER_H_

#include "base/types/expected.h"
#include "services/webnn/public/mojom/webnn_graph_builder.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_ml_operand_data_type.h"
#include "third_party/blink/renderer/core/typed_arrays/array_buffer_view_helpers.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer_view.h"
#include "third_party/blink/renderer/modules/ml/webnn/allow_shared_buffer_source_util.h"
#include "third_party/blink/renderer/modules/ml/webnn/ml_graph.h"
#include "third_party/blink/renderer/modules/ml/webnn/ml_operator.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_vector.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/heap/visitor.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_remote.h"

namespace blink {

class ExceptionState;
class MLArgMinMaxOptions;
class MLBatchNormalizationOptions;
class MLContext;
class MLClampOptions;
class MLConv2dOptions;
class MLConvTranspose2dOptions;
class MLCumulativeSumOptions;
class MLEluOptions;
class MLGatherOptions;
class MLGemmOptions;
class MLGruOptions;
class MLGruCellOptions;
class MLGraph;
class MLHardSigmoidOptions;
class MLInstanceNormalizationOptions;
class MLLayerNormalizationOptions;
class MLLeakyReluOptions;
class MLLinearOptions;
class MLLstmOptions;
class MLLstmCellOptions;
class MLOperatorOptions;
class MLPadOptions;
class MLPool2dOptions;
class MLReduceOptions;
class MLResample2dOptions;
class MLReverseOptions;
class MLScatterOptions;
class MLSliceOptions;
class MLSplitOptions;
class MLTensor;
class MLTransposeOptions;
class MLTriangularOptions;
class MLOperand;
class MLOperandDescriptor;
class ScriptState;

typedef HeapVector<std::pair<String, Member<MLOperand>>> MLNamedOperands;

class MODULES_EXPORT MLGraphBuilder final : public ScriptWrappable {
  DEFINE_WRAPPERTYPEINFO();

 public:
  static MLGraphBuilder* Create(ScriptState* script_state,
                                MLContext* context,
                                ExceptionState& exception_state);

  explicit MLGraphBuilder(
      ExecutionContext* execution_context,
      MLContext* context,
      mojo::PendingAssociatedRemote<webnn::mojom::blink::WebNNGraphBuilder>
          pending_remote);

  MLGraphBuilder(const MLGraphBuilder&) = delete;
  MLGraphBuilder& operator=(const MLGraphBuilder&) = delete;

  ~MLGraphBuilder() override;

  void Trace(Visitor* visitor) const override;

  ExecutionContext* GetExecutionContext() const;

  MLContext* GetContext() const;

  struct Size2D {
    uint32_t height;
    uint32_t width;
  };

  // ml_graph_builder.idl
  MLOperand* input(ScriptState* script_state,
                   String name,
                   const MLOperandDescriptor* desc,
                   ExceptionState& exception_state);
  MLOperand* constant(ScriptState* script_state,
                      const MLOperandDescriptor* desc,
                      AllowSharedBufferSource* buffer,
                      ExceptionState& exception_state);
  MLOperand* constant(ScriptState* script_state,
                      MLTensor* tensor,
                      ExceptionState& exception_state);

  // The order of operations declaration is the same as spec.
  MLOperand* argMin(MLOperand* input,
                    const uint32_t axis,
                    MLArgMinMaxOptions* options,
                    ExceptionState& exception_state);
  MLOperand* argMax(MLOperand* input,
                    const uint32_t axis,
                    MLArgMinMaxOptions* options,
                    ExceptionState& exception_state);

  MLOperand* batchNormalization(MLOperand* input,
                                MLOperand* mean,
                                MLOperand* variance,
                                MLBatchNormalizationOptions* options,
                                ExceptionState& exception_state);

  MLOperand* clamp(MLOperand* input,
                   MLClampOptions* options,
                   ExceptionState& exception_state);

  MLOperand* concat(const HeapVector<Member<MLOperand>>& inputs,
                    const uint32_t axis,
                    MLOperatorOptions* options,
                    ExceptionState& exception_state);

  MLOperand* conv2d(MLOperand* input,
                    MLOperand* filter,
                    MLConv2dOptions* options,
                    ExceptionState& exception_state);

  MLOperand* convTranspose2d(MLOperand* input,
                             MLOperand* filter,
                             MLConvTranspose2dOptions* options,
                             ExceptionState& exception_state);

  MLOperand* cumulativeSum(MLOperand* input,
                           const uint32_t axis,
                           MLCumulativeSumOptions* options,
                           ExceptionState& exception_state);

  // Element-wise binary operations
  MLOperand* add(MLOperand* a,
                 MLOperand* b,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* sub(MLOperand* a,
                 MLOperand* b,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* mul(MLOperand* a,
                 MLOperand* b,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* div(MLOperand* a,
                 MLOperand* b,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* max(MLOperand* a,
                 MLOperand* b,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* min(MLOperand* a,
                 MLOperand* b,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* pow(MLOperand* a,
                 MLOperand* b,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* equal(MLOperand* a,
                   MLOperand* b,
                   MLOperatorOptions* options,
                   ExceptionState& exception_state);
  MLOperand* greater(MLOperand* a,
                     MLOperand* b,
                     MLOperatorOptions* options,
                     ExceptionState& exception_state);
  MLOperand* greaterOrEqual(MLOperand* a,
                            MLOperand* b,
                            MLOperatorOptions* options,
                            ExceptionState& exception_state);
  MLOperand* lesser(MLOperand* a,
                    MLOperand* b,
                    MLOperatorOptions* options,
                    ExceptionState& exception_state);
  MLOperand* lesserOrEqual(MLOperand* a,
                           MLOperand* b,
                           MLOperatorOptions* options,
                           ExceptionState& exception_state);
  MLOperand* notEqual(MLOperand* a,
                      MLOperand* b,
                      MLOperatorOptions* options,
                      ExceptionState& exception_state);
  MLOperand* logicalAnd(MLOperand* a,
                        MLOperand* b,
                        MLOperatorOptions* options,
                        ExceptionState& exception_state);
  MLOperand* logicalOr(MLOperand* a,
                       MLOperand* b,
                       MLOperatorOptions* options,
                       ExceptionState& exception_state);
  MLOperand* logicalXor(MLOperand* a,
                        MLOperand* b,
                        MLOperatorOptions* options,
                        ExceptionState& exception_state);

  // Element-wise unary operations
  MLOperand* abs(MLOperand* input,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* ceil(MLOperand* input,
                  MLOperatorOptions* options,
                  ExceptionState& exception_state);
  MLOperand* cos(MLOperand* input,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* exp(MLOperand* input,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* floor(MLOperand* input,
                   MLOperatorOptions* options,
                   ExceptionState& exception_state);
  MLOperand* log(MLOperand* input,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* neg(MLOperand* input,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* sign(MLOperand* input,
                  MLOperatorOptions* options,
                  ExceptionState& exception_state);
  MLOperand* sin(MLOperand* input,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* tan(MLOperand* input,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* erf(MLOperand* input,
                 MLOperatorOptions* options,
                 ExceptionState& exception_state);
  MLOperand* identity(MLOperand* input,
                      MLOperatorOptions* options,
                      ExceptionState& exception_state);
  MLOperand* logicalNot(MLOperand* input,
                        MLOperatorOptions* options,
                        ExceptionState& exception_state);
  MLOperand* reciprocal(MLOperand* input,
                        MLOperatorOptions* options,
                        ExceptionState& exception_state);
  MLOperand* sqrt(MLOperand* input,
                  MLOperatorOptions* options,
                  ExceptionState& exception_state);

  MLOperand* cast(MLOperand* input,
                  const V8MLOperandDataType output_data_type,
                  MLOperatorOptions* options,
                  ExceptionState& exception_state);

  MLOperand* dequantizeLinear(MLOperand* input,
                              MLOperand* scale,
                              MLOperand* zeroPoint,
                              MLOperatorOptions* options,
                              ExceptionState& exception_state);

  MLOperand* elu(MLOperand* input,
                 MLEluOptions* options,
                 ExceptionState& exception_state);

  MLOperand* expand(MLOperand* input,
                    const Vector<uint32_t>& new_shape,
                    MLOperatorOptions* options,
                    ExceptionState& exception_state);

  MLOperand* gather(MLOperand* input,
                    MLOperand* indices,
                    MLGatherOptions* options,
                    ExceptionState& exception_state);

  MLOperand* gatherElements(MLOperand* input,
                            MLOperand* indices,
                            MLGatherOptions* options,
                            ExceptionState& exception_state);

  MLOperand* gatherND(MLOperand* input,
                      MLOperand* indices,
                      MLOperatorOptions* options,
                      ExceptionState& exception_state);

  MLOperand* gelu(MLOperand* input,
                  MLOperatorOptions* options,
                  ExceptionState& exception_state);

  MLOperand* gemm(MLOperand* a,
                  MLOperand* b,
                  MLGemmOptions* options,
                  ExceptionState& exception_state);

  HeapVector<Member<MLOperand>> gru(MLOperand* input,
                                    MLOperand* weight,
                                    MLOperand* recurrent_weight,
                                    const uint32_t steps,
                                    const uint32_t hidden_size,
                                    MLGruOptions* options,
                                    ExceptionState& exception_state);

  MLOperand* gruCell(MLOperand* input,
                     MLOperand* weight,
                     MLOperand* recurrent_weight,
                     MLOperand* hidden_state,
                     uint32_t hidden_size,
                     MLGruCellOptions* options,
                     ExceptionState& exception_state);

  MLOperand* hardSigmoid(MLOperand* input,
                         MLHardSigmoidOptions* options,
                         ExceptionState& exception_state);

  MLOperand* hardSwish(MLOperand* input,
                       MLOperatorOptions* options,
                       ExceptionState& exception_state);

  MLOperand* instanceNormalization(MLOperand* input,
                                   MLInstanceNormalizationOptions* options,
                                   ExceptionState& exception_state);

  MLOperand* layerNormalization(MLOperand* input,
                                MLLayerNormalizationOptions* options,
                                ExceptionState& exception_state);

  MLOperand* leakyRelu(MLOperand* input,
                       MLLeakyReluOptions* options,
                       ExceptionState& exception_state);

  MLOperand* linear(MLOperand* input,
                    MLLinearOptions* options,
                    ExceptionState& exception_state);

  HeapVector<Member<MLOperand>> lstm(MLOperand* input,
                                     MLOperand* weight,
                                     MLOperand* recurrent_weight,
                                     const uint32_t steps,
                                     const uint32_t hidden_size,
                                     MLLstmOptions* options,
                                     ExceptionState& exception_state);

  HeapVector<Member<MLOperand>> lstmCell(MLOperand* input,
                                         MLOperand* weight,
                                         MLOperand* recurrent_weight,
                                         MLOperand* hidden_state,
                                         MLOperand* cell_state,
                                         uint32_t hidden_size,
                                         MLLstmCellOptions* options,
                                         ExceptionState& exception_state);

  MLOperand* matmul(MLOperand* a,
                    MLOperand* b,
                    MLOperatorOptions* options,
                    ExceptionState& exception_state);

  MLOperand* pad(ScriptState* script_state,
                 MLOperand* input,
                 const Vector<uint32_t>& beginningPadding,
                 const Vector<uint32_t>& endingPadding,
                 MLPadOptions* options,
                 ExceptionState& exception_state);

  // Pooling operations
  MLOperand* averagePool2d(MLOperand* input,
                           MLPool2dOptions* options,
                           ExceptionState& exception_state);
  MLOperand* l2Pool2d(MLOperand* input,
                      MLPool2dOptions* options,
                      ExceptionState& exception_state);
  MLOperand* maxPool2d(MLOperand* input,
                       MLPool2dOptions* options,
                       ExceptionState& exception_state);

  MLOperand* prelu(MLOperand* input,
                   MLOperand* slope,
                   MLOperatorOptions* options,
                   ExceptionState& exception_state);

  MLOperand* quantizeLinear(MLOperand* input,
                            MLOperand* scale,
                            MLOperand* zeroPoint,
                            MLOperatorOptions* options,
                            ExceptionState& exception_state);

  // Reduction operations
  MLOperand* reduceL1(MLOperand* input,
                      MLReduceOptions* options,
                      ExceptionState& exception_state);
  MLOperand* reduceL2(MLOperand* input,
                      MLReduceOptions* options,
                      ExceptionState& exception_state);
  MLOperand* reduceLogSum(MLOperand* input,
                          MLReduceOptions* options,
                          ExceptionState& exception_state);
  MLOperand* reduceLogSumExp(MLOperand* input,
                             MLReduceOptions* options,
                             ExceptionState& exception_state);
  MLOperand* reduceMax(MLOperand* input,
                       MLReduceOptions* options,
                       ExceptionState& exception_state);
  MLOperand* reduceMean(MLOperand* input,
                        MLReduceOptions* options,
                        ExceptionState& exception_state);
  MLOperand* reduceMin(MLOperand* input,
                       MLReduceOptions* options,
                       ExceptionState& exception_state);
  MLOperand* reduceProduct(MLOperand* input,
                           MLReduceOptions* options,
                           ExceptionState& exception_state);
  MLOperand* reduceSum(MLOperand* input,
                       MLReduceOptions* options,
                       ExceptionState& exception_state);
  MLOperand* reduceSumSquare(MLOperand* input,
                             MLReduceOptions* options,
                             ExceptionState& exception_state);

  MLOperand* relu(MLOperand* input,
                  MLOperatorOptions* options,
                  ExceptionState& exception_state);

  MLOperand* reshape(MLOperand* input,
                     const Vector<uint32_t>& new_shape,
                     MLOperatorOptions* options,
                     ExceptionState& exception_state);

  MLOperand* reverse(MLOperand* input,
                     MLReverseOptions* options,
                     ExceptionState& exception_state);

  MLOperand* resample2d(ScriptState* script_state,
                        MLOperand* input,
                        MLResample2dOptions* options,
                        ExceptionState& exception_state);

  MLOperand* scatterElements(MLOperand* input,
                             MLOperand* indices,
                             MLOperand* updates,
                             MLScatterOptions* options,
                             ExceptionState& exception_state);

  MLOperand* scatterND(MLOperand* input,
                       MLOperand* indices,
                       MLOperand* updates,
                       MLOperatorOptions* options,
                       ExceptionState& exception_state);

  MLOperand* sigmoid(MLOperand* input,
                     MLOperatorOptions* options,
                     ExceptionState& exception_state);

  MLOperand* slice(MLOperand* input,
                   const Vector<uint32_t>& starts,
                   const Vector<uint32_t>& sizes,
                   MLSliceOptions* options,
                   ExceptionState& exception_state);

  MLOperand* softmax(MLOperand* input,
                     uint32_t axis,
                     MLOperatorOptions* options,
                     ExceptionState& exception_state);
  MLOperand* softmax(MLOperand* input,
                     MLOperatorOptions* options,
                     ExceptionState& exception_state);

  MLOperand* softplus(MLOperand* input,
                      MLOperatorOptions* options,
                      ExceptionState& exception_state);

  MLOperand* softsign(MLOperand* input,
                      MLOperatorOptions* options,
                      ExceptionState& exception_state);

  HeapVector<Member<MLOperand>> split(MLOperand* input,
                                      const uint32_t splits,
                                      MLSplitOptions* options,
                                      ExceptionState& exception_state);
  HeapVector<Member<MLOperand>> split(MLOperand* input,
                                      const Vector<uint32_t>& splits,
                                      MLSplitOptions* options,
                                      ExceptionState& exception_state);

  MLOperand* tanh(MLOperand* input,
                  MLOperatorOptions* options,
                  ExceptionState& exception_state);

  MLOperand* tile(MLOperand* input,
                  const Vector<uint32_t>& repetitions,
                  MLOperatorOptions* options,
                  ExceptionState& exception_state);

  MLOperand* transpose(MLOperand* input,
                       MLTransposeOptions* options,
                       ExceptionState& exception_state);

  MLOperand* triangular(MLOperand* input,
                        MLTriangularOptions* options,
                        ExceptionState& exception_state);

  MLOperand* where(MLOperand* condition,
                   MLOperand* true_value,
                   MLOperand* false_value,
                   MLOperatorOptions* options,
                   ExceptionState& exception_state);

  ScriptPromise<MLGraph> build(ScriptState* script_state,
                               MLNamedOperands& outputs,
                               ExceptionState& exception_state);

  void OnConnectionError();

 private:
  void DidCreateWebNNGraph(
      ScriptPromiseResolver<blink::MLGraph>* resolver,
      std::pair<MLGraph::NamedOperandDescriptors,
                MLGraph::NamedOperandDescriptors> input_and_output_constraints,
      base::expected<webnn::mojom::blink::CreateGraphSuccessPtr,
                     webnn::mojom::blink::ErrorPtr> result);

  // Check whether the graph builder is in an invalid state when the
  // `has_built_` is true or the `remote_` is unbound due to context lost. It
  // must be run for each method of the graph builder.
  [[nodiscard]] base::expected<void, String> ValidateGraphBuilderState() const;

  // Performs platform-agnostic and operand-agnostic validation checks which
  // must be run for each built operand. Returns an error message which may be
  // used to throw a TypeError if `input` is not valid to use with this builder.
  [[nodiscard]] base::expected<void, String> ValidateInput(
      const MLOperand* input);
  // Convenience method to validate several inputs at once.
  [[nodiscard]] base::expected<void, String> ValidateInputs(
      const HeapVector<Member<MLOperand>>& inputs);

  Member<ExecutionContext> execution_context_;

  Member<MLContext> ml_context_;

  HeapMojoAssociatedRemote<webnn::mojom::blink::WebNNGraphBuilder> remote_;

  // Tracks whether `build()` has been called (with valid inputs). If so, `this`
  // is effectively invalid and all methods should reject.
  bool has_built_ = false;

  // Keep the unresolved `ScriptPromiseResolver` which will be rejected when the
  // Mojo pipe is unexpectedly disconnected.
  Member<ScriptPromiseResolver<MLGraph>> pending_resolver_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_ML_WEBNN_ML_GRAPH_BUILDER_H_