Skip to content
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

Formatting in extensions #255

Merged
merged 24 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
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 v1"
plugins:
- JuliaCI/julia#v1:
version: "1"
- JuliaCI/julia-test#v1: ~
- JuliaCI/julia-coverage#v1:
codecov: true
dirs:
- src
- ext
agents:
queue: "juliagpu"
cuda: "*"
Expand Down
121 changes: 61 additions & 60 deletions ext/MooncakeCUDAExt.jl
Original file line number Diff line number Diff line change
@@ -1,71 +1,72 @@
module MooncakeCUDAExt

using LinearAlgebra, Random, Mooncake
using LinearAlgebra, Random, Mooncake

using Base: IEEEFloat
using CUDA: CuArray, cu
using Base: IEEEFloat
using CUDA: CuArray, cu

import Mooncake:
MinimalCtx,
rrule!!,
@is_primitive,
tangent_type,
zero_tangent,
randn_tangent,
increment!!,
set_to_zero!!,
_add_to_primal,
_diff,
_dot,
_scale,
TestUtils,
CoDual,
NoPullback
import Mooncake:
MinimalCtx,
rrule!!,
@is_primitive,
tangent_type,
zero_tangent,
randn_tangent,
increment!!,
set_to_zero!!,
_add_to_primal,
_diff,
_dot,
_scale,
TestUtils,
CoDual,
NoPullback

import Mooncake.TestUtils: populate_address_map!, AddressMap, __increment_should_allocate
import Mooncake.TestUtils: populate_address_map!, AddressMap, __increment_should_allocate

# Tell Mooncake.jl how to handle CuArrays.
# Tell Mooncake.jl how to handle CuArrays.

tangent_type(::Type{P}) where {P<:CuArray{<:IEEEFloat}} = P
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
increment!!(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = x .+= y
__increment_should_allocate(::Type{<:CuArray{<:IEEEFloat}}) = true
set_to_zero!!(x::CuArray{<:IEEEFloat}) = x .= 0
_add_to_primal(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = x + y
_diff(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = x - y
_dot(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = Float64(dot(x, y))
_scale(x::Float64, y::P) where {T<:IEEEFloat, P<:CuArray{T}} = T(x) * y
function populate_address_map!(m::AddressMap, p::CuArray, t::CuArray)
k = pointer_from_objref(p)
v = pointer_from_objref(t)
haskey(m, k) && (@assert m[k] == v)
m[k] = v
return m
end
function Mooncake._verify_fdata_value(p::CuArray, f::CuArray)
if size(p) != size(f)
throw(InvalidFDataException("p has size $(size(p)) but f has size $(size(f))"))
end
return nothing
tangent_type(::Type{P}) where {P<:CuArray{<:IEEEFloat}} = P
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
increment!!(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = x .+= y
__increment_should_allocate(::Type{<:CuArray{<:IEEEFloat}}) = true
set_to_zero!!(x::CuArray{<:IEEEFloat}) = x .= 0
_add_to_primal(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = x + y
_diff(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = x - y
_dot(x::P, y::P) where {P<:CuArray{<:IEEEFloat}} = Float64(dot(x, y))
_scale(x::Float64, y::P) where {T<:IEEEFloat, P<:CuArray{T}} = T(x) * y
function populate_address_map!(m::AddressMap, p::CuArray, t::CuArray)
k = pointer_from_objref(p)
v = pointer_from_objref(t)
haskey(m, k) && (@assert m[k] == v)
m[k] = v
return m
end
function Mooncake._verify_fdata_value(p::CuArray, f::CuArray)
if size(p) != size(f)
throw(InvalidFDataException("p has size $(size(p)) but f has size $(size(f))"))

Check warning on line 51 in ext/MooncakeCUDAExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MooncakeCUDAExt.jl#L49-L51

Added lines #L49 - L51 were not covered by tests
end
return nothing

Check warning on line 53 in ext/MooncakeCUDAExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MooncakeCUDAExt.jl#L53

Added line #L53 was not covered by tests
end

# Basic rules for operating on CuArrays.
# Basic rules for operating on CuArrays.

@is_primitive(
MinimalCtx,
Tuple{Type{<:CuArray}, UndefInitializer, Vararg{Int, N}} where {N},
)
function rrule!!(
p::CoDual{Type{P}},
init::CoDual{UndefInitializer},
dims::CoDual{Int}...
) where {P<:CuArray{<:Base.IEEEFloat}}
_dims = map(primal, dims)
y = CoDual(P(undef, _dims), P(undef, _dims))
return y, NoPullback(p, init, dims...)
end

@is_primitive(
MinimalCtx,
Tuple{Type{<:CuArray}, UndefInitializer, Vararg{Int, N}} where {N},
)
function rrule!!(
p::CoDual{Type{P}},
init::CoDual{UndefInitializer},
dims::CoDual{Int}...
) where {P<:CuArray{<:Base.IEEEFloat}}
_dims = map(primal, dims)
y = CoDual(P(undef, _dims), P(undef, _dims))
return y, NoPullback(p, init, dims...)
end
end
13 changes: 7 additions & 6 deletions ext/MooncakeSpecialFunctionsExt.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module MooncakeSpecialFunctionsExt

using SpecialFunctions, Mooncake
using SpecialFunctions, Mooncake

import Mooncake: @from_rrule, DefaultCtx
import Mooncake: @from_rrule, DefaultCtx

@from_rrule DefaultCtx Tuple{typeof(airyai), Float64}
@from_rrule DefaultCtx Tuple{typeof(airyaix), Float64}
@from_rrule DefaultCtx Tuple{typeof(erfc), Float64}
@from_rrule DefaultCtx Tuple{typeof(erfcx), Float64}

@from_rrule DefaultCtx Tuple{typeof(airyai), Float64}
@from_rrule DefaultCtx Tuple{typeof(airyaix), Float64}
@from_rrule DefaultCtx Tuple{typeof(erfc), Float64}
@from_rrule DefaultCtx Tuple{typeof(erfcx), Float64}
end
4 changes: 2 additions & 2 deletions test/integration_testing/cuda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ using CUDA
# Check we can operate on CuArrays.
test_tangent(
Xoshiro(123456),
CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}(undef, 8, 8);
CuArray{Float32, 2, CUDA.DeviceMemory}(undef, 8, 8);
interface_only=false,
)

# Check we can instantiate a CuArray.
test_rule(
sr(123456), CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, undef, 256;
sr(123456), CuArray{Float32, 1, CUDA.DeviceMemory}, undef, 256;
interface_only=true, is_primitive=true,
willtebbutt marked this conversation as resolved.
Show resolved Hide resolved
)
end
Loading