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

Fix rrules for Symmetric and Diagonal constructors #23

Merged
merged 1 commit into from Apr 23, 2019

Conversation

ararslan
Copy link
Member

Currently these definitions are extending rrule(::typeof(T), x) where T is a type. However, typeof(Diagonal) == UnionAll, which means this is not defining the method it looks like it might be defining. The only reason this worked when originally implemented was that one of the rrule definitions was for rrule(UnionAll, Matrix) and the other for rrule(UnionAll, Vector), so dispatch still worked.

This replaces these problematic ::typeof(T)s with ::Type{<:T}.

Copy link
Member

@willtebbutt willtebbutt left a comment

Choose a reason for hiding this comment

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

Great job @ararslan . Could you please add a test which would have broken the old implementation before merging though, just to prevent accidental reversion at some point?

@ararslan
Copy link
Member Author

I had thought of doing so but wasn't able to determine how to formulate such a test. The only way I can think to properly catch this case would be to add more matrix constructor sensitivities, which would show method overwriting warnings for rrule(::Type{UnionAll}, ::Matrix). Any suggestions?

@willtebbutt
Copy link
Member

willtebbutt commented Apr 23, 2019

I was just thinking something simple like checking that

rrule(Diagonal{Float64}, x)
rrule(Diagonal, x)

should do sensible things. Wouldn't generalise to new types of course, but would at least prevent these particular ones from reverting.

Update: perhaps update the rrule_test invocations in the existing tests. I think we'll just have to be vigilant moving forwards to make sure that this stuff is tested appropriately on a case-by-case basis.

@ararslan
Copy link
Member Author

perhaps update the rrule_test invocations in the existing tests

Update them in what way?

@willtebbutt
Copy link
Member

Update them in what way?

Sorry, I was just thinking something along the lines of changing this and related to

rrule_test(typeof(Diagonal(randn(rng, N))), randn(rng, N, N), (randn(rng, N), randn(rng, N)))

Currently these definitions are extending `rrule(::typeof(T), x)` where
`T` is a type. However, `typeof(Diagonal) == UnionAll`, which means this
is not defining the method it looks like it might be defining. The only
reason this worked when originally implemented was that one of the
`rrule` definitions was for `rrule(UnionAll, Matrix)` and the other for
`rrule(UnionAll, Vector)`, so dispatch still worked.

This replaces these problematic `::typeof(T)`s with `::Type{<:T}`.
@ararslan
Copy link
Member Author

Okay, I've implemented that for Diagonal, but Symmetric is considerably more difficult, as no one-argument constructor exists for Symmetric concrete subtypes.

@willtebbutt
Copy link
Member

but Symmetric is considerably more difficult, as no one-argument constructor exists for Symmetric concrete subtypes.

Is Symmetric(randn(N, N)) not viable?

@ararslan
Copy link
Member Author

Well it is, but that's covered by the existing Symmetric tests. If you look at my recent addition to the Diagonal tests, you'll see that you can pass the concrete type easily as a constructor to rrule_test, and all is well. But rrule_test calls f(x), and no such method exists if f is a concrete subtype of Symmetric; it requires a second argument specifying which is the triangle to consider "primary."

@willtebbutt
Copy link
Member

Ahh I see what you mean. Good point -- we should probably sort this out in another PR as we'll probably have more of these things to check in the future. Would you mind raising an issue regarding this?

I'm happy with the PR now though. Thanks for sorting this out.

@ararslan
Copy link
Member Author

Issue in #24.

@ararslan ararslan merged commit 2c009d1 into master Apr 23, 2019
@ararslan ararslan deleted the aa/fix-linalg-rules branch April 23, 2019 18:33
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

3 participants