-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: automatic tensor backend and type checks #64
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pytest test/test_typing.py -s
yxlao
force-pushed
the
yixing/auto-backend
branch
from
July 3, 2024 07:50
5f5d42a
to
8494b36
Compare
yxlao
changed the title
feat: automatic backend selection and improved type checks
feat: automatic tensor backend and type checks
Jul 3, 2024
yxlao
added a commit
that referenced
this pull request
Jul 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Design Goals
Goals for Multi-Backend Support
camtools
should be able to process and return both NumPy arrays and Torch tensors. This is achieved automatically by decorators.camtools
. The library can detect if Torch is installed and enable the corresponding backends.set_backend()
.Goals for Type Checking
camtools
should be type-hinted with the expected tensor shape and dtype.ct.sanity
.Changes in This PR
ivy
,jaxtyping
, andeinops
.ivy
: Interoperates between NumPy and Torch. Used internally only.jaxtyping
: Provides type hinting for the library.ct.backend.get_backend()
andct.backend.set_backend()
to get and set the default backend forcamtools
. This change is global and affects all subsequent invocations of camtools functions.@ct.backend.tensor_auto_backend
tries to derive backend from input arguments. It also converts list inputs to native tensor format if the type hint is a tensor. Seetest/test_backend.py
for examples.@ct.backend.tensor_numpy_backend
and@ct.backend.tensor_torch_backend
wrap functions to enforce using NumPy and Torch backends by converting input tensors to NumPy or Torch, respectively.@ct.backend.tensor_type_check
enforces shape and dtype checks based on the type hints for tensors. Seetest/test_backend.py
for examples. This check will eventually replace most of the functions inct.sanity
.Known Issues
Per our benchmark, the overhead of type-checking and backend conversion is small. However
ivy
introduces significant overheads. We may look for alternative methods, such as lightweight wrappers to wrap numpy/torch to replace ivy in the future.