-
Notifications
You must be signed in to change notification settings - Fork 17
Add CuPy interface #218
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
Open
frostedoyster
wants to merge
2
commits into
main
Choose a base branch
from
cupy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add CuPy interface #218
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import argparse | ||
|
|
||
| import cupy as cp | ||
| import numpy as np | ||
|
|
||
| import sphericart | ||
|
|
||
| docstring = """ | ||
| An example of the CuPy interface of the `sphericart` library. | ||
|
|
||
| Computes Cartesian spherical harmonics on the GPU for a random array of 3D | ||
| points and compares the result against the NumPy CPU backend. | ||
| """ | ||
|
|
||
|
|
||
| def sphericart_cupy_example(l_max=10, n_samples=10000): | ||
| xyz_cpu = np.random.rand(n_samples, 3) | ||
| xyz_gpu = cp.asarray(xyz_cpu) | ||
|
|
||
| sh_calculator = sphericart.SphericalHarmonics(l_max) | ||
|
|
||
| sh_cpu = sh_calculator.compute(xyz_cpu) | ||
| sh_gpu = sh_calculator.compute(xyz_gpu) | ||
|
|
||
| print( | ||
| "CPU vs GPU relative error: %12.8e" | ||
| % (np.linalg.norm(sh_cpu - cp.asnumpy(sh_gpu)) / np.linalg.norm(sh_cpu)) | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description=docstring) | ||
|
|
||
| parser.add_argument("-l", type=int, default=10, help="maximum angular momentum") | ||
| parser.add_argument("-s", type=int, default=1000, help="number of samples") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| sphericart_cupy_example(args.l, args.s) |
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import ctypes | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
||
| try: | ||
| import cupy as cp | ||
| from cupy import ndarray as cupy_ndarray | ||
| except ImportError: | ||
| cp = None | ||
|
|
||
| class cupy_ndarray: # type: ignore[no-redef] | ||
| pass | ||
|
|
||
|
|
||
| def is_array(array): | ||
| return isinstance(array, np.ndarray) or isinstance(array, cupy_ndarray) | ||
|
|
||
|
|
||
| def is_cupy_array(array): | ||
| return isinstance(array, cupy_ndarray) | ||
|
|
||
|
|
||
| def make_contiguous(array): | ||
| if isinstance(array, np.ndarray): | ||
| return np.ascontiguousarray(array) | ||
| if isinstance(array, cupy_ndarray): | ||
| return cp.ascontiguousarray(array) | ||
| raise TypeError(f"only numpy and cupy arrays are supported, found {type(array)}") | ||
|
|
||
|
|
||
| def empty_like(shape, array): | ||
| if isinstance(array, np.ndarray): | ||
| return np.empty(shape, dtype=array.dtype) | ||
| if isinstance(array, cupy_ndarray): | ||
| return cp.empty(shape, dtype=array.dtype) | ||
| raise TypeError(f"only numpy and cupy arrays are supported, found {type(array)}") | ||
|
|
||
|
|
||
| def get_pointer(array): | ||
| if array.dtype == np.float32: | ||
| ptr_type = ctypes.POINTER(ctypes.c_float) | ||
| elif array.dtype == np.float64: | ||
| ptr_type = ctypes.POINTER(ctypes.c_double) | ||
| else: | ||
| raise TypeError( | ||
| f"only float32 and float64 arrays are supported, found {array.dtype}" | ||
| ) | ||
|
|
||
| if isinstance(array, np.ndarray): | ||
| return array.ctypes.data_as(ptr_type) | ||
| if isinstance(array, cupy_ndarray): | ||
| return ctypes.cast(array.data.ptr, ptr_type) | ||
| raise TypeError(f"only numpy and cupy arrays are supported, found {type(array)}") | ||
|
|
||
|
|
||
| def get_cuda_stream(array): | ||
| if not isinstance(array, cupy_ndarray): | ||
| return None | ||
| return ctypes.c_void_p(cp.cuda.get_current_stream().ptr) |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine for now, but should we look into simplifying the API a bit? Maybe adding explicit
deviceparameter like vesin, and a separate sph_kind="SphericalHarmonics | SolidHarmonics"?