CMS 3D CMS Logo

nnet_dense.h
Go to the documentation of this file.
1 #ifndef NNET_DENSE_H_
2 #define NNET_DENSE_H_
3 
4 #include "nnet_common.h"
5 #include "nnet_mult.h"
6 #include <math.h>
7 
8 namespace nnet {
9 
10  struct dense_config {
11  // Internal data type definitions
12  typedef float bias_t;
13  typedef float weight_t;
14  typedef float accum_t;
15 
16  // Layer Sizes
17  static const unsigned n_in = 10;
18  static const unsigned n_out = 10;
19 
20  // Resource reuse info
23  int reuse_factor = 1;
24  static const bool store_weights_in_bram = false;
25  int n_zeros = 0;
26  // partitioning arrays cyclically to go with roll factors?
27  // Product function to use
28  template <class x_T, class y_T>
30  };
31 
32  template <class data_T, class res_T, typename CONFIG_T>
33  void dense(data_T data[CONFIG_T::n_in],
34  res_T res[CONFIG_T::n_out],
35  typename CONFIG_T::weight_t weights[CONFIG_T::n_in * CONFIG_T::n_out],
36  typename CONFIG_T::bias_t biases[CONFIG_T::n_out]) {
37  data_T cache;
38  typename CONFIG_T::accum_t mult[CONFIG_T::n_in * CONFIG_T::n_out];
39  typename CONFIG_T::accum_t acc[CONFIG_T::n_out];
40 
41  // Do the matrix-multiply
42  for (unsigned ii = 0; ii < CONFIG_T::n_in; ii++) {
43  cache = data[ii];
44  for (unsigned jj = 0; jj < CONFIG_T::n_out; jj++) {
45  unsigned index = ii * CONFIG_T::n_out + jj;
46  mult[index] = CONFIG_T::template product<data_T, typename CONFIG_T::weight_t>::product(cache, weights[index]);
47  }
48  }
49 
50  // Initialize accumulator with input biases
51  for (unsigned iacc = 0; iacc < CONFIG_T::n_out; iacc++) {
52  acc[iacc] = (typename CONFIG_T::accum_t)biases[iacc];
53  }
54 
55  // Accumulate multiplication result
56  for (unsigned ii = 0; ii < CONFIG_T::n_in; ii++) {
57  for (unsigned jj = 0; jj < CONFIG_T::n_out; jj++) {
58  unsigned index = ii * CONFIG_T::n_out + jj;
59  acc[jj] += mult[index];
60  }
61  }
62 
63  // Cast to "res_t" type
64  for (unsigned ires = 0; ires < CONFIG_T::n_out; ires++) {
65  // res[ires] = (res_T) (acc[ires]);
66  res[ires] = cast<data_T, res_T, CONFIG_T>(acc[ires]);
67  }
68  }
69 
70 } // namespace nnet
71 
72 #endif
static const bool store_weights_in_bram
Definition: nnet_dense.h:24
void dense(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_out], typename CONFIG_T::weight_t weights[CONFIG_T::n_in *CONFIG_T::n_out], typename CONFIG_T::bias_t biases[CONFIG_T::n_out])
Definition: nnet_dense.h:33
static const unsigned n_out
Definition: nnet_dense.h:18
Definition: Electron.h:6
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
ii
Definition: cuy.py:589
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
def cache(function)
Definition: utilities.py:3
static const unsigned n_in
Definition: nnet_dense.h:17
strategy
Definition: nnet_common.h:18