| 35 | |
| 36 | template <typename TensorDataType, data_layout T_layout, El::Device Dev> |
| 37 | void 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) { |
nothing calls this directly
no test coverage detected