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

Narrower version of @non_differentiable params #2118

Merged
merged 1 commit into from
Nov 25, 2022
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 @@ -38,7 +38,7 @@ ProgressLogging = "0.1"
Reexport = "0.2, 1.0"
SpecialFunctions = "1.8.2, 2.1.2"
StatsBase = "0.33"
Zygote = "0.6.34"
Zygote = "0.6.49"
julia = "1.6"

[extras]
Expand Down
5 changes: 4 additions & 1 deletion src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ function params(m...)
end

# Allows caching of the parameters when params is called within gradient() to fix #2040.
@non_differentiable params(m...)
# @non_differentiable params(m...) # https://github.com/FluxML/Flux.jl/pull/2054
# That speeds up implicit use, and silently breaks explicit use.
# From @macroexpand Zygote.@nograd params(m...) and https://github.com/FluxML/Zygote.jl/pull/1248
Zygote._pullback(::Zygote.Context{true}, ::typeof(params), m...) = params(m), _ -> nothing

struct FluxCUDAAdaptor end
adapt_storage(to::FluxCUDAAdaptor, x) = CUDA.cu(x)
Expand Down
14 changes: 14 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,20 @@ end
@test size.(Flux.params(m)) == [(2,), (1, 2)]
end

@testset "params gradient" begin
m = (x=[1,2.0], y=[3.0]);

# Explicit -- was broken by #2054
gnew = gradient(m -> (sum(norm, Flux.params(m))), m)[1]
@test gnew.x ≈ [0.4472135954999579, 0.8944271909999159]
@test gnew.y ≈ [1.0]

# Implicit
gold = gradient(() -> (sum(norm, Flux.params(m))), Flux.params(m))
@test gold[m.x] ≈ [0.4472135954999579, 0.8944271909999159]
@test gold[m.y] ≈ [1.0]
end

@testset "Precision" begin
m = Chain(Dense(10, 5, relu), Dense(5, 2))
x64 = rand(Float64, 10)
Expand Down