-
Notifications
You must be signed in to change notification settings - Fork 36
[WIP] Use Tullio for pairwise distances #386
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
Draft
theogf
wants to merge
23
commits into
master
Choose a base branch
from
tgf/tullio
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
79efb28
Introduction of Tullio to perform pairwise operations
theogf a6afab1
Fix formatting
theogf ec4b5cd
Fix one colwise
theogf 5b279f2
Corrections
theogf 5f3ea12
Add specialization Euclidean
theogf 8ac0294
Formatting fixes
theogf 98a9f63
Remove VecOfVecs and reorder types
theogf e6555b9
Merge branch 'tgf/tullio' of github.com:JuliaGaussianProcesses/Kernel…
theogf 4a0154a
Merge branch 'master' into tgf/tullio
theogf 851b50c
Merge branch 'master' into tgf/tullio
theogf cabf715
Update src/distances/euclidean.jl
theogf 3d16cd7
Update src/distances/euclidean.jl
theogf adac02c
Apply suggestions from code review
theogf 5d3cd59
Update src/distances/euclidean.jl
theogf 5b36f3d
Add simplification for dotproduct
theogf a1482be
Merge branch 'tgf/tullio' of github.com:JuliaGaussianProcesses/Kernel…
theogf 65c5885
Merge branch 'master' into tgf/tullio
theogf 65e09b0
Merge branch 'master' into tgf/tullio
theogf e6bf11f
Add rrules
theogf d7bc47f
Solve formatting
theogf d9b8846
Merge branch 'master' into tgf/tullio
theogf 48f6727
Add colwise for euclidean
theogf 5f2b08b
Merge branch 'master' into tgf/tullio
theogf 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,21 +1,22 @@ | ||
## DotProduct is not following the PreMetric rules since d(x, x) != 0 and d(x, y) >= 0 for all x, y | ||
struct DotProduct <: Distances.UnionPreMetric end | ||
struct DotProduct <: AbstractBinaryOp end | ||
|
||
@inline function Distances._evaluate(::DotProduct, a::AbstractVector, b::AbstractVector) | ||
@boundscheck if length(a) != length(b) | ||
throw( | ||
DimensionMismatch( | ||
"first array has length $(length(a)) which does not match the length of the second, $(length(b)).", | ||
), | ||
) | ||
end | ||
return dot(a, b) | ||
(::DotProduct)(a::AbstractVector, b::AbstractVector) = dot(a, b) | ||
|
||
(::DotProduct)(a::Number, b::Number) = a * b | ||
|
||
function pairwise(::DotProduct, x::ColVecs, y::ColVecs) | ||
return @tullio out[i, j] := x.X[k, i] * y.X[k, j] | ||
end | ||
|
||
Distances.result_type(::DotProduct, Ta::Type, Tb::Type) = promote_type(Ta, Tb) | ||
function pairwise(::DotProduct, x::RowVecs, y::RowVecs) | ||
return @tullio out[i, j] := x.X[i, k] * y.X[j, k] | ||
end | ||
|
||
function colwise(::DotProduct, x::RowVecs, y::RowVecs=x) | ||
return @tullio out[i] := x.X[i, k] * y.X[i, k] | ||
end | ||
|
||
@inline Distances.eval_op(::DotProduct, a::Real, b::Real) = a * b | ||
@inline function (dist::DotProduct)(a::AbstractArray, b::AbstractArray) | ||
return Distances._evaluate(dist, a, b) | ||
function colwise(::DotProduct, x::ColVecs, y::ColVecs=x) | ||
theogf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return @tullio out[i] := x.X[k, i] * y.X[k, i] | ||
end | ||
@inline (dist::DotProduct)(a::Number, b::Number) = a * b |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Tullio specialization for Euclidean and SqEuclidean metrics | ||
|
||
function pairwise(::Euclidean, x::ColVecs, y::ColVecs) | ||
return @tullio out[i, j] := | ||
sqrt <| (x.X[k, i] - y.X[k, j])^2 | ||
theogf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
function pairwise(::Euclidean, x::RowVecs, y::RowVecs) | ||
return @tullio out[i, j] := | ||
sqrt <| x.X[i, k]^2 - 2 * x.X[i, k] * y.X[j, k] + y.X[j, k]^2 | ||
theogf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
function pairwise(::SqEuclidean, x::ColVecs, y::ColVecs) | ||
return @tullio out[i, j] := x.X[k, i]^2 - 2 * x.X[k, i] * y.X[k, j] + y.X[k, j]^2 | ||
theogf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
function pairwise(::SqEuclidean, x::RowVecs, y::RowVecs) | ||
return @tullio out[i, j] := x.X[i, k]^2 - 2 * x.X[i, k] * y.X[j, k] + y.X[j, k]^2 | ||
theogf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end |
This file contains hidden or 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 |
---|---|---|
@@ -1,70 +1,28 @@ | ||
# Add our own pairwise function to be able to apply it on vectors | ||
|
||
function pairwise(d::PreMetric, X::AbstractVector, Y::AbstractVector) | ||
return broadcast(d, X, permutedims(Y)) | ||
function pairwise(d::BinaryOp, X::AbstractVector, Y::AbstractVector=X) | ||
return @tullio out[i, j] := d(X[i], Y[j]) | ||
end | ||
|
||
pairwise(d::PreMetric, X::AbstractVector) = pairwise(d, X, X) | ||
|
||
function pairwise!(out::AbstractMatrix, d::PreMetric, X::AbstractVector, Y::AbstractVector) | ||
return broadcast!(d, out, X, permutedims(Y)) | ||
end | ||
|
||
pairwise!(out::AbstractMatrix, d::PreMetric, X::AbstractVector) = pairwise!(out, d, X, X) | ||
|
||
function pairwise(d::PreMetric, x::AbstractVector{<:Real}) | ||
return Distances_pairwise(d, reshape(x, :, 1); dims=1) | ||
end | ||
|
||
function pairwise(d::PreMetric, x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) | ||
return Distances_pairwise(d, reshape(x, :, 1), reshape(y, :, 1); dims=1) | ||
end | ||
|
||
function pairwise!(out::AbstractMatrix, d::PreMetric, x::AbstractVector{<:Real}) | ||
return Distances.pairwise!(out, d, reshape(x, :, 1); dims=1) | ||
end | ||
|
||
function pairwise!( | ||
out::AbstractMatrix, d::PreMetric, x::AbstractVector{<:Real}, y::AbstractVector{<:Real} | ||
) | ||
return Distances.pairwise!(out, d, reshape(x, :, 1), reshape(y, :, 1); dims=1) | ||
function pairwise!(out::AbstractMatrix, d::BinaryOp, X::AbstractVector, Y::AbstractVector=X) | ||
return @tullio out[i, j] = d(X[i], Y[j]) | ||
end | ||
|
||
# Also defines the colwise method for abstractvectors | ||
|
||
function colwise(d::PreMetric, x::AbstractVector) | ||
# We have different methods for PreMetric and AbstractBinaryOp | ||
# Since colwise on AbstractBinaryOp is not guaranteed to be equal to 0 | ||
function colwise(d::Distances.PreMetric, x::AbstractVector) | ||
return zeros(Distances.result_type(d, x, x), length(x)) # Valid since d(x,x) == 0 by definition | ||
end | ||
|
||
function colwise(d::PreMetric, x::ColVecs) | ||
function colwise(d::Distances.PreMetric, x::Union{ColVecs,RowVecs}) | ||
return zeros(Distances.result_type(d, x.X, x.X), length(x)) # Valid since d(x,x) == 0 by definition | ||
end | ||
|
||
function colwise(d::PreMetric, x::RowVecs) | ||
return zeros(Distances.result_type(d, x.X, x.X), length(x)) # Valid since d(x,x) == 0 by definition | ||
end | ||
|
||
## The following is a hack for DotProduct and Delta to still work | ||
function colwise(d::Distances.UnionPreMetric, x::ColVecs) | ||
return Distances.colwise(d, x.X, x.X) | ||
end | ||
|
||
function colwise(d::Distances.UnionPreMetric, x::RowVecs) | ||
return Distances.colwise(d, x.X', x.X') | ||
end | ||
|
||
function colwise(d::Distances.UnionPreMetric, x::AbstractVector) | ||
return map(d, x, x) | ||
end | ||
|
||
function colwise(d::PreMetric, x::ColVecs, y::ColVecs) | ||
return Distances.colwise(d, x.X, y.X) | ||
end | ||
|
||
function colwise(d::PreMetric, x::RowVecs, y::RowVecs) | ||
return Distances.colwise(d, x.X', y.X') | ||
function colwise(d::AbstractBinaryOp, x::AbstractVector) | ||
return @tullio out[i] := d(x[i], x[i]) | ||
end | ||
|
||
function colwise(d::PreMetric, x::AbstractVector, y::AbstractVector) | ||
return map(d, x, y) | ||
function colwise(d::BinaryOp, x::AbstractVector, y::AbstractVector) | ||
return @tullio out[i] := d(x[i], y[i]) | ||
end |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.