Flux.jl icon indicating copy to clipboard operation
Flux.jl copied to clipboard

Some models saved under Flux v0.14 do not load on v0.15+

Open dorn-gerhard opened this issue 11 months ago • 2 comments

Using u = Flux.loadmodel!(empty_model, model_state) leads to the following error

Image since in Flux v0.14 pad_size is not stored extra (model_state2) whereas in the new version it is (model_state)

Image

Is there a workaround to upgrade old model states?

dorn-gerhard avatar Jan 18 '25 11:01 dorn-gerhard

I don't recall any built-in layers which have a pad_size field, can you provide a MWE? Depending on what pad_size represents, I think the easiest solution could be to use Accessors.jl + maybe Functors.jl to remove pad_size from model_state.

ToucheSir avatar Jan 18 '25 22:01 ToucheSir

A possible cause is Functors v0.5 recursing into arbitrary custom structs?

For built-in layers, Flux.state does typically save integers like padding, does not load them (since they are immutable), but does complain if they don't match:

julia> st = Flux.state(Conv((2,), 1=>1, pad=1, stride=2))
(σ = (), weight = Float32[-0.8863135; -0.70230323;;;], bias = Float32[0.0], stride = (2,), pad = (1, 1), dilation = (1,), groups = 1)

julia> Flux.loadmodel!(Conv((2,), 1=>1, pad=0, stride=0), st)
Conv((2,), 1 => 1, stride=0)  # 3 parameters

julia> Flux.loadmodel!(Conv((2,), 1=>1, pad=0, stride=0), st[propertynames(st)[1:end-1]])
ERROR: ArgumentError: Tried to load (:pad, :σ, :weight, :bias, :stride, :dilation) into (:pad, :σ, :weight, :bias, :groups, :stride, :dilation) but the structures do not match.

One possible remedy might be to define a method of loadmodel! for your struct, as was done when some built-in layers changed what fields they had:

https://github.com/FluxML/Flux.jl/blob/44695a0b4fe7ffbd04d10030cd70a1928e41f7f9/src/deprecations.jl#L24-L28

mcabbott avatar Jan 18 '25 22:01 mcabbott