# \`apply\` do not work anymore with StatefulLuxLayer (by Luxv1.19)

**URL:** <https://discourse.julialang.org/t/apply-do-not-work-anymore-with-statefulluxlayer-by-luxv1-19/132317>\
**Category:** Machine Learning\
**Created:** [September 12, 2025, 9:25am UTC](https://discourse.julialang.org/t/apply-do-not-work-anymore-with-statefulluxlayer-by-luxv1-19/132317 "2025-09-12T09:25:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [September 12, 2025, 9:25am UTC](https://discourse.julialang.org/t/apply-do-not-work-anymore-with-statefulluxlayer-by-luxv1-19/132317/1 "2025-09-12T09:25:19Z")

</div>

```julia-auto
using Lux, Random

nn = Lux.Dense(10, 10, tanh)

ps, st = Lux.setup(Xoshiro(2024), nn)
    
nn_st = Lux.StatefulLuxLayer{true}(nn, nothing, st, nothing) 

input_data = rand(10)
Lux.apply(nn_st, input_data, ps) # do not work on Luxv1.21.0 but worked on Luxv1.18.0
# ERROR: MethodError: no method matching apply(::StatefulLuxLayer{…}, ::Vector{…}, ::@NamedTuple{…})
# The function `apply` exists, but no method is defined for this combination of argument types.

# Closest candidates are:
# apply(::AbstractLuxLayer, ::Any, ::Any, ::Any)

nn_st(input_data, ps) # works in both cases

```

Since v1.19 `StatefulLuxLayer` have been moved to LuxCore and documentation says this is not an `AbstractLuxLayer`.  
Since that update, the `apply` do no longer work. Only `nn_st(x, ps)` works, but I thought it was better to use `apply` (see [docs](https://lux.csail.mit.edu/stable/api/Building_Blocks/LuxCore#General)).

Since I just want to use `StatefulLuxLayer` to keep the state while changing parameters, `Lux.StatefulLuxLayer{true}(nn, nothing, st, nothing)` with apply seemed like the best option.

What is the best post-Luxv1.19 way to do that?

BTW I don’t know what the 2nd `nothing` refers to (it is a code from one of my old student) in the `StatefullLuxlayer` def 🙄.

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [September 13, 2025, 5:19pm UTC](https://discourse.julialang.org/t/apply-do-not-work-anymore-with-statefulluxlayer-by-luxv1-19/132317/2 "2025-09-13T17:19:34Z")

</div>

Removing the `apply` dispatch was accidental. I will patch it ([fix: accidental apply dispatch removal by avik-pal · Pull Request #1476 · LuxDL/Lux.jl · GitHub](https://github.com/LuxDL/Lux.jl/pull/1476)) in the next release (few hrs) (though that dispatch was never meant to be publicly used 😅, the `nn_st(input_data)` is the correct one)

> [@dmetivie](#):
>
> to keep the state while changing parameters,

Try this

```julia
using Setfield, Lux

@set! nn_st.ps = ps_new

```

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [September 15, 2025, 9:25am UTC](https://discourse.julialang.org/t/apply-do-not-work-anymore-with-statefulluxlayer-by-luxv1-19/132317/3 "2025-09-15T09:25:48Z")

</div>

Thanks!  
So looking at the PR code change, it seems that

```julia-auto
apply(nn_st, x, ps_new) 
# or
nn_st(x, ps_new)
# or
@set! nn_st.ps = ps_new # and
nn_st(x)

```

are all doing the same exact thing so I can pick the syntax I prefer, there are no penalties changing parameters one way or another (correct?)

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [September 15, 2025, 12:39pm UTC](https://discourse.julialang.org/t/apply-do-not-work-anymore-with-statefulluxlayer-by-luxv1-19/132317/4 "2025-09-15T12:39:02Z")

</div>

Correct there are no penalties.

`nn_st(x, ps_new)` comes from the SciML land and is the widely used one, so I would recommend this one, but all others are equivalent

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [September 16, 2025, 8:27am UTC](https://discourse.julialang.org/t/apply-do-not-work-anymore-with-statefulluxlayer-by-luxv1-19/132317/5 "2025-09-16T08:27:43Z")

</div>

I just had another bug introduced with the update.  
This feels more like a breaking change so it might be expected but in case not I just wanted to share.

I had saved some models

```julia-auto
julia> typeof(nn_st_t)
StatefulLuxLayer{Static.True, CompactLuxLayer{:₋₋₋no_special_dispatch₋₋₋, Serialization. __deserialized_types__.var"#5#6", Nothing, @NamedTuple{}, Lux.CompactMacroImpl.ValueStorage{@NamedTuple{}, @NamedTuple{}}, Tuple{Tuple{}, Tuple{}}}, Nothing, @NamedTuple{}}

```

In that case `nn_st_t(x)` does not work.

Apparently the `fixed_state_type` type must now be written as

```julia-auto
julia> @set! nn_st_t.fixed_state_type = Val{true}()
julia> typeof(nn_st_t)
StatefulLuxLayer{Val{true}, CompactLuxLayer{:₋₋₋no_special_dispatch₋₋₋, Serialization. __deserialized_types__.var"#5#6", Nothing, @NamedTuple{}, Lux.CompactMacroImpl.ValueStorage{@NamedTuple{}, @NamedTuple{}}, Tuple{Tuple{}, Tuple{}}}, Nothing, @NamedTuple{}}

```

which solves the issue.

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [September 16, 2025, 1:52pm UTC](https://discourse.julialang.org/t/apply-do-not-work-anymore-with-statefulluxlayer-by-luxv1-19/132317/6 "2025-09-16T13:52:15Z")

</div>

Did you serialize the `StatefulLuxLayer`? Generally models are not guaranteed to be stable under serialization and you should save the parameters and states (which are guaranteed to not change in the same major version similar to pytorch’s `state_dict()`). [Training a Simple LSTM | Lux.jl Docs](https://lux.csail.mit.edu/stable/tutorials/beginner/3_SimpleRNN#Saving-the-Model)
