LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
kfac_block_channelwise_fc.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2019, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_EXECUTION_ALGORITHMS_KFAC_BLOCK_CHANNELWISE_FC_HPP_INCLUDED
28 #define LBANN_EXECUTION_ALGORITHMS_KFAC_BLOCK_CHANNELWISE_FC_HPP_INCLUDED
29 
31 
32 namespace lbann {
33 
37 template <El::Device Device>
38 class kfac_block_channelwise_fc final : public kfac_block<Device>
39 {
40 public:
45  const size_t layer_id,
46  const size_t inverse_proc_rank,
47  const bool enable_copy_errors,
48  const bool enable_copy_activations,
49  const int input_size,
50  const int output_size)
51  : kfac_block<Device>(layer,
52  context,
53  layer_id,
54  inverse_proc_rank,
55  enable_copy_errors,
56  enable_copy_activations,
57  input_size,
58  output_size),
59  m_has_bias(layer->num_weights() > 1)
60  {}
61 
64  operator=(const kfac_block_channelwise_fc&) = default;
65 
67  {
68  int total_size = 0;
69  total_size +=
71  total_size +=
73  total_size +=
75  total_size +=
77  total_size +=
79  total_size +=
81  total_size += m_grad_buffer_v.Height() * m_grad_buffer_v.Width();
82  return total_size;
83  }
84 
86  bool print_matrix,
87  bool print_matrix_summary) final;
88 
89  const std::vector<El::AbstractMatrix<DataType>*>
91  {
92  std::vector<El::AbstractMatrix<DataType>*> ret = {
95  return ret;
96  }
97 
99  DataType kronecker_decay,
100  bool print_matrix,
101  bool print_matrix_summary) final;
102 
104  bool use_pi,
105  DataType damping_act,
106  DataType damping_err,
107  DataType learning_rate_factor,
108  bool use_eigen_decomposition,
109  bool print_matrix,
110  bool print_matrix_summary,
111  bool print_time) final;
112 
114  DataType learning_rate_factor,
115  bool print_matrix,
116  bool print_matrix_summary,
117  bool print_time) final;
118 
120  int num_local_activations,
121  int num_local_errors,
122  int num_weights) final;
123 
125  void end_communication_forward_end(lbann_comm* comm) final;
127  void end_communication_backward_end(lbann_comm* comm) final;
128 
129  const std::vector<El::AbstractMatrix<DataType>*>
131 
132  int get_inverse_matrices(El::Matrix<DataType, Device>& output,
133  int offset) final;
134 
135  int get_inverse_matrices_size(lbann_comm* comm) final;
136 
137  std::vector<int> get_inverse_matrices_size_vector(lbann_comm* comm) final;
138 
140  El::Matrix<double, El::Device::CPU>& inverse_matrices_size,
141  int block_number) final;
142 
143  int set_inverse_matrices(El::Matrix<DataType, Device>& workspace,
144  int offset,
145  lbann_comm* comm) final;
146 
147  std::string get_info() const final
148  {
149  std::ostringstream oss;
150  oss << kfac_block<Device>::get_info();
151  return oss.str();
152  }
153 
154 private:
156  static void
157  get_kronecker_factor_fc(El::AbstractMatrix<DataType>& factor,
158  const El::AbstractMatrix<DataType>& activations,
159  DataType alpha);
160 
162  static double compute_pi(const El::Matrix<DataType, Device>& A,
163  const El::Matrix<DataType, Device>& G,
164  El::Matrix<DataType, Device>& ws,
165  const El::SyncInfo<Device>& sync_info);
166 
170  {
171  return dynamic_cast<
173  this->m_layer);
174  }
175 
176  std::vector<std::tuple<std::string, size_t, size_t>>
177  get_internal_matrix_info() const override;
178 
180  const bool m_has_bias;
183 
185  El::Matrix<DataType, Device> m_kronecker_factor_buf_A,
187 
190 
192  El::Matrix<DataType, Device> m_kronecker_average_A, m_kronecker_average_G;
193 
195  El::Matrix<DataType, Device> m_kronecker_inverse_A, m_kronecker_inverse_G;
196 
200 
202  El::Matrix<DataType, Device> m_grad_buffer_v;
203 };
204 
205 } // namespace lbann
206 
207 #endif // LBANN_EXECUTION_ALGORITHMS_KFAC_kfac_block_channelwise_fc_HPP_INCLUDED
El::Matrix< DataType, Device > m_kronecker_factor_buf_A
Lower triangle buffers of Kronecker factors.
kfac_block_channelwise_fc & operator=(const kfac_block_channelwise_fc &)=default
El::Matrix< DataType, Device > m_kronecker_average_A
Exponential moving average of Kronecker factors.
static double compute_pi(const El::Matrix< DataType, Device > &A, const El::Matrix< DataType, Device > &G, El::Matrix< DataType, Device > &ws, const El::SyncInfo< Device > &sync_info)
Returns the pi constant.
Layer * m_layer
The target layer.
Definition: kfac_block.hpp:214
const std::vector< El::AbstractMatrix< DataType > * > get_local_kronecker_buffers() final
Get buffers of Kronecker factors for reduce-scatter.
El::Matrix< DataType, Device > m_kronecker_factor_buf_G
std::vector< std::tuple< std::string, size_t, size_t > > get_internal_matrix_info() const override
Return the list of internal matrices&#39; (name, height, width) for debugging. All internal matrices shou...
El::Matrix< DataType, Device > m_kronecker_average_G
std::vector< int > get_inverse_matrices_size_vector(lbann_comm *comm) final
Get inverse matrices size vector.
int get_inverse_matrices(El::Matrix< DataType, Device > &output, int offset) final
Copy inverse matrices to output buffer.
void start_communication_backward_end(lbann_comm *comm) final
void initialize_activations_and_errors(lbann_comm *comm, int num_local_activations, int num_local_errors, int num_weights) final
Copies activations, errors, and weights from model class to private variables to be used in KFAC comp...
Neural network tensor operation.
Definition: layer.hpp:285
int set_inverse_matrices(El::Matrix< DataType, Device > &workspace, int offset, lbann_comm *comm) final
Copy inverse matrices from output buffer.
El::Matrix< DataType, Device > m_kronecker_inverse_G
constexpr El::Device Device
convolution_layer< DataType, data_layout::DATA_PARALLEL, Device > * get_conv_layer()
Get the pointer to its convolution_layer.
void update_kronecker_average(lbann_comm *comm, DataType kronecker_decay, bool print_matrix, bool print_matrix_summary) final
Update the average Kronecker factors.
size_t m_Ainv_height
Size and height of inverse matrices.
int get_local_memory_consumption() final
Get local Memory Consumption.
void end_communication_backward_end(lbann_comm *comm) final
El::Matrix< DataType, Device > m_kronecker_inverse_A
Inverse of the average Kronecker factors.
void end_communication_forward_end(lbann_comm *comm) final
static void get_kronecker_factor_fc(El::AbstractMatrix< DataType > &factor, const El::AbstractMatrix< DataType > &activations, DataType alpha)
Gets the Kronecker factor matrix of a FC layer.
void update_kronecker_inverse(lbann_comm *comm, bool use_pi, DataType damping_act, DataType damping_err, DataType learning_rate_factor, bool use_eigen_decomposition, bool print_matrix, bool print_matrix_summary, bool print_time) final
Compute the inverse of the average Kronecker factors.
size_t m_height_A
The heights of the Kronecker factors.
const bool m_has_bias
Information to perform its computation.
void resize_inverse_matrices_size(El::Matrix< double, El::Device::CPU > &inverse_matrices_size, int block_number) final
Get inverse matrices size vector.
void compute_preconditioned_gradients(lbann_comm *comm, DataType learning_rate_factor, bool print_matrix, bool print_matrix_summary, bool print_time) final
Compute the inverse of the average Kronecker factors.
El::Matrix< DataType, Device > m_grad_buffer_v
Vectorized gradient buffer (only for fully-connecter layers).
kfac_block_channelwise_fc(Layer *layer, kfac::KFACExecutionContext *context, const size_t layer_id, const size_t inverse_proc_rank, const bool enable_copy_errors, const bool enable_copy_activations, const int input_size, const int output_size)
void compute_local_kronecker_factors(lbann_comm *comm, bool print_matrix, bool print_matrix_summary) final
Compute Kronecker factors.
std::string get_info() const final
Get block&#39;s information in one line.
const std::vector< El::AbstractMatrix< DataType > * > get_preconditioned_grad_buffers() final
Get buffers of preconditioned parameter gradients.
void start_communication_forward_end(lbann_comm *comm) final
int get_inverse_matrices_size(lbann_comm *comm) final
Get inverse matrices size (offset).