-
Notifications
You must be signed in to change notification settings - Fork 25
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
Constrain type in to_vec(::AbstractArray/Vector)
#156
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b711591
make strided
7795bd6
bump version
404ef6b
FillVector should work
c29d71b
rename local variable vals to values
9d5bd94
remove inference checks
b4d0c03
Unions because dispatch is weird
0400793
move from StridedArray to DenseArray
3122d89
use old SubArray implementation
c94df20
use old version
023f262
add a wrapperarray test
a326978
blue style kwargs spacing
6887b08
patch bump
1d05438
move wrapper outside of the testset
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,13 +11,15 @@ end | |||||||||
Base.:(==)(x::DummyType, y::DummyType) = x.X == y.X | ||||||||||
Base.length(x::DummyType) = size(x.X, 1) | ||||||||||
|
||||||||||
# A dummy FillVector. This is a type for which the fallback implementation of | ||||||||||
# `to_vec` should fail loudly. | ||||||||||
# A dummy FillVector | ||||||||||
struct FillVector <: AbstractVector{Float64} | ||||||||||
x::Float64 | ||||||||||
len::Int | ||||||||||
end | ||||||||||
|
||||||||||
Base.size(x::FillVector) = (x.len,) | ||||||||||
Base.getindex(x::FillVector, n::Int) = x.x | ||||||||||
|
||||||||||
# For testing Composite{ThreeFields} | ||||||||||
struct ThreeFields | ||||||||||
a | ||||||||||
|
@@ -32,9 +34,6 @@ struct Nested | |||||||||
y::Singleton | ||||||||||
end | ||||||||||
|
||||||||||
Base.size(x::FillVector) = (x.len,) | ||||||||||
Base.getindex(x::FillVector, n::Int) = x.x | ||||||||||
|
||||||||||
function test_to_vec(x::T; check_inferred = true) where {T} | ||||||||||
check_inferred && @inferred to_vec(x) | ||||||||||
x_vec, back = to_vec(x) | ||||||||||
|
@@ -67,8 +66,8 @@ end | |||||||||
test_to_vec(UpperTriangular(randn(T, 13, 13))) | ||||||||||
test_to_vec(Diagonal(randn(T, 7))) | ||||||||||
test_to_vec(DummyType(randn(T, 2, 9))) | ||||||||||
test_to_vec(SVector{2, T}(1.0, 2.0)) | ||||||||||
test_to_vec(SMatrix{2, 2, T}(1.0, 2.0, 3.0, 4.0)) | ||||||||||
test_to_vec(SVector{2, T}(1.0, 2.0); check_inferred = false) | ||||||||||
test_to_vec(SMatrix{2, 2, T}(1.0, 2.0, 3.0, 4.0); check_inferred = false) | ||||||||||
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.
Suggested change
Bluetyle spacing in kwargs 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. changed in other places as well now |
||||||||||
test_to_vec(@view randn(T, 10)[1:4]) # SubArray -- Vector | ||||||||||
test_to_vec(@view randn(T, 10, 2)[1:4, :]) # SubArray -- Matrix | ||||||||||
test_to_vec(Base.ReshapedArray(rand(T, 3, 3), (9,), ())) | ||||||||||
|
@@ -173,9 +172,7 @@ end | |||||||||
end | ||||||||||
|
||||||||||
@testset "FillVector" begin | ||||||||||
x = FillVector(5.0, 10) | ||||||||||
x_vec, from_vec = to_vec(x) | ||||||||||
@test_throws MethodError from_vec(randn(10)) | ||||||||||
test_to_vec(FillVector(5.0, 10); check_inferred=false) | ||||||||||
end | ||||||||||
|
||||||||||
@testset "fallback" begin | ||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe the parent array of
SubArray
should be restricted to e.g.StridedArray
or something?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.
We want e.g. a
to dispatch on the current
SubArray
implementation. UnfortunatelyStridedArray
is aUnion
of several more specificSubArrays
(not just in the parent argument)So we have two options:
SubArray
(very verbose*)StridedArray
toUnion{StridedArray, SubArray}
so that the currentSubArray
becomes more specific.And similarly for the
ReshapedArray
.I don't particularly like either option but it's probably better than the status quo.
*Number 1) looks like this, I want to nope out of that 😂
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.
@willtebbutt how do you feel about that?
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.
The thing I'm concerned about is if someone attempts to
to_vec
something likeLol yeah, that's really not fun.
Is there a reason that we can't go with something like
and just recurse into
ReshapedArray
s?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.
The dispatch for the
Diagonal
example is the same (the dedicatedSubArray
method) whether we constrain the parent of theSubArray
to be strided or not.The example breaks, but because
SparseArray.SparseMatrixCSC
does not have ato_vec
defined.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.
The status is:
to_vec(Base.SubArray)
breaks some teststest_to_vec(view(Diagonal(randn(5)), 1:5, 1:3); check_inferred=false)
, (the thing you suggested should be checked) breaks on master (StackOverflowError
), on the current PR (SparseMatrixCSC
error), and on the current PR withSubArray
removed from the Union (SparseMatrixCSC
error).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.
Ahh I see.
I've run it further locally, and found that the following seems to resolve everything for me:
DenseVector
andDenseArray
, fromUnion{StridedArray...}
SubArray
:if you don't have this, it thinks that the integer fields should also be
to_vec
ed.3. comment out the
SubArray
-specific methodThere 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.
Oh yeah that's much better. Are we happy with that?
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'm happy with it, but I feel that we should get an additional review. Maybe @oxinabox or @sethaxen ?
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.
Actually with this one ChainRules test fails, and the reason is that the current implementation returns the whole parent vector
Keeping the old version below works just fine. No issues now
StridedArray
has been replaced byDenseArray
(where we used to haveAbstractArray
)