-
-
Notifications
You must be signed in to change notification settings - Fork 608
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
Fix bug that caused Flux.params(x) call to not be cached (Closes issue #2040) #2048
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,16 +36,14 @@ Possible values include: | |
""" | ||
trainmode!(m, mode = true) = mode isa Bool ? testmode!(m, !mode) : testmode!(m, mode) | ||
|
||
params!(p::Params, x::DenseArray{<:Number}, seen = IdSet()) = Functors.isleaf(x) && push!(p, x) | ||
|
||
function params!(p::Params, x, seen = IdSet()) | ||
if x isa AbstractArray{<:Number} && Functors.isleaf(x) | ||
return push!(p, x) | ||
elseif x in seen | ||
nothing | ||
else | ||
push!(seen, x) | ||
for child in trainable(x) | ||
params!(p, child, seen) | ||
end | ||
x in seen && return | ||
|
||
push!(seen, x) | ||
for child in trainable(x) | ||
params!(p, child, seen) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if I have a leaf type which isn't a DenseArray? The current behaviour is:
What I meant with the DenseArray idea was that this method could be a short-cut for the common case, in addition to the existing method. Of course the tests as always don't try very hard. But I do think that it ought to keep working with wrappers like NamedDims. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any wrappers already on the dependency chain which have this same behaviour outside of NamedDims? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe SubArray? ReshapedArray, SymTridiagonal ... for tests I guess you want something unlikely to be
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC ReshapedArray was the tricky one, as its type doesn't have the shape. |
||
end | ||
|
||
|
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.
Can a
DenseArray{<:Number}
ever not be a leaf?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.
Technically yes,
Base.Experimental.Const
is a pure wrapper type and subtypesDenseArray
. I've seen it used in JuliaGPU libraries, but am unsure if those would ever come in contact withparams
.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.
That's an interesting type. But I guess it will always be leaf-like, Functors should treat it as it would an SArray right?
More broadly if this method has a test for
isleaf
, then it has to do something with the other branch. And then it's the other method. I guess it could assert isleaf just to make sure you get an error if someone does something really weird.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.
I think it has to be a non-leaf for the same reason
Transpose
does: shared inner arrays.RE the other branch, I thought the latest change addressed that but it appears I misremembered. Silently dropping an array instead of recursing is definitely not good.
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.
I guess. Although transposing one of two shared arrays is common, but marking as Const only one of the two seems perverse.
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.
Let me update my suggestion. I think this ought to be safe, and will at least throw an error should someone ever
@functor Base.Experimental.Const
(or its CUDA analogue):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 code returns
size.(Flux.params((x = view([1,2,3]pi, 1:2), y = transpose([4 5]pi)))) == [(1, 2)]
.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 code and what else?
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 code with the above suggestion.
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.
Ok. On this example, the suggestion changes nothing compared to the PR. It just moves the
isleaf
test to be an error not an ignore.I think such a fast method should exist alongside the method which was here before the PR, which handles all cases (but has more branches). That should be correct. Whether it still solves 2040 I don't know.