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

Return empty NamedTuple in @functor instead of empty Tuple #36

Merged
merged 1 commit into from
Nov 15, 2022

Conversation

ToucheSir
Copy link
Member

FluxML/Flux.jl#1862 made me realize that this was inconsistent.

@mcabbott
Copy link
Member

mcabbott commented Feb 5, 2022

Maybe a test?

@ToucheSir
Copy link
Member Author

Need to think about the ramifications this has on isleaf a bit first, since that checks === (). I think this is more correct, but it's also breaking unless isleaf is changed too.

@ToucheSir ToucheSir force-pushed the bc/macro-empty-namedtuple branch from e87740d to 7c87d42 Compare November 15, 2022 01:22
@ToucheSir
Copy link
Member Author

Latest commit mainly adds some tests for the existing changes. After some thought, not having isleaf check for NamedTuple{{}, Tuple{}} does make some sense. This allows one to distinguish between an "empty" node and a leaf when performing traversals, thus making exclude functions more precise. One could argue nothing or a custom singleton struct might be a better sentinel value for this, but we'd want better downstream testing before trying that approach.

As a bonus, this brings normal @functor in line with @flexiblefunctor, which has been generating a NamedTuple this whole time.

@mcabbott
Copy link
Member

To check I understand right, what this will change is things like this:

julia> using Functors: @functor, functor

julia> functor(nothing)
((), Returns{Nothing}(nothing))

julia> @functor Nothing  # struct with no fields

julia> functor(nothing)  # this will change to (;), ...
((), var"#7#8"())

@ToucheSir
Copy link
Member Author

That's correct. I don't believe we have any field-less structs in FluxML, and while norm layers (except LayerNorm) can return (;) if they're not affine that is for trainable, not functor.

@ToucheSir ToucheSir merged commit 3b45274 into master Nov 15, 2022
@ToucheSir ToucheSir deleted the bc/macro-empty-namedtuple branch November 15, 2022 23:26
@mcabbott mcabbott mentioned this pull request Nov 25, 2022
3 tasks
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.

3 participants