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

Enzyme: add make_zero of cuarrays #2600

Merged
merged 9 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ steps:
build.message !~ /\[only/ && !build.pull_request.draft &&
build.message !~ /\[skip tests\]/ &&
build.message !~ /\[skip downstream\]/
timeout_in_minutes: 30
soft_fail: true
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong: 6bdb146

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah okay will fix, but also I’m now confused from a high level. There an obvious undef var usage in the patch (which I will fix), which breaks CI but I never hit locally just running test.

is this not run as part of the default tests?

timeout_in_minutes: 60
soft_fail:
- exit_status: 3

- group: ":eyes: Special"
depends_on: "cuda"
Expand Down
53 changes: 53 additions & 0 deletions ext/EnzymeCoreExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,59 @@ function EnzymeCore.EnzymeRules.noalias(::Type{CT}, ::UndefInitializer, args...)
return nothing
end

@inline function EnzymeCore.make_zero(
x::DenseCuArray{FT},
) where {FT<:AbstractFloat}
return Base.zero(x)
end
@inline function EnzymeCore.make_zero(
x::DenseCuArray{Complex{FT}},
) where {FT<:AbstractFloat}
return Base.zero(x)
end

@inline function EnzymeCore.make_zero(
::Type{CT},
seen::IdDict,
prev::CT,
::Val{copy_if_inactive} = Val(false),
)::CT where {copy_if_inactive, FT<:AbstractFloat, CT <: Union{DenseCuArray{FT},DenseCuArray{Complex{FT}}}}
if haskey(seen, prev)
return seen[prev]
end
newa = Base.zero(prev)
seen[prev] = newa
return newa
end

@inline function EnzymeCore.make_zero!(
prev::DenseCuArray{FT},
seen::ST,
)::Nothing where {FT<:AbstractFloat,ST}
if !isnothing(seen)
if prev in seen
return nothing
end
push!(seen, prev)
end
fill!(prev, zero(FT))
return nothing
end

@inline function EnzymeCore.make_zero!(
prev::DenseCuArray{Complex{FT}},
seen::ST,
)::Nothing where {FT<:AbstractFloat,ST}
if !isnothing(seen)
if prev in seen
return nothing
end
push!(seen, prev)
end
fill!(prev, zero(Complex{FT}))
return nothing
end

function EnzymeCore.EnzymeRules.forward(config, ofn::Const{typeof(GPUArrays.mapreducedim!)},
::Type{RT},
f::EnzymeCore.Const{typeof(Base.identity)},
Expand Down
9 changes: 9 additions & 0 deletions test/extensions/enzyme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ using CUDA
@test EnzymeCore.compiler_job_from_backend(CUDABackend(), typeof(()->nothing), Tuple{}) isa GPUCompiler.CompilerJob
end

@testset "Make_zero" begin
A = CUDA.ones(64)
dA = Enzyme.make_zero(A)
@test all(dA .≈ 0)
dA = CUDA.ones(64)
Enzyme.make_zero!(dA)
@test all(dA .≈ 0)
end

function square_kernel!(x)
i = threadIdx().x
x[i] *= x[i]
Expand Down
Loading