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

Osd cs weight one fix #38

Open
wants to merge 3 commits into
base: l4_dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
153 changes: 70 additions & 83 deletions src_cpp/osd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,45 @@
#include <vector>
#include <memory>
#include <iterator>
#include <cmath>
#include <cmath>
#include <limits>

#include "bp.hpp"
#include "sort.hpp"
#include "util.hpp"
#include "gf2sparse_linalg.hpp"

namespace ldpc::osd{
namespace ldpc::osd {


enum OsdMethod{
OSD_OFF,
OSD_0,
EXHAUSTIVE,
COMBINATION_SWEEP
};
enum OsdMethod {
OSD_OFF,
OSD_0,
EXHAUSTIVE,
COMBINATION_SWEEP
};


class OsdDecoder{
class OsdDecoder {
public:
OsdMethod osd_method;
int osd_order;
int k, bit_count, check_count;
ldpc::bp::BpSparse& pcm;
std::vector<double>& channel_probabilities;
ldpc::bp::BpSparse &pcm;
std::vector<double> &channel_probabilities;
std::vector<uint8_t> osd0_decoding;
std::vector<uint8_t> osdw_decoding;
std::vector<std::vector<uint8_t>> osd_candidate_strings;
std::vector<int> column_ordering;
ldpc::gf2sparse_linalg::RowReduce<ldpc::bp::BpEntry>* LuDecomposition;
ldpc::gf2sparse_linalg::RowReduce<ldpc::bp::BpEntry> *LuDecomposition;

OsdDecoder(
ldpc::bp::BpSparse& parity_check_matrix,
OsdMethod osd_method,
int osd_order,
std::vector<double>& channel_probs):
pcm(parity_check_matrix),
channel_probabilities(channel_probs)
{
ldpc::bp::BpSparse &parity_check_matrix,
OsdMethod osd_method,
int osd_order,
std::vector<double> &channel_probs) :
pcm(parity_check_matrix),
channel_probabilities(channel_probs) {

this->bit_count = this->pcm.n;
this->check_count = this->pcm.m;
Expand All @@ -56,44 +55,48 @@ class OsdDecoder{

}

int osd_setup(){
int osd_setup() {

this->osd_candidate_strings.clear();

if(this->osd_method == OSD_OFF) return 0;

if (this->osd_method == OSD_OFF) {
return 0;
}

this->LuDecomposition = new ldpc::gf2sparse_linalg::RowReduce<ldpc::bp::BpEntry>(this->pcm);
this->column_ordering.resize(this->pcm.n);
int osd_candidate_string_count;
this->LuDecomposition->rref(false,true);
this->LuDecomposition->rref(false, true);
this->k = this->pcm.n - this->LuDecomposition->rank;

if(this->osd_method == OSD_0 || this->osd_order==0){
if (this->osd_method == OSD_0 || this->osd_order == 0) {
return 1;
}

if(this->osd_method == EXHAUSTIVE){
osd_candidate_string_count = pow(2,this->osd_order);
for(int i=1; i<osd_candidate_string_count; i++){
this->osd_candidate_strings.push_back(ldpc::util::decimal_to_binary_reverse(i,k));
if (this->osd_method == EXHAUSTIVE) {
osd_candidate_string_count = pow(2, this->osd_order);
for (int i = 1; i < osd_candidate_string_count; i++) {
this->osd_candidate_strings.push_back(ldpc::util::decimal_to_binary_reverse(i, k));
}
}

if(this->osd_method == COMBINATION_SWEEP){
for(int i=0; i<k; i++) {
// If osd with higher order lambda>0 is used, try lambda first wt 1 solutions, otherwise all
if (this->osd_method == COMBINATION_SWEEP) {
auto lambda = this->osd_order > 0 ? this->osd_order : this->k;
for (int i = 0; i < lambda; i++) {
std::vector<uint8_t> osd_candidate;
osd_candidate.resize(k,0);
osd_candidate[i]=1;
osd_candidate.resize(k, 0);
osd_candidate[i] = 1;
this->osd_candidate_strings.push_back(osd_candidate);
}

for(int i = 0; i<this->osd_order;i++){
for(int j = 0; j<this->osd_order; j++){
if(j<=i) continue;
for (int i = 0; i < this->osd_order; i++) {
for (int j = 0; j < this->osd_order; j++) {
if (j <= i) continue;
std::vector<uint8_t> osd_candidate;
osd_candidate.resize(k,0);
osd_candidate[i]=1;
osd_candidate[j]=1;
osd_candidate.resize(k, 0);
osd_candidate[i] = 1;
osd_candidate[j] = 1;
this->osd_candidate_strings.push_back(osd_candidate);
}
}
Expand All @@ -102,93 +105,77 @@ class OsdDecoder{
return 1;
}

~OsdDecoder(){
~OsdDecoder() {
delete this->LuDecomposition;
};


std::vector<uint8_t>& decode(std::vector<uint8_t>& syndrome, std::vector<double>& log_prob_ratios) {

ldpc::sort::soft_decision_col_sort(log_prob_ratios, this->column_ordering,bit_count);
std::vector<uint8_t> &decode(std::vector<uint8_t> &syndrome, std::vector<double> &log_prob_ratios) {
ldpc::sort::soft_decision_col_sort(log_prob_ratios, this->column_ordering, bit_count);

if(this->osd_order == 0){
this->osd0_decoding = this->osdw_decoding = this->LuDecomposition->fast_solve(syndrome,this->column_ordering);
if (this->osd_order == 0) {
this->osd0_decoding = this->osdw_decoding = this->LuDecomposition->fast_solve(syndrome,
this->column_ordering);
return this->osd0_decoding;
}

//row reduce the matrix according to the new column ordering
this->LuDecomposition->rref(false,true,this->column_ordering);

this->LuDecomposition->rref(false, true, this->column_ordering);
// find the OSD0 solution
this->osd0_decoding = this->osdw_decoding = LuDecomposition->lu_solve(syndrome);

// this->osd0_decoding = this->LuDecomposition->fast_solve(syndrome,this->column_ordering);

// if(osd_order==0){
// return this->osd0_decoding;
// }

double candidate_weight, osd_min_weight;

osd_min_weight=0;
for(int i=0; i<this->pcm.n; i++){
if(this->osd0_decoding[i]==1){
osd_min_weight+=log(1/this->channel_probabilities[i]);
osd_min_weight = 0;
for (int i = 0; i < this->pcm.n; i++) {
if (this->osd0_decoding[i] == 1) {
osd_min_weight += log(1 / this->channel_probabilities[i]);
}
}

std::vector<int> non_pivot_columns;
std::vector<ldpc::gf2sparse::GF2Entry*> delete_entries;
for(int i = this->LuDecomposition->rank; i<this->pcm.n; i++){
std::vector<ldpc::gf2sparse::GF2Entry *> delete_entries;
for (int i = this->LuDecomposition->rank; i < this->pcm.n; i++) {
int col = this->LuDecomposition->cols[i];
non_pivot_columns.push_back(col);
for(auto& e: this->LuDecomposition->U.iterate_column(col)){

for (auto &e: this->LuDecomposition->U.iterate_column(col)) {
delete_entries.push_back(&e);
}
}

for (auto e: delete_entries){
for (auto e: delete_entries) {
this->LuDecomposition->U.remove(*e);
}

for(auto& candidate_string: this->osd_candidate_strings){

for (auto &candidate_string: this->osd_candidate_strings) {
auto t_syndrome = syndrome;
int col_index = 0;
for(int col: non_pivot_columns){
if(candidate_string[col_index]==1){
for(auto& e: this->pcm.iterate_column(col)){
for (int col: non_pivot_columns) {
if (candidate_string[col_index] == 1) {
for (auto &e: this->pcm.iterate_column(col)) {
t_syndrome[e.row_index] ^= 1;
}
}
col_index++;
}

auto candidate_solution = this->LuDecomposition->lu_solve(t_syndrome);
for(int i=0; i<k; i++){
candidate_solution[non_pivot_columns[i]]=candidate_string[i];
for (int i = 0; i < k; i++) {
candidate_solution[non_pivot_columns[i]] = candidate_string[i];
}
candidate_weight=0;
for(int i=0; i<this->pcm.n; i++){
if(candidate_solution[i]==1){
candidate_weight+=log(1/this->channel_probabilities[i]);
candidate_weight = 0;
for (int i = 0; i < this->pcm.n; i++) {
if (candidate_solution[i] == 1) {
candidate_weight += log(1 / this->channel_probabilities[i]);
}
}
if(candidate_weight<osd_min_weight){
if (candidate_weight < osd_min_weight) {
osd_min_weight = candidate_weight;

this->osdw_decoding = candidate_solution;

}

}

return this->osdw_decoding;

}

};
};

}//end osd namespace

Expand Down
5 changes: 4 additions & 1 deletion src_python/ldpc/bposd_decoder/_bposd_decoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ cdef class BpOsdDecoder(BpDecoderBase):
osd_method : int, optional
The OSD method used. Must be one of {'OSD_0', 'OSD_E', 'OSD_CS'}.
osd_order : int, optional
The OSD order, by default 0.
The OSD order, by default 0. Note that if OSD_CS is chosen with osd_order > 0, the algorithm will consider weight
one solutions of the osd_order many most reliable bits only.

Notes
-----
Expand Down Expand Up @@ -129,6 +130,8 @@ cdef class BpOsdDecoder(BpDecoderBase):
for i in range(self.n):
out[i] = self.bpd.decoding[i]
else:
if self.osdD.osd_method == OSD_CS and self.osd_order > 0:
print(f"Using OSD_CS with osd_order={self.osd_order}, checking osd_order many weight one solutions only.")
self.osdD.decode(self._syndrome, self.bpd.log_prob_ratios)
for i in range(self.n):
out[i] = self.osdD.osdw_decoding[i]
Expand Down
Loading