Commit 5df737e0 by chengshuyao

heuristic search the variable order

parent f6a8ddff
...@@ -10,7 +10,7 @@ public: ...@@ -10,7 +10,7 @@ public:
bool right_node_neg = 0; bool right_node_neg = 0;
int non_equal_number = 0; int non_equal_number = 0;
bool has_equal_father = 0; bool has_equal_father = 0;
bool which_root_node_all[1001] = {0}; bool which_root_node_all[111] = {0};
int which_root_node = 0; int which_root_node = 0;
int which_bit_output = 0; int which_bit_output = 0;
...@@ -121,7 +121,8 @@ public: ...@@ -121,7 +121,8 @@ public:
bool* left_mask_output_data_all ; bool* left_mask_output_data_all ;
bool* right_mask_output_data_all ; bool* right_mask_output_data_all ;
int partition_depth = 1000; int partition_depth = 10;
int partition_parts = 2;
int partition_into_how_many_parts =2; int partition_into_how_many_parts =2;
int* partition_start_node_numbers; int* partition_start_node_numbers;
int** partition_index; int** partition_index;
...@@ -132,12 +133,12 @@ public: ...@@ -132,12 +133,12 @@ public:
int BSD_samples_train_each_layer(); int BSD_samples_train_each_layer();
int BSD_samples_sort_each_layer(); int BSD_samples_sort_each_layer();
int BSD_variable_order[parameter_input_bit_width]; int** BSD_variable_order;
int BSD_variable_order_number; int BSD_variable_order_number;
int feature_area;
int* split_nodes_each_layer;
int split_nodes_each_layer[parameter_input_bit_width+1]; int* accuracy_each_layer;
int accuracy_each_layer[parameter_input_bit_width+1];
BDD_class(){ BDD_class(){
most_influence = new int [parameter_input_bit_width]; most_influence = new int [parameter_input_bit_width];
...@@ -159,7 +160,9 @@ public: ...@@ -159,7 +160,9 @@ public:
left_mask_output_data_all = new bool [long(parameter_max_BDD_width)*long(parameter_max_samples)]; left_mask_output_data_all = new bool [long(parameter_max_BDD_width)*long(parameter_max_samples)];
right_mask_output_data_all = new bool [long(parameter_max_BDD_width)*long(parameter_max_samples)]; right_mask_output_data_all = new bool [long(parameter_max_BDD_width)*long(parameter_max_samples)];
split_nodes_each_layer = new int [parameter_input_bit_width+1];
accuracy_each_layer = new int [parameter_input_bit_width+1];
BSD_variable_order = new int* [parameter_max_partition_parts];
}; };
...@@ -233,6 +236,8 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int ...@@ -233,6 +236,8 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int
int root_nodes_leafs[how_many_start_nodes]; int root_nodes_leafs[how_many_start_nodes];
int leaf_nodes_roots[how_many_start_nodes]; int leaf_nodes_roots[how_many_start_nodes];
feature_area=0;
for(i=0;i<start_depth+1;i++) for(i=0;i<start_depth+1;i++)
BDD[i] = new BDD_node[how_many_start_nodes]; BDD[i] = new BDD_node[how_many_start_nodes];
for (i=0;i<how_many_start_nodes;i++){ for (i=0;i<how_many_start_nodes;i++){
...@@ -271,10 +276,11 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int ...@@ -271,10 +276,11 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int
} }
total_nodes_amount_recursive = total_nodes_amount; total_nodes_amount_recursive = total_nodes_amount;
total_split_nodes_recursive = total_split_nodes; total_split_nodes_recursive = total_split_nodes;
bool this_layer_need_partition = (i==partition_depth)&&(BDD_width_each_layer[i]>100); feature_area += total_nodes_amount;
bool this_layer_need_partition = (i==partition_depth);
if(this_layer_need_partition){ if(this_layer_need_partition){
cout<<"go for partition"<<endl; cout<<"go for partition"<<endl;
partition_into_how_many_parts = max(2,int(BDD_width_each_layer[i]/100)); partition_into_how_many_parts = min(2,int(partition_parts));
BDD_partition = new BDD_class [partition_into_how_many_parts]; BDD_partition = new BDD_class [partition_into_how_many_parts];
partition_start_node_numbers = new int [partition_into_how_many_parts]; partition_start_node_numbers = new int [partition_into_how_many_parts];
partition_index = new int* [partition_into_how_many_parts]; partition_index = new int* [partition_into_how_many_parts];
...@@ -309,6 +315,7 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int ...@@ -309,6 +315,7 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int
for(int zj=0;zj<parameter_input_bit_width+1;zj++){ for(int zj=0;zj<parameter_input_bit_width+1;zj++){
BDD_partition[zi].has_been_unfold[zj] = has_been_unfold[zj]; BDD_partition[zi].has_been_unfold[zj] = has_been_unfold[zj];
BDD_partition[zi].most_influence[zj] = most_influence[zj]; BDD_partition[zi].most_influence[zj] = most_influence[zj];
BDD_partition[zi].BSD_variable_order[0][zj] = BSD_variable_order[zi+1][zj];
} }
for(int zj=0;zj<BDD_partition[zi].how_many_start_nodes;zj++){ for(int zj=0;zj<BDD_partition[zi].how_many_start_nodes;zj++){
if(partition_index[zi][zj] < BDD_width_each_layer[i]){ if(partition_index[zi][zj] < BDD_width_each_layer[i]){
...@@ -372,12 +379,20 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int ...@@ -372,12 +379,20 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int
delete [] simplify_list ; delete [] simplify_list ;
delete [] mask_input_data; delete [] mask_input_data;
delete [] split_nodes_each_layer;
delete [] accuracy_each_layer ;
for (int zj=0;zj<parameter_max_partition_parts;zj++){
delete [] BSD_variable_order[zj] ;
}
delete []BSD_variable_order ;
for(zi=0;zi<partition_into_how_many_parts;zi++){ for(zi=0;zi<partition_into_how_many_parts;zi++){
BDD_partition[zi].BDD_FULL_PROCESS(); BDD_partition[zi].BDD_FULL_PROCESS();
total_nodes_amount_recursive += BDD_partition[zi].total_nodes_amount_recursive; total_nodes_amount_recursive += BDD_partition[zi].total_nodes_amount_recursive;
total_split_nodes_recursive += BDD_partition[zi].total_split_nodes_recursive; total_split_nodes_recursive += BDD_partition[zi].total_split_nodes_recursive;
//} //}
} }
cout<<"BSD total split nodes recursive = " << total_split_nodes_recursive <<endl;
break; break;
} }
...@@ -480,7 +495,7 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int ...@@ -480,7 +495,7 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int
break; break;
} }
} }
if((i == (parameter_early_stop_depth-1))&&(which_BDD==0) || ((i == (parameter_early_stop_depth-1))&&(which_BDD==1)) || (total_split_nodes > parameter_early_stop_split_nodes)){ if((i == (parameter_early_stop_depth-1))&&(which_BDD==0) || ((i == (parameter_early_stop_depth-1))&&(which_BDD==1)) || ((total_split_nodes_recursive+feature_area/100) > parameter_early_stop_split_nodes)){
if(left_mask_output_data_sum > (BSD_samples/2)){ if(left_mask_output_data_sum > (BSD_samples/2)){
all_one_left = 1; all_one_left = 1;
all_zero_left = 0; all_zero_left = 0;
...@@ -708,9 +723,12 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int ...@@ -708,9 +723,12 @@ int BDD_class::train_BDD(BDD_node** BDD,int* most_influence,int start_depth, int
split_nodes_each_layer[i] = total_split_nodes; split_nodes_each_layer[i] = total_split_nodes;
accuracy_each_layer[i] = ((total_finish_weight)/pow(2.0,20))/double(parameter_output_bit_width); accuracy_each_layer[i] = ((total_finish_weight)/pow(2.0,20))/double(parameter_output_bit_width);
total_nodes_amount_recursive = total_nodes_amount;
total_split_nodes_recursive = total_split_nodes;
cout<<"BSD "<<BDD_id<<" nodes = "<<total_nodes_amount<<endl; cout<<"BSD "<<BDD_id<<" nodes = "<<total_nodes_amount<<endl;
cout<<"BSD "<<BDD_id<<" split nodes = "<<total_split_nodes; cout<<"BSD "<<BDD_id<<" split nodes = "<<total_split_nodes<<endl;
cout<<"BSD "<<BDD_id<<" feature area = "<<feature_area;
cout<<" Finish trained weight = "<<setprecision(12)<<(total_finish_weight)/pow(2.0,20)<<endl; cout<<" Finish trained weight = "<<setprecision(12)<<(total_finish_weight)/pow(2.0,20)<<endl;
gettimeofday(&finish_time,NULL); gettimeofday(&finish_time,NULL);
train_time = double(finish_time.tv_usec-start_time.tv_usec+1000000*(finish_time.tv_sec-start_time.tv_sec))/1000000; train_time = double(finish_time.tv_usec-start_time.tv_usec+1000000*(finish_time.tv_sec-start_time.tv_sec))/1000000;
...@@ -849,7 +867,6 @@ int BDD_class::BDD_FULL_PROCESS(){ ...@@ -849,7 +867,6 @@ int BDD_class::BDD_FULL_PROCESS(){
gettimeofday(&initial_start_time,NULL); gettimeofday(&initial_start_time,NULL);
gettimeofday(&start_time,NULL); gettimeofday(&start_time,NULL);
BDD_id = GLOBAL_BDD_id_number; BDD_id = GLOBAL_BDD_id_number;
GLOBAL_BDD_id_number += 1;
cout<<"switch_BDD: "<<BDD_id<<endl; cout<<"switch_BDD: "<<BDD_id<<endl;
cout<<"######################################################################"<<endl; cout<<"######################################################################"<<endl;
...@@ -867,12 +884,15 @@ int BDD_class::BDD_FULL_PROCESS(){ ...@@ -867,12 +884,15 @@ int BDD_class::BDD_FULL_PROCESS(){
for (j=0;j<parameter_max_samples;j++){ for (j=0;j<parameter_max_samples;j++){
mask_input_data[j] = new bool[parameter_input_bit_width*2]; mask_input_data[j] = new bool[parameter_input_bit_width*2];
} }
for (j=0;j<parameter_max_partition_parts;j++){
BSD_variable_order[j] = new int [parameter_input_bit_width];
}
for (int zi=0;zi<parameter_max_BDD_width;zi++){ for (int zi=0;zi<parameter_max_BDD_width;zi++){
simplify_list[zi] = new bool[parameter_max_samples]; simplify_list[zi] = new bool[parameter_max_samples];
} }
train_BDD(BDD,most_influence,start_depth,how_many_start_nodes,start_nodes); train_BDD(BDD,most_influence,start_depth,how_many_start_nodes,start_nodes);
BDD_infer(BDD,most_influence); BDD_infer(BDD,most_influence);
print_circuit(BDD,most_influence); //print_circuit(BDD,most_influence);
delete [] test_input_data; delete [] test_input_data;
//GLOBAL_BDD_nodes += total_nodes_amount; //GLOBAL_BDD_nodes += total_nodes_amount;
......
...@@ -28,82 +28,10 @@ bool* io_generator_alu_function_5(bool* input_data,bool* output_data); ...@@ -28,82 +28,10 @@ bool* io_generator_alu_function_5(bool* input_data,bool* output_data);
bool* io_generator_cpu_function_1(bool* input_data,bool* output_data);//add_shift bool* io_generator_cpu_function_1(bool* input_data,bool* output_data);//add_shift
#include"io_generator/cpp_comb/parallel_serial_16.h" //#include"io_generator/cpp_comb/parallel_serial_16.h"
//#include"patch.h"
bool* io_generator_function(bool input_data[parameter_input_bit_width],int which_demo_function, bool* output_data) {
//bool* output_data = new bool [parameter_input_bit_width*2];
int i,j;
for(i=0;i<parameter_output_bit_width;i++){
output_data[i] = 0;
}
///if(which_demo_function!=9999){
/// switch(which_demo_function){
/// case 0: io_generator_single_function_0 (input_data,output_data);break;
/// case 1: io_generator_single_function_1 (input_data,output_data);break;
/// case 2: io_generator_single_function_2 (input_data,output_data);break;
/// case 3: io_generator_single_function_3 (input_data,output_data);break;
/// case 4: io_generator_single_function_4 (input_data,output_data);break;
/// case 5: io_generator_single_function_5 (input_data,output_data);break;
/// case 6: io_generator_single_function_6 (input_data,output_data);break;
/// case 7: io_generator_single_function_7 (input_data,output_data);break;
/// case 8: io_generator_single_function_8 (input_data,output_data);break;
/// case 9: io_generator_single_function_9 (input_data,output_data);break;
/// case 10: io_generator_single_function_10(input_data,output_data);break;
/// case 11: io_generator_single_function_11(input_data,output_data);break;
/// case 12: io_generator_single_function_12(input_data,output_data);break;
/// case 13: io_generator_single_function_13(input_data,output_data);break;
/// case 14: io_generator_single_function_14(input_data,output_data);break;
/// case 15: io_generator_single_function_15(input_data,output_data);break;
/// case 16: io_generator_alu_function_1 (input_data,output_data);break;
/// case 17: io_generator_alu_function_2 (input_data,output_data);break;
/// case 18: io_generator_alu_function_3 (input_data,output_data);break;
/// case 19: io_generator_alu_function_4 (input_data,output_data);break;
/// case 20: io_generator_alu_function_5 (input_data,output_data);break;
/// case 21: io_generator_single_function_mul(input_data,output_data);break;
/// case 22: io_generator_single_function_fp32_add(input_data,output_data);break;
/// }
///}
///else{
/// io_generator_outer(input_data,output_data);
/// //long data = cvt_bit_to_number_unsigned(input_data,parameter_input_bit_width);
/// //for(i=0;i<parameter_output_bit_width;i++){
/// // output_data[i] = truth_table[data];
/// //}
///}
io_generator_outer(input_data,output_data);
//long data = cvt_bit_to_number_unsigned(input_data,parameter_input_bit_width);
// for(i=0;i<parameter_output_bit_width;i++){
// output_data[i] = truth_table[data];
// }
//bool* reg_data_a = new bool [parameter_CPU_data_width];
//bool* reg_data_b = new bool [parameter_CPU_data_width];
//
//bool* op_code = new bool[5];
//bool* reg_data_a_index = new bool[5];
//bool* reg_data_b_index = new bool[5];
//int number_reg_data_a_index;
//int number_reg_data_b_index;
//int number_op_code;
//int i,j,zi,zj;
//for(i=0;i<5;i++){
// reg_data_a_index[i] = input_data[128+i];
// reg_data_b_index[i] = input_data[133+i];
// op_code[i] = input_data[138+i];
//}
//number_reg_data_a_index = cvt_bit_to_number(reg_data_a_index,5)%4;
//number_reg_data_b_index = cvt_bit_to_number(reg_data_b_index,5)%4;
//number_op_code = cvt_bit_to_number(op_code,5);
//for(i=0;i<parameter_CPU_data_width;i++){
// reg_data_a[i] = input_data[32*number_reg_data_a_index+i];
// reg_data_b[i] = input_data[32*number_reg_data_b_index+i];
//}
return output_data;
}
bool* io_generator_single_function_0(bool* input_data,bool* output_data){//== bool* io_generator_single_function_0(bool* input_data,bool* output_data){//==
int i,j; int i,j;
......
...@@ -117,14 +117,14 @@ int BDD_class::next_bit_layer_single(int depth,int which_node_this_layer){ ...@@ -117,14 +117,14 @@ int BDD_class::next_bit_layer_single(int depth,int which_node_this_layer){
int BDD_class::next_bit_layer(int depth){ int BDD_class::next_bit_layer(int depth){
cout<<depth<<" "<<BSD_variable_order_number<<endl; cout<<depth<<" "<<BSD_variable_order_number<<endl;
if(depth < BSD_variable_order_number){ if((depth < BSD_variable_order_number) ){
most_influence[depth] = BSD_variable_order[depth]; most_influence[depth] = BSD_variable_order[0][depth];
has_been_unfold[most_influence[depth]]=1; has_been_unfold[most_influence[depth]]=1;
cout<<most_influence[depth]<<endl; cout<<most_influence[depth]<<endl;
return BSD_variable_order[depth]; return BSD_variable_order[0][depth];
} }
else else
return next_bit_layer_0(depth); return next_bit_layer_1(depth);
} }
int BDD_class::next_bit_layer_1(int depth){ int BDD_class::next_bit_layer_1(int depth){
...@@ -357,7 +357,7 @@ int BDD_class::next_bit_layer_0(int depth){ ...@@ -357,7 +357,7 @@ int BDD_class::next_bit_layer_0(int depth){
} }
} }
int BSD_samples_influence = BSD_samples_influence_max; int BSD_samples_influence = BSD_samples_influence_max;
for (zz=0;zz<100;zz++){ for (zz=0;zz<10;zz++){
if(depth < parameter_multi_output_index){ if(depth < parameter_multi_output_index){
break; break;
} }
...@@ -465,12 +465,12 @@ int BDD_class::next_bit_layer_0(int depth){ ...@@ -465,12 +465,12 @@ int BDD_class::next_bit_layer_0(int depth){
double amount_turn_max_divide_average = amount_turn_static[most_influence_next]/amount_turn_average; double amount_turn_max_divide_average = amount_turn_static[most_influence_next]/amount_turn_average;
double amount_turn_max_ratio = amount_turn[most_influence_next]/(zz+1); double amount_turn_max_ratio = amount_turn[most_influence_next]/(zz+1);
//USE_THIS:BEST //USE_THIS:BEST
//if((depth<parameter_input_bit_width-2) ){ if((depth<parameter_input_bit_width-2) ){
// if((amount_turn_max_divide_average < 4) || (amount_turn_max_ratio < 0.3) || ((amount_turn_max_ratio>0.8)&&((amount_turn_max_ratio<0.999)))){ if((amount_turn_max_divide_average < 4) || (amount_turn_max_ratio < 0.3) || ((amount_turn_max_ratio>0.8)&&((amount_turn_max_ratio<0.999)))){
// has_been_unfold[most_influence_next] = 0; has_been_unfold[most_influence_next] = 0;
// most_influence_next = 999999; most_influence_next = 999999;
// } }
//} }
//delete[]amount_turn; //delete[]amount_turn;
return most_influence_next; return most_influence_next;
}; };
src/ src/
The latest version of the BSD Learner. Still working for the first released version on Github. The latest version of the BSD Learner.
BSD.cpp main code of our algorithm 1. Use BSD_Learner to design your own circuit.
cvt.h head and some tool functions
print_circuit.h turn the graph into verilog
1. When we want more accurate/complex circuit: Edit top.h:
BSD.cpp line 20: This parameter "bit_width" means total input bit amounts. Line 1: modify input bitwidth;
line 21: If accuracy is not enough, get this parameter larger. In CPU tapeout task, it is set from 200k to 1M. Line 2: modify output bitwidth;
line 24: This paramter means how many root nodes of one BSD. Line 3: include your own io_generator file.
line 29: If BSD is not efficient enough in merging nodes together, get this parameter larger may help.
Note that this parameter should not be larger than line 21
line 32: How many samples are there in the test set. Should be larger if we want more accurate circuits.
line 41: The biggest BDD width. Could be smaller in the demo case, but should be large if we are facing more complex circuit.
2. When we are doing ablation study/tradeoff:
BSD.cpp line 22: Set to a larger number than "bit_width", this program produces a decision tree.
line 31: A tradeoff between time/area, set to 1 (default) is the slowest but most area effient. Set to other N+ is quicker.
line 34: Until XX depth to stop expansion. It is a time/accuracy tradeoff. Default is the most accurate.
extern const int parameter_input_bit_width = 33+6; //输入bit有几位,写在+6前面。+6是为了防止一些bug的冗余设计。
extern const int parameter_output_bit_width = 1 ; //输出bit有几位
#include"io_generator/c1908.h"
bool* io_generator_function(bool input_data[parameter_input_bit_width],int which_demo_function, bool* output_data) {
int i,j;
for(i=0;i<parameter_output_bit_width;i++){
output_data[i] = 0;
}
io_generator_outer(input_data,output_data);
output_data[0] = output_data[12];
return output_data;
}
...@@ -2,5 +2,5 @@ export PATH=/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/bin:/bin:/lustre ...@@ -2,5 +2,5 @@ export PATH=/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/bin:/bin:/lustre
export LIBRARY_PATH=/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/bin:/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/lib export LIBRARY_PATH=/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/bin:/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/lib
export LD_LIBRARY_PATH=/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/bin:/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/lib export LD_LIBRARY_PATH=/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/bin:/lustre/S/chengshuyao/haoshiming/anaconda3/envs/BSD/lib
rm rtl/* rm rtl/*
g++ BSD.cpp -O3 -std=c++11 -fopenmp -pg g++ BSD.cpp -O0 -std=c++11 -fopenmp -pg
./a.out ./a.out
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment