LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
covariance.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_MISC_COVARIANCE_HPP_INCLUDED
28 #define LBANN_LAYERS_MISC_COVARIANCE_HPP_INCLUDED
29 
31 #include "lbann/layers/layer.hpp"
33 #include "lbann/proto/layers.pb.h"
34 
35 namespace lbann {
36 
49 template <typename TensorDataType, data_layout Layout, El::Device Device>
50 class covariance_layer : public data_type_layer<TensorDataType>
51 {
52 public:
54 
57  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
58 
60 
61 public:
62  covariance_layer(lbann_comm* comm, bool biased)
63  : data_type_layer<TensorDataType>(comm), m_biased(biased)
64  {
66  }
68  : data_type_layer<TensorDataType>(other),
69  m_biased(other.m_biased),
70  m_means(other.m_means ? other.m_means->Copy() : nullptr),
71  m_workspace(other.m_workspace ? other.m_workspace->Copy() : nullptr)
72  {}
74  {
76  m_biased = other.m_biased;
77  m_means.reset(other.m_means ? other.m_means->Copy() : nullptr);
78  m_workspace.reset(other.m_workspace ? other.m_workspace->Copy() : nullptr);
79  return *this;
80  }
81 
82  covariance_layer* copy() const override
83  {
84  return new covariance_layer(*this);
85  }
86 
88 
90  template <typename ArchiveT>
91  void serialize(ArchiveT& ar);
92 
94 
95  std::string get_type() const override { return "covariance"; }
96  data_layout get_data_layout() const override { return Layout; }
97  El::Device get_device_allocation() const override { return Device; }
98  bool can_run_inplace() const override { return false; }
99  int get_backprop_requirements() const override
100  {
102  }
103 
104  description get_description() const override
105  {
107  desc.add("Biased", m_biased);
108  return desc;
109  }
110 
111 protected:
113  void write_specific_proto(lbann_data::Layer& proto) const final;
114 
115  friend class cereal::access;
116  covariance_layer() : covariance_layer(nullptr, false) {}
117 
118  void setup_data(size_t max_mini_batch_size) override;
119 
120  void setup_dims() override;
121 
122  void fp_compute() override;
123  void bp_compute() override;
124 
125 private:
127  bool m_biased;
128 
130  std::unique_ptr<AbsDistMatrixType> m_means;
132  std::unique_ptr<AbsDistMatrixType> m_workspace;
133 };
134 
135 template <typename T, data_layout L, El::Device D>
137  lbann_data::Layer& proto) const
138 {
139  proto.set_datatype(proto::ProtoDataType<T>);
140  auto* msg = proto.mutable_covariance();
141  msg->set_biased(m_biased);
142 }
143 
144 #ifndef LBANN_COVARIANCE_LAYER_INSTANTIATE
145 #define PROTO_DEVICE(T, Device) \
146  extern template class covariance_layer<T, \
147  data_layout::DATA_PARALLEL, \
148  Device>; \
149  extern template class covariance_layer<T, data_layout::MODEL_PARALLEL, Device>
150 
152 #undef PROTO_DEVICE
153 #endif // LBANN_COVARIANCE_LAYER_INSTANTIATE
154 
155 } // namespace lbann
156 
157 #endif // LBANN_LAYERS_MISC_COVARIANCE_HPP_INCLUDED
covariance_layer(lbann_comm *comm, bool biased)
Definition: covariance.hpp:62
std::string get_type() const override
Get the layer type&#39;s name.
Definition: covariance.hpp:95
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: covariance.hpp:97
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
Definition: covariance.hpp:57
void fp_compute() override
Apply layer operation. Called by the &#39;forward_prop&#39; function. Given the input tensors, the output tensors are populated with computed values.
friend class cereal::access
Definition: covariance.hpp:115
covariance_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: covariance.hpp:82
void serialize(ArchiveT &ar)
Estimate covariance.
Definition: covariance.hpp:50
Generates nicely formatted description messages.
Definition: description.hpp:49
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: covariance.hpp:96
virtual description get_description() const
Human-readable description.
constexpr El::Device Device
covariance_layer(const covariance_layer &other)
Definition: covariance.hpp:67
void setup_dims() override
Setup tensor dimensions Called by the &#39;setup&#39; function. If there are any input tensors, the base method sets all uninitialized output tensor dimensions equal to the first input tensor dimensions.
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
std::unique_ptr< AbsDistMatrixType > m_means
Definition: covariance.hpp:130
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: covariance.hpp:99
std::unique_ptr< AbsDistMatrixType > m_workspace
Definition: covariance.hpp:132
void write_specific_proto(lbann_data::Layer &proto) const final
Definition: covariance.hpp:136
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
void setup_data(size_t max_mini_batch_size) override
Setup layer data. Called by the &#39;setup&#39; function. Memory is allocated for distributed matrices...
description get_description() const override
Human-readable description.
Definition: covariance.hpp:104
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: covariance.hpp:98
covariance_layer & operator=(const covariance_layer &other)
Definition: covariance.hpp:73
int m_expected_num_parent_layers
Definition: layer.hpp:838
data_type_layer & operator=(data_type_layer &&other)=default