Skip to content

[JS/WebGPU] Squeeze operator implementation#16024

Merged
satyajandhyala merged 5 commits into
microsoft:mainfrom
visheratin:squeeze
May 26, 2023
Merged

[JS/WebGPU] Squeeze operator implementation#16024
satyajandhyala merged 5 commits into
microsoft:mainfrom
visheratin:squeeze

Conversation

@visheratin
Copy link
Copy Markdown
Contributor

Description

This PR adds an implementation of the Squeeze operator to WebGPU JSEP. The implementation follows the operator schema and allows one or two inputs.

How was it tested

  1. I created two models. Without axes:
import onnx.helper

node = onnx.helper.make_node(
    "Squeeze",
    inputs=["T"],
    outputs=["y"],
)
graph = onnx.helper.make_graph([node], "test", [onnx.helper.make_tensor_value_info("T", 1, [3, 1, 4, 5])], 
    [onnx.helper.make_tensor_value_info("y", 1, [3, 4, 5])])
onnx.save(onnx.helper.make_model(graph), "squeeze.onnx")

And with axes:

import onnx.helper

node = onnx.helper.make_node(
    "Squeeze",
    inputs=["T", "axes"],
    outputs=["y"],
)
graph = onnx.helper.make_graph([node], "test", [onnx.helper.make_tensor_value_info("T", 1, [3, 1, 4, 5]), onnx.helper.make_tensor_value_info("axes", 7, [1])], [onnx.helper.make_tensor_value_info("y", 1, [3, 4, 5])])
onnx.save(onnx.helper.make_model(graph), "squeeze-dim.onnx")
  1. I compiled the runtime using @fs-eire's instructions.
  2. I ran the test models in the browser using this minimal setup:
<html>
    <script src=".\dist\ort.webgpu.min.js"></script>
    <script>
        async function run() {
            const session = await ort.InferenceSession.create('squeeze-dim.onnx', {executionProviders: ['webgpu']});
            console.log(session);
            const input = new ort.Tensor('float32', new Float32Array(60), [3, 1, 4, 5]);
            const dim = new ort.Tensor('int64', [-3n], [1]);
            const output = await session.run({ "T": input, "axes": dim });
            console.log(output);
        }
        run();
    </script>
</html>

Motivation and Context

Improve operator coverage for WebGPU JSEP.

@visheratin
Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Comment thread onnxruntime/core/providers/js/operators/squeeze.h Fixed
@satyajandhyala satyajandhyala added the platform:web issues related to ONNX Runtime web; typically submitted using template label May 23, 2023
Comment thread onnxruntime/core/providers/js/js_execution_provider.cc Outdated
Comment thread onnxruntime/core/providers/js/operators/squeeze.h Outdated
Copy link
Copy Markdown
Contributor

@satyajandhyala satyajandhyala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check ONNX Squeeze documentation here
The Sequeeze versions are 1, 11 and 13. So we group, from 1 to 10, from 11 to 12 and from 13 and up.

@visheratin visheratin requested a review from satyajandhyala May 24, 2023 04:35
@guschmue
Copy link
Copy Markdown
Contributor

/azp run Linux CPU CI Pipeline,Linux CPU Minimal Build E2E CI Pipeline,Linux GPU CI Pipeline,Linux GPU TensorRT CI Pipeline,Linux OpenVINO CI Pipeline,Linux QNN CI Pipeline,MacOS CI Pipeline,ONNX Runtime Web CI Pipeline,Windows ARM64 QNN CI Pipeline,Windows CPU CI Pipeline

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 10 pipeline(s).

guschmue
guschmue previously approved these changes May 25, 2023
@guschmue
Copy link
Copy Markdown
Contributor

/azp run Windows GPU CI Pipeline,Windows GPU TensorRT CI Pipeline,onnxruntime-binary-size-checks-ci-pipeline,orttraining-linux-ci-pipeline,orttraining-linux-gpu-ci-pipeline,orttraining-ortmodule-distributed

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 6 pipeline(s).

@visheratin
Copy link
Copy Markdown
Contributor Author

I see that some pipelines failed, but I don't know if I can fix it.

@fs-eire
Copy link
Copy Markdown
Contributor

fs-eire commented May 26, 2023

as suggested in the error message, you may need to run "npm run build:doc" in folder /js/web and checkin the changes of updated file under /js/docs/

@visheratin
Copy link
Copy Markdown
Contributor Author

I tried it, but the command didn't generate any committable changes. Maybe I didn't add something that can be used by generate-operator-md.ts?

@fs-eire
Copy link
Copy Markdown
Contributor

fs-eire commented May 26, 2023

if you have file generate-operator-md.ts in your source code, you need to merge latest main branch, where we added generate-webgpu-operator-md.ts and js/web/docs/webgpu-operators.md

please make sure do npm ci in /js/web before running npm run build:doc.

@visheratin
Copy link
Copy Markdown
Contributor Author

Thanks! Now the changes were generated.

@guschmue
Copy link
Copy Markdown
Contributor

/azp run Linux CPU CI Pipeline,Linux CPU Minimal Build E2E CI Pipeline,Linux GPU CI Pipeline,Linux GPU TensorRT CI Pipeline,Linux OpenVINO CI Pipeline,Linux QNN CI Pipeline,MacOS CI Pipeline,ONNX Runtime Web CI Pipeline,Windows ARM64 QNN CI Pipeline,Windows CPU CI Pipeline

@guschmue
Copy link
Copy Markdown
Contributor

/azp run Windows GPU CI Pipeline,Windows GPU TensorRT CI Pipeline,onnxruntime-binary-size-checks-ci-pipeline,orttraining-linux-ci-pipeline,orttraining-linux-gpu-ci-pipeline,orttraining-ortmodule-distributed

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 10 pipeline(s).

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 6 pipeline(s).

@guschmue guschmue self-requested a review May 26, 2023 19:11
@satyajandhyala satyajandhyala merged commit 415c26e into microsoft:main May 26, 2023
@guschmue
Copy link
Copy Markdown
Contributor

@visheratin , thanks for the contribution!
We sure apricate help with adding ops for webgpu.

@hariharans29
Copy link
Copy Markdown
Member

Should this be uncommented now -

// "test_squeeze_negative_axes",
?

@fs-eire
Copy link
Copy Markdown
Contributor

fs-eire commented May 27, 2023

Should this be uncommented now -

// "test_squeeze_negative_axes",

?

good catch... since it's already merged , maybe we should fix it together with a similar implementation of Unsqueeze in another PR?

@visheratin
Copy link
Copy Markdown
Contributor Author

Sorry, I missed the part with commented tests. Will fix with the Unsqueeze implementation this weekend.

fs-eire pushed a commit that referenced this pull request Jun 1, 2023
### Description

This PR adds an implementation of the Squeeze operator to WebGPU JSEP.
The implementation follows the [operator
schema](https://github.com/onnx/onnx/blob/main/docs/Operators.md#Unsqueeze).

To implement the `Unsqueeze` operator in the same fashion as the
`Squeeze`, I added the `ComputeOutputShape()` method to the
`UnsqueezeBase` class and made some slight modifications. Please let me
know if it is a bad idea and if I should move this method to the JS
implementation.

I also uncommented test case lines in the `suite-test-list.jsonc` file
for both Squeeze and Unsqueeze operators following @hariharans29's
[comment](#16024 (comment)).

### How was it tested

1. I created a model with only one operator:

```Python
import onnx.helper

node = onnx.helper.make_node(
    "Unsqueeze",
    inputs=["T", "axes"],
    outputs=["y"],
)
graph = onnx.helper.make_graph([node], "test", [onnx.helper.make_tensor_value_info("T", 1, [3, 4, 5]), onnx.helper.make_tensor_value_info("axes", 7, [2])], [onnx.helper.make_tensor_value_info("y", 1, [3, 1, 4, 5, 1])])
onnx.save(onnx.helper.make_model(graph), "unsqueeze.onnx")
```

2. I compiled the runtime using @fs-eire's
[instructions](https://gist.github.com/fs-eire/a55b2c7e10a6864b9602c279b8b75dce).
3. I ran the test models in the browser using this minimal setup:
```HTML
<html>
    <script src=".\dist\ort.webgpu.min.js"></script>
    <script>
        async function run() {
            const session = await ort.InferenceSession.create('unsqueeze.onnx', {executionProviders: ['webgpu']});
            console.log(session);
            const input = new ort.Tensor('float32', new Float32Array(60), [3, 4, 5]);
            const dim = new ort.Tensor('int64', [1n, 4n], [2]);
            const output = await session.run({ "T": input, "axes": dim });
            console.log(output);
        }
        run();
    </script>
</html>
```

### Motivation and Context

Improve operator coverage for WebGPU JSEP.
siweic0 pushed a commit to siweic0/onnxruntime-web that referenced this pull request May 9, 2024
### Description

This PR adds an implementation of the `Squeeze` operator to WebGPU JSEP.
The implementation follows the [operator
schema](https://github.com/onnx/onnx/blob/main/docs/Operators.md#Squeeze)
and allows one or two inputs.

### How was it tested

1. I created two models. Without `axes`:

```Python
import onnx.helper

node = onnx.helper.make_node(
    "Squeeze",
    inputs=["T"],
    outputs=["y"],
)
graph = onnx.helper.make_graph([node], "test", [onnx.helper.make_tensor_value_info("T", 1, [3, 1, 4, 5])], 
    [onnx.helper.make_tensor_value_info("y", 1, [3, 4, 5])])
onnx.save(onnx.helper.make_model(graph), "squeeze.onnx")
```

And with `axes`:

```Python
import onnx.helper

node = onnx.helper.make_node(
    "Squeeze",
    inputs=["T", "axes"],
    outputs=["y"],
)
graph = onnx.helper.make_graph([node], "test", [onnx.helper.make_tensor_value_info("T", 1, [3, 1, 4, 5]), onnx.helper.make_tensor_value_info("axes", 7, [1])], [onnx.helper.make_tensor_value_info("y", 1, [3, 4, 5])])
onnx.save(onnx.helper.make_model(graph), "squeeze-dim.onnx")
```

2. I compiled the runtime using @fs-eire's
[instructions](https://gist.github.com/fs-eire/a55b2c7e10a6864b9602c279b8b75dce).
3. I ran the test models in the browser using this minimal setup:
```HTML
<html>
    <script src=".\dist\ort.webgpu.min.js"></script>
    <script>
        async function run() {
            const session = await ort.InferenceSession.create('squeeze-dim.onnx', {executionProviders: ['webgpu']});
            console.log(session);
            const input = new ort.Tensor('float32', new Float32Array(60), [3, 1, 4, 5]);
            const dim = new ort.Tensor('int64', [-3n], [1]);
            const output = await session.run({ "T": input, "axes": dim });
            console.log(output);
        }
        run();
    </script>
</html>
```

### Motivation and Context

Improve operator coverage for WebGPU JSEP.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform:web issues related to ONNX Runtime web; typically submitted using template

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants