Skip to content

Commit 090f043

Browse files
authored
fix |> gpu bug in autosize (#2085)
1 parent c7ed5fe commit 090f043

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/outputsize.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ julia> @autosize (img..., 1, 32) Chain( # size is only needed at ru
204204
Dense(_ => _÷4, relu, init=Flux.rand32), # can calculate output size _÷4
205205
SkipConnection(Dense(_ => _, relu), +),
206206
Dense(_ => 10),
207-
) |> gpu # moves to GPU after initialisation
207+
)
208208
Chain(
209209
Chain(
210210
c = Conv((3, 3), 1 => 5, pad=1, stride=2), # 50 parameters
@@ -290,8 +290,6 @@ mutable struct LazyLayer
290290
layer
291291
end
292292

293-
@functor LazyLayer
294-
295293
function (l::LazyLayer)(x::AbstractArray, ys::AbstractArray...)
296294
l.layer === nothing || return l.layer(x, ys...)
297295
made = l.make(x) # for something like `Bilinear((_,__) => 7)`, perhaps need `make(xy...)`, later.
@@ -320,6 +318,9 @@ function ChainRulesCore.rrule(::typeof(striplazy), m)
320318
end
321319

322320
params!(p::Params, x::LazyLayer, seen = IdSet()) = error("LazyLayer should never be used within params(m). Call striplazy(m) first.")
321+
322+
Functors.functor(::Type{<:LazyLayer}, x) = error("LazyLayer should not be walked with Functors.jl, as the arrays which Flux.gpu wants to move may not exist yet.")
323+
323324
function Base.show(io::IO, l::LazyLayer)
324325
printstyled(io, "LazyLayer(", color=:light_black)
325326
if l.layer == nothing

test/outputsize.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ end
220220
Dense(_ => _÷4, relu, init=Flux.rand32), # can calculate output size _÷4
221221
SkipConnection(Dense(_ => _, relu), +),
222222
Dense(_ => 10),
223-
) |> gpu # moves to GPU after initialisation
224-
@test randn(Float32, img..., 1, 32) |> gpu |> m |> size == (10, 32)
223+
)
224+
@test randn(Float32, img..., 1, 32) |> m |> size == (10, 32)
225225
end
226226

227227
@testset "LazyLayer" begin
@@ -241,4 +241,7 @@ end
241241
@test_throws Exception Flux.params(lm)
242242
@test_throws Exception gradient(x -> sum(abs2, lm(x)), [1,2])
243243
@test_throws Exception gradient(m -> sum(abs2, Flux.striplazy(m)([1,2])), ld)
244+
245+
# Can't let |> gpu act before the arrays are materialized... so it's an error:
246+
@test_throws ErrorException @eval @autosize (1,2,3) Dense(_=>2) |> f64
244247
end

0 commit comments

Comments
 (0)