chromium/third_party/blink/renderer/modules/ml/ml_context.idl

// 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.

// This will be a shared interface by two APIs:
// - The Model Loader API,
//     https://github.com/webmachinelearning/model-loader/blob/main/explainer.md
// - The WebNN API,
//     https://github.com/webmachinelearning/webnn/blob/main/explainer.md
// We follow WebNN spec's definition,
//   - https://webmachinelearning.github.io/webnn/#api-mlcontext
// In the future, this interface may be extended when WebNN is implemented so
// a dictionary may be insufficient.

typedef record<DOMString, ArrayBufferView> MLNamedArrayBufferViews;

dictionary MLComputeResult {
  MLNamedArrayBufferViews inputs;
  MLNamedArrayBufferViews outputs;
};

dictionary MLContextLostInfo {
  DOMString message;
};

dictionary MLSupportLimits {
  sequence<DOMString> dataTypes;
};

dictionary MLBinarySupportLimits {
  MLSupportLimits a;
  MLSupportLimits b;
  MLSupportLimits output;
};

dictionary MLConcatSupportLimits {
  MLSupportLimits inputs;
  MLSupportLimits output;
};

dictionary MLGatherSupportLimits {
  MLSupportLimits input;
  MLSupportLimits indices;
  MLSupportLimits output;
};

dictionary MLGemmSupportLimits {
  MLSupportLimits a;
  MLSupportLimits b;
  MLSupportLimits c;
  MLSupportLimits output;
};

dictionary MLPreluSupportLimits {
  MLSupportLimits input;
  MLSupportLimits slope;
  MLSupportLimits output;
};

dictionary MLLogicalNotSupportLimits {
  MLSupportLimits a;
  MLSupportLimits output;
};

dictionary MLSingleInputSupportLimits {
  MLSupportLimits input;
  MLSupportLimits output;
};

dictionary MLWhereSupportLimits {
  MLSupportLimits condition;
  MLSupportLimits trueValue;
  MLSupportLimits falseValue;
  MLSupportLimits output;
};

dictionary MLOpSupportLimits {
  MLInputOperandLayout preferredInputLayout;
  MLSupportLimits input;
  MLSupportLimits constant;
  MLSupportLimits output;

  MLSingleInputSupportLimits argMin;
  MLSingleInputSupportLimits argMax;
  MLSingleInputSupportLimits cast;
  MLSingleInputSupportLimits clamp;
  MLConcatSupportLimits concat;

  // Element-wise binary ops.
  MLBinarySupportLimits add;
  MLBinarySupportLimits sub;
  MLBinarySupportLimits mul;
  MLBinarySupportLimits div;
  MLBinarySupportLimits max;
  MLBinarySupportLimits min;
  MLBinarySupportLimits pow;

  // Element-wise logical ops.
  MLBinarySupportLimits equal;
  MLBinarySupportLimits greater;
  MLBinarySupportLimits greaterOrEqual;
  MLBinarySupportLimits lesser;
  MLBinarySupportLimits lesserOrEqual;
  MLLogicalNotSupportLimits logicalNot;

  // Element-wise unary ops.
  MLSingleInputSupportLimits abs;
  MLSingleInputSupportLimits ceil;
  MLSingleInputSupportLimits cos;
  MLSingleInputSupportLimits erf;
  MLSingleInputSupportLimits exp;
  MLSingleInputSupportLimits floor;
  MLSingleInputSupportLimits identity;
  MLSingleInputSupportLimits log;
  MLSingleInputSupportLimits neg;
  MLSingleInputSupportLimits reciprocal;
  MLSingleInputSupportLimits sign;
  MLSingleInputSupportLimits sin;
  MLSingleInputSupportLimits sqrt;
  MLSingleInputSupportLimits tan;

  MLSingleInputSupportLimits elu;
  MLSingleInputSupportLimits expand;
  MLGatherSupportLimits gather;
  MLGatherSupportLimits gatherElements;
  MLSingleInputSupportLimits gelu;
  MLGemmSupportLimits gemm;
  MLSingleInputSupportLimits hardSigmoid;
  MLSingleInputSupportLimits hardSwish;
  MLSingleInputSupportLimits leakyRelu;
  MLSingleInputSupportLimits linear;
  MLBinarySupportLimits matmul;
  MLSingleInputSupportLimits pad;
  MLPreluSupportLimits prelu;

  // Pool2d.
  MLSingleInputSupportLimits averagePool2d;
  MLSingleInputSupportLimits l2Pool2d;
  MLSingleInputSupportLimits maxPool2d;

  // Reduction ops.
  MLSingleInputSupportLimits reduceL1;
  MLSingleInputSupportLimits reduceL2;
  MLSingleInputSupportLimits reduceLogSum;
  MLSingleInputSupportLimits reduceLogSumExp;
  MLSingleInputSupportLimits reduceMax;
  MLSingleInputSupportLimits reduceMean;
  MLSingleInputSupportLimits reduceMin;
  MLSingleInputSupportLimits reduceProduct;
  MLSingleInputSupportLimits reduceSum;
  MLSingleInputSupportLimits reduceSumSquare;

  MLSingleInputSupportLimits relu;
  MLSingleInputSupportLimits resample2d;
  MLSingleInputSupportLimits reshape;
  MLSingleInputSupportLimits sigmoid;
  MLSingleInputSupportLimits slice;
  MLSingleInputSupportLimits softmax;
  MLSingleInputSupportLimits softplus;
  MLSingleInputSupportLimits softsign;
  MLSingleInputSupportLimits split;
  MLSingleInputSupportLimits tanh;
  MLSingleInputSupportLimits transpose;
  MLSingleInputSupportLimits triangular;
  MLWhereSupportLimits where;
};

typedef record<DOMString, MLBuffer> MLNamedBuffers;

[
  RuntimeEnabled=MachineLearningNeuralNetwork,
  SecureContext,
  Exposed=(Window, DedicatedWorker)
] interface MLContext {
  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState
  ] readonly attribute Promise<MLContextLostInfo> lost;

  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState,
    RaisesException
  ] void destroy();

  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState,
    RaisesException,
    Measure
  ] Promise<MLComputeResult> compute(
      MLGraph graph, MLNamedArrayBufferViews inputs, MLNamedArrayBufferViews outputs);

  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState,
    RaisesException
  ] Promise<MLBuffer> createBuffer(MLBufferDescriptor descriptor);

  // TODO(crbug.com/328105506): enable partial MLBuffer reads/writes.
  // TODO(crbug.com/40278771): consider moving arguments into a dictonary 
  // per W3C recommendations:
  // https://w3ctag.github.io/design-principles/#prefer-dictionaries
  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState,
    RaisesException
  ] void writeBuffer(
        MLBuffer dstBuffer,
        [AllowShared] ArrayBufferView srcData,
        optional MLSize64 srcElementOffset = 0,
        optional MLSize64 srcElementSize);

  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState,
    RaisesException
  ] void writeBuffer(
        MLBuffer dstBuffer,
        ArrayBuffer srcData,
        optional MLSize64 srcByteOffset = 0,
        optional MLSize64 srcByteSize);

  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState,
    RaisesException
  ] Promise<ArrayBuffer> readBuffer(
        MLBuffer srcBuffer);

  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState,
    RaisesException
  ] Promise<undefined> readBuffer(
        MLBuffer srcBuffer,
        [AllowShared] ArrayBufferView dstData);

  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState,
    RaisesException
  ] Promise<undefined> readBuffer(
        MLBuffer srcBuffer,
        [AllowShared] ArrayBuffer dstData);

  // TODO(crbug.com/1273291): enable partial buffer dispatches.
  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState,
    RaisesException,
    Measure
  ] void dispatch(
      MLGraph graph, MLNamedBuffers inputs, MLNamedBuffers outputs);
  [
    RuntimeEnabled=MachineLearningNeuralNetwork,
    CallWith=ScriptState
  ] MLOpSupportLimits opSupportLimits();
};