-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from jax.interpreters import xla, mlir | ||
from jax.lib import xla_client | ||
from jaxlib.hlo_helpers import custom_call | ||
from functools import partial | ||
from brainpy._src.dependency_check import (import_cupy) | ||
from brainpy.errors import PackageMissingError | ||
|
||
cp = import_cupy(error_if_not_found=False) | ||
|
||
|
||
def _cupy_xla_gpu_translation_rule(kernel, c, *args, **kwargs): | ||
# TODO: implement the translation rule | ||
mod = cp.RawModule(code=kernel) | ||
# compile | ||
try: | ||
kernel_ptr = mod.get_function('kernel') | ||
except AttributeError: | ||
raise ValueError('The \'kernel\' function is not found in the module.') | ||
return xla_client.ops.CustomCallWithLayout( | ||
c, | ||
b'cupy_kernel_call_gpu', | ||
|
||
) | ||
... | ||
|
||
|
||
def register_cupy_xla_gpu_translation_rule(primitive, gpu_kernel): | ||
xla.backend_specific_translations['gpu'][primitive] = partial(_cupy_xla_gpu_translation_rule, gpu_kernel) | ||
|
||
|
||
def _cupy_mlir_gpu_translation_rule(kernel, c, *args, **kwargs): | ||
# TODO: implement the translation rule | ||
... | ||
|
||
def register_cupy_mlir_gpu_translation_rule(primitive, gpu_kernel): | ||
if cp is None: | ||
raise PackageMissingError("cupy", 'register cupy mlir gpu translation rule') | ||
|
||
rule = partial(_cupy_mlir_gpu_translation_rule, gpu_kernel) | ||
mlir.register_primitive_rule(primitive, rule, platform='gpu') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import jax.numpy as jnp | ||
import jax | ||
import cupy as cp | ||
from time import time | ||
|
||
import brainpy.math as bm | ||
from brainpy._src.math import as_jax | ||
bm.set_platform('gpu') | ||
|
||
time1 = time() | ||
a = bm.random.rand(4, 4) | ||
time2 = time() | ||
c = cp.from_dlpack(jax.dlpack.to_dlpack(as_jax(a))) | ||
time3 = time() | ||
|
||
c *= c | ||
print(f'c: {c}') | ||
print(f'a: {a}') |