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

Consistent sync/async handling, allow more functions to be async for wasm. #1936

Merged
merged 12 commits into from
Jul 2, 2024

Conversation

ArthurBrussee
Copy link
Contributor

@ArthurBrussee ArthurBrussee commented Jun 28, 2024

Pull Request Template

Checklist

  • Confirmed that run-checks all script has been executed.
  • Made sure the book is up to date with changes in this PR.

Changes

Currently, there is a split between wasm/non-wasm targets. Some functions are sync by default, and async just on wasm, which forces you to conditionally compile them. Other functions are always sync, but migth panic on wasm.

This change makes it so that API is consistently not async, but allows some more functions (argwhere, nonzero) to be called as async if need be. All other functions that might need to be async, now instead panic if they really cannot synchronize. This means the wasm-sync flag can be removed - things will "just work" if you use a backend with sync data reads (ndarray), and might get panics on WebGPU, telling you you really have to switch to async. When going through the trouble of async-ifying your code you can now also just keep things async, rather than having to conditionally compile & have futures block within your async loop which is generally not recommend.

Engineering wise, I tried to have the async functions now use "normal" async-ness. Before, it used some mix of a futures, and a "Reader" trait which basically implemented a small Future with continuations etc. This should be a bit more consistent. Performance wise, it all compiles away if the future resolves immediatly (even if the future is boxed).

Some other misc notes:

  • The async sorting functions are gone. We could re-introduce all sorting with async versions, but really sorting isn't inherently async, it just happens that it doesn't have a GPU implementation yet, so it seems like a different kind of beast.
  • MSPC now uses async channels. It's necesarry at least for reading, and mixing a normal and async channel didn't seem necesarry.
  • There was already a futures_intrusive channel as a dep I saw later on. async-channel is more up to data afaik so maybe better to keep that.
  • If in the future WASM can be properly threaded / block / ... it should hopefully now be easier to just deprecate / delete all _async methods as people can rely on the normal API being sync.

Testing

Burn tests, internal project using the async methods on wasm for readbacks.

- Better error messages
- Handle failure when formatting tensors.
- Explain some of the weirdness with the refcell channel
- MSPC channel now handles async
- Use futures_lite instead of pollster. No need to have both dependencies.
- client now has  read_async & read as utility. Might make it easier in the future if read_async can be removed.
- No need to define async arghwere in two places
It's ~5 lines of code to just implement this.
Can also mark them Send now
@antimora
Copy link
Collaborator

Thanks for the PR!

no-tests are failing currently.

Copy link

codecov bot commented Jun 28, 2024

Codecov Report

Attention: Patch coverage is 71.58273% with 79 lines in your changes missing coverage. Please review.

Project coverage is 85.06%. Comparing base (cdd1fa1) to head (bfc9ab9).
Report is 8 commits behind head on main.

Files Patch % Lines
crates/burn-compute/src/channel/mpsc.rs 0.00% 50 Missing ⚠️
crates/burn-autodiff/src/ops/bool_tensor.rs 33.33% 4 Missing ⚠️
crates/burn-common/src/reader.rs 78.94% 4 Missing ⚠️
crates/burn-jit/src/tensor/base.rs 0.00% 4 Missing ⚠️
crates/burn-tensor/src/tensor/api/base.rs 87.50% 4 Missing ⚠️
crates/burn-fusion/src/tensor.rs 50.00% 3 Missing ⚠️
crates/burn-autodiff/src/ops/int_tensor.rs 0.00% 2 Missing ⚠️
crates/burn-tch/src/ops/bool_tensor.rs 50.00% 2 Missing ⚠️
crates/burn-tch/src/ops/int_tensor.rs 0.00% 2 Missing ⚠️
crates/burn-wgpu/src/compute/server.rs 93.33% 2 Missing ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1936      +/-   ##
==========================================
+ Coverage   84.60%   85.06%   +0.46%     
==========================================
  Files         793      793              
  Lines       93814    94555     +741     
==========================================
+ Hits        79368    80432    +1064     
+ Misses      14446    14123     -323     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@nathanielsimard nathanielsimard left a comment

Choose a reason for hiding this comment

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

Just found a couple of typos, but otherwise I think it's going to be easier this way to handle asyncness on wasm. Normaly feature flags should be additive, not break an API, so this fixes that.

crates/burn-tensor/src/tensor/api/base.rs Outdated Show resolved Hide resolved
crates/burn-tensor/src/tensor/api/sort.rs Outdated Show resolved Hide resolved
crates/burn-tensor/src/tensor/api/sort.rs Outdated Show resolved Hide resolved
@ArthurBrussee
Copy link
Contributor Author

Thanks for the quick review, glad you think this can work until WASM sorts out the woeful threading situation :) Fixed the typos.

@nathanielsimard nathanielsimard merged commit 849c8f4 into tracel-ai:main Jul 2, 2024
14 checks passed
@ArthurBrussee ArthurBrussee deleted the async-sync-read branch July 2, 2024 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants