-
Notifications
You must be signed in to change notification settings - Fork 18
Add HyperDualNumbersExt #179
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9e9b9ba
Add HyperDualNumbersExt
CheukHinHoJerry eec3bba
add test and dependency
CheukHinHoJerry 2d4b743
minor fix on test
CheukHinHoJerry ae5c086
tests fix with proper testsets
CheukHinHoJerry abcbfbf
fix hyperduals ext
CheukHinHoJerry 61811b0
bump version
CheukHinHoJerry 1a10ea4
some format fix
CheukHinHoJerry 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| module HyperDualNumbersExt | ||
|
|
||
| using HyperDualNumbers: Hyper | ||
| using Octavian: ArrayInterface, | ||
| @turbo, @tturbo, | ||
| One, Zero, | ||
| indices, static | ||
| import Octavian: real_rep, _matmul!, _matmul_serial! | ||
|
|
||
| real_rep(a::AbstractArray{DualT}) where {T,DualT<:Hyper{T}} = | ||
| reinterpret(reshape, T, a) | ||
| _view1(B::AbstractMatrix) = @view(B[1, :]) | ||
| _view1(B::AbstractArray{<:Any,3}) = @view(B[1, :, :]) | ||
|
|
||
| for AbstractVectorOrMatrix in (:AbstractVector, :AbstractMatrix) | ||
| # multiplication of dual vector/matrix by standard matrix from the left | ||
| @eval function _matmul!( | ||
| _C::$(AbstractVectorOrMatrix){DualT}, | ||
| A::AbstractMatrix, | ||
| _B::$(AbstractVectorOrMatrix){DualT}, | ||
| α, | ||
| β = Zero(), | ||
| nthread::Nothing = nothing, | ||
| MKN = nothing, | ||
| contig_axis = nothing | ||
| ) where {T, DualT<:Hyper{T}} | ||
| B = real_rep(_B) | ||
| C = real_rep(_C) | ||
|
|
||
| @tturbo for n ∈ indices((C, B), 3), | ||
| m ∈ indices((C, A), (2, 1)), | ||
| l in indices((C, B), 1) | ||
|
|
||
| Cₗₘₙ = zero(eltype(C)) | ||
| for k ∈ indices((A, B), 2) | ||
| Cₗₘₙ += A[m, k] * B[l, k, n] | ||
| end | ||
| C[l, m, n] = α * Cₗₘₙ + β * C[l, m, n] | ||
| end | ||
|
|
||
| _C | ||
| end | ||
|
|
||
| # multiplication of dual matrix by standard vector/matrix from the right | ||
| @eval @inline function _matmul!( | ||
| _C::$(AbstractVectorOrMatrix){DualT}, | ||
| _A::AbstractMatrix{DualT}, | ||
| B::$(AbstractVectorOrMatrix), | ||
| α = One(), | ||
| β = Zero(), | ||
| nthread::Nothing = nothing, | ||
| MKN = nothing | ||
| ) where {T,DualT<:Hyper{T}} | ||
| if Bool(ArrayInterface.is_dense(_C)) && | ||
| Bool(ArrayInterface.is_column_major(_C)) && | ||
| Bool(ArrayInterface.is_dense(_A)) && | ||
| Bool(ArrayInterface.is_column_major(_A)) | ||
| # we can avoid the reshape and call the standard method | ||
| A = reinterpret(T, _A) | ||
| C = reinterpret(T, _C) | ||
| _matmul!(C, A, B, α, β, nthread, nothing) | ||
| else | ||
| # we cannot use the standard method directly | ||
| A = real_rep(_A) | ||
| C = real_rep(_C) | ||
|
|
||
| @tturbo for n ∈ indices((C, B), (3, 2)), | ||
| m ∈ indices((C, A), 2), | ||
| l in indices((C, A), 1) | ||
|
|
||
| Cₗₘₙ = zero(eltype(C)) | ||
| for k ∈ indices((A, B), (3, 1)) | ||
| Cₗₘₙ += A[l, m, k] * B[k, n] | ||
| end | ||
| C[l, m, n] = α * Cₗₘₙ + β * C[l, m, n] | ||
| end | ||
| end | ||
|
|
||
| _C | ||
| end | ||
|
|
||
| @eval @inline function _matmul!( | ||
| _C::$(AbstractVectorOrMatrix){DualT}, | ||
| _A::AbstractMatrix{DualT}, | ||
| _B::$(AbstractVectorOrMatrix){DualT}, | ||
| α = One(), | ||
| β = Zero(), | ||
| nthread::Nothing = nothing, | ||
| MKN = nothing, | ||
| contig = nothing | ||
| ) where {T,DualT<:Hyper{T}} | ||
| A = real_rep(_A) | ||
| C = real_rep(_C) | ||
| B = real_rep(_B) | ||
| if Bool(ArrayInterface.is_dense(_C)) && | ||
| Bool(ArrayInterface.is_column_major(_C)) && | ||
| Bool(ArrayInterface.is_dense(_A)) && | ||
| Bool(ArrayInterface.is_column_major(_A)) | ||
| # we can avoid the reshape and call the standard method | ||
| Ar = reinterpret(T, _A) | ||
| Cr = reinterpret(T, _C) | ||
| _matmul!(Cr, Ar, _view1(B), α, β, nthread, nothing) | ||
| else | ||
| # we cannot use the standard method directly | ||
| @tturbo for n ∈ indices((C, B), 3), | ||
| m ∈ indices((C, A), 2), | ||
| l in indices((C, A), 1) | ||
|
|
||
| Cₗₘₙ = zero(eltype(C)) | ||
| for k ∈ indices((A, B), (3, 2)) | ||
| Cₗₘₙ += A[l, m, k] * B[1, k, n] | ||
| end | ||
| C[l, m, n] = α * Cₗₘₙ + β * C[l, m, n] | ||
| end | ||
| end | ||
| @tturbo for n ∈ indices((B, C), 3), m ∈ indices((A, C), 2), p ∈ 1:3 | ||
| Cₚₘₙ = zero(eltype(C)) | ||
| for k ∈ indices((A, B), (3, 2)) | ||
| Cₚₘₙ += A[1, m, k] * B[p+1, k, n] | ||
| end | ||
| C[p+1, m, n] = C[p+1, m, n] + α * Cₚₘₙ | ||
| end | ||
| _C | ||
| end | ||
|
|
||
| # multiplication of dual vector/matrix by standard matrix from the left | ||
| @eval function _matmul_serial!( | ||
| _C::$(AbstractVectorOrMatrix){DualT}, | ||
| A::AbstractMatrix, | ||
| _B::$(AbstractVectorOrMatrix){DualT}, | ||
| α, | ||
| β, | ||
| MKN | ||
| ) where {T, DualT<:Hyper{T}} | ||
| B = real_rep(_B) | ||
| C = real_rep(_C) | ||
|
|
||
| @turbo for n ∈ indices((C, B), 3), | ||
| m ∈ indices((C, A), (2, 1)), | ||
| l in indices((C, B), 1) | ||
|
|
||
| Cₗₘₙ = zero(eltype(C)) | ||
| for k ∈ indices((A, B), 2) | ||
| Cₗₘₙ += A[m, k] * B[l, k, n] | ||
| end | ||
| C[l, m, n] = α * Cₗₘₙ + β * C[l, m, n] | ||
| end | ||
|
|
||
| _C | ||
| end | ||
|
|
||
| # multiplication of dual matrix by standard vector/matrix from the right | ||
| @eval @inline function _matmul_serial!( | ||
| _C::$(AbstractVectorOrMatrix){DualT}, | ||
| _A::AbstractMatrix{DualT}, | ||
| B::$(AbstractVectorOrMatrix), | ||
| α, | ||
| β, | ||
| MKN | ||
| ) where {T,DualT<:Hyper{T}} | ||
| if Bool(ArrayInterface.is_dense(_C)) && | ||
| Bool(ArrayInterface.is_column_major(_C)) && | ||
| Bool(ArrayInterface.is_dense(_A)) && | ||
| Bool(ArrayInterface.is_column_major(_A)) | ||
| # we can avoid the reshape and call the standard method | ||
| A = reinterpret(T, _A) | ||
| C = reinterpret(T, _C) | ||
| _matmul_serial!(C, A, B, α, β, nothing) | ||
| else | ||
| # we cannot use the standard method directly | ||
| A = real_rep(_A) | ||
| C = real_rep(_C) | ||
|
|
||
| @turbo for n ∈ indices((C, B), (3, 2)), | ||
| m ∈ indices((C, A), 2), | ||
| l in indices((C, A), 1) | ||
|
|
||
| Cₗₘₙ = zero(eltype(C)) | ||
| for k ∈ indices((A, B), (3, 1)) | ||
| Cₗₘₙ += A[l, m, k] * B[k, n] | ||
| end | ||
| C[l, m, n] = α * Cₗₘₙ + β * C[l, m, n] | ||
| end | ||
| end | ||
|
|
||
| _C | ||
| end | ||
|
|
||
| @eval @inline function _matmul_serial!( | ||
| _C::$(AbstractVectorOrMatrix){DualT}, | ||
| _A::AbstractMatrix{DualT}, | ||
| _B::$(AbstractVectorOrMatrix){DualT}, | ||
| α, | ||
| β, | ||
| MKN | ||
| ) where {T, DualT<:Hyper{T}} | ||
| A = real_rep(_A) | ||
| C = real_rep(_C) | ||
| B = real_rep(_B) | ||
| if Bool(ArrayInterface.is_dense(_C)) && | ||
| Bool(ArrayInterface.is_column_major(_C)) && | ||
| Bool(ArrayInterface.is_dense(_A)) && | ||
| Bool(ArrayInterface.is_column_major(_A)) | ||
| # we can avoid the reshape and call the standard method | ||
| Ar = reinterpret(T, _A) | ||
| Cr = reinterpret(T, _C) | ||
| _matmul_serial!(Cr, Ar, _view1(B), α, β, nothing) | ||
| else | ||
| # we cannot use the standard method directly | ||
| @turbo for n ∈ indices((C, B), 3), | ||
| m ∈ indices((C, A), 2), | ||
| l in indices((C, A), 1) | ||
|
|
||
| Cₗₘₙ = zero(eltype(C)) | ||
| for k ∈ indices((A, B), (3, 2)) | ||
| Cₗₘₙ += A[l, m, k] * B[1, k, n] | ||
| end | ||
| C[l, m, n] = α * Cₗₘₙ + β * C[l, m, n] | ||
| end | ||
| end | ||
|
|
||
| @turbo for n ∈ indices((B, C), 3), m ∈ indices((A, C), 2), p ∈ 1:3 | ||
| Cₚₘₙ = zero(eltype(C)) | ||
| for k ∈ indices((A, B), (3, 2)) | ||
| Cₚₘₙ += A[1, m, k] * B[p+1, k, n] | ||
| end | ||
| C[p+1, m, n] = C[p+1, m, n] + α * Cₚₘₙ | ||
| end | ||
| _C | ||
| end | ||
| end # for | ||
|
|
||
| end # module |
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||
| function randdual(x) | ||||||
| _x = zeros(HyperDualNumbers.Hyper{Float64}, size(x)...) | ||||||
| for i in eachindex(x) | ||||||
| _x = HyperDualNumbers.Hyper(x[i], rand(), rand(), rand()) | ||||||
| end | ||||||
| return _x | ||||||
| end | ||||||
|
|
||||||
| function reinterpretHD(T, A) | ||||||
| tmp = reinterpret(T, A) | ||||||
| return tmp[1:4:end, :] | ||||||
| end | ||||||
|
|
||||||
| @time @testset "HyperDualNumbers.jl" begin | ||||||
| m = 53 | ||||||
| n = 63 | ||||||
| k = 73 | ||||||
|
|
||||||
| A1 = rand(Float64, m, k) | ||||||
| B1 = rand(Float64, k, n) | ||||||
| C1 = rand(Float64, m, n) | ||||||
|
|
||||||
| A2 = deepcopy(A1) | ||||||
| B2 = deepcopy(B1) | ||||||
| C2 = deepcopy(C1) | ||||||
|
|
||||||
| α = Float64(2.0) | ||||||
| β = Float64(2.0) | ||||||
|
|
||||||
| Octavian.matmul!(C1, A1, B1, α, β) | ||||||
| LinearAlgebra.mul!(C2, A2, B2, α, β) | ||||||
| @test C1 ≈ C2 | ||||||
|
||||||
| @test C1 ≈ C2 | |
| @test reinterpretH(C1) ≈ reinterpretHD(C2) |
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.
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.
why the slice?
Uh oh!
There was an error while loading. Please reload this page.
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.
reinterpreting a$N \times M$ matrix containing HyperDuals returns a $4N \times M$ matrix containing val, $\epsilon_{1}$ , $\epsilon_{2}$ and $\epsilon_{12}$ part of each of the entries.
This is essentially checking only the val part of the matrix$A$ .
I will do some updates and name the tests properly on what they are checking.
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.
Don't we also want to check the epsilons?
Uh oh!
There was an error while loading. Please reload this page.
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 thought what you mean in #179 (comment) is that we only check val part. Sorry for misunderstanding that and I will fix it now.
Uh oh!
There was an error while loading. Please reload this page.
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 meant that is what
isapproxonly checks the real part, so we need to reinterpret to check the entire thing.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 now check the entire thing and it should be ready for review. Sorry for iterating so many times.