CMS 3D CMS Logo

Classes | Enumerations | Functions
nnet Namespace Reference

Classes

struct  activ_config
 
struct  layer_config
 

Enumerations

enum  io_type { io_parallel = 0, io_serial }
 

Functions

template<class data_T , class res_T , typename CONFIG_T >
void compute_layer (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])
 
float exp_fcn_float (float input)
 
template<typename CONFIG_T , int N_TABLE>
void init_exp_table (typename CONFIG_T::table_t table_out[N_TABLE])
 
template<typename CONFIG_T , int N_TABLE>
void init_invert_table (typename CONFIG_T::table_t table_out[N_TABLE])
 
template<class res_T , typename CONFIG_T , int N_TABLE>
void init_sigmoid_table (res_T table_out[N_TABLE])
 
template<typename CONFIG_T , int N_TABLE>
void init_tanh_table (typename CONFIG_T::table_t table_out[N_TABLE])
 
template<class data_T , class res_T , typename CONFIG_T >
void linear (data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
 
template<class data_T , class res_T , typename CONFIG_T >
void relu (data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
 
template<class data_T , class res_T , typename CONFIG_T >
void relu6 (data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
 
template<class data_T , class res_T , int MAX_INT, typename CONFIG_T >
void relu_max (data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
 
template<class data_T , class res_T , typename CONFIG_T >
void sigmoid (data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
 
template<class out_T >
out_T sigmoid_fcn_float (float input)
 
template<class data_T , class res_T , typename CONFIG_T >
void softmax (data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
 
template<class data_T , class res_T , typename CONFIG_T >
void tanh (data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
 

Enumeration Type Documentation

◆ io_type

Enumerator
io_parallel 
io_serial 

Definition at line 28 of file nnet_common.h.

Function Documentation

◆ compute_layer()

template<class data_T , class res_T , typename CONFIG_T >
void nnet::compute_layer ( 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 at line 50 of file nnet_layer.h.

References utilities::cache(), data, DIV_ROUNDUP, cuy::ii, findQualityFiles::jj, VarParsing::mult, and hltDeepSecondaryVertexTagInfosPFPuppi_cfi::weights.

53  {
54  unsigned cycle_factor = DIV_ROUNDUP(CONFIG_T::n_in * CONFIG_T::n_out, CONFIG_T::reuse_factor);
55  typename CONFIG_T::weight_t mult[CONFIG_T::n_in * CONFIG_T::n_out];
56  /*
57  if(CONFIG_T::use_lowlatency) {
58  int multiplier_limit = ceil(float(CONFIG_T::n_in*CONFIG_T::n_out) / float(CONFIG_T::reuse_factor)) - floor(float(CONFIG_T::n_zeros) / float(CONFIG_T::reuse_factor));
59  }
60  */
61  typename CONFIG_T::accum_t acc[CONFIG_T::n_out];
62  for (unsigned iacc = 0; iacc < CONFIG_T::n_out; iacc++) {
63  acc[iacc] = (typename CONFIG_T::accum_t)biases[iacc];
64  }
65  unsigned rufactor = CONFIG_T::reuse_factor;
66  if (CONFIG_T::use_lowlatency) {
67  rufactor = CONFIG_T::n_in;
68  cycle_factor = CONFIG_T::n_out;
69  }
70  data_T cache;
71  for (unsigned ii = 0; ii < rufactor; ii++) {
72  if (CONFIG_T::use_lowlatency) {
73  cache = data[ii];
74  }
75  for (unsigned jj = 0; jj < cycle_factor; jj++) {
76  unsigned windex = ii * cycle_factor + jj;
77  unsigned index = windex / CONFIG_T::n_out;
78  if (windex > CONFIG_T::n_in * CONFIG_T::n_out - 1)
79  continue;
80  if (CONFIG_T::use_lowlatency) {
81  mult[windex] = cache * (weights[windex]);
82  } else {
83  int aindex = windex / CONFIG_T::n_in;
84  acc[aindex] += data[index] * weights[windex];
85  }
86  }
87  }
88  if (CONFIG_T::use_lowlatency) {
89  // Accumulate multiplication result
90  for (unsigned ii = 0; ii < CONFIG_T::n_in; ii++) {
91  for (unsigned jj = 0; jj < CONFIG_T::n_out; jj++) {
92  int index = ii * CONFIG_T::n_out + jj;
93  acc[jj] += mult[index];
94  }
95  }
96  }
97  for (unsigned ires = 0; ires < CONFIG_T::n_out; ires++) {
98  res[ires] = (res_T)(acc[ires]);
99  }
100  }
Definition: Electron.h:6
#define DIV_ROUNDUP(n, d)
Definition: nnet_layer.h:47
ii
Definition: cuy.py:589
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
def cache(function)
Definition: utilities.py:3

◆ exp_fcn_float()

float nnet::exp_fcn_float ( float  input)
inline

Definition at line 134 of file nnet_activation.h.

References JetChargeProducer_cfi::exp, and input.

Referenced by init_exp_table().

134 { return exp(input); }
static std::string const input
Definition: EdmProvDump.cc:50

◆ init_exp_table()

template<typename CONFIG_T , int N_TABLE>
void nnet::init_exp_table ( typename CONFIG_T::table_t  table_out[N_TABLE])

Definition at line 137 of file nnet_activation.h.

References exp_fcn_float(), dqmMemoryStats::float, cuy::ii, and P2L1HTMHTEmu::N_TABLE.

137  {
138  for (unsigned ii = 0; ii < N_TABLE; ii++) {
139  // First, convert from table index to X-value (signed 8-bit, range -8 to +8)
140  float in_val = 2 * 8.0 * (ii - float(N_TABLE) / 2.0) / float(N_TABLE);
141  // Next, compute lookup table function
142  typename CONFIG_T::table_t real_val = exp_fcn_float(in_val);
143  //std::cout << "Lookup table In Value: " << in_val << " Result: " << real_val << std::endl;
144  table_out[ii] = real_val;
145  }
146  }
ii
Definition: cuy.py:589
static constexpr int N_TABLE
float exp_fcn_float(float input)

◆ init_invert_table()

template<typename CONFIG_T , int N_TABLE>
void nnet::init_invert_table ( typename CONFIG_T::table_t  table_out[N_TABLE])

Definition at line 149 of file nnet_activation.h.

References dqmMemoryStats::float, cuy::ii, and P2L1HTMHTEmu::N_TABLE.

149  {
150  // Inversion function:
151  // result = 1/x
152  for (unsigned ii = 0; ii < N_TABLE; ii++) {
153  // First, convert from table index to X-value (signed 8-bit, range 0 to +64)
154  float in_val = 64.0 * ii / float(N_TABLE);
155  // Next, compute lookup table function
156  if (in_val > 0.0)
157  table_out[ii] = 1.0 / in_val;
158  else
159  table_out[ii] = 0.0;
160  }
161  }
ii
Definition: cuy.py:589
static constexpr int N_TABLE

◆ init_sigmoid_table()

template<class res_T , typename CONFIG_T , int N_TABLE>
void nnet::init_sigmoid_table ( res_T  table_out[N_TABLE])

Definition at line 98 of file nnet_activation.h.

References dqmMemoryStats::float, cuy::ii, and P2L1HTMHTEmu::N_TABLE.

98  {
99  // Default logistic sigmoid function:
100  // result = 1/(1+e^(-x))
101  for (unsigned ii = 0; ii < N_TABLE; ii++) {
102  // First, convert from table index to X-value (signed 8-bit, range -8 to +8)
103  float in_val = 2 * 8.0 * (ii - float(N_TABLE) / 2.0) / float(N_TABLE);
104  // Next, compute lookup table function
105  res_T real_val = sigmoid_fcn_float<res_T>(in_val);
106  //std::cout << "Lookup table In Value: " << in_val << " Result: " << real_val << std::endl;
107  table_out[ii] = (res_T)real_val;
108  }
109  }
ii
Definition: cuy.py:589
static constexpr int N_TABLE

◆ init_tanh_table()

template<typename CONFIG_T , int N_TABLE>
void nnet::init_tanh_table ( typename CONFIG_T::table_t  table_out[N_TABLE])

Definition at line 214 of file nnet_activation.h.

References dqmMemoryStats::float, cuy::ii, P2L1HTMHTEmu::N_TABLE, and tanh().

214  {
215  // Implement tanh lookup
216  for (unsigned ii = 0; ii < N_TABLE; ii++) {
217  // First, convert from table index to X-value (signed 8-bit, range -4 to +4)
218  float in_val = 2 * 4.0 * (ii - float(N_TABLE) / 2.0) / float(N_TABLE);
219  // Next, compute lookup table function
220  typename CONFIG_T::table_t real_val = tanh(in_val);
221  //std::cout << "Tanh: Lookup table Index: " << ii<< " In Value: " << in_val << " Result: " << real_val << std::endl;
222  table_out[ii] = real_val;
223  }
224  }
void tanh(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
ii
Definition: cuy.py:589
static constexpr int N_TABLE

◆ linear()

template<class data_T , class res_T , typename CONFIG_T >
void nnet::linear ( data_T  data[CONFIG_T::n_in],
res_T  res[CONFIG_T::n_in] 
)

Definition at line 49 of file nnet_activation.h.

References data, and cuy::ii.

49  {
50  for (unsigned ii = 0; ii < CONFIG_T::n_in; ii++) {
51  res[ii] = data[ii];
52  }
53  }
Definition: Electron.h:6
ii
Definition: cuy.py:589
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79

◆ relu()

template<class data_T , class res_T , typename CONFIG_T >
void nnet::relu ( data_T  data[CONFIG_T::n_in],
res_T  res[CONFIG_T::n_in] 
)

Definition at line 59 of file nnet_activation.h.

References data, and cuy::ii.

59  {
60  data_T datareg;
61  for (unsigned ii = 0; ii < CONFIG_T::n_in; ii++) {
62  datareg = data[ii];
63  if (datareg > 0)
64  res[ii] = datareg;
65  else
66  res[ii] = 0;
67  }
68  }
Definition: Electron.h:6
ii
Definition: cuy.py:589
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79

◆ relu6()

template<class data_T , class res_T , typename CONFIG_T >
void nnet::relu6 ( data_T  data[CONFIG_T::n_in],
res_T  res[CONFIG_T::n_in] 
)

Definition at line 85 of file nnet_activation.h.

References data.

85  {
86  relu_max<data_T, res_T, 6, CONFIG_T>(data, res);
87  }
Definition: Electron.h:6
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79

◆ relu_max()

template<class data_T , class res_T , int MAX_INT, typename CONFIG_T >
void nnet::relu_max ( data_T  data[CONFIG_T::n_in],
res_T  res[CONFIG_T::n_in] 
)

Definition at line 71 of file nnet_activation.h.

References data, and cuy::ii.

71  {
72  data_T datareg;
73  for (unsigned ii = 0; ii < CONFIG_T::n_in; ii++) {
74  datareg = data[ii];
75  if (datareg < 0)
76  res[ii] = 0;
77  else if (datareg > MAX_INT)
78  res[ii] = MAX_INT;
79  else
80  res[ii] = datareg;
81  }
82  }
Definition: Electron.h:6
ii
Definition: cuy.py:589
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79

◆ sigmoid()

template<class data_T , class res_T , typename CONFIG_T >
void nnet::sigmoid ( data_T  data[CONFIG_T::n_in],
res_T  res[CONFIG_T::n_in] 
)

Definition at line 112 of file nnet_activation.h.

References data, and cuy::ii.

112  {
113  // Initialize the lookup table
114  res_T sigmoid_table[CONFIG_T::table_size];
115  init_sigmoid_table<res_T, CONFIG_T, CONFIG_T::table_size>(sigmoid_table);
116 
117  // Index into the lookup table based on data
118  int data_round;
119  unsigned index;
120  for (unsigned ii = 0; ii < CONFIG_T::n_in; ii++) {
121  data_round = data[ii] * CONFIG_T::table_size / 16;
122  index = data_round + 8 * CONFIG_T::table_size / 16;
123  /*if (index < 0)
124  index = 0;*/
125  if (index > CONFIG_T::table_size - 1)
126  index = CONFIG_T::table_size - 1;
127  res[ii] = (res_T)sigmoid_table[index];
128  }
129  }
Definition: Electron.h:6
ii
Definition: cuy.py:589
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79

◆ sigmoid_fcn_float()

template<class out_T >
out_T nnet::sigmoid_fcn_float ( float  input)
inline

Definition at line 93 of file nnet_activation.h.

References JetChargeProducer_cfi::exp, and input.

93  {
94  return 1.0 / (1 + exp(-input));
95  }
static std::string const input
Definition: EdmProvDump.cc:50

◆ softmax()

template<class data_T , class res_T , typename CONFIG_T >
void nnet::softmax ( data_T  data[CONFIG_T::n_in],
res_T  res[CONFIG_T::n_in] 
)

Definition at line 164 of file nnet_activation.h.

References data, cuy::ii, and findQualityFiles::jj.

164  {
165  // Initialize the lookup table
166  typename CONFIG_T::table_t exp_table[CONFIG_T::table_size];
167  init_exp_table<CONFIG_T, CONFIG_T::table_size>(exp_table);
168 
169  typename CONFIG_T::table_t invert_table[CONFIG_T::table_size];
170  init_invert_table<CONFIG_T, CONFIG_T::table_size>(invert_table);
171 
172  // Index into the lookup table based on data for exponentials
173  typename CONFIG_T::table_t exp_res[CONFIG_T::n_in]; // different, independent, fixed point precision
174  typename CONFIG_T::table_t exp_diff_res[CONFIG_T::n_in]
175  [CONFIG_T::n_in]; // different, independent, fixed point precision
176  int data_round;
177  int index;
178  for (int ii = 0; ii < CONFIG_T::n_in; ii++) {
179  exp_res[ii] = 0;
180  }
181  for (int ii = 0; ii < CONFIG_T::n_in; ii++) {
182  for (int jj = 0; jj < CONFIG_T::n_in; jj++) {
183  if (ii == jj)
184  exp_diff_res[ii][jj] = 1;
185  else {
186  data_round = (data[jj] - data[ii]) * CONFIG_T::table_size / 16;
187  index = data_round + 8 * CONFIG_T::table_size / 16;
188  if (index < 0)
189  index = 0;
190  if (index > CONFIG_T::table_size - 1)
191  index = CONFIG_T::table_size - 1;
192  exp_diff_res[ii][jj] = exp_table[index];
193  }
194  exp_res[ii] += exp_diff_res[ii][jj];
195  }
196  }
197 
198  //Second loop to invert
199  for (unsigned ii = 0; ii < CONFIG_T::n_in; ii++) {
200  int exp_res_index = exp_res[ii] * CONFIG_T::table_size / 64;
201  if (exp_res_index < 0)
202  exp_res_index = 0;
203  if (exp_res_index > CONFIG_T::table_size - 1)
204  exp_res_index = CONFIG_T::table_size - 1;
205  //typename CONFIG_T::table_t exp_res_invert = invert_table[exp_res_index];
206  res[ii] = (res_T)invert_table[exp_res_index];
207  }
208  }
Definition: Electron.h:6
ii
Definition: cuy.py:589
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79

◆ tanh()

template<class data_T , class res_T , typename CONFIG_T >
void nnet::tanh ( data_T  data[CONFIG_T::n_in],
res_T  res[CONFIG_T::n_in] 
)

Definition at line 227 of file nnet_activation.h.

References data, and cuy::ii.

Referenced by ZCountingElectrons::analyze(), HcalGeomCheck::analyze(), CSCGattiFunction::binValue(), HFRecoEcalCandidateAlgo::correctEPosition(), HcalHardcodeGeometryLoader::fillHE(), HcalFlexiHardcodeGeometryLoader::fillHE(), GflashHadronShowerProfile::fTanh(), FWExpressionValidator::FWExpressionValidator(), PythiaFilterIsolatedTrack::getDistInCM(), spr::getDistInCMatEcal(), spr::getDistInCMatHcal(), IsoTrig::getGoodTracks(), HFGflash::gfParameterization(), init_tanh_table(), reco::HcalIsolatedTrackCandidate::l1jetp(), CaloTowerHardcodeGeometryLoader::makeCell(), SiPixelDigitizerAlgorithm::missCalibrate(), reco::parser::tanh_f::operator()(), HFRecoEcalCandidateAlgo::produce(), L2MuonSeedGeneratorFromL1TkMu::produce(), CaloDualConeSelector< T >::selectCallback(), CaloConeSelector< T >::selectCallback(), GflashShowino::simulateFirstInteractionPoint(), QGLikelihoodCalculator::smearingFunction(), reco::isodeposit::Direction::theta(), and pat::PackedGenParticle::unpack().

227  {
228  // Initialize the lookup table
229  typename CONFIG_T::table_t tanh_table[CONFIG_T::table_size];
230  init_tanh_table<CONFIG_T, CONFIG_T::table_size>(tanh_table);
231 
232  // Index into the lookup table based on data
233  int data_round;
234  int index;
235  for (int ii = 0; ii < CONFIG_T::n_in; ii++) {
236  data_round = data[ii] * CONFIG_T::table_size / 8;
237  index = data_round + 4 * CONFIG_T::table_size / 8;
238  //std::cout << "Input: " << data[ii] << " Round: " << data_round << " Index: " << index << std::endl;
239  if (index < 0)
240  index = 0;
241  if (index > CONFIG_T::table_size - 1)
242  index = CONFIG_T::table_size - 1;
243  res[ii] = (res_T)tanh_table[index];
244  }
245  }
Definition: Electron.h:6
ii
Definition: cuy.py:589
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79