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

Model no longer type stable when using destructure and restructure #1569

Closed
JTaets opened this issue Apr 9, 2021 · 2 comments
Closed

Model no longer type stable when using destructure and restructure #1569

JTaets opened this issue Apr 9, 2021 · 2 comments

Comments

@JTaets
Copy link

JTaets commented Apr 9, 2021

Hi,

When restructuring a model after Flux.destructure, it becomes type unstable (and slow).
Is there a way to avoid this?

using Flux
using BenchmarkTools

model = Chain(Dense(1,64,σ),Dense(64,1))
p, re = Flux.destructure(model)
re_model(x,p,re) = re(p)(x)  # passing re here doesn't make a difference

@code_warntype model([1f0])          # type stable
@btime $model($[1f0])                # 460.406 ns (4 allocations: 864 bytes)

@code_warntype re_model([1f0],p,re)  # type unstable
@btime $re_model($[1f0],$p,$re)      # 3.850 μs (57 allocations: 3.86 KiB)
@JTaets
Copy link
Author

JTaets commented Apr 9, 2021

Like 1 second after posting i realize that re(p) is the thing that makes it slower

@btime $re($p) #3.237 μs (53 allocations: 3.02 KiB)

So i guess i'll restructure outside of re_model

@JTaets JTaets closed this as completed Apr 9, 2021
@DhairyaLGandhi
Copy link
Member

You should only need to restructure the model once and it would work like new.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants