Skip to content

Commit

Permalink
NNlib on the GPU (#435)
Browse files Browse the repository at this point in the history
* nnlib tweaks

* Proper cuda tests

* Depend on GPUArraysCore

* Tweak NNlib implementation

* Extra methods in CUDA

* Make tuple_map always generated

* Make tuple_map always generated

* Remove redundant code

* Tidy up build_tangent implementation

* Add failing test cases

* Tweak CUDA test file

* Revert stuff added in a different PR

* Tweaks to CUDA tests

* Fix zero_rdata_from_type

* Fix formatting

* Make nnlib tests pass

* Renable CuArray construction test

* Make buildkite actually run nnlib tests

* Fix display

* Formatting

* CI formatting

* Revert tuple_map implementation change

* Revert changes to tuple_map entirely

* Bump patch version
  • Loading branch information
willtebbutt authored Jan 9, 2025
1 parent 35b432c commit 094dca6
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 99 deletions.
15 changes: 10 additions & 5 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ env:
SECRET_CODECOV_TOKEN: "nkcRFVXdaPNAbiI0x3qK/XUG8rWjBc8fU73YEyP35SeS465XORqrIYrHUbHuJTRyeyqNRdsHaBcV1P7TBbKAaTQAjHQ1Q0KYfd0uRMSWpZSCgTBz5AwttAxVfFrX+Ky3PzTi2TfDe0uPFZtFo0Asq6sUEr1on+Oo+j+q6br2NK6CrA5yKKuTX4Q2V/UPOIK4vNXY3+zDTKSNtr+HQOlcVEeRIk/0ZQ78Cjd52flEaVw8GWo/CC4YBzLtcOZgaFdgOTEDNHMr0mw6zLE4Y6nxq4lHVSoraSjxjhkB0pXTZ1c51yHX8Jc+q6HC5s87+2Zq5YtsuQSGao+eMtkTAYwfLw==;U2FsdGVkX18z27J3+gNgxsPNnXA0ad4LvZnXeohTam7/6UPqX5+3BYI0tAiVkCho4vlJyL7dd8JEyNtk9BFXsg=="

steps:
- label: "Julia v{{matrix}}"
- label: "Julia v{{matrix.version}}, {{matrix.label}}"
plugins:
- JuliaCI/julia#v1:
version: "{{matrix}}"
version: "{{matrix.version}}"
- JuliaCI/julia-coverage#v1:
dirs:
- src
Expand All @@ -17,8 +17,13 @@ steps:
if: build.message !~ /\[skip tests\]/
timeout_in_minutes: 60
env:
LABEL: cuda
LABEL: "{{matrix.label}}"
TEST_TYPE: ext
matrix:
- "1"
- "1.10"
setup:
version:
- "1"
- "1.10"
label:
- "cuda"
- "nnlib"
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Mooncake"
uuid = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
authors = ["Will Tebbutt, Hong Ge, and contributors"]
version = "0.4.76"
version = "0.4.77"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand All @@ -11,6 +11,7 @@ DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
DiffTests = "de460e47-3fe3-5279-bb4a-814414816d5d"
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e"
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down Expand Up @@ -49,6 +50,7 @@ DiffRules = "1"
DiffTests = "0.1"
ExprTools = "0.1"
FunctionWrappers = "1.1.3"
GPUArraysCore = "0.1"
Graphs = "1"
InteractiveUtils = "1"
JET = "0.9"
Expand Down
19 changes: 17 additions & 2 deletions ext/MooncakeCUDAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Mooncake:
rrule!!,
@is_primitive,
tangent_type,
tangent,
zero_tangent,
randn_tangent,
increment!!,
Expand All @@ -20,7 +21,9 @@ import Mooncake:
_scale,
TestUtils,
CoDual,
NoPullback
NoPullback,
to_cr_tangent,
increment_and_get_rdata!

import Mooncake.TestUtils: populate_address_map!, AddressMap, __increment_should_allocate

Expand All @@ -31,7 +34,11 @@ zero_tangent(x::CuArray{<:IEEEFloat}) = zero(x)
function randn_tangent(rng::AbstractRNG, x::CuArray{Float32})
return cu(randn(rng, Float32, size(x)...))
end
TestUtils.has_equal_data(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = x == y
function TestUtils.has_equal_data_internal(
x::P, y::P, equal_undefs::Bool, d::Dict{Tuple{UInt,UInt},Bool}
) where {P<:CuArray{<:IEEEFloat}}
return isapprox(x, y)
end
increment!!(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = x .+= y
__increment_should_allocate(::Type{<:CuArray{<:IEEEFloat}}) = true
_set_to_zero!!(::Mooncake.IncCache, x::CuArray{<:IEEEFloat}) = x .= 0
Expand All @@ -52,6 +59,14 @@ function Mooncake._verify_fdata_value(p::CuArray, f::CuArray)
end
return nothing
end
tangent_type(::Type{P}, ::Type{NoRData}) where {P<:CuArray} = P
tangent(p::CuArray, ::NoRData) = p

to_cr_tangent(x::CuArray{<:IEEEFloat}) = x
function increment_and_get_rdata!(f::T, ::NoRData, t::T) where {T<:CuArray{<:IEEEFloat}}
f .+= t
return NoRData()
end

# Basic rules for operating on CuArrays.

Expand Down
51 changes: 36 additions & 15 deletions ext/MooncakeNNlibExt.jl
Original file line number Diff line number Diff line change
@@ -1,54 +1,75 @@
module MooncakeNNlibExt

using NNlib, Random, Mooncake
using GPUArraysCore, NNlib, Random, Mooncake
using Base: IEEEFloat
using NNlib: dropout

using NNlib: conv, depthwiseconv
import Mooncake: @from_rrule, DefaultCtx, MinimalCtx

# Array types which we test rules against, so are confident work.
const SupportedArray{P,N} = Union{Array{P,N},AbstractGPUArray{P,N}}

@from_rrule(
MinimalCtx, Tuple{typeof(batched_mul),Array{P,3},Array{P,3}} where {P<:IEEEFloat},
MinimalCtx,
Tuple{typeof(batched_mul),SupportedArray{P,3},SupportedArray{P,3}} where {P<:IEEEFloat},
)
@from_rrule(
MinimalCtx, Tuple{typeof(dropout),AbstractRNG,Array{P},P} where {P<:IEEEFloat}, true,
MinimalCtx,
Tuple{typeof(dropout),AbstractRNG,SupportedArray{P},P} where {P<:IEEEFloat},
true,
)
@from_rrule(MinimalCtx, Tuple{typeof(softmax),Array{<:IEEEFloat}}, true)
@from_rrule(MinimalCtx, Tuple{typeof(logsoftmax),Array{<:IEEEFloat}}, true)
@from_rrule(MinimalCtx, Tuple{typeof(logsumexp),Array{<:IEEEFloat}}, true)
@from_rrule(MinimalCtx, Tuple{typeof(softmax),SupportedArray{<:IEEEFloat}}, true)
@from_rrule(MinimalCtx, Tuple{typeof(logsoftmax),SupportedArray{<:IEEEFloat}}, true)
@from_rrule(MinimalCtx, Tuple{typeof(logsumexp),SupportedArray{<:IEEEFloat}}, true)
@from_rrule(
MinimalCtx, Tuple{typeof(upsample_nearest),Array{<:IEEEFloat},NTuple{N,Int} where {N}},
MinimalCtx,
Tuple{typeof(upsample_nearest),SupportedArray{<:IEEEFloat},NTuple{N,Int} where {N}},
)
@from_rrule(
MinimalCtx,
Tuple{typeof(NNlib.fold),Array{<:IEEEFloat},NTuple{N,Int} where {N},DenseConvDims},
Tuple{
typeof(NNlib.fold),SupportedArray{<:IEEEFloat},NTuple{N,Int} where {N},DenseConvDims
},
)
@from_rrule(MinimalCtx, Tuple{typeof(NNlib.unfold),Array{<:IEEEFloat},DenseConvDims})
@from_rrule(
MinimalCtx, Tuple{typeof(NNlib.scatter),Any,Array,Array{<:Union{Integer,Tuple}}}, true,
MinimalCtx, Tuple{typeof(NNlib.unfold),SupportedArray{<:IEEEFloat},DenseConvDims}
)
@from_rrule(
MinimalCtx,
Tuple{typeof(NNlib.scatter),Any,SupportedArray,SupportedArray{<:Union{Integer,Tuple}}},
true,
)
for conv in [:conv, :depthwiseconv]
local ∇conv_data, ∇conv_filter = Symbol.(:∇, conv, [:_data, :_filter])

@eval @from_rrule(
MinimalCtx,
Tuple{typeof($conv),Array{P},Array{P},ConvDims} where {P<:IEEEFloat},
Tuple{
typeof($conv),SupportedArray{P},SupportedArray{P},ConvDims
} where {P<:IEEEFloat},
true,
)
@eval @from_rrule(
MinimalCtx,
Tuple{typeof($∇conv_data),Array{P},Array{P},ConvDims} where {P<:IEEEFloat},
Tuple{
typeof($∇conv_data),SupportedArray{P},SupportedArray{P},ConvDims
} where {P<:IEEEFloat},
true,
)
end
@from_rrule(
MinimalCtx,
Tuple{typeof(∇conv_filter),Array{P},Array{P},ConvDims} where {P<:IEEEFloat},
Tuple{
typeof(∇conv_filter),SupportedArray{P},SupportedArray{P},ConvDims
} where {P<:IEEEFloat},
true,
)
for pool in [:maxpool, :meanpool]
@eval @from_rrule(MinimalCtx, Tuple{typeof($pool),Array{<:IEEEFloat},PoolDims}, true)
@eval @from_rrule(
MinimalCtx, Tuple{typeof($pool),SupportedArray{<:IEEEFloat},PoolDims}, true
)
end
@from_rrule(MinimalCtx, Tuple{typeof(pad_constant),Array,Any,Any}, true)
@from_rrule(MinimalCtx, Tuple{typeof(pad_constant),SupportedArray,Any,Any}, true)

end
1 change: 1 addition & 0 deletions test/ext/cuda/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
AllocCheck = "9b6a8646-10ed-4001-bbdc-1d2f46dfbb1a"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
Expand Down
12 changes: 5 additions & 7 deletions test/ext/cuda/cuda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ using Pkg
Pkg.activate(@__DIR__)
Pkg.develop(; path=joinpath(@__DIR__, "..", "..", ".."))

using CUDA, JET, Mooncake, StableRNGs, Test
using Mooncake.TestUtils: test_tangent, test_rule
using AllocCheck, CUDA, JET, Mooncake, StableRNGs, Test
using Mooncake.TestUtils: test_tangent_consistency, test_fwds_rvs_data, test_rule

@testset "cuda" begin

# Check we can operate on CuArrays.
test_tangent(
StableRNG(123456),
CuArray{Float32,2,CUDA.DeviceMemory}(undef, 8, 8);
interface_only=false,
)
p = CuArray{Float32,2,CUDA.DeviceMemory}(undef, 8, 8)
test_tangent_consistency(StableRNG(123456), p; interface_only=false)
test_fwds_rvs_data(StableRNG(123456), p)

# Check we can instantiate a CuArray.
test_rule(
Expand Down
2 changes: 2 additions & 0 deletions test/ext/nnlib/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
cuDNN = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"
Loading

2 comments on commit 094dca6

@willtebbutt
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/122705

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.77 -m "<description of version>" 094dca6d18e2ac8de8c59faab522fbbae9551c56
git push origin v0.4.77

Please sign in to comment.