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

fvec #31

Closed
wants to merge 6 commits into from
Closed

fvec #31

wants to merge 6 commits into from

Conversation

mcabbott
Copy link
Member

@mcabbott mcabbott commented Jan 23, 2022

This is an attempt to write a function which does what Flux.destructure does, but more carefully. It allows for missing branches of the structural gradient, and for shared weights having separate gradients, which ought to accumulate in making a vector. When returning to a structural gradient, only the first node gets the gradient, to avoid mistakenly doubling it.

It also declares some Base types like Transpose to be functors, since a decoder with transposed weights seems like the canonical example. And sets up a way to allow their gradients not to be Transpose.

And, thirdly, it changes fmap to not use the cache on isbits objects. This is to avoid falsely tying two parameters initially say SA[0,0,0]. We thought elsewhere that the way to indicate that you do what such things tied is to wrap them in some TiedArray type like https://gist.github.com/mcabbott/35f660ff9fb8e7b0b23a2abb94618cf4, but not in this PR.


Most of this can simply run with a different walk to hit only trainable arrays. Which I think ought to be done in Optimisers.jl, which owns trainable. The exception is that the gradient of fcopy doesn't seem to fit into just using a different walk, maybe there's a way? It takes a different children function instead.

Not sure what should be exported here, or what anything should be called. I think the name destructure should belong to Optimisers.jl too. Perhaps this package should export a function which similarly returns a vector and a reconstructor. That's just one line after the pieces defined here. (But easier to write & test the pieces separately.)


Needs more tests, still.

@mcabbott mcabbott closed this Jan 23, 2022
Comment on lines +28 to +29
functor(::Type{<:Transpose}, x) = (parent = parent(x),), y -> transpose(only(y))
lazywrap(x::Transpose) = parent(x), transpose
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is #28 and should perhaps be pulled out.

From FluxML/Optimisers.jl#42 I think the right pattern is actually more like this -- calling parent(x) is not safe, and we may want to transpose things which aren't matrix-like.

Functors.functor(::Type{<:Transpose}, x) = (parent = _transpose(x),), y -> _transpose(only(y))
_transpose(x) = transpose(x)
_transpose(x::NamedTuple{(:parent,)}) = x.parent  # "structural" gradient
# _transpose(bc::Broadcast.Broadcasted{S}) where S = Broadcast.Broadcasted{S}(map(_transpose, bc.args)) do args...
#     transpose(bc.f(map(transpose, args)...))
# end
_transpose(bc::Broadcast.Broadcasted) = transpose(Broadcast.materialize(bc))

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

Successfully merging this pull request may close these issues.

None yet

1 participant