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

Fix Conv transfer to AMDGPU #2235

Merged
merged 3 commits into from
Apr 23, 2023
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
cuDNN = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"

[compat]
AMDGPU = "0.4.8"
AMDGPU = "0.4.13"
Adapt = "3.0"
CUDA = "3, 4"
ChainRulesCore = "1.12"
Expand Down
69 changes: 25 additions & 44 deletions ext/AMDGPUExt/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,70 +26,51 @@ function ChainRulesCore.rrule(
adapt_storage(FluxAMDAdaptor(), unthunk(dx)))
end

function _amd(x)
check_use_amdgpu()
USE_AMDGPU[] || return x
fmap(x -> Adapt.adapt(FluxAMDAdaptor(), x), x; exclude=_isleaf)
end

# Since MIOpen supports only cross-correlation as convolution,
# for the actual convolution, we flip horizontally and vertically the weights.
# Same for CPU -> GPU & GPU -> CPU movements.
# Note, that gradients are also flipped.

# CPU -> GPU
const FLUX_CONV{M} = Union{
Flux.Conv{<:Any, <:Any, <:Any, <:M, <:Any},
Flux.ConvTranspose{<:Any, <:Any, <:Any, <:M, <:Any}}
const CPU_CONV = FLUX_CONV{Array}
const AMD_CONV = FLUX_CONV{ROCArray}

_conv_basetype(::Conv) = Conv
_conv_basetype(::ConvTranspose) = ConvTranspose

_conv_basetype(c::Type{C}) where C <: Conv = Conv
_conv_basetype(c::Type{C}) where C <: ConvTranspose = ConvTranspose
Flux._isleaf(::AMD_CONV) = true

function adapt_storage(to::FluxAMDAdaptor, m::C) where C <: Union{Conv, ConvTranspose}
_exclude(x) = _isleaf(x)
_exclude(::CPU_CONV) = true

function _amd(x)
check_use_amdgpu()
USE_AMDGPU[] || return x
fmap(x -> Adapt.adapt(FluxAMDAdaptor(), x), x; exclude=_exclude)
end

# CPU -> GPU

function Adapt.adapt_structure(to::FluxAMDAdaptor, m::CPU_CONV)
flipped_weight = reverse(m.weight; dims=ntuple(i -> i, ndims(m.weight) - 2))
_conv_basetype(C)(
_conv_basetype(m)(
Adapt.adapt(to, m.σ),
Adapt.adapt(to, flipped_weight),
Adapt.adapt(to, m.bias),
m.stride, m.pad, m.dilation, m.groups)
end

# Don't adapt again.
function adapt_storage(
to::FluxAMDAdaptor, m::Conv{N, M, F, A, V},
) where {N, M, F, A <: ROCArray, V}
return m
end

function adapt_storage(
to::FluxAMDAdaptor, m::ConvTranspose{N, M, F, A, V},
) where {N, M, F, A <: ROCArray, V}
return m
end

_amd(m::Union{Conv, ConvTranspose}) = adapt_storage(FluxAMDAdaptor(), m)
Adapt.adapt_structure(to::FluxAMDAdaptor, m::AMD_CONV) = m

# GPU -> CPU

function Flux.cpu(m::Conv{N, M, F, A, V}) where {N, M, F, A <: ROCArray, V}
adapt_storage(FluxCPUAdaptor(), m)
end

function Flux.cpu(m::ConvTranspose{N, M, F, A, V}) where {N, M, F, A <: ROCArray, V}
adapt_storage(FluxCPUAdaptor(), m)
end

function adapt_storage(
to::FluxCPUAdaptor, m::Conv{N, M, F, A, V},
) where {N, M, F, A <: ROCArray, V}
dims = ntuple(i -> i, ndims(m.weight) - 2)
Conv(
Adapt.adapt(to, m.σ), reverse(Adapt.adapt(to, m.weight); dims),
Adapt.adapt(to, m.bias), m.stride, m.pad, m.dilation, m.groups)
end

function adapt_storage(
to::FluxCPUAdaptor, m::ConvTranspose{N, M, F, A, V},
) where {N, M, F, A <: ROCArray, V}
function Adapt.adapt_structure(to::FluxCPUAdaptor, m::AMD_CONV)
dims = ntuple(i -> i, ndims(m.weight) - 2)
ConvTranspose(
_conv_basetype(m)(
Adapt.adapt(to, m.σ), reverse(Adapt.adapt(to, m.weight); dims),
Adapt.adapt(to, m.bias), m.stride, m.pad, m.dilation, m.groups)
end
18 changes: 17 additions & 1 deletion test/amd/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,26 @@ end
end
end

@testset "Chain(Conv)" begin
m = Chain(Conv((3, 3), 3 => 3)) |> f32
x = rand(Float32, 10, 10, 3, 2)
gpu_autodiff_test(m, x; atol=1f-3, checkgrad=false)

md = m |> gpu |> cpu
@test md[1].weight ≈ m[1].weight atol=1f-3

m = Chain(ConvTranspose((3, 3), 3 => 3)) |> f32
x = rand(Float32, 10, 10, 3, 2)
gpu_autodiff_test(m, x; atol=1f-3, checkgrad=false)

md = m |> gpu |> cpu
@test md[1].weight ≈ m[1].weight atol=1f-3
end

@testset "Cross-correlation" begin
m = CrossCor((2, 2), 3 => 4) |> f32
x = rand(Float32, 10, 10, 3, 2)
gpu_autodiff_test(m, x)
gpu_autodiff_test(m, x; atol=1f-3)
end

@testset "Restructure" begin
Expand Down
14 changes: 4 additions & 10 deletions test/amd/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
Flux.gpu_backend!("AMD")

AMDGPU.allowscalar(false)

# Extend test utils to AMDGPU.

function check_grad(
g_gpu::ROCArray{Float32}, g_cpu::Array{Float32}, atol, rtol;
allow_nothing::Bool,
g_gpu::ROCArray{Float32}, g_cpu::Array{Float32};
atol, rtol, allow_nothing::Bool,
)
@test g_cpu ≈ collect(g_gpu) atol=atol rtol=rtol
end

function check_grad(
g_gpu::ROCArray{Float32}, g_cpu::Zygote.FillArrays.AbstractFill,
atol, rtol; allow_nothing::Bool,
g_gpu::ROCArray{Float32}, g_cpu::Zygote.FillArrays.AbstractFill;
atol, rtol, allow_nothing::Bool,
)
@test g_cpu ≈ collect(g_gpu) atol=atol rtol=rtol
end
Expand Down
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ Random.seed!(0)

if get(ENV, "FLUX_TEST_AMDGPU", "false") == "true"
using AMDGPU
AMDGPU.versioninfo()
Flux.gpu_backend!("AMD")
AMDGPU.allowscalar(false)

if AMDGPU.functional() && AMDGPU.functional(:MIOpen)
@show AMDGPU.MIOpen.version()
@testset "AMDGPU" begin
include("amd/runtests.jl")
end
Expand Down
1 change: 1 addition & 0 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function check_grad(g_gpu, g_cpu;
rtol=1e-4, atol=1e-4,
allow_nothing::Bool=false)
allow_nothing && return
@warn "Unsupported types in `check_grad`: $(typeof(g_gpu)), $(typeof(g_cpu))"
@show g_gpu g_cpu
@test false
end
Expand Down