Skip to content

Commit

Permalink
Merge pull request #547 from LuxDL/ap/docs_gadjoint
Browse files Browse the repository at this point in the history
Minor fixes
  • Loading branch information
avik-pal authored Mar 18, 2024
2 parents 99e2a03 + 0adbce7 commit bcea47c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Lux"
uuid = "b2108857-7c20-44ae-9111-449ecde12c47"
authors = ["Avik Pal <[email protected]> and contributors"]
version = "0.5.23"
version = "0.5.24"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ try
end
catch e
rmprocs(workers()...)
@error e
rethrow(e)
end
2 changes: 1 addition & 1 deletion ext/LuxComponentArraysExt.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module LuxComponentArraysExt

using ComponentArrays, Functors, Lux, Optimisers
using Adapt, ComponentArrays, Functors, Lux, Optimisers
import TruncatedStacktraces: @truncate_stacktrace
import ChainRulesCore as CRC

Expand Down
24 changes: 9 additions & 15 deletions ext/LuxSimpleChainsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import Lux: SimpleChainsModelConversionError, __to_simplechains_adaptor,
import Optimisers

function __fix_input_dims_simplechain(layers::Vector, input_dims)
return SimpleChains.SimpleChain(input_dims, layers...)
L = Tuple(layers)
return SimpleChains.SimpleChain{typeof(input_dims), typeof(L)}(input_dims, L)
end

function __fix_input_dims_simplechain(layers, input_dims)
@warn "The model provided is not a `Chain`. Trying to wrap it into a `Chain` but this \
might fail. Please consider using `Chain` directly (potentially with \
`disable_optimizations = true`)."
return __fix_input_dims_simplechain([layers], input_dims)
end

__equivalent_simplechains_fn(::typeof(Lux.relu)) = SimpleChains.relu
Expand Down Expand Up @@ -75,18 +83,4 @@ function NNlib.logsoftmax!(y::SimpleChains.StrideArray{T1, 2},
return y
end

# Nicer Interactions with Optimisers.jl
# function Optimisers._setup(opt::Optimisers.AbstractRule,
# ps::Union{SimpleChains.StrideArray, SimpleChains.PtrArray}; cache)
# ℓ = Leaf(rule, init(rule, x))
# if isbits(x)
# cache[nothing] = nothing # just to disable the warning
#
# else
# cache[x] = ℓ
# end
# error(1)
# return Optimisers.setup(opt, ps .- ps)
# end

end
23 changes: 23 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ end
struct LuxEltypeAdaptor{T} end

(l::LuxEltypeAdaptor)(x) = fmap(adapt(l), x)
function (l::LuxEltypeAdaptor)(x::AbstractArray{T}) where {T}
return isbitstype(T) ? adapt(l, x) : map(adapt(l), x)
end

function Adapt.adapt_storage(
::LuxEltypeAdaptor{T}, x::AbstractArray{<:AbstractFloat}) where {T <: AbstractFloat}
Expand All @@ -212,6 +215,26 @@ for (fname, ftype) in zip((:f16, :f32, :f64), (Float16, Float32, Float64))
end
end

# Common incorrect usage
for f in (f16, f32, f64)
warn_msg = "$(f) is not meant to be broadcasted like `$(f).(x)` or `x .|> $(f)`, and \
this might give unexpected results and could lead to crashes. Directly use \
`$(f)` as `$(f)(x)` or `x |> $(f)` instead."
@eval begin
function Base.Broadcast.broadcasted(::typeof($(f)), arg1)
@warn $(warn_msg)
arg1′ = Broadcast.broadcastable(arg1)
return Broadcast.broadcasted(Broadcast.combine_styles(arg1′), $(f), arg1′)
end

function Base.Broadcast.broadcasted(::typeof(|>), arg1, ::typeof($(f)))
@warn $(warn_msg)
arg1′ = Broadcast.broadcastable(arg1)
return Broadcast.broadcasted(Broadcast.combine_styles(arg1′), $(f), arg1′)
end
end
end

# Used in freezing
## Extend for custom types
@inline _pairs(x) = pairs(x)
Expand Down
17 changes: 17 additions & 0 deletions test/transform/simple_chains_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@
gs = Zygote.gradient((x, p) -> sum(first(simple_chains_model(x, p, st))), x, ps)
@test size(gs[1]) == size(x)
@test length(gs[2].params) == length(ps.params)

@testset "Single Layer Conversion: LuxDL/Lux.jl#545" begin
lux_model = Dense(10 => 5)

adaptor = ToSimpleChainsAdaptor((static(10),))

simple_chains_model = @test_warn "The model provided is not a `Chain`. Trying to wrap it into a `Chain` but this might fail. Please consider using `Chain` directly (potentially with `disable_optimizations = true`)." adaptor(lux_model)

ps, st = Lux.setup(Random.default_rng(), simple_chains_model)

x = randn(Float32, 10, 3)
@test size(first(simple_chains_model(x, ps, st))) == (5, 3)

gs = Zygote.gradient((x, p) -> sum(first(simple_chains_model(x, p, st))), x, ps)
@test size(gs[1]) == size(x)
@test length(gs[2].params) == length(ps.params)
end
end

2 comments on commit bcea47c

@avik-pal
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/103114

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.24 -m "<description of version>" bcea47c7d4deddb69a74b13eb10aed14d0bdfb70
git push origin v0.5.24

Please sign in to comment.