Vowpal Wabbit
Functions
accumulate.h File Reference
#include "global_data.h"

Go to the source code of this file.

Functions

void accumulate (vw &all, parameters &weights, size_t o)
 
float accumulate_scalar (vw &all, float local_sum)
 
void accumulate_weighted_avg (vw &all, parameters &weights)
 
void accumulate_avg (vw &all, parameters &weights, size_t o)
 

Function Documentation

◆ accumulate()

void accumulate ( vw all,
parameters weights,
size_t  o 
)

Definition at line 20 of file accumulate.cc.

References parameters::dense_weights, vw::num_bits, parameters::sparse, parameters::sparse_weights, dense_parameters::stride_shift(), sparse_parameters::stride_shift(), and UINT64_ONE.

Referenced by average_diff(), bs_predict_mean(), calculate_sd(), ldamath::expdigammify(), exploration::generate_bag(), CCB::parse_label(), and process_pass().

21 {
22  uint64_t length = UINT64_ONE << all.num_bits; // This is size of gradient
23  float* local_grad = new float[length];
24 
25  if (weights.sparse)
26  for (uint64_t i = 0; i < length; i++)
27  local_grad[i] = (&(weights.sparse_weights[i << weights.sparse_weights.stride_shift()]))[offset];
28  else
29  for (uint64_t i = 0; i < length; i++)
30  local_grad[i] = (&(weights.dense_weights[i << weights.dense_weights.stride_shift()]))[offset];
31 
32  all_reduce<float, add_float>(all, local_grad, length); // TODO: modify to not use first()
33 
34  if (weights.sparse)
35  for (uint64_t i = 0; i < length; i++)
36  (&(weights.sparse_weights[i << weights.sparse_weights.stride_shift()]))[offset] = local_grad[i];
37  else
38  for (uint64_t i = 0; i < length; i++)
39  (&(weights.dense_weights[i << weights.dense_weights.stride_shift()]))[offset] = local_grad[i];
40 
41  delete[] local_grad;
42 }
uint32_t stride_shift() const
uint32_t num_bits
Definition: global_data.h:398
dense_parameters dense_weights
constexpr uint64_t UINT64_ONE
sparse_parameters sparse_weights
uint32_t stride_shift() const

◆ accumulate_avg()

void accumulate_avg ( vw all,
parameters weights,
size_t  o 
)

Definition at line 51 of file accumulate.cc.

References vw::all_reduce, parameters::dense_weights, vw::num_bits, parameters::sparse, parameters::sparse_weights, dense_parameters::stride_shift(), sparse_parameters::stride_shift(), and AllReduce::total.

Referenced by GD::end_pass().

52 {
53  uint32_t length = 1 << all.num_bits; // This is size of gradient
54  float numnodes = (float)all.all_reduce->total;
55  float* local_grad = new float[length];
56 
57  if (weights.sparse)
58  for (uint64_t i = 0; i < length; i++)
59  local_grad[i] = (&(weights.sparse_weights[i << weights.sparse_weights.stride_shift()]))[offset];
60  else
61  for (uint64_t i = 0; i < length; i++)
62  local_grad[i] = (&(weights.dense_weights[i << weights.dense_weights.stride_shift()]))[offset];
63 
64  all_reduce<float, add_float>(all, local_grad, length); // TODO: modify to not use first()
65 
66  if (weights.sparse)
67  for (uint64_t i = 0; i < length; i++)
68  (&(weights.sparse_weights[i << weights.sparse_weights.stride_shift()]))[offset] = local_grad[i] / numnodes;
69  else
70  for (uint64_t i = 0; i < length; i++)
71  (&(weights.dense_weights[i << weights.dense_weights.stride_shift()]))[offset] = local_grad[i] / numnodes;
72 
73  delete[] local_grad;
74 }
uint32_t stride_shift() const
const size_t total
Definition: allreduce.h:80
uint32_t num_bits
Definition: global_data.h:398
AllReduce * all_reduce
Definition: global_data.h:381
dense_parameters dense_weights
sparse_parameters sparse_weights
uint32_t stride_shift() const

◆ accumulate_scalar()

float accumulate_scalar ( vw all,
float  local_sum 
)

Definition at line 44 of file accumulate.cc.

Referenced by end_pass(), process_pass(), summarize_holdout_set(), and VW::sync_stats().

45 {
46  float temp = local_sum;
47  all_reduce<float, add_float>(all, &temp, 1);
48  return temp;
49 }

◆ accumulate_weighted_avg()

void accumulate_weighted_avg ( vw all,
parameters weights 
)

Definition at line 117 of file accumulate.cc.

References parameters::adaptive, parameters::dense_weights, do_weighting(), dense_parameters::first(), vw::num_bits, parameters::sparse, parameters::sparse_weights, dense_parameters::stride_shift(), sparse_parameters::stride_shift(), parameters::stride_shift(), and vw::trace_message.

Referenced by GD::end_pass().

118 {
119  if (!weights.adaptive)
120  {
121  all.trace_message << "Weighted averaging is implemented only for adaptive gradient, use accumulate_avg instead\n";
122  return;
123  }
124 
125  uint32_t length = 1 << all.num_bits; // This is the number of parameters
126  float* local_weights = new float[length];
127 
128  if (weights.sparse)
129  for (uint64_t i = 0; i < length; i++)
130  local_weights[i] = (&(weights.sparse_weights[i << weights.sparse_weights.stride_shift()]))[1];
131  else
132  for (uint64_t i = 0; i < length; i++)
133  local_weights[i] = (&(weights.dense_weights[i << weights.dense_weights.stride_shift()]))[1];
134 
135  // First compute weights for averaging
136  all_reduce<float, add_float>(all, local_weights, length);
137 
138  if (weights.sparse)
139  do_weighting(all, length, local_weights, weights.sparse_weights);
140  else
141  do_weighting(all, length, local_weights, weights.dense_weights);
142 
143  if (weights.sparse)
144  std::cout << "sparse parameters not supported with parallel computation!" << std::endl;
145  else
146  all_reduce<float, add_float>(
147  all, weights.dense_weights.first(), ((size_t)length) * (1ull << weights.stride_shift()));
148  delete[] local_weights;
149 }
uint32_t stride_shift() const
uint32_t num_bits
Definition: global_data.h:398
vw_ostream trace_message
Definition: global_data.h:424
void do_weighting(vw &all, uint64_t length, float *local_weights, T &weights)
Definition: accumulate.cc:95
dense_parameters dense_weights
sparse_parameters sparse_weights
uint32_t stride_shift()
uint32_t stride_shift() const