-
Notifications
You must be signed in to change notification settings - Fork 2
Use dlpack for array interop
#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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
702ec12
Use dlpack for array interop
rejuvyesh 22c5967
more tests
rejuvyesh 65d8036
rebase but tests are flaky
rejuvyesh 890c517
refactor to avoid repetition
rejuvyesh a3b3995
missed import
rejuvyesh 42a1fc5
small change to enable later use for gpu
rejuvyesh ddf2b7e
update for DLPack.jl refactor
rejuvyesh 7888b40
update jax for new DLPack.jl
rejuvyesh 4d09900
latest DLPack; all issues resolved
rejuvyesh 5e93561
minor cleanup
rejuvyesh 923a8b5
update for upcoming DLPack interface for sharing jlarrays to python
rejuvyesh 8e10397
get jax cuda working
rejuvyesh 3de2ec2
start getting ready for GPUs
rejuvyesh fd81252
fix for new version
rejuvyesh afb9f5b
acknowledgement
rejuvyesh ab108fc
minor cleanup
rejuvyesh 4b29709
use device
rejuvyesh 8dcff72
fix for DLPack's share interface for PyCall
rejuvyesh 58c2b00
update version since we are DLPack based now
rejuvyesh 7158777
add link
rejuvyesh 5eefee9
make CUDA optional
rejuvyesh 3873560
relax tolerance
rejuvyesh b2ef824
relax CUDA versions
rejuvyesh 4c064a7
update version requirements
rejuvyesh 5ab994d
simply adapt
rejuvyesh 18c56f3
update readme for gpu
rejuvyesh 6493802
update readme
rejuvyesh fe00e4c
improve jax install instructions
rejuvyesh c1223df
add basic kwargs support
rejuvyesh 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,11 @@ authors = ["rejuvyesh <[email protected]> and contributors"] | |
| version = "0.1.1" | ||
|
|
||
| [deps] | ||
| CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" | ||
| ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" | ||
| DLPack = "53c2dc0f-f7d5-43fd-8906-6c0220547083" | ||
| FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" | ||
| Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196" | ||
| PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" | ||
| Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
|
|
||
|
|
||
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
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 |
|---|---|---|
| @@ -1,36 +1,85 @@ | ||
| using PyCallChainRules.Jax: JaxFunctionWrapper, jax, numpy, stax, reversedims, ispysetup | ||
| using PyCallChainRules: ReverseDimsArray | ||
|
|
||
| using PyCallChainRules.Jax: JaxFunctionWrapper, jax, numpy, stax, pyto_dlpack, pyfrom_dlpack, ispysetup | ||
|
|
||
| using Test | ||
| using ChainRulesTestUtils | ||
|
|
||
| using Zygote | ||
| using ChainRulesCore: NoTangent | ||
| using CUDA | ||
| using Random | ||
| using PyCall | ||
| using DLPack | ||
| #using Flux | ||
|
|
||
|
|
||
|
|
||
| if !ispysetup[] | ||
| return | ||
| end | ||
|
|
||
|
|
||
| function reversedims(a::AbstractArray{T,N}) where {T<:AbstractFloat,N} | ||
| permutedims(a, N:-1:1) | ||
| end | ||
|
|
||
| @testset "dlpack" begin | ||
| key = jax.random.PRNGKey(0) | ||
| for dims in ((10,), (1, 10), (2, 3, 5), (2, 3, 4, 5)) | ||
| xto = jax.random.normal(key, dims) | ||
| xjl = DLPack.wrap(xto, pyto_dlpack) | ||
| @test Tuple(xto.shape) == reverse(size(xjl)) | ||
| @test isapprox(sum(numpy.array(xto)), sum(xjl)) | ||
| end | ||
| end | ||
|
|
||
| batchsize = 1 | ||
| indim = 3 | ||
| outdim = 2 | ||
|
|
||
| init_lin, apply_lin = stax.Dense(outdim) | ||
| _, params = init_lin(jax.random.PRNGKey(0), (-1, indim)) | ||
| params_np = map(reversedims ∘ numpy.array, params) | ||
| params_np = map(x->((DLPack.wrap(x, pyto_dlpack))), params) | ||
| linwrap = JaxFunctionWrapper(apply_lin) | ||
| x = randn(Float32, indim, batchsize) | ||
| if CUDA.functional() | ||
| params_np = map(cu, params_np) | ||
| x = cu(x) | ||
| end | ||
| y = linwrap(params_np, x) | ||
| @test size(y) == (outdim, batchsize) | ||
|
|
||
| # CRTU check TODO | ||
| test_rrule(linwrap, params_np, x; check_inferred=false, check_thunked_output_tangent=false, rtol=1e-4, atol=1e-4) | ||
| #test_rrule(linwrap, params_np, x; check_inferred=false, check_thunked_output_tangent=false, rtol=1e-4, atol=1e-4) | ||
|
|
||
| # Zygote check | ||
| if CUDA.functional() | ||
| params_np = map(cu, params_np) | ||
| x = cu(x) | ||
| end | ||
|
|
||
| grad, = Zygote.gradient(p->sum(linwrap(p, x)), params_np) | ||
| py""" | ||
| import jax | ||
| import jax.numpy as jnp | ||
| def grad(fn, params, x): | ||
| f2 = lambda p, z: jnp.sum(fn(p, z)) | ||
| return jax.grad(f2)(params, x) | ||
| """ | ||
| jaxgrad = map(x->(DLPack.wrap(x, pyto_dlpack)), (py"grad")(apply_lin, params, DLPack.share(x, pyfrom_dlpack))) | ||
| @test length(grad) == length(params_np) | ||
| @test size(grad[1]) == size(params_np[1]) | ||
| @test size(grad[2]) == size(params_np[2]) | ||
| @test isapprox(Array(grad[1]), Array(jaxgrad[1])) | ||
| @test isapprox(Array(grad[2]), Array(jaxgrad[2])) | ||
|
|
||
| grad, = Zygote.gradient(z->sum(linwrap(params_np, z)), x) | ||
| @test size(grad) == size(x) | ||
| @test size(grad) == size(x) | ||
| py""" | ||
| import jax | ||
| import jax.numpy as jnp | ||
| def gradx(fn, params, x): | ||
| f2 = lambda p, z: jnp.sum(fn(p, z)) | ||
| return jax.grad(f2, argnums=(1,))(params, x) | ||
| """ | ||
| jaxgrad = map(x->(DLPack.wrap(x, pyto_dlpack)), (py"gradx")(apply_lin, params, DLPack.share(x, pyfrom_dlpack))) | ||
| @test isapprox(Array(jaxgrad[1]), Array(grad)) |
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 can't be the best way to handle
FillArrays?