Skip to content

[webgpu] Optimize Conv by im2col-matmul - #26603

Merged
Guenther Schmuelling (guschmue) merged 11 commits into
microsoft:mainfrom
daijh:im2col-matmul
Dec 3, 2025
Merged

[webgpu] Optimize Conv by im2col-matmul#26603
Guenther Schmuelling (guschmue) merged 11 commits into
microsoft:mainfrom
daijh:im2col-matmul

Conversation

@daijh

Copy link
Copy Markdown
Contributor

Description

This PR optimizes the Conv operation by implementing two new compute shaders: oihw_to_ohwi and im2col-matmul.

oihw_to_ohwi:
Improves performance over the default Transpose shader by utilizing workgroup memory to ensure continuous memory read/write patterns.

im2col-matmul:

  • Employs a workgroup size of 64.
  • Dynamically selects tile sizes (32x64 or 16x64) based on the source/weight shape.
  • Each invocation handles a dedicated weight element.
  • Uses subgroupShuffle to efficiently access the source tile, leveraging k_vec4 vectorization for better memory throughput.

Testing on Lunar Lake demonstrated up to an 87% performance improvement in Conv_2D operations.

Motivation and Context

See above.

@daijh

Copy link
Copy Markdown
Contributor Author

Lunar Lake
onnxruntime commit d55ade0

Operation

Milliseconds conv2d-mm im2col-matmul
src: 1x128x512x512
weight: 128x128x3x3
56.071 42.824
src: 1x2560x8x8
weight: 1280x2560x3x3
21.066 11.263
src: 1x1280x8x8
weight: 1280x1280x3x3
10.384 6.357

sd-turbo

Milliseconds conv2d-mm im2col-matmul
sd-turbo-unet-fp16-demo.onnx 1010.245 612.092
sd-turbo-vae-decoder-fp16-demo.onnx 2317.391 1848.545

@daijh

Copy link
Copy Markdown
Contributor Author

@guschmue Guenther Schmuelling (guschmue) added the ep:WebGPU ort-web webgpu provider label Nov 21, 2025
Comment thread onnxruntime/core/providers/webgpu/nn/im2col_matmul.cc Outdated
Comment thread onnxruntime/core/providers/webgpu/nn/im2col_matmul.cc Outdated
Comment thread onnxruntime/core/providers/webgpu/nn/im2col_matmul.cc
Comment thread onnxruntime/core/providers/webgpu/nn/im2col_matmul.wgsl.template

@qjia7 Jiajia Qin (qjia7) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Jianhui Dai (@daijh) Are you ok to submit to commit to remove the device limitation so that the CI can test your new path? If the CI is happy, you can revert it again? Or add a test config which can also go to this path so the the CI can test it? Guenther Schmuelling (@guschmue) Yulong Wang (@fs-eire) How's opinions on this?

@daijh

Copy link
Copy Markdown
Contributor Author

Certainly.
I should note, however, that performance may be negatively impacted on un-tested devices.
Given that the CI process is focused on functional correctness, this impact may be acceptable.

@daijh

Copy link
Copy Markdown
Contributor Author

Fix defined but not used [-Werror=unused-function] for Release build

@daijh

Copy link
Copy Markdown
Contributor Author

Suppresses the unused variable warning, please help to re-trigger.
Thanks.

Comment thread onnxruntime/core/providers/webgpu/nn/im2col_matmul.cc Fixed
@daijh

Copy link
Copy Markdown
Contributor Author

Thanks.
The WebGPU/Web CI completed successfully. The failure noted in the logs is not relevant to this PR's changes.

Revert previous change to prevent performance regression on untested devices.

@guschmue

Copy link
Copy Markdown
Contributor

/azp run Linux QNN CI Pipeline,Win_TRT_Minimal_CUDA_Test_CI,Windows ARM64 QNN CI Pipeline,Windows GPU Doc Gen CI Pipeline

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 4 pipeline(s).

@daijh

Copy link
Copy Markdown
Contributor Author

The failure in the CI is not relevant to this PR's changes.
Let me rebase the main if fixing the CUDA/TensorRT CI.

…ively impacted on un-tested devices"

This reverts commit 8eb8ecc.
@guschmue

Copy link
Copy Markdown
Contributor

/azp run Linux QNN CI Pipeline,Win_TRT_Minimal_CUDA_Test_CI,Windows ARM64 QNN CI Pipeline,Windows GPU Doc Gen CI Pipeline

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 4 pipeline(s).

@guschmue
Guenther Schmuelling (guschmue) merged commit 8f8069d into microsoft:main Dec 3, 2025
89 checks passed
@daijh
Jianhui Dai (daijh) deleted the im2col-matmul branch December 4, 2025 01:50
alex-spacemit pushed a commit to spacemit-com/onnxruntime that referenced this pull request Dec 8, 2025
### Description
This PR optimizes the `Conv` operation by implementing two new compute
shaders: `oihw_to_ohwi` and `im2col-matmul`.

`oihw_to_ohwi`: 
Improves performance over the default Transpose shader by utilizing
workgroup memory to ensure continuous memory read/write patterns.

`im2col-matmul`:
- Employs a workgroup size of 64.
- Dynamically selects tile sizes (32x64 or 16x64) based on the
source/weight shape.
- Each invocation handles a dedicated weight element.
- Uses subgroupShuffle to efficiently access the source tile, leveraging
k_vec4 vectorization for better memory throughput.

Testing on Lunar Lake demonstrated **up to an 87%** performance
improvement in Conv_2D operations.

### Motivation and Context
See above.
Sumit Kumar (Sumit2318) pushed a commit that referenced this pull request Jan 6, 2026
### Description
This PR optimizes the `Conv` operation by implementing two new compute
shaders: `oihw_to_ohwi` and `im2col-matmul`.

`oihw_to_ohwi`: 
Improves performance over the default Transpose shader by utilizing
workgroup memory to ensure continuous memory read/write patterns.

`im2col-matmul`:
- Employs a workgroup size of 64.
- Dynamically selects tile sizes (32x64 or 16x64) based on the
source/weight shape.
- Each invocation handles a dedicated weight element.
- Uses subgroupShuffle to efficiently access the source tile, leveraging
k_vec4 vectorization for better memory throughput.

Testing on Lunar Lake demonstrated **up to an 87%** performance
improvement in Conv_2D operations.

### Motivation and Context
See above.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ep:WebGPU ort-web webgpu provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants