LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
log_softmax.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_ACTIVATIONS_LOG_SOFTMAX_HPP_INCLUDED
28 #define LBANN_LAYERS_ACTIVATIONS_LOG_SOFTMAX_HPP_INCLUDED
29 
32 #if defined LBANN_HAS_DNN_LIB
34 #endif // LBANN_HAS_DNN_LIB
35 #include "lbann/proto/layers.pb.h"
36 
37 namespace lbann {
38 
43 template <typename TensorDataType, data_layout Layout, El::Device Device>
44 class log_softmax_layer : public data_type_layer<TensorDataType>
45 {
46 public:
48 
51  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
52 
54 
55 public:
58  : data_type_layer<TensorDataType>(comm)
59 #ifdef LBANN_HAS_DNN_LIB
60  ,
61  m_tensors_dnn_desc(this)
62 #endif // LBANN_HAS_DNN_LIB
63  {}
64 
66  : data_type_layer<TensorDataType>(other),
67  m_workspace(other.m_workspace ? other.m_workspace->Copy() : nullptr)
68 #ifdef LBANN_HAS_DNN_LIB
69  ,
70  m_tensors_dnn_desc(other.m_tensors_dnn_desc)
71 #endif // LBANN_HAS_DNN_LIB
72  {
73 #ifdef LBANN_HAS_DNN_LIB
74  m_tensors_dnn_desc.set_layer(this);
75 #endif // LBANN_HAS_DNN_LIB
76  }
77 
79  {
81  m_workspace.reset(other.m_workspace ? other.m_workspace->Copy() : nullptr);
82 #ifdef LBANN_HAS_DNN_LIB
83  m_tensors_dnn_desc = other.m_tensors_dnn_desc;
84  m_tensors_dnn_desc.set_layer(this);
85 #endif // LBANN_HAS_DNN_LIB
86  return *this;
87  }
88 
89  ~log_softmax_layer() = default;
90 
91  log_softmax_layer* copy() const override
92  {
93  return new log_softmax_layer(*this);
94  }
95  std::string get_type() const override { return "log softmax"; }
96  data_layout get_data_layout() const override { return Layout; }
97  El::Device get_device_allocation() const override { return Device; }
98 
99  // Multi-stage log softmax can run in-place (local workspace acts as an
100  // intermediate buffer)
101  bool can_run_inplace() const override { return true; }
102  int get_backprop_requirements() const override
103  {
104  return ERROR_SIGNALS | ACTIVATIONS;
105  }
106 
107  void setup_dims() override
108  {
110  this->set_output_dims(this->get_input_dims());
111  }
112 
113  void setup_data(size_t max_mini_batch_size) override
114  {
115  data_type_layer<TensorDataType>::setup_data(max_mini_batch_size);
116  auto dist = this->get_prev_activations().DistData();
117  dist.colDist = El::STAR;
118  m_workspace.reset(AbsDistMatrixType::Instantiate(dist));
119 #ifdef HYDROGEN_HAVE_CUB
120  if (m_workspace->GetLocalDevice() == El::Device::GPU) {
121  m_workspace->Matrix().SetMemoryMode(1); // CUB memory pool
122  }
123 #endif // HYDROGEN_HAVE_CUB
124  }
125 
126  void fp_compute() override;
127  void bp_compute() override;
128 
129  template <typename U>
131  template <typename U>
133 
135 
137  template <typename ArchiveT>
138  void serialize(ArchiveT& ar);
139 
141 
142 protected:
144  void write_specific_proto(lbann_data::Layer& proto) const final;
145 
146 private:
148  std::unique_ptr<AbsDistMatrixType> m_workspace;
149 
150 #ifdef LBANN_HAS_DNN_LIB
151 
153  m_tensors_dnn_desc;
154 #endif // LBANN_HAS_DNN_LIB
155 };
156 
157 template <typename T, data_layout L, El::Device D>
159  lbann_data::Layer& proto) const
160 {
161  proto.set_datatype(proto::ProtoDataType<T>);
162  proto.mutable_log_softmax();
163 }
164 
165 #ifndef LBANN_LOG_SOFTMAX_LAYER_INSTANTIATE
166 #define PROTO_DEVICE(T, Device) \
167  extern template class log_softmax_layer<T, \
168  data_layout::DATA_PARALLEL, \
169  Device>; \
170  extern template class log_softmax_layer<T, \
171  data_layout::MODEL_PARALLEL, \
172  Device>
173 
175 #undef PROTO_DEVICE
176 #endif // LBANN_LOG_SOFTMAX_LAYER_INSTANTIATE
177 
178 } // namespace lbann
179 
180 #endif // LBANN_LAYERS_ACTIVATIONS_LOG_SOFTMAX_HPP_INCLUDED
virtual void setup_dims()
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.
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
std::unique_ptr< AbsDistMatrixType > m_workspace
std::vector< int > get_input_dims(size_t input_index=0) const
Get input tensor dimensions.
log_softmax_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: log_softmax.hpp:91
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: log_softmax.hpp:96
constexpr El::Device Device
InputAbsDistMatrixType & get_prev_activations(int parent_index=0)
Logarithm of softmax function.
Definition: log_softmax.hpp:44
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
friend void bp_compute_impl(log_softmax_layer< U, Layout, Device > &l)
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.
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
Definition: log_softmax.hpp:51
log_softmax_layer(lbann_comm *comm)
Definition: log_softmax.hpp:57
log_softmax_layer & operator=(const log_softmax_layer &other)
Definition: log_softmax.hpp:78
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.
std::string get_type() const override
Get the layer type&#39;s name.
Definition: log_softmax.hpp:95
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
void write_specific_proto(lbann_data::Layer &proto) const final
friend void fp_compute_impl(log_softmax_layer< U, Layout, Device > &l)
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
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: log_softmax.hpp:97
void setup_data(size_t max_mini_batch_size) override
void serialize(ArchiveT &ar)
data_type_layer & operator=(data_type_layer &&other)=default
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...
log_softmax_layer(const log_softmax_layer &other)
Definition: log_softmax.hpp:65