LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
distconv_layers.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, 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_LAYERS_LEARNING_DISTCONV_LAYERS
28 #define LBANN_LAYERS_LEARNING_DISTCONV_LAYERS
29 #include "distconv/base.hpp"
30 #include "distconv/tensor/tensor.hpp"
31 #include "distconv/tensor/tensor_mpi.hpp"
32 #include "lbann/utils/distconv.hpp"
33 
34 #ifdef LBANN_HAS_DISTCONV
35 namespace distconv {
36 template <typename Backend, typename DataType>
37 class ChannelwiseFullyConnected
38 {
40 
41 public:
42  ChannelwiseFullyConnected(Backend& backend) : m_be(backend){};
43 
44  template <typename Allocator>
45  int forward(
46  bool transpose_A,
47  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& input,
48  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& linearity,
49  tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& output);
50 
51  template <typename Allocator>
52  int apply_bias(
53  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& bias,
54  tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& output);
55 
56  template <typename Allocator>
57  int backward_wrt_input(
58  bool transpose_A,
59  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& output_grad,
60  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& linearity,
61  tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& input_grad);
62 
63  template <typename Allocator>
64  int backward_wrt_weight(
65  bool transpose,
66  DataType dst_scale,
67  DataType gradient_scale,
68  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& input,
69  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& output_grad,
70  tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& linearity_grad);
71 
72  template <typename Allocator>
73  int backward_wrt_bias(
74  DataType gradient_scale,
75  DataType dst_scale,
76  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& output_grad,
77  tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& bias_grad);
78 
79 protected:
80  Backend& m_be;
81 }; // class definition ChannelwiseFullyConnected
82 
83 template <typename DataType, typename locale, typename Allocator>
84 tensor::Shape get_fc_output_local_tensor_shape(
85  const tensor::Tensor<DataType, locale, Allocator>& input,
86  const int_vector& linearity_dims,
87  bool transpose)
88 {
89 
90  // https://github.com/LLNL/DiHydrogen/blob/7f86db1f9701ac3afb5e16aefdd57563d57a1698/legacy/include/distconv/distconv.hpp#L173
91 
92  // Get the input layer local tensor shape
93 
94  auto output_local_shape = input.get_local_shape();
95  output_local_shape[0] = transpose ? linearity_dims[1] : linearity_dims[0];
96  return output_local_shape;
97 }
98 extern template class ChannelwiseFullyConnected<::distconv::BackendDNNLib,
99  float>;
100 extern template class ChannelwiseFullyConnected<::distconv::BackendDNNLib,
101  double>;
102 } // namespace distconv
103 
104 #endif // LBANN_HAS_DISTCONV
105 #endif // LBANN_LAYERS_LEARNING_DISTCONV_LAYERS
std::map< El::Int, std::set< El::Int > > transpose(const std::set< El::Int > &nodes, const std::map< El::Int, std::set< El::Int >> &edges)
::distconv::tensor::LocaleMPI LocaleMPI
::distconv::tensor::Shape Shape