Skip to content

Make MLTensor strongly typed? #39

Open

Description

@wacky6

Some issues with using a dict:

  • dimensions isn't validated against TypedArray.bytesLength. I can set arbitrary dimensions (like [4,4,4] to a Float32Array that only has 1 element.
  • Hard to create an overloaded WebNN dispatch() and WebML compute() for single input/output graphs.
    • WebIDL can't distinguish between dict<NodeName, dict<data, dimensions>> and dict<NodeName "data", NodeName "dimensions">, thus fails to bind JavaScript object to C++ implementation.

Discovered this during WebML prototyping for https://crbug.com/1338067

See related issue in WebNN webmachinelearning/webnn#275

Activity

  1. wacky6 commented on Nov 9, 2022

    @wacky6
    ContributorAuthor

    Alternatively, should we simplify to drop dimensions and simply use bytes[]?

    https://www.w3.org/TR/webnn/#typedefdef-mlnamedarraybufferviews

    ATM, I don't think we support arbitrary size inputs (i.e. CNN style network still has a fixed input / output size). Validation can simply check byte length matches the model's input/output description.

    If the user provides an array with the correct byte length, but in a different type (e.g. uint16 -> float16), they just get wrong results.

  2. huningxin commented on Nov 9, 2022

    @huningxin

    @wacky6

    Alternatively, should we simplify to drop dimensions and simply use bytes[]?

    Do you mean using ArrayBuffer instead?

  3. wacky6 commented on Nov 9, 2022

    @wacky6
    ContributorAuthor

    I mean align with WebNN and use ArrayBufferView.

    Since we just care about byteLength matches the expected input/output type+shape (both are known at graph build / model loading time)

  4. huningxin commented on Nov 9, 2022

    @huningxin

    I mean align with WebNN and use ArrayBufferView.

    Sounds good to me.

    My open is whether it should support a union of ArrayBuffer and ArrayBufferView that is BufferSource typedef of Web IDL.

    Although the WebNN prototyping CL 4006509 uses this typedef as workaround for a Blink generator issue, it reminds me whether we should consider that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions