# On the future of Flux.destructure and SciML integration

**URL:** <https://discourse.julialang.org/t/on-the-future-of-flux-destructure-and-sciml-integration/104760>\
**Category:** Machine Learning\
**Tags:** flux, sciml\
**Created:** [October 9, 2023, 5:42pm UTC](https://discourse.julialang.org/t/on-the-future-of-flux-destructure-and-sciml-integration/104760 "2023-10-09T17:42:55Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [October 9, 2023, 7:55pm UTC](https://discourse.julialang.org/t/on-the-future-of-flux-destructure-and-sciml-integration/104760/4 "2023-10-09T19:55:33Z")

</div>

> [@Bizzi](#):
>
> where de/restructuring is very frequent (mostly as a hack to use Flux with explicit parameters) the performance cost does seems quite severe, unless I’m doing something wrong. On my machine, small PINNs seem to be 3-4x slower when destructured

In this regime it is expensive. While I haven’t checked this example today, my claim above is that using ComponentArrays.jl will also be expensive. I’d say that explicit/implicit is not the right axis here, it’s more like solid-vector vs. nested structure. For sufficiently small networks, converting back & forth may take longer than the matrix multiplications etc.

There is more that could be done here, e.g. Optimisers.jl could add a `destructure!` which re-uses the same model quite easily. It might save quite a bit here. See e.g. [issue 146](https://github.com/FluxML/Optimisers.jl/issues/146).

This is the regime where [SimpleChains.jl](https://github.com/PumasAI/SimpleChains.jl) is likely to be much faster. It’s a completely different design, which never uses a nested structure at all. But has various other limitations.

> [@Bizzi](#):
>
> more nicely with tied parameters

This is a key difference from ComponentArrays.jl, which perhaps we should highlight. (As well as being the main source of coding headaches!) I’m glad if it’s useful to someone.

```julia
julia> using ComponentArrays, Optimisers

julia> let twice = [1.0, 2.0]
        cv = ComponentArray(x=twice, y=twice, z=[1.0, 2.0])
        cv.x[1] += 999
        cv # this has 6 independent scalar parameters
       end
ComponentVector{Float64}(x = [1000.0, 2.0], y = [1.0, 2.0], z = [1.0, 2.0])

julia> let twice = [1.0, 2.0]
        v, re = destructure((x=twice, y=twice, z=[1.0, 2.0]))
        @show v # only 4 indep parameters
        v[1] += 999
        re(v)
       end
v = [1.0, 2.0, 1.0, 2.0]
(x = [1000.0, 2.0], y = [1000.0, 2.0], z = [1.0, 2.0])

```

---

_[View the full topic](https://discourse.julialang.org/t/on-the-future-of-flux-destructure-and-sciml-integration/104760)._
