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

some performance optimizations in decoders #369

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,25 @@ parity_checks(d::BeliefPropDecoder) = d.H
parity_checks(d::BitFlipDecoder) = d.H

function decode(d::BeliefPropDecoder, syndrome_sample)
row_x = syndrome_sample[1:d.cx]
row_z = syndrome_sample[d.cx+1:d.cx+d.cz]
row_x = @view syndrome_sample[1:d.cx]
row_z = @view syndrome_sample[d.cx+1:d.cx+d.cz]
Fe-r-oz marked this conversation as resolved.
Show resolved Hide resolved
guess_z, success = LDPCDecoders.decode!(d.bpdecoderx, row_x)
guess_x, success = LDPCDecoders.decode!(d.bpdecoderz, row_z)
return vcat(guess_x, guess_z)
result = Matrix{Int}(undef, 2, length(guess_x))
@inbounds result[1, 1:length(guess_x)] .= guess_x
@inbounds result[2, 1:length(guess_z)] .= guess_z
return result
Fe-r-oz marked this conversation as resolved.
Show resolved Hide resolved
end

function decode(d::BitFlipDecoder, syndrome_sample)
row_x = syndrome_sample[1:d.cx]
row_z = syndrome_sample[d.cx+1:d.cx+d.cz]
row_x = @view syndrome_sample[1:d.cx]
row_z = @view syndrome_sample[d.cx+1:d.cx+d.cz]
guess_z, success = LDPCDecoders.decode!(d.bfdecoderx, row_x)
guess_x, success = LDPCDecoders.decode!(d.bfdecoderz, row_z)
return vcat(guess_x, guess_z)
result = Matrix{Int}(undef, 2, length(guess_x))
@inbounds result[1, 1:length(guess_x)] .= guess_x
@inbounds result[2, 1:length(guess_z)] .= guess_z
return result
Fe-r-oz marked this conversation as resolved.
Show resolved Hide resolved
end

end
28 changes: 19 additions & 9 deletions ext/QuantumCliffordPyQDecodersExt/QuantumCliffordPyQDecodersExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ end
parity_checks(d::PyBP) = d.H

function decode(d::PyBP, syndrome_sample)
row_x = syndrome_sample[1:d.nx] # TODO These copies and indirections might be costly!
row_z = syndrome_sample[d.nx+1:end]
row_x = @view syndrome_sample[1:d.nx]
row_z = @view syndrome_sample[d.nx+1:end]
guess_z_errors = PythonCall.PyArray(d.pyx.decode(np.array(row_x)))
guess_x_errors = PythonCall.PyArray(d.pyz.decode(np.array(row_z)))
vcat(guess_x_errors, guess_z_errors)
result = Matrix{Int}(undef, 2, length(guess_x_errors))
@inbounds result[1, 1:length(guess_x_errors)] .= guess_x_errors
@inbounds result[2, 1:length(guess_z_errors)] .= guess_z_errors
return result
Fe-r-oz marked this conversation as resolved.
Show resolved Hide resolved
end

struct PyMatchingDecoder <: AbstractSyndromeDecoder # TODO all these decoders have the same fields, maybe we can factor out a common type
Expand Down Expand Up @@ -106,19 +109,26 @@ end
parity_checks(d::PyMatchingDecoder) = d.H

function decode(d::PyMatchingDecoder, syndrome_sample)
row_x = syndrome_sample[1:d.nx] # TODO This copy is costly!
row_z = syndrome_sample[d.nx+1:end]
row_x = @view syndrome_sample[1:d.nx]
row_z = @view syndrome_sample[d.nx+1:end]
guess_z_errors = PythonCall.PyArray(d.pyx.decode(row_x))
guess_x_errors = PythonCall.PyArray(d.pyz.decode(row_z))
vcat(guess_x_errors, guess_z_errors)
result = Matrix{Int}(undef, 2, length(guess_x_errors))
@inbounds result[1, 1:length(guess_x_errors)] .= guess_x_errors
@inbounds result[2, 1:length(guess_z_errors)] .= guess_z_errors
return result
Fe-r-oz marked this conversation as resolved.
Show resolved Hide resolved
end

function batchdecode(d::PyMatchingDecoder, syndrome_samples)
row_x = syndrome_samples[:,1:d.nx] # TODO This copy is costly!
row_z = syndrome_samples[:,d.nx+1:end]
row_x = @view syndrome_samples[:,1:d.nx]
row_z = @view syndrome_samples[:,d.nx+1:end]
guess_z_errors = PythonCall.PyArray(d.pyx.decode_batch(row_x))
guess_x_errors = PythonCall.PyArray(d.pyz.decode_batch(row_z))
hcat(guess_x_errors, guess_z_errors)
n_cols_x = size(guess_x_errors, 2)
result = Matrix{Int}(undef, size(guess_x_errors, 1), n_cols_x + size(guess_z_errors, 2))
@inbounds result[:,1:n_cols_x] .= guess_x_errors
@inbounds result[:,n_cols_x+1:end] .= guess_z_errors
return result
Fe-r-oz marked this conversation as resolved.
Show resolved Hide resolved
end

end
24 changes: 18 additions & 6 deletions src/ecc/decoder_pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function batchdecode(d::AbstractSyndromeDecoder, syndrome_samples)
samples, _s = size(syndrome_samples)
s == _s || throw(ArgumentError(lazy"The syndromes given to `batchdecode` have the wrong dimensions. The syndrome length is $(_s) while it should be $(s)"))
results = falses(samples, 2n)
for (i,syndrome_sample) in enumerate(eachrow(syndrome_samples))
@inbounds for i in 1:samples
syndrome_sample = @view syndrome_samples[i,:]
Fe-r-oz marked this conversation as resolved.
Show resolved Hide resolved
guess = decode(d, syndrome_sample)# TODO use `decode!`
isnothing(guess) || (results[i,:] = guess)
end
Expand Down Expand Up @@ -171,10 +172,21 @@ end

function evaluate_guesses(measured_faults, guesses, faults_matrix)
nsamples = size(guesses, 1)
guess_faults = (faults_matrix * guesses') .% 2 # TODO this can be faster and non-allocating by turning it into a loop
decoded = 0
for i in 1:nsamples # TODO this can be faster and non-allocating by having the loop and the matrix multiplication on the line above work together and not store anything
(@view guess_faults[:,i]) == (@view measured_faults[i,:]) && (decoded += 1)
for i in 1:nsamples
is_decoded = true
for j in 1:size(faults_matrix, 1)
sum_mod = 0
@inbounds @simd for k in 1:size(faults_matrix, 2)
sum_mod += faults_matrix[j, k] * guesses[i, k]
end
sum_mod %= 2
if sum_mod != measured_faults[i, j]
is_decoded = false
break
end
end
decoded += is_decoded
Copy link
Member

Choose a reason for hiding this comment

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

This seems promising, but is it actually faster on its own? It probably needs to be benchmarked separately

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most of the performance improvements occurred due to evaluate guesses. I noticed that the in-place approach to vcat and hcat slowed things down, increased memory/time ratio, that was why the improvements was not that visible. I have reverted back as per your comments. Now, it looks better: #369 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I pushed the evaluate_guess as a separate commit so before and after performance are present in the log:
Before evaluate guesses: Results, After evaluate guesses: Results.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#Micro benchmarks: evaluate guesses 

julia> using BenchmarkTools
julia> n_faults = 500;
julia> n_guesses = 100;
julia> measured_faults = rand(0:1, n_guesses, n_faults);
julia> guesses = rand(0:1, n_guesses, n_faults);
julia> faults_matrix = rand(0:1, n_faults, n_faults);
julia> function evaluate_guesses_view(measured_faults, guesses, faults_matrix)
           nsamples = size(guesses, 1)
           guess_faults = (faults_matrix * guesses') .% 2
           decoded = 0
           for i in 1:nsamples
               (@view guess_faults[:,i]) == (@view measured_faults[i,:]) && (decoded += 1)
           end
           return (nsamples - decoded) / nsamples
       end
julia> function evaluate_guesses_loop(measured_faults, guesses, faults_matrix)
           nsamples = size(guesses, 1)
           decoded = 0
           for i in 1:nsamples
               is_decoded = true
               for j in 1:size(faults_matrix, 1)
                   sum_mod = 0
                   @inbounds @simd for k in 1:size(faults_matrix, 2)
                       sum_mod += faults_matrix[j, k] * guesses[i, k]
                   end
                   sum_mod %= 2
                   if sum_mod != measured_faults[i, j]
                       is_decoded = false
                       break
                   end
               end
               decoded += is_decoded
           end
           return (nsamples - decoded) / nsamples
       end
julia> @benchmark evaluate_guesses_view($measured_faults, $guesses, $faults_matrix)
BenchmarkTools.Trial: 423 samples with 1 evaluation.
 Range (min … max):   9.462 ms … 100.313 ms  ┊ GC (min … max): 0.00% … 87.81%
 Time  (median):     11.613 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   11.808 ms ±   4.717 ms  ┊ GC (mean ± σ):  2.29% ±  5.55%

  ▂▂ ▅█▇▄                      ▂                                
  ██▅████▅▄▂▃▄▃▄▄▃▄▄▅▇▆▇▃▇▇▄█▇▇█▆▅█▆▇▆▇▆▅▇▄▃▂▅▂▃▃▃▂▂▃▂▃▁▂▁▁▁▂▂ ▄
  9.46 ms         Histogram: frequency by time         15.5 ms <

 Memory estimate: 812.11 KiB, allocs estimate: 8.

julia> @benchmark evaluate_guesses_loop($measured_faults, $guesses, $faults_matrix)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  119.402 μs …  1.440 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     127.033 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   135.949 μs ± 49.806 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █  ▇▃▄▃▂▁▁  ▁▁▁▂▁     ▁                                      ▁
  █▆▄███████████████████████████▇▇██▇▇▇▆▆▇▇▆▆▅▅▅▄▆▇▆▄▆▄▄▄▄▅▃▃▅ █
  119 μs        Histogram: log(frequency) by time       246 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.


# Micro benchmarks attached, with and without view:

julia> syndrome_samples = rand(1000, 1000) ;
julia> d = Dict(:nx => 500);
julia> function with_view(syndrome_samples, d)
           row_x = @view syndrome_samples[:, 1:d[:nx]]
           row_z = @view syndrome_samples[:, d[:nx]+1:end]
           return row_x, row_z
       end
julia> function without_view(syndrome_samples, d)
           row_x = syndrome_samples[:, 1:d[:nx]]
           row_z = syndrome_samples[:, d[:nx]+1:end]
           return row_x, row_z
       end

julia> @benchmark with_view($syndrome_samples, $d)

       # Benchmarking without @view
BenchmarkTools.Trial: 10000 samples with 996 evaluations.
 Range (min … max):  24.653 ns … 225.071 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     24.912 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   27.309 ns ±   7.016 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █ ▇▄▄▄                           ▁                           ▂
  █▁█████▄▄▃▃▄▅▄▃▃▁▃▁▃▄▇▇▇▆▄▇▆▆█▇▇▇██▇█▆▆▅▅▅▄▄▆▆▇▆▅▅▆▇▇▄▄▆▅▆▆▇ █
  24.7 ns       Histogram: log(frequency) by time        60 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark without_view($syndrome_samples, $d)
BenchmarkTools.Trial: 3337 samples with 1 evaluation.
 Range (min … max):  995.098 μs …   7.976 ms  ┊ GC (min … max): 0.00% … 85.61%
 Time  (median):       1.064 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):     1.491 ms ± 795.193 μs  ┊ GC (mean ± σ):  6.96% ± 13.21%

  ██▆▄▂                     ▁▃▃▂                ▂▃▂▁    ▁▂▃▂    ▁
  ██████▆▆▅▆▃▅▃▃▁▁▅▁▁▃▄▄▃▃▁▆█████▇▄▄▄▃▅▃▁▁▁▁▁▁▁▇█████▆▅▆█████▇▆ █
  995 μs        Histogram: log(frequency) by time       3.33 ms <

 Memory estimate: 7.63 MiB, allocs estimate: 4.

end
return (nsamples - decoded) / nsamples
end
Expand Down Expand Up @@ -253,8 +265,8 @@ function create_lookup_table(code::Stabilizer)
lookup_table
end;

function decode(d::TableDecoder, syndrome_sample)
d.lookup_buffer .= syndrome_sample # TODO have this work without data copying, by supporting the correct types, especially in the batch decode case
function decode(d::TableDecoder, syndrome_sample::AbstractVector{Bool})
Fe-r-oz marked this conversation as resolved.
Show resolved Hide resolved
@inbounds copyto!(d.lookup_buffer, syndrome_sample)
Fe-r-oz marked this conversation as resolved.
Show resolved Hide resolved
return get(d.lookup_table, d.lookup_buffer, nothing)
end

Expand Down
Loading