Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use deep copy for setting internal slots? #395

Closed
zolkis opened this issue Jun 5, 2023 · 10 comments
Closed

Use deep copy for setting internal slots? #395

zolkis opened this issue Jun 5, 2023 · 10 comments
Assignees

Comments

@zolkis
Copy link
Collaborator

zolkis commented Jun 5, 2023

          Should we do deep copy of the descriptor? Would the change of original operand's descriptor impact the copied one?

Originally posted by @huningxin in #348 (comment)

@zolkis
Copy link
Collaborator Author

zolkis commented Jun 5, 2023

ECMAScript doesn't really shed light on this. Internal slots are supposed to be created and then not modified unless specified differently.

Now we seem to have 2 options:

  1. change every line that sets an internal slot to replace the argument with "a copy of the argument"
  2. or, create steps to "set the internal slot [[name]] to <arg>" steps, where we say "set [[name]] to a copy of <arg>".

@anssiko @dontcallmedom any input here?

@anssiko
Copy link
Member

anssiko commented Jun 5, 2023

If we need to do this in many places maybe it makes sense to define an abstract operation for doing a deep copy? Did you check whether any of the infra specs incl. HTML have reusable helpers for this?

For some inspiration, for structured data there's a bunch of abstract operations in HTML https://html.spec.whatwg.org/multipage/structured-data.html referenced by other specs such as IndexedDB, Web Workers.

@zolkis
Copy link
Collaborator Author

zolkis commented Jun 5, 2023

Yes, we can define separate "set internal slot" steps for that and when it becomes specified in INFRA or ECMA, we can switch.

What we need is to prevent changes from client after setting, i.e. to pass ownership of the init dict to the internal slot's object. An emulation of that is [deep] copy / pass by value, usually referenced as "copy of" in WebIDL, as suggested above.
Is that (option 2) good to go?

@anssiko
Copy link
Member

anssiko commented Jun 5, 2023

Yes, I think a PR for that option would help.

@zolkis
Copy link
Collaborator Author

zolkis commented Jun 5, 2023

Since it affects all internal slots and the whole spec, I will do it after the current batch of PRs is merged.
Maybe after the stylistic changes.

@zolkis
Copy link
Collaborator Author

zolkis commented Aug 21, 2023

Setting internal slots has been explained here.

@inexorabletash
Copy link
Member

I've been going through the spec and I don't see anywhere where an internal slot holds a non-primitive data type, and the properties are mutated after it is set. Most specifically, once a MLOperand's [[descriptor]] is set I never see it changing.

The closest I've spotted is in resample output sizes where the input descriptor is cloned then mutated with:

Let desc be an MLOperandDescriptor initialized to input.[[descriptor]].

This could be made more explicit with "Let desc be a new MLOperandDescriptor..."

Otherwise... I haven't spotted any usage that would require more explicit deep copies. Am I missing any?

FWIW, I was "grepping the spec" using this on the console:

console.log([...document.body.innerText.matchAll(/set .+?\[\[\w+\]\]\..+ to .*/ig)].map(m => m[0]).join('\n'))

inexorabletash added a commit to inexorabletash/webnn that referenced this issue Jan 19, 2024
* Per webmachinelearning#395 make it clearer that the algorithm is working on a new
  descriptor (a copy of the input's)

* When invoking the algorithm, pass the required input argument.

* Rename algorithm from "resample output sizes" to "calculate resample
  output sizes" which more clearly descripts what is happening and
  aligns with similar algorithms in the spec.
@inexorabletash
Copy link
Member

The calculate matmul output sizes may also be misleading, as it does:

Let shapeA be a.[[descriptor]].dimensions ...
... then insert 1 in the front of shapeA ...

The intent here is a copy, not mutating the input's descriptor.

inexorabletash added a commit to inexorabletash/webnn that referenced this issue Jan 20, 2024
For gemm() - make lists that are mutated be explicit clones using
Infra terminology, and simplify the wording reversing the lists.

For matmul() - make lists that are mutated be explicit clones using
Infra terminology, use append/prepend definitions from Infra, convert
a variable change from "let" to "set" and drop use of "array".

For webmachinelearning#395
inexorabletash added a commit to inexorabletash/webnn that referenced this issue Jan 22, 2024
For gemm() - make lists that are mutated be explicit clones using
Infra terminology, and simplify the wording reversing the lists.

For matmul() - make lists that are mutated be explicit clones using
Infra terminology, use append/prepend definitions from Infra, convert
a variable change from "let" to "set" and drop use of "array".

For webmachinelearning#395
inexorabletash added a commit to inexorabletash/webnn that referenced this issue Jan 23, 2024
For gemm() - make lists that are mutated be explicit clones using
Infra terminology, and simplify the wording reversing the lists.

For matmul() - make lists that are mutated be explicit clones using
Infra terminology, use append/prepend definitions from Infra, convert
a variable change from "let" to "set" and drop use of "array".

For webmachinelearning#395
inexorabletash added a commit to inexorabletash/webnn that referenced this issue Jan 24, 2024
For gemm() - make lists that are mutated be explicit clones using
Infra terminology, and simplify the wording reversing the lists.

For matmul() - make lists that are mutated be explicit clones using
Infra terminology, use append/prepend definitions from Infra, convert
a variable change from "let" to "set" and drop use of "array".

For webmachinelearning#395
inexorabletash added a commit to inexorabletash/webnn that referenced this issue Jan 24, 2024
* Per webmachinelearning#395 make it clearer that the algorithm is working on a new
  descriptor (a copy of the input's)

* When invoking the algorithm, pass the required input argument.

* Rename algorithm from "resample output sizes" to "calculate resample
  output sizes" which more clearly descripts what is happening and
  aligns with similar algorithms in the spec, and add types.
wchao1115 pushed a commit that referenced this issue Jan 27, 2024
* Per #395 make it clearer that the algorithm is working on a new
  descriptor (a copy of the input's)

* When invoking the algorithm, pass the required input argument.

* Rename algorithm from "resample output sizes" to "calculate resample
  output sizes" which more clearly descripts what is happening and
  aligns with similar algorithms in the spec, and add types.
inexorabletash added a commit to inexorabletash/webnn that referenced this issue Jan 27, 2024
For gemm() - make lists that are mutated be explicit clones using
Infra terminology, and simplify the wording reversing the lists.

For matmul() - make lists that are mutated be explicit clones using
Infra terminology, use append/prepend definitions from Infra, convert
a variable change from "let" to "set" and drop use of "array".

For webmachinelearning#395
inexorabletash added a commit to inexorabletash/webnn that referenced this issue Jan 30, 2024
…achinelearning#520)

* Per webmachinelearning#395 make it clearer that the algorithm is working on a new
  descriptor (a copy of the input's)

* When invoking the algorithm, pass the required input argument.

* Rename algorithm from "resample output sizes" to "calculate resample
  output sizes" which more clearly descripts what is happening and
  aligns with similar algorithms in the spec, and add types.
inexorabletash added a commit to inexorabletash/webnn that referenced this issue Feb 6, 2024
For gemm() - make lists that are mutated be explicit clones using
Infra terminology, and simplify the wording reversing the lists.

For matmul() - make lists that are mutated be explicit clones using
Infra terminology, use append/prepend definitions from Infra, convert
a variable change from "let" to "set" and drop use of "array".

For webmachinelearning#395
inexorabletash added a commit to inexorabletash/webnn that referenced this issue Feb 7, 2024
For gemm() - make lists that are mutated be explicit clones using
Infra terminology, and simplify the wording reversing the lists.

For matmul() - make lists that are mutated be explicit clones using
Infra terminology, use append/prepend definitions from Infra, convert
a variable change from "let" to "set" and drop use of "array".

For webmachinelearning#395
inexorabletash added a commit to inexorabletash/webnn that referenced this issue Feb 13, 2024
For gemm() - make lists that are mutated be explicit clones using
Infra terminology, and simplify the wording reversing the lists.

For matmul() - make lists that are mutated be explicit clones using
Infra terminology, use append/prepend definitions from Infra, convert
a variable change from "let" to "set" and drop use of "array".

For webmachinelearning#395
fdwr added a commit that referenced this issue Feb 14, 2024
* Wording change: Tighten up output shape calculation algorithms

For gemm() - make lists that are mutated be explicit clones using
Infra terminology, and simplify the wording reversing the lists.

For matmul() - make lists that are mutated be explicit clones using
Infra terminology, use append/prepend definitions from Infra, convert
a variable change from "let" to "set" and drop use of "array".

For #395

* Rewrite to match Chromium impl

* Update index.bs

Co-authored-by: Dwayne Robinson <[email protected]>

---------

Co-authored-by: Dwayne Robinson <[email protected]>
@inexorabletash
Copy link
Member

@zolkis - do you think we can close this out? Or can you spot any other changes we should make?

@zolkis
Copy link
Collaborator Author

zolkis commented Feb 19, 2024

@inexorabletash yes, we can close it, thanks for the lots of help here.

@zolkis zolkis closed this as completed Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants