LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
matmul.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_LAYER_MATH_MATMUL_HPP_INCLUDED
28 #define LBANN_LAYER_MATH_MATMUL_HPP_INCLUDED
29 
31 
32 #ifdef LBANN_HAS_DISTCONV
35 #endif // LBANN_HAS_DISTCONV
36 
37 namespace lbann {
38 
39 #ifdef LBANN_HAS_DISTCONV
40 namespace dc {
41 using Backend = ::distconv::BackendDNNLib;
42 template <typename TensorDataType>
43 using MatMul = ::distconv::MatMul<Backend, TensorDataType>;
44 } // namespace dc
45 
46 template <typename TensorDataType, data_layout Layout, El::Device Device>
47 class matmul_distconv_adapter
48  : public data_type_distconv_adapter<TensorDataType>
49 {
50 public:
51  using TensorDevType =
53 
54  matmul_distconv_adapter(Layer& layer)
55  : data_type_distconv_adapter<TensorDataType>(layer)
56  {}
57 
58  virtual ~matmul_distconv_adapter() = default;
59  void setup_distributions(tensor_overlap_constraints& constraints) override;
60  void setup_layer(size_t workspace_capacity) override;
61  void fp_compute();
62  void bp_compute();
63  dc::Shape get_activations_local_shape(int index = 0) const override;
64  std::unique_ptr<dc::MatMul<TensorDataType>> m_matmul_operator;
65 }; // class definition matmul_distconv_adapter
66 
67 #endif // LBANN_HAS_DISTCONV
68 
79 template <typename TensorDataType,
81  El::Device Device = El::Device::CPU>
82 class matmul_layer : public data_type_layer<TensorDataType>
83 {
84  static_assert(Layout == data_layout::DATA_PARALLEL,
85  "matmul_layer only supports "
86  "data-parallel data layout");
87 
88 public:
90  bool transpose_a = false,
91  bool transpose_b = false);
92  matmul_layer(const matmul_layer& other) = default;
93  matmul_layer& operator=(const matmul_layer& other) = default;
94  matmul_layer* copy() const override;
95 
96  std::string get_type() const override;
97  data_layout get_data_layout() const override;
98  El::Device get_device_allocation() const override;
99  bool can_run_inplace() const override { return false; }
100  int get_backprop_requirements() const override
101  {
103  }
104 
105  description get_description() const override;
106 
107  template <typename ArchiveT>
108  void serialize(ArchiveT& ar);
109 
110 protected:
112  void write_specific_proto(lbann_data::Layer& proto) const final;
113 
114  friend class cereal::access;
115  matmul_layer() : matmul_layer(nullptr, false, false) {}
116 
117  void setup_dims() override;
118  void fp_compute() override;
119  void bp_compute() override;
120 
121 #ifdef LBANN_HAS_DISTCONV
122  friend class matmul_distconv_adapter<TensorDataType, Layout, Device>;
123 
124 protected:
125  void setup_distconv_adapter() override;
126  bool is_distconv_supported() const override;
127  matmul_distconv_adapter<TensorDataType, Layout, Device>&
128  get_distconv_adapter() override;
129  const matmul_distconv_adapter<TensorDataType, Layout, Device>&
130  get_distconv_adapter() const override;
131 #endif // LBANN_HAS_DISTCONV
132 
133 private:
140 
141  template <typename U>
142  friend void fp_compute_impl(matmul_layer<U, Layout, Device>&, bool, bool);
143  template <typename U>
144  friend void bp_compute_impl(matmul_layer<U, Layout, Device>&, bool, bool);
145 };
146 
147 // =========================================================
148 // Implementation
149 // =========================================================
150 
151 template <typename TensorDataType, data_layout Layout, El::Device Device>
153  bool transpose_a,
154  bool transpose_b)
155  : data_type_layer<TensorDataType>(comm),
156  m_transpose_a{transpose_a},
157  m_transpose_b{transpose_b}
158 {
160 }
161 
162 template <typename TensorDataType, data_layout Layout, El::Device Device>
165 {
166  return new matmul_layer(*this);
167 }
168 
169 template <typename TensorDataType, data_layout Layout, El::Device Device>
171 {
172  return "matrix multiply";
173 }
174 
175 template <typename TensorDataType, data_layout Layout, El::Device Device>
178 {
179  return Layout;
180 }
181 
182 template <typename TensorDataType, data_layout Layout, El::Device Device>
185 {
186  return Device;
187 }
188 
189 template <typename TensorDataType, data_layout Layout, El::Device Device>
192 {
194  desc.add("Transpose A", m_transpose_a);
195  desc.add("Transpose B", m_transpose_b);
196  return desc;
197 }
198 
199 // =========================================================
200 // Explicit template instantiation
201 // =========================================================
202 
203 #ifndef LBANN_MATMUL_LAYER_INSTANTIATE
204 
205 #define PROTO_DEVICE(T, Device) \
206  extern template class matmul_layer<T, data_layout::DATA_PARALLEL, Device>
207 
209 #undef PROTO_DEVICE
210 
211 #endif // LBANN_MATMUL_LAYER_INSTANTIATE
212 
213 } // namespace lbann
214 
215 #endif // LBANN_LAYER_MATH_MATMUL_HPP_INCLUDED
matmul_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: matmul.hpp:164
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
Definition: matmul.hpp:177
void serialize(std::ostream &os, google::protobuf::Message const &msg)
Serialize the protobuf message to a stream.
Generates nicely formatted description messages.
Definition: description.hpp:49
virtual description get_description() const
Human-readable description.
constexpr El::Device Device
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
Definition: matmul.hpp:184
description get_description() const override
Human-readable description.
Definition: matmul.hpp:191
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: matmul.hpp:100
Matrix multiplication.
Definition: matmul.hpp:82
::distconv::tensor::Shape Shape
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: matmul.hpp:99
std::string get_type() const override
Get the layer type&#39;s name.
Definition: matmul.hpp:170
int m_expected_num_parent_layers
Definition: layer.hpp:838
dc::TensorDev< OutputTensorDataType > TensorDevType