MCPcopy Create free account
hub / github.com/LBANN/lbann / fp_compute

Method fp_compute

src/layers/regularizers/batch_normalization.cpp:37–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35
36template <typename TensorDataType, data_layout T_layout, El::Device Dev>
37void batch_normalization_layer<TensorDataType, T_layout, Dev>::fp_compute()
38{
39 const TensorDataType zero = El::TypeTraits<TensorDataType>::Zero();
40 const TensorDataType one = El::TypeTraits<TensorDataType>::One();
41 const bool is_training =
42 this->m_model->get_execution_context().get_execution_mode() ==
43 execution_mode::training;
44
45 // Matrices
46 const auto& input = this->get_prev_activations();
47 const auto& local_input = input.LockedMatrix();
48 auto& local_output = this->get_local_activations();
49
50 // Matrix parameters
51 const auto& width = input.Width();
52 const auto& local_width = local_input.Width();
53 const auto& output_dims = this->get_output_dims();
54 const auto& num_channels = output_dims[0];
55 const auto& channel_size = this->get_output_size() / num_channels;
56
57 const int correction = this->m_bessel_correction ? 1 : 0;
58
59 // Compute statistics
60 if (is_training) {
61 using ValuesGetter = weights_details::SafeWeightsAccessor<TensorDataType>;
62 // Local matrices
63 auto& local_mean = this->m_mean_v->Matrix();
64 auto& local_var = this->m_var_v->Matrix();
65 auto& local_running_mean =
66 ValuesGetter::mutable_values(this->get_weights(2)).Matrix();
67 auto& local_running_var =
68 ValuesGetter::mutable_values(this->get_weights(3)).Matrix();
69 // Compute sums and sums of squares
70 LBANN_OMP_PARALLEL_FOR
71 for (El::Int channel = 0; channel < num_channels; ++channel) {
72 TensorDataType sum = zero;
73 TensorDataType sqsum = zero;
74 const auto& row_start = channel * channel_size;
75 const auto& row_end = (channel + 1) * channel_size;
76 for (El::Int col = 0; col < local_width; ++col) {
77 for (El::Int row = row_start; row < row_end; ++row) {
78 const auto& x = local_input(row, col);
79 sum += x;
80 sqsum += x * x;
81 }
82 }
83 local_mean(channel, 0) = sum;
84 local_var(channel, 0) = sqsum;
85 }
86 El::Int num_per_sum;
87 if (this->m_statistics_group_size == 0) {
88 // Global statistics aggregation; allreduce on fused buffer.
89 this->get_comm()->allreduce(*this->m_mean_and_var,
90 this->m_mean_and_var->RedundantComm(),
91 El::mpi::SUM);
92 num_per_sum = channel_size * width;
93 }
94 else if (this->m_statistics_group_size == 1) {

Callers

nothing calls this directly

Calls 11

ZeroClass · 0.85
SqrtClass · 0.85
WidthMethod · 0.80
get_output_dimsMethod · 0.80
countMethod · 0.80
maxFunction · 0.50
get_execution_modeMethod · 0.45
get_output_sizeMethod · 0.45
get_weightsMethod · 0.45
allreduceMethod · 0.45
get_commMethod · 0.45

Tested by

no test coverage detected