-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fast Hadamard Transform #49
Open
nathanielpritchard
wants to merge
40
commits into
master
Choose a base branch
from
np_fast_hada_trans
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.
Open
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
c0e55ab
Added Row SRHT
nathanielpritchard 054acc9
added column SRHT
nathanielpritchard 20e6c85
Added FJLT for column
nathanielpritchard f0c01e2
made sketching matrix be returned for column SRHT and FJLT
nathanielpritchard e1a0ef6
added missing file
nathanielpritchard 699b1a1
removed overlapping documentation
nathanielpritchard f38f2ce
Added citations for the samplers
nathanielpritchard 5aa0c91
removed helper function folder and moved hadamard to sampler_helper
nathanielpritchard 8562145
Made minor changes
nathanielpritchard cf1b1ba
Removed unnecessary files
nathanielpritchard 362520c
Reverted unecessary files to master
nathanielpritchard 052e04c
Merge branch 'master' into np_fast_hada_trans
nathanielpritchard aefbf89
Updated the dependency list
nathanielpritchard c96d3ca
merMerging brarnches:wq
nathanielpritchard 49cb963
Fixed small formatting issues and added assertion to fwht
nathanielpritchard 2a8c963
Updated with master'
nathanielpritchard 5e77713
Rebased lambda commmit
nathanielpritchard a82691b
Added Row SRHT
nathanielpritchard 7d150ba
added column SRHT
nathanielpritchard 1ea1969
Fixed conflict with new samplers
nathanielpritchard ed42c49
made sketching matrix be returned for column SRHT and FJLT
nathanielpritchard 135d30b
added missing file
nathanielpritchard c1b8644
removed overlapping documentation
nathanielpritchard 9f763a2
Added citations for the samplers
nathanielpritchard 6d7d809
removed helper function folder and moved hadamard to sampler_helper
nathanielpritchard aea41fe
Made minor changes
nathanielpritchard ad57568
Fixed merge conflict
nathanielpritchard 7578849
Updated testing issue
nathanielpritchard 3cac0fe
Updated the dependency list
nathanielpritchard e60b187
Fixed small formatting issues and added assertion to fwht
nathanielpritchard 9b3ff07
Small wording changes in comments
nathanielpritchard ae2cb96
Changed the setup to return the sampling matrix, and renamed variable…
nathanielpritchard b3ecad0
updated tests set
nathanielpritchard d44dac7
Removed redundant directory
nathanielpritchard a513978
Deleted redundant file
nathanielpritchard 8e4fed8
Fxing merge conflicts
nathanielpritchard bc2a5a0
Merge branch 'master' into np_fast_hada_trans
nathanielpritchard 4883898
updated the log to the master
nathanielpritchard b373485
Fixed errors to have working test
nathanielpritchard dfd9e53
fixed merging issues with ma
nathanielpritchard 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## Internal Helper Functions | ||
|
||
```@contents | ||
Pages = ["helper_functions.md"] | ||
``` | ||
## Fast Hadamard transform | ||
|
||
```@docs | ||
RLinearAlgebra.fwht! | ||
``` |
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
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
""" | ||
LinSysBlockColFJLT <: LinSysBlkColSampler | ||
|
||
A mutable structure with fields to handle FJLT row sketching. For this procedure, | ||
the hadamard transform and random sign swaps are applied once, then that matrix is repeatably | ||
sampled. | ||
|
||
# Fields | ||
- `block_size::Int64`, the size of the sketching dimension | ||
- `sparsity::Float64`, the sparsity of the sampling matrix, should be between 0 and 1 | ||
- `padded_size::Int64`, the size of the matrix when padded | ||
- `sampling_matrix::Union{SparseMatrixCSC, Nothing}`, storage for sparse sketching matrix | ||
- `hadamard::Union{AbstractMatrix, Nothing}`, storage for the hadamard matrix. | ||
- `Ap::Union{AbstractMatrix, Nothing}`, storage for padded matrix | ||
- `bp::Union{AbstractMatrix, Nothing}`, storage for padded vector | ||
- `signs::Union{Vector{Bool}, Nothing}`, storage for random sign flips. | ||
- `scaling::Float64`, storage for the scaling of the sketches. | ||
|
||
Calling `LinSysBlockColFJLT()` defaults to setting `sparsity` to .3 and the blocksize to 2. | ||
|
||
Nir Ailon and Bernard Chazelle. 2006. Approximate nearest neighbors and the fast Johnson-Lindenstrauss transform. In Proceedings of the thirty-eighth annual ACM symposium on Theory of Computing (STOC '06). Association for Computing Machinery, New York, NY, USA, 557–563. https://doi.org/10.1145/1132516.1132597 | ||
""" | ||
mutable struct LinSysBlockColFJLT <: LinSysBlkColSampler | ||
block_size::Int64 | ||
sparsity::Float64 | ||
padded_size::Int64 | ||
sampling_matrix::Union{SparseMatrixCSC, Nothing} | ||
hadamard::Union{AbstractMatrix, Nothing} | ||
Ap::Union{AbstractMatrix, Nothing} | ||
bp::Union{AbstractVector, Nothing} | ||
signs::Union{Vector{Bool}, Nothing} | ||
scaling::Float64 | ||
end | ||
|
||
LinSysBlockColFJLT(;blocksize = 2, sparsity = .3) = LinSysBlockColFJLT( | ||
blocksize, | ||
sparsity, | ||
0, | ||
nothing, | ||
nothing, | ||
nothing, | ||
nothing, | ||
nothing, | ||
0.0 | ||
) | ||
|
||
# Common sample interface for linear systems | ||
function sample( | ||
type::LinSysBlockColFJLT, | ||
A::AbstractArray, | ||
b::AbstractVector, | ||
x::AbstractVector, | ||
iter::Int64 | ||
) | ||
if iter == 1 | ||
m, n = size(A) | ||
# If matrix is not a power of 2 then pad the rows | ||
if rem(log(2, n), 1) != 0 | ||
type.padded_size = Int64(2^(div(log(2, n), 1) + 1)) | ||
# Find nearest power 2 and allocate | ||
type.Ap = zeros(m, type.padded_size) | ||
# Pad matrix and constant vector | ||
type.Ap[:, 1:n] .= A | ||
else | ||
type.padded_size = n | ||
type.Ap = A | ||
end | ||
type.hadamard = hadamard(type.padded_size) | ||
# Compute scaling and sign flips | ||
type.scaling = sqrt(type.block_size / (type.padded_size * type.sparsity)) | ||
type.signs = bitrand(type.padded_size) | ||
# Apply FWHT to padded matrix and vector | ||
for i = 1:m | ||
Av = view(type.Ap, i, :) | ||
# Perform the fast walsh hadamard transform and update the ith column of Ap | ||
fwht!(Av, signs = type.signs, scaling = type.scaling) | ||
end | ||
|
||
end | ||
|
||
type.sampling_matrix = sprandn(type.padded_size, type.block_size, type.sparsity) | ||
AS = type.Ap * type.sampling_matrix | ||
# Residual of the linear system | ||
res = A * x - b | ||
grad = AS' * res | ||
sgn = [type.signs[i] ? 1 : -1 for i in 1:type.padded_size] | ||
return ((sgn .* type.hadamard) * type.sampling_matrix .* type.scaling), AS, res, grad | ||
end |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
""" | ||
LinSysBlockColSRHT <: LinSysBlkColSampler | ||
|
||
A mutable structure with fields to handle SRHT column sketching. For this procedure, | ||
the hadamard transform and random sign swaps are applied once, then that matrix is repeatably | ||
sampled. | ||
|
||
# Fields | ||
- `block_size::Int64`, the size of blocks being chosen | ||
- `padded_size::Int64`, the size of the matrix when padded | ||
- `block::Union{Vector{Int64}, Nothing}`, storage for block indices | ||
- `hadamard::Union{AbstractMatrix, Nothing}`, storage for the hadamard matrix. | ||
- `Ap::Union{AbstractMatrix, Nothing}`, storage for padded matrix | ||
- `signs::Union{Vector{Bool}, Nothing}`, storage for random sign flips. | ||
- `scaling::Float64`, storage for the scaling of the sketches. | ||
|
||
Calling `LinSysBlockColSRHT()` defaults to setting `block_size` to 2. | ||
|
||
Nir Ailon and Bernard Chazelle. 2006. Approximate nearest neighbors and the fast Johnson-Lindenstrauss transform. In Proceedings of the thirty-eighth annual ACM symposium on Theory of Computing (STOC '06). Association for Computing Machinery, New York, NY, USA, 557–563. https://doi.org/10.1145/1132516.1132597 | ||
""" | ||
mutable struct LinSysBlockColSRHT <: LinSysBlkColSampler | ||
block_size::Int64 | ||
padded_size::Int64 | ||
block::Union{Vector{Int64}, Nothing} | ||
hadamard::Union{AbstractMatrix, Nothing} | ||
Ap::Union{AbstractMatrix, Nothing} | ||
signs::Union{Vector{Bool}, Nothing} | ||
scaling::Float64 | ||
end | ||
|
||
LinSysBlockColSRHT(block_size) = LinSysBlockColSRHT( | ||
block_size, | ||
0, | ||
nothing, | ||
nothing, | ||
nothing, | ||
nothing, | ||
0.0 | ||
) | ||
LinSysBlockColSRHT() = LinSysBlockColSRHT(2, 0, nothing, nothing, nothing, nothing, 0.0) | ||
|
||
# Common sample interface for linear systems | ||
function sample( | ||
type::LinSysBlockColSRHT, | ||
A::AbstractArray, | ||
b::AbstractVector, | ||
x::AbstractVector, | ||
iter::Int64 | ||
) | ||
if iter == 1 | ||
m, n = size(A) | ||
# If matrix is not a power of 2 then pad the rows | ||
if rem(log(2, n), 1) != 0 | ||
type.padded_size = Int64(2^(div(log(2, n), 1) + 1)) | ||
# Find nearest power 2 and allocate | ||
type.Ap = zeros(m, type.padded_size) | ||
# Pad matrix and constant vector | ||
type.Ap[:, 1:n] .= A | ||
else | ||
type.padded_size = n | ||
type.Ap = A | ||
end | ||
type.hadamard = hadamard(type.padded_size) | ||
# Compute scaling and sign flips | ||
type.scaling = sqrt(type.block_size / type.padded_size) | ||
type.signs = bitrand(type.padded_size) | ||
for i = 1:m | ||
Av = view(type.Ap, i, :) | ||
# Perform the fast walsh hadamard transform and update the ith column of Ap | ||
fwht!(Av, signs = type.signs, scaling = type.scaling) | ||
end | ||
|
||
type.block = zeros(Int64, type.block_size) | ||
end | ||
|
||
type.block .= randperm(type.padded_size)[1:type.block_size] | ||
AS = type.Ap[:, type.block] | ||
# Residual of the linear system | ||
res = A * x - b | ||
grad = AS'res | ||
H = hadamard(type.padded_size) | ||
sgn = [type.signs[i] ? 1 : -1 for i in 1:type.padded_size] | ||
return ((sgn .* type.hadamard) .* type.scaling)[:, type.block], AS, res, grad | ||
end |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
""" | ||
LinSysBlockRowFJLT <: LinSysBlkRowSampler | ||
|
||
A mutable structure with fields to handle FJLT row sketching. For this procedure, | ||
the hadamard transform and random sign swaps are applied once, then that matrix is repeatably | ||
sampled. | ||
|
||
# Fields | ||
- `block_size::Int64`, the size of the sketching dimension | ||
- `sparsity::Float64`, the sparsity of the sampling matrix | ||
- `padded_size::Int64`, the size of the matrix when padded | ||
- `sampling_matrix::Union{SparseMatrixCSC, Nothing}`, storage for sparse sketching matrix | ||
- `hadamard::Union{AbstractMatrix, Nothing}`, storage for the hadamard matrix. | ||
- `Ap::Union{AbstractMatrix, Nothing}`, storage for padded matrix | ||
- `bp::Union{AbstractMatrix, Nothing}`, storage for padded vector | ||
- `signs::Union{Vector{Bool}, Nothing}`, storage for random sign flips. | ||
- `scaling::Float64`, storage for the scaling of the sketches. | ||
|
||
Calling `LinSysBlockRowFJLT()` defaults to setting `sparsity` to .3 and the blocksize to 2. | ||
|
||
Nir Ailon and Bernard Chazelle. 2006. Approximate nearest neighbors and the fast Johnson-Lindenstrauss transform. In Proceedings of the thirty-eighth annual ACM symposium on Theory of Computing (STOC '06). Association for Computing Machinery, New York, NY, USA, 557–563. https://doi.org/10.1145/1132516.1132597 | ||
""" | ||
mutable struct LinSysBlockRowFJLT <: LinSysBlkRowSampler | ||
block_size::Int64 | ||
sparsity::Float64 | ||
padded_size::Int64 | ||
sampling_matrix::Union{SparseMatrixCSC, Nothing} | ||
hadamard::Union{AbstractMatrix, Nothing} | ||
Ap::Union{AbstractMatrix, Nothing} | ||
bp::Union{AbstractVector, Nothing} | ||
signs::Union{Vector{Bool}, Nothing} | ||
scaling::Float64 | ||
end | ||
|
||
LinSysBlockRowFJLT(;blocksize = 2, sparsity = .3) = LinSysBlockRowFJLT( | ||
blocksize, | ||
sparsity, | ||
0, | ||
nothing, | ||
nothing, | ||
nothing, | ||
nothing, | ||
nothing, | ||
0.0 | ||
) | ||
|
||
# Common sample interface for linear systems | ||
function sample( | ||
type::LinSysBlockRowFJLT, | ||
A::AbstractArray, | ||
b::AbstractVector, | ||
x::AbstractVector, | ||
iter::Int64 | ||
) | ||
if iter == 1 | ||
m, n = size(A) | ||
# If matrix is not a power of 2 then pad the rows | ||
if rem(log(2, m), 1) != 0 | ||
type.padded_size = Int64(2^(div(log(2, m), 1) + 1)) | ||
# Find nearest power 2 and allocate | ||
type.Ap = zeros(type.padded_size, n) | ||
type.bp = zeros(type.padded_size) | ||
# Pad matrix and constant vector | ||
type.Ap[1:m, :] .= A | ||
type.bp[1:m] .= b | ||
else | ||
type.padded_size = m | ||
type.Ap = A | ||
type.bp = b | ||
end | ||
type.hadamard = hadamard(type.padded_size) | ||
# Compute scaling and sign flips | ||
type.scaling = sqrt(type.block_size / (type.padded_size * type.sparsity)) | ||
type.signs = bitrand(type.padded_size) | ||
# Apply FWHT to padded matrix and vector | ||
fwht!(type.bp, signs = type.signs, scaling = type.scaling) | ||
for i = 1:n | ||
Av = view(type.Ap, :, i) | ||
# Perform the fast walsh hadamard transform and update the ith row of Ap | ||
@views fwht!(Av, signs = type.signs, scaling = type.scaling) | ||
end | ||
|
||
end | ||
|
||
type.sampling_matrix = sprandn(type.block_size, type.padded_size, type.sparsity) | ||
SA = type.sampling_matrix * type.Ap | ||
Sb = type.sampling_matrix * type.bp | ||
# Residual of the linear system | ||
res = SA * x - Sb | ||
sgn = [type.signs[i] ? 1 : -1 for i in 1:type.padded_size] | ||
return type.sampling_matrix * (sgn .* type.hadamard) .* type.scaling, SA, res | ||
end |
Oops, something went wrong.
Oops, something went wrong.
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.
@nathanielpritchard we need to be more disciplined about incrementing the version.