Skip to content

Autogen webgpu dawn, removing wgpu-py dependency (f16 support part 1) - #8646

Merged
geohot merged 42 commits into
tinygrad:masterfrom
wpmed92:switch-wgpu-to-dawn
Feb 7, 2025
Merged

Autogen webgpu dawn, removing wgpu-py dependency (f16 support part 1)#8646
geohot merged 42 commits into
tinygrad:masterfrom
wpmed92:switch-wgpu-to-dawn

Conversation

@wpmed92

@wpmed92 wpmed92 commented Jan 16, 2025

Copy link
Copy Markdown
Contributor

This PR changes the WebGPU runtime used from wgpu to Dawn (used by Chrome):

  • wgpu is not closely following the WebGPU specs, it doesn't impose a limit on storage buffers per kernel, but Dawn does, so some tests had to be skipped due to more than the supported number of buffers per kernel
  • the python bindings used are created by me, it's published as dawn-python (supports both Mac (Metal), and Linux (Vulkan)
  • WebGPU specs doesn’t allow using the same buffer for input and output, JIT does this, and was fine on wgpu (non spec compliant). I patch it in ops_webgpu

Here is a demo of f16 stable diffusion deployed from another branch that includes the f16 support: https://wpmed92.github.io/stable-diffusion-tinygrad-f16/
I see around 30% perf gain from f16 in SD.
The f16 wgsl change is coming in a follow-up PR, I didn't want to put these two relatively big changes into 1 PR.

@wpmed92
wpmed92 force-pushed the switch-wgpu-to-dawn branch from 8218e32 to 172d3bf Compare January 16, 2025 15:16
@geohot geohot added the bounty locked Bounty is locked to someone label Jan 16, 2025
@geohot

geohot commented Jan 16, 2025

Copy link
Copy Markdown
Collaborator

Why are there new skips and that for loop for input/output? Why wasn't this a problem in wgpu? Read the PR message, understood.

I'm worried about the long term maintenance of "dawn-python", is it possible to do this with a ctypes autogen script and put this in the tinygrad repo instead? It looks like it's doing this in the dawn-python package already. We're moving all the backends away from having Python deps.

@wpmed92

wpmed92 commented Jan 16, 2025

Copy link
Copy Markdown
Contributor Author

@geohot Yeah, i created dawn-python using clang2py, which we use for autogen, i can move it into the tinygrad repo. I think it's fine to grab the underlying prebuilt binaries from my repo though. (dawn.so, dawn.dylib)

@geohot

geohot commented Jan 16, 2025

Copy link
Copy Markdown
Collaborator

Yea, I'm fine with grabbing the bins from a GitHub release (like remu or ocelot)

@geohot

geohot commented Jan 27, 2025

Copy link
Copy Markdown
Collaborator

Progress on adding dawn bindings to autogen?

@wpmed92
wpmed92 force-pushed the switch-wgpu-to-dawn branch from 172d3bf to e9c463c Compare January 27, 2025 13:24
@wpmed92
wpmed92 marked this pull request as draft January 27, 2025 13:28
@wpmed92

wpmed92 commented Jan 27, 2025

Copy link
Copy Markdown
Contributor Author

@geohot I moved dawn-python stuff to tinygrad autogen, I have all tests passing locally on my Mac, but have to clean it up and make it pass in CI.

@wpmed92
wpmed92 force-pushed the switch-wgpu-to-dawn branch from 45586a9 to 585e21e Compare January 27, 2025 13:33
@geohot

geohot commented Jan 28, 2025

Copy link
Copy Markdown
Collaborator

I don't think there should be a support for this. Why can't it go in the webgpu file?

@wpmed92
wpmed92 force-pushed the switch-wgpu-to-dawn branch 5 times, most recently from 6df1b50 to f7ca268 Compare January 28, 2025 12:53
@wpmed92

wpmed92 commented Jan 28, 2025

Copy link
Copy Markdown
Contributor Author

@geohot

  • removed support/webgpu and moved the code into ops_webgpu
  • refactored callback info creation -> removed a lot of the duplications
  • Fixed autogen verification
  • Fixed mypy

Everything is passing, except for the line limit test.
I'm 100 lines above the limit, but there's more to save in ops_webgpu than what i saved so far, so will work on it more.

@wpmed92
wpmed92 marked this pull request as ready for review January 28, 2025 13:33
@wpmed92 wpmed92 changed the title Switch WebGPU runtime to Dawn (f16 support part 1) Autogen webgpu dawn, removing wgpu-py dependency (f16 support part 1) Jan 29, 2025
@wpmed92

wpmed92 commented Jan 29, 2025

Copy link
Copy Markdown
Contributor Author

From the inital 421 lines ops_webgpu it's down to 324, need to save only 28 more.

Screenshot 2025-01-29 at 15 03 24

Comment thread tinygrad/runtime/ops_webgpu.py Outdated
@wpmed92
wpmed92 force-pushed the switch-wgpu-to-dawn branch from 6dd568c to b3836d7 Compare January 30, 2025 13:01
Comment thread tinygrad/runtime/ops_webgpu.py Outdated
Comment thread tinygrad/runtime/ops_webgpu.py Outdated
Comment thread tinygrad/runtime/ops_webgpu.py Outdated
Comment thread tinygrad/runtime/ops_webgpu.py Outdated

if wait: webgpu.wgpuCommandEncoderResolveQuerySet(command_encoder, query_set, 0, 2, query_buf, 0)

cmd_buf = webgpu.wgpuCommandEncoderFinish(command_encoder, webgpu.WGPUCommandBufferDescriptor())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why is this a variable?


def cb(status, compute_pipeline_impl, msg, u1, u2): pipeline_result[:] = status, compute_pipeline_impl, from_wgpu_str(msg)

cb_info = create_cb_info(webgpu.WGPUCreateComputePipelineAsyncCallbackInfo2, webgpu.WGPUCreateComputePipelineAsyncCallback2, cb)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why is this a variable? (multiple places)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It consistently segfaults if it's not saved into a variable before passing it in.

Comment thread tinygrad/runtime/ops_webgpu.py Outdated
@wpmed92
wpmed92 force-pushed the switch-wgpu-to-dawn branch from c2ad6e8 to d26440a Compare February 5, 2025 08:58
@wpmed92
wpmed92 force-pushed the switch-wgpu-to-dawn branch from d26440a to 0aa47c8 Compare February 5, 2025 09:00
@github-actions

github-actions Bot commented Feb 5, 2025

Copy link
Copy Markdown
Contributor

Changes

Name                              Lines    Diff    Tokens/Line    Diff
------------------------------  -------  ------  -------------  ------
tinygrad/runtime/ops_webgpu.py      171    +113           14.2    -2.0


total lines changes: +113

@geohot

geohot commented Feb 7, 2025

Copy link
Copy Markdown
Collaborator

Bumped the line limit to merge this, really happy to have that wgpu dep gone.

@geohot
geohot merged commit 133caca into tinygrad:master Feb 7, 2025
@wpmed92
wpmed92 deleted the switch-wgpu-to-dawn branch February 7, 2025 07:28
@Korijn

Korijn commented Feb 16, 2025

Copy link
Copy Markdown

Bumped the line limit to merge this, really happy to have that wgpu dep gone.

Hello, just checking in here as one of the maintainers of wgpu-py, would any of you mind summarizing your reasons to move away from our library? Anything we could do better? Thanks!

cc @almarklein

@wpmed92

wpmed92 commented Feb 16, 2025

Copy link
Copy Markdown
Contributor Author

@Korijn
The reason for the removal has nothing to do with something wgpu-py did wrong. I love wgpu-py and used it as a reference when rewriting our runtime with Dawn (see read_buffer implementation as an example).
The removal has to do with the general direction of the project and the tinygrad philosophy: reducing complexity, and removing dependencies.
We try to not use python interface libs, i.e. libs that provide a wrapper interface around a binary. Instead, we autogen this interface with clang2py, and we only depend on the binary (Dawn in this case). As to why we chose dawn and not wgpu as the lib: dawn is more mature than wgpu, and we want our webgpu runtime to behave as close as possible to how an exported model behaves in Google Chrome, the most widely used browser.
We also removed llvmlite and use autogened llvm instead. We also use autogened opencl, etc.
As to what could be better, but it’s still not wgu-py: I think that maybe wgpu (not wgpu-py) is moving a bit slowly, f16 is still not merged although it’s close. (the pr is open since last may)
cc: @geohot

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

Labels

bounty locked Bounty is locked to someone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants