Skip to content
This repository was archived by the owner on Mar 12, 2021. It is now read-only.
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
15 changes: 9 additions & 6 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,18 @@ Adapt.adapt_storage(::CUDAnative.Adaptor, xs::CuArray{T,N}) where {T,N} =
# We don't convert isbits types in `adapt`, since they are already
# considered GPU-compatible.

Adapt.adapt_storage(::Type{<:CuArray}, xs::AbstractArray) =
Adapt.adapt_storage(::Type{CuArray}, xs::AbstractArray) =
isbits(xs) ? xs : convert(CuArray, xs)

Adapt.adapt_storage(::Type{<:CuArray{T}}, xs::AbstractArray{<:Real}) where T <: AbstractFloat =
# aggressively convert arrays of floats to float32
Adapt.adapt_storage(::Type{CuArray}, xs::AbstractArray{<:AbstractFloat}) =
isbits(xs) ? xs : convert(CuArray{Float32}, xs)

# if an element type is specified, convert to it
Adapt.adapt_storage(::Type{<:CuArray{T}}, xs::AbstractArray) where {T} =
isbits(xs) ? xs : convert(CuArray{T}, xs)

Adapt.adapt_storage(::Type{<:Array}, xs::CuArray) = convert(Array, xs)
Adapt.adapt_storage(::Type{Array}, xs::CuArray) = convert(Array, xs)

Base.collect(x::CuArray{T,N}) where {T,N} = copyto!(Array{T,N}(undef, size(x)), x)

Expand Down Expand Up @@ -311,9 +316,7 @@ end

## utilities

cu(xs) = adapt(CuArray{Float32}, xs)
cu(::Type{Array{T,N}}) where {T,N} = CuArray{T,N,Nothing}
cu(::Type{Array{T}}) where {T} = CuArray{T}
cu(xs) = adapt(CuArray, xs)
Base.getindex(::typeof(cu), xs...) = CuArray([xs...])

zeros(T::Type, dims...) = fill!(CuArray{T}(undef, dims...), 0)
Expand Down
2 changes: 1 addition & 1 deletion src/dnn/rnn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# GRU: [weight, bias] × [input, hidden] × [reset, update, newmem]
# LSTM: [weight, bias] × [input, hidden] × [input, forget, newmem, output]

using LinearAlgebra: copy_transpose!
using LinearAlgebra

function params(w::CuVector, input, hidden, n = 1)
slice(offset, shape) = reshape(view(w, offset.+(1:prod(shape))), shape)
Expand Down
10 changes: 7 additions & 3 deletions test/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ end
@test cu(1:3) === 1:3
@test Base.elsize(xs) == sizeof(Int)
@test CuArray{Int, 2}(xs) === xs
@test cu(Array{Float64,1}) == CuArray{Float64,1, Nothing}
@test cu(Array{Float64,4}) == CuArray{Float64,4, Nothing}
@test cu(Array{Float64}) == CuArray{Float64}

# test aggressive conversion to Float32, but only for floats
@test cu([1]) isa AbstractArray{Int}
@test cu(Float64[1]) isa AbstractArray{Float32}

@test_throws ArgumentError Base.unsafe_convert(Ptr{Int}, xs)
@test_throws ArgumentError Base.unsafe_convert(Ptr{Float32}, xs)
Expand Down Expand Up @@ -241,6 +242,9 @@ end
@test testf((x,y)->copyto!(y, selectdim(x, 2, 1)), ones(2,2,2), zeros(2,2))
## inability to copyto! smaller destination
@test testf((x,y)->copyto!(y, selectdim(x, 2, 1)), ones(2,2,2), zeros(3,3))

# but in conversion of indices (#506)
show(devnull, cu(view(ones(1), [1])))
end

@testset "reshape" begin
Expand Down