-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
extend fmapstructure
and add KeyPath
, fmap_with_path
, fmapstructure_with_path
#74
Conversation
KeyPath
and fmap_with_path
fmapstructure
and add KeyPath
, fmap_with_path
, fmapstructure_with_path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great. We've talked about adding something like KeyPath
to give nodes an "address" for things like weight sharing in Optimisers.jl (for immutable weights).
The KeyPath
implementation look fine. But it would be nice to simplify the implementation so that we don't have two copies of every walk style.
Ultimately, the key path only matters for ExcludeWalk
. What if we change the API so that every walk accepts a key path always? So, now the implementation for StructuralWalk
is the body of StructuralWalkWithKeyPath
, and we eliminate the latter. Finally, we have ExcludeWalk
with a boolean flag indicating whether exclude
accepts a key path or not. If false
, then just drop passing in the key path. Alternatively, we have ExcludeWalk
and ExcludeWalkWithoutPath
.
It would be a breaking change, but I think it's cleaner and more maintainable in the long run.
So from the interface perspective, the suggestion would be to go from fmap(x -> x.^2, model, exclude = x -> isleaf(x), walk = WalkNOTTakingKeyPath())
fmap_with_path((kp, x) -> x.^2, model, exclude = (kp, x) -> isleaf(x), walk = WalkTakingKeyPath()) to fmap(x -> x.^2, model, exclude = x -> isleaf(x), walk = WalkTakingKeyPath())
fmap_with_path((kp, x) -> x.^2, model, exclude = (kp, x) -> isleaf(x), walk = WalkTakingKeyPath()) It seems reasonable. (maybe has an unnecessary performance hit on some exotic scenarios? I don't know). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair but @ToucheSir may want to weigh in on these changes and re: breakage now or later.
add to docs fmap_with_keypath simplify names add fmapstructure_with_path update fmapstructure to handle multiple arguments fixes Update src/Functors.jl Co-authored-by: Kyle Daruwalla <[email protected]> Update src/Functors.jl Co-authored-by: Kyle Daruwalla <[email protected]> Update src/Functors.jl Co-authored-by: Kyle Daruwalla <[email protected]>
8dbe275
to
4183248
Compare
My only comment for now would be whether separate |
Adding some utilities for working with paths along trees:
KeyPath
fmap_with_path
fmapstructure_with_path
Since it came for free, I also extended
fmapstructure
to handle multiple inputs.In Jax they have KeyPath and tree_map_with_path
TODO
fmap_with_path