Skip to content

Commit

Permalink
Correcting Viterbi Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ioeddk committed May 30, 2024
1 parent b7f8e22 commit a523dc1
Show file tree
Hide file tree
Showing 8 changed files with 1,624 additions and 46 deletions.
6 changes: 6 additions & 0 deletions cosim_scaling.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

python /home/centos/workspace/DP-HLS/py-hls/auto_cosim.py --config /home/centos/workspace/DP-HLS/compile_configs/scaling_global_linear/global_linear_scaling_pe.json --simulate True
python /home/centos/workspace/DP-HLS/py-hls/auto_cosim.py --config /home/centos/workspace/DP-HLS/compile_configs/scaling_global_linear/global_linear_scaling_blocks.json --simulate True
python /home/centos/workspace/DP-HLS/py-hls/auto_cosim.py --config /home/centos/workspace/DP-HLS/compile_configs/scaling_global_dtw/global_dtw_scaling_pes.json --simulate True
python /home/centos/workspace/DP-HLS/py-hls/auto_cosim.py --config /home/centos/workspace/DP-HLS/compile_configs/scaling_global_dtw/global_dtw_scaling_blocks.json --simulate True
5 changes: 5 additions & 0 deletions further_throughput_1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

python /home/centos/workspace/DP-HLS/py-hls/parallel_compile.py --config /home/centos/workspace/DP-HLS/compile_configs/global_two_piece_affine/two_piece_affine_common_config_max.json --compile True --num_workers 1 --all True
python /home/centos/workspace/DP-HLS/py-hls/parallel_compile.py --config /home/centos/workspace/DP-HLS/compile_configs/global_affine/global_affine_common_config.json --compile True --num_workers 1 --all True
python /home/centos/workspace/DP-HLS/py-hls/parallel_compile.py --config /home/centos/workspace/DP-HLS/compile_configs/local_linear/local_linear_common_configs.json --compile True --num_workers 1 --all True
4 changes: 4 additions & 0 deletions further_throughput_2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

python /home/centos/workspace/DP-HLS/py-hls/parallel_compile.py --config /home/centos/workspace/DP-HLS/compile_configs/overlap_suffix_prefix/overlap_suffix_prefix_config.json --compile True --num_workers 1 --all True
python /home/centos/workspace/DP-HLS/py-hls/parallel_compile.py --config /home/centos/workspace/DP-HLS/compile_configs/semiglobal_short_long/local_linear_common_configs.json --compile True --num_workers 1 --all True
21 changes: 11 additions & 10 deletions include/solution_viterbi.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void viterbi_solution(std::string query, std::string reference, PENALTY_T &penal
double del_up;
double ins_left;
double main_diag, main_up, main_left;
double ins_diag, del_diag;

if (i == 0 && j == 0)
{
Expand All @@ -97,10 +98,10 @@ void viterbi_solution(std::string query, std::string reference, PENALTY_T &penal
main_left = initial_col[0][1];

ins_left = initial_col[0][0];
// ins_diag = 0;
ins_diag = 0;

del_up = initial_row[0][2];
// del_diag = 0;
del_diag = 0;
}
else if (i == 0 && j > 0) // In first row, not column
{
Expand All @@ -109,10 +110,10 @@ void viterbi_solution(std::string query, std::string reference, PENALTY_T &penal
main_left = score_mat[1][i][j - 1];

ins_left = score_mat[0][0][j - 1];
// ins_diag = initial_row[j - 1][0];
ins_diag = initial_row[j - 1][0];

del_up = initial_row[j][2];
// del_diag = initial_row[j - 1][2];
del_diag = initial_row[j - 1][2];
}
else if (i > 0 && j == 0)
{
Expand All @@ -121,10 +122,10 @@ void viterbi_solution(std::string query, std::string reference, PENALTY_T &penal
main_left = initial_col[i][1];

ins_left = initial_col[i][0];
// ins_diag = initial_col[i - 1][0];
ins_diag = initial_col[i - 1][0];

del_up = score_mat[2][i - 1][j];
// del_diag = initial_col[i - 1][2];
del_diag = initial_col[i - 1][2];
}
else
{
Expand All @@ -133,10 +134,10 @@ void viterbi_solution(std::string query, std::string reference, PENALTY_T &penal
main_left = score_mat[1][i][j - 1];

ins_left = score_mat[0][i][j - 1];
// ins_diag = score_mat[0][i-1][j-1];
ins_diag = score_mat[0][i-1][j-1];

del_up = score_mat[2][i - 1][j];
// del_diag = score_mat[2][i-1][j-1];
del_diag = score_mat[2][i-1][j-1];
}

double del_write, ins_write, main_write; // values write to the score matrix
Expand All @@ -152,8 +153,8 @@ void viterbi_solution(std::string query, std::string reference, PENALTY_T &penal
del_write = penalties.transition[4][HostUtils::Sequence::base_to_num(reference[j])] + (del_open_b ? del_open : del_extend);

double main_match = penalties.log_1_m_2_lambda + main_diag;
double main_ins = penalties.log_mu + ins_left;
double main_del = penalties.log_mu + del_up;
double main_ins = penalties.log_mu + ins_diag;
double main_del = penalties.log_mu + del_diag;

double main_max = main_match;
main_max = main_max > main_ins ? main_max : main_ins;
Expand Down
37 changes: 21 additions & 16 deletions kernels/viterbi/kernel_viterbi.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "frontend.h"

#ifdef CMAKEDEBUG

#endif

void Viterbi::PE::Compute(char_t local_query_val,
char_t local_reference_val,
score_vec_t up_prev,
Expand Down Expand Up @@ -49,24 +53,24 @@ void Viterbi::PE::Compute(char_t local_query_val,
#endif

const type_t match_M = penalties.log_1_m_2_lambda + diag_prev[1];
const type_t match_I = penalties.log_mu + left_prev[0];
const type_t match_D = penalties.log_mu + up_prev[2];
const type_t match_I = penalties.log_mu + diag_prev[0];
const type_t match_D = penalties.log_mu + diag_prev[2];

#ifdef CMAKEDEBUG
auto diag_prev_s = diag_prev[1].to_float();
auto local_query_val_s = local_query_val.to_int();
auto local_reference_val_s = local_reference_val.to_int();
#endif

type_t max_val = match_M;
tbp_t dir_tb = TB_DIAG;

type_t transition_score = penalties.transition[local_query_val][local_reference_val];

#ifdef CMAKEDEBUG
auto write_score_1_s = write_score[1].to_float();
#endif

type_t max_val = match_M;
tbp_t dir_tb = TB_DIAG;

// Set traceback pointer based on the direction of the maximum score.
if (max_val < match_I)
{ // Insert Case
Expand Down Expand Up @@ -129,49 +133,50 @@ void Viterbi::InitializeMaxScores(ScorePack (&max)[PE_NUM], idx_t qry_len, idx_t

void Viterbi::Traceback::StateMapping(tbp_t tbp, TB_STATE &state, tbr_t &navigation)
{
#ifdef CMAKEDEBUG
std::cout << state << " ";
#endif

if (state == TB_STATE::MM)
{
if (tbp(1, 0) == TB_DIAG)
{
navigation = AL_MMI;

}
else if (tbp(1, 0) == TB_UP)
{
state = TB_STATE::DEL;
navigation = AL_NULL;
}
else if (tbp(1, 0) == TB_LEFT)
{
state = TB_STATE::INS;
navigation = AL_NULL;

} else {
navigation = AL_END;
}
navigation = AL_MMI;
}
else if (state == TB_STATE::DEL)
{
if ((bool)tbp[3])
{ // deletion extending
// states remains the same.
// printf("delete extend");
{
state = TB_STATE::MM; // set the state back to MM
}
else
{ // deletion closing
state = TB_STATE::MM; // set the state back to MM

}
navigation = AL_DEL;
}
else if (state == TB_STATE::INS)
{
if ((bool)tbp[2])
{ // insertion extending
// states remains the same.
// ("delete extend");
{
state = TB_STATE::MM; // set the state back to MM
}
else
{ // insertion closing
state = TB_STATE::MM; // set the state back to MM

}
navigation = AL_INS;
}
Expand Down
5 changes: 2 additions & 3 deletions kernels/viterbi/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <hls_vector.h>
#include <ap_int.h>

#define MAX_QUERY_LENGTH 64
#define MAX_REFERENCE_LENGTH 64
#define MAX_QUERY_LENGTH 256
#define MAX_REFERENCE_LENGTH 256
#define N_BLOCKS 1
#define PE_NUM 8

Expand All @@ -28,7 +28,6 @@ typedef ap_uint<4> tbp_t; // Traceback Pointer Type

// Legacy Debugger Configuration
#define DEBUG_OUTPUT_PATH "/home/[email protected]/DP-HLS-Debug/global_affine/"
#define DEBUG_FILENAME "debug_kernel"

// Define Traceback Pointer Navigation Direction
#define TB_PH (tbp_t) 0b0000
Expand Down
37 changes: 20 additions & 17 deletions testbench/test_csim_viterbi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,32 @@ struct Penalties_sol {
double log_1_m_mu;
double transition[5][5];
};
/*
Query : TTACTATTAATGCAAATGCACCTGATGGACGTATCGCAAATGCGTAACTCCCTCCGGTGTAACTAATAAAAGACTCTACGATAAAGGAACTACAGCGGGAACCGTCCGCTCTTTCGCGGATTCAGAGGCGCCACATGGGATCCAGGAAAAAGGTCAGGTAACGAGGAAAAATTTTGAAATTGGGAGATTCAGTTGTCTATGAAGCAAATGGCATAGCATCGTCCACATATACAATGAATC
Reference: CCCTGGCAGTCTGCCCAAACTGCTAGCCCGATTATTTCGAAAGCTCACCCAGTGTCGGGGGATCGTTGAGGCAGCGCAACCAATTTGCAAATGTCGTAAAAACCTTAACACGGCTCTCCATAAGTGGTATGGTCCGGGTCCGGTATGCTGGACGGAGGCGGATGCACCCACGGTGGCACTACAACCCTGTTGGTTATATGGAGCTGATTAAAGGAGCGCACCATACAACCATCCCGACAGGGTCAGCCGCCTCG
Solution Aligned Query : ______________________________________________________________________________________________________________________________________________TTACTATTAATGCAAATGCACCTGATGGACGTATCGCAAATGCGTAACTCCCTCCGGTGTAACTAATAAAAGACTCTACGATAAAGGAACTACAGCGGGAACCGTCCGCTCTTTCGCGGATTCAGAGGCGCCACATGGGATCCAGGAAAAAGGTCAGGTAACGAGGAAAAATTTTGAAATTGGGAGATTCAGTTGTCTATGAAGCAAATGGCATAGCATCGTCCACATATACAATGAATC
Solution Aligned Reference: CCCTGGCAGTCTGCCCAAACTGCTAGCCCGATTATTTCGAAAGCTCACCCAGTGTCGGGGGATCGTTGAGGCAGCGCAACCAATTTGCAAATGTCGTAAAAACCTTAACACGGCTCTCCATAAGTGGTATGGTCCGGGTCCGGT___A__________TGC___TG___GACGGA_______G_GC__________GGATGCACCCA________C_____GGTG___GCACTACA______AC_C___C_TGT_TG____GTTATA_TGGAGC__T_G__ATTA_A___A__GGA__GCGCACCA_______T___ACA_________ACCAT________CCCGACA_GGGTCA_GC______CGCCT_______CG___
*/

#define MU 0.4
#define LAMBDA 0.4

#define MU 0.6321
#define LAMBDA 0.1353

int main(){
// std::string query_string = "AGTCTG"; // CCGTAGACCCGAACTTCGCGGTACACCTTCTGAAACCGTCCCTAATCCGACGAGCGCCTTGAGAACG";
// std::string reference_string = "TGCCGAT"; // TGAGAACGTAGTCTAGGCGAATCGGCCCTTGTATATCGGGGCCGTAGACCCGAACTTCGCGGTACAC";
std::string query_string = "CCGTAGACCCGAACTTCGCGGTACACCTTCTGAAACCGTCCCTAATCCGACGAGCGCCTTGAGAACG";
std::string reference_string = "TGAGAACGTAGTCTAGGCGAATCGGCCCTTGTATATCGGGGCCGTAGACCCGAACTTCGCGGTACAC";
char alphabet[4] = {'A', 'T', 'G', 'C'};
std::string query_string = Random::Sequence<4>(alphabet, INPUT_QUERY_LENGTH);
std::string reference_string = Random::Sequence<4>(alphabet, INPUT_REFERENCE_LENGTH);
// std::string query_string = Random::Sequence<4>(alphabet, INPUT_QUERY_LENGTH);
// std::string reference_string = Random::Sequence<4>(alphabet, INPUT_REFERENCE_LENGTH);

// float transition_[5][5] = {
// {0.8, 0.3, 0.3, 0.3, 0.2},
// {0.3, 0.8, 0.3, 0.3, 0.2},
// {0.3, 0.3, 0.8, 0.3, 0.2},
// {0.3, 0.3, 0.3, 0.8, 0.2},
// {0.2, 0.2, 0.2, 0.2, 0.2}
// };

float transition_[5][5] = {
{0.8, 0.3, 0.3, 0.3, 0.2},
{0.3, 0.8, 0.3, 0.3, 0.2},
{0.3, 0.3, 0.8, 0.3, 0.2},
{0.3, 0.3, 0.3, 0.8, 0.2},
{0.2, 0.2, 0.2, 0.2, 0.2}
{20.08, 0.1353, 0.1353, 0.1353, 0.3678},
{0.1353, 20.08, 0.1353, 0.1353, 0.3678},
{0.1353, 0.1353, 20.08, 0.1353, 0.3678},
{0.1353, 0.1353, 0.1353, 20.08, 0.3678},
{0.3678, 0.3678, 0.3678, 0.3678, 0.3678}
};

float log_transitions_[5][5];
Expand Down
Loading

0 comments on commit a523dc1

Please sign in to comment.