Skip to content

Commit e1014b2

Browse files
committed
Removed trailing whitespace and 1 integer bit
1 parent 631332c commit e1014b2

File tree

208 files changed

+34778
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

208 files changed

+34778
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#ifndef KL_LAYER_H_
2+
#define KL_LAYER_H_
3+
4+
#include "nnet_activation.h"
5+
#include "nnet_common.h"
6+
#include <cmath>
7+
#include <cstdlib>
8+
9+
namespace nnet {
10+
11+
struct distance_config {
12+
// IO size
13+
static const unsigned n_in = 10;
14+
static const unsigned n_out = 1;
15+
16+
// Internal data type definitions
17+
typedef float accum_t;
18+
typedef float sum_t;
19+
typedef ap_fixed<18, 8> exp_table_t;
20+
21+
// Internal info
22+
static const unsigned table_size = 1024;
23+
static constexpr unsigned exp_range = 8;
24+
};
25+
26+
template <typename CONFIG_T, int N_TABLE> void init_klloss_exp_table(typename CONFIG_T::exp_table_t table_out[N_TABLE]) {
27+
for (int ii = 0; ii < N_TABLE; ii++) {
28+
// First, convert from table index to X-value (range -1 to +1)
29+
float in_val = 2 * CONFIG_T::exp_range * (ii - float(N_TABLE) / 2.0) / float(N_TABLE);
30+
// Next, compute lookup table function
31+
typename CONFIG_T::exp_table_t real_val = exp_fcn_float(in_val);
32+
// std::cout << "Lookup table In Value: " << in_val << " Result: " << real_val << " Index: " << ii << std::endl;
33+
table_out[ii] = real_val;
34+
}
35+
}
36+
template <class data1_T, class data2_T, class res_T, typename CONFIG_T>
37+
void klloss(data1_T mean[CONFIG_T::n_in], data2_T log_var[CONFIG_T::n_in], res_T res[CONFIG_T::n_out]) {
38+
//#pragma HLS PIPELINE
39+
// Initialize the lookup tables
40+
#ifdef __HLS_SYN__
41+
bool initialized = false;
42+
typename CONFIG_T::exp_table_t exp_table[CONFIG_T::table_size];
43+
#else
44+
static bool initialized = false;
45+
static typename CONFIG_T::exp_table_t exp_table[CONFIG_T::table_size];
46+
#endif
47+
if (!initialized) {
48+
init_klloss_exp_table<CONFIG_T, CONFIG_T::table_size>(exp_table);
49+
initialized = true;
50+
}
51+
typename CONFIG_T::accum_t kl[CONFIG_T::n_in];
52+
//#pragma HLS ARRAY_PARTITION variable=kl complete
53+
typename CONFIG_T::accum_t mean_sq[CONFIG_T::n_in];
54+
//#pragma HLS ARRAY_PARTITION variable=mean_sq complete
55+
typename CONFIG_T::accum_t kl_sum(0);
56+
for (unsigned i = 0; i < CONFIG_T::n_in; i++) {
57+
//#pragma HLS UNROLL
58+
mean_sq[i] = mean[i] * mean[i];
59+
kl[i] = data2_T(1.) + log_var[i];
60+
// std::cout << "Log var: " << log_var[i] << " Result: " << kl[i] << std::endl;
61+
}
62+
constexpr unsigned table_scale = (unsigned)(CONFIG_T::table_size / (2 * CONFIG_T::exp_range));
63+
constexpr unsigned index_scale = (unsigned)(CONFIG_T::exp_range * table_scale);
64+
for (unsigned i = 0; i < CONFIG_T::n_in; i++) {
65+
//#pragma HLS UNROLL
66+
auto data_round = log_var[i] * table_scale;
67+
auto index = data_round + index_scale;
68+
if (index < 0)
69+
index = 0;
70+
if (index > CONFIG_T::table_size - 1)
71+
index = CONFIG_T::table_size - 1;
72+
kl[i] -= exp_table[index];
73+
// std::cout << "Exp var: " << exp_table[index] << " Result: " << kl[i] << " Index: " << index << std::endl;
74+
}
75+
for (unsigned i = 0; i < CONFIG_T::n_in; i++) {
76+
//#pragma HLS UNROLL
77+
kl[i] -= mean_sq[i];
78+
}
79+
Op_add<typename CONFIG_T::accum_t> op_add;
80+
kl_sum = reduce<typename CONFIG_T::accum_t, CONFIG_T::n_in, Op_add<typename CONFIG_T::accum_t>>(kl, op_add);
81+
// std::cout << "KL sum: " << kl_sum << std::endl;
82+
kl_sum *= typename CONFIG_T::accum_t(1. / CONFIG_T::n_in);
83+
res[0] = res_T(-0.5) * kl_sum;
84+
}
85+
} // namespace nnet
86+
87+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef DEFINES_H_
2+
#define DEFINES_H_
3+
4+
#include "nnet_utils/nnet_types.h"
5+
#include <ac_channel.h>
6+
#include <ac_fixed.h>
7+
#include <ac_int.h>
8+
#include <cstddef>
9+
#include <cstdio>
10+
11+
// hls-fpga-machine-learning insert numbers
12+
13+
// hls-fpga-machine-learning insert layer-precision
14+
15+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
3+
#include "myproject.h"
4+
#include "parameters.h"
5+
6+
#include <mc_scverify.h>
7+
8+
//#pragma hls_design top
9+
// hls-fpga-machine-learning insert IFSynPragmas
10+
void CCS_BLOCK(myproject)(
11+
// hls-fpga-machine-learning insert header
12+
) {
13+
14+
// hls-fpga-machine-learning insert IO
15+
16+
#ifndef __SYNTHESIS__
17+
static bool loaded_weights = false;
18+
if (!loaded_weights) {
19+
// hls-fpga-machine-learning insert load weights
20+
loaded_weights = true;
21+
}
22+
#endif
23+
24+
// ****************************************
25+
// NETWORK INSTANTIATION
26+
// ****************************************
27+
28+
// hls-fpga-machine-learning insert layers
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef MYPROJECT_H_
2+
#define MYPROJECT_H_
3+
4+
#include <ac_channel.h>
5+
#include <ac_fixed.h>
6+
#include <ac_int.h>
7+
8+
#include "defines.h"
9+
10+
// Prototype of top level function for C-synthesis
11+
void myproject(
12+
// hls-fpga-machine-learning insert header
13+
);
14+
15+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef PARAMETERS_H_
2+
#define PARAMETERS_H_
3+
4+
#include <ac_fixed.h>
5+
#include <ac_int.h>
6+
7+
#include "nnet_utils/nnet_code_gen.h"
8+
#include "nnet_utils/nnet_helpers.h"
9+
// hls-fpga-machine-learning insert includes
10+
11+
// hls-fpga-machine-learning insert weights
12+
13+
// hls-fpga-machine-learning insert layer-config
14+
15+
#endif
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef MYPROJECT_BRIDGE_H_
2+
#define MYPROJECT_BRIDGE_H_
3+
4+
#include "firmware/myproject.h"
5+
#include "nnet_helpers.h"
6+
#include <algorithm>
7+
#include <map>
8+
9+
// hls-fpga-machine-learning insert weights dir
10+
11+
const char *get_weights_dir() { return s_weights_dir.c_str(); }
12+
13+
// hls-fpga-machine-learning insert bram
14+
15+
// hls-fpga-machine-learning insert declare weights
16+
17+
namespace nnet {
18+
bool trace_enabled = false;
19+
std::map<std::string, void *> *trace_outputs = NULL;
20+
size_t trace_type_size = sizeof(double);
21+
} // namespace nnet
22+
23+
extern "C" {
24+
25+
struct trace_data {
26+
const char *name;
27+
void *data;
28+
};
29+
30+
void allocate_trace_storage(size_t element_size) {
31+
nnet::trace_enabled = true;
32+
nnet::trace_outputs = new std::map<std::string, void *>;
33+
nnet::trace_type_size = element_size;
34+
// hls-fpga-machine-learning insert trace_outputs
35+
}
36+
37+
void free_trace_storage() {
38+
for (std::map<std::string, void *>::iterator i = nnet::trace_outputs->begin(); i != nnet::trace_outputs->end(); i++) {
39+
void *ptr = i->second;
40+
free(ptr);
41+
}
42+
nnet::trace_outputs->clear();
43+
delete nnet::trace_outputs;
44+
nnet::trace_outputs = NULL;
45+
nnet::trace_enabled = false;
46+
}
47+
48+
void collect_trace_output(struct trace_data *c_trace_outputs) {
49+
int ii = 0;
50+
for (std::map<std::string, void *>::iterator i = nnet::trace_outputs->begin(); i != nnet::trace_outputs->end(); i++) {
51+
c_trace_outputs[ii].name = i->first.c_str();
52+
c_trace_outputs[ii].data = i->second;
53+
ii++;
54+
}
55+
}
56+
57+
// Wrapper of top level function for Python bridge
58+
void myproject_float(
59+
// hls-fpga-machine-learning insert header #float
60+
) {
61+
62+
// hls-fpga-machine-learning insert wrapper #float
63+
}
64+
65+
void myproject_double(
66+
// hls-fpga-machine-learning insert header #double
67+
) {
68+
// hls-fpga-machine-learning insert wrapper #double
69+
}
70+
}
71+
72+
#endif

0 commit comments

Comments
 (0)