LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
channelwise_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_REGULARIZERS_CHANNELWISE_SOFTMAX_HPP_INCLUDED
28 #define LBANN_LAYERS_REGULARIZERS_CHANNELWISE_SOFTMAX_HPP_INCLUDED
29 
32 
33 #include "lbann/proto/layers.pb.h"
34 
35 namespace lbann {
36 
50 template <typename TensorDataType, data_layout Layout, El::Device Device>
51 class channelwise_softmax_layer : public data_type_layer<TensorDataType>
52 {
53  static_assert(Layout == data_layout::DATA_PARALLEL,
54  "channelwise_softmax_layer only supports "
55  "data-parallel data layout");
56 
57 public:
59  int64_t dim,
60  bool single_dim_mode);
61 
64  operator=(const channelwise_softmax_layer& other) = default;
65  channelwise_softmax_layer* copy() const override;
66 
68 
70  template <typename ArchiveT>
71  void serialize(ArchiveT& ar);
72 
74 
75  std::string get_type() const override;
76  data_layout get_data_layout() const override;
77  El::Device get_device_allocation() const override;
78  bool can_run_inplace() const override { return true; }
79  int get_backprop_requirements() const override
80  {
81  return ERROR_SIGNALS | ACTIVATIONS;
82  }
83 
84 protected:
86  void write_specific_proto(lbann_data::Layer& proto) const final;
87 
88  friend class cereal::access;
90 
91  void setup_dims() override;
92 
93  void fp_compute() override;
94  void bp_compute() override;
95 
96 private:
97  void get_channel_size_and_stride(El::Int& channel_size,
98  El::Int& channel_stride,
99  El::Int& num_channels) const;
100 
102  int64_t m_dim;
103 
107 };
108 
109 // Builder function
110 
111 // =========================================================
112 // Implementation
113 // =========================================================
114 
115 template <typename T, data_layout L, El::Device D>
117  lbann_data::Layer& proto) const
118 {
119  proto.set_datatype(proto::ProtoDataType<T>);
120  auto* msg = proto.mutable_channelwise_softmax();
121  msg->set_dim(m_dim);
122  msg->set_single_dim_mode(m_single_dim_mode);
123 }
124 
125 template <typename TensorDataType, data_layout Layout, El::Device Device>
127  channelwise_softmax_layer(lbann_comm* comm, int64_t dim, bool single_dim_mode)
128  : data_type_layer<TensorDataType>(comm),
129  m_dim(dim),
130  m_single_dim_mode(single_dim_mode)
131 {}
132 
133 template <typename TensorDataType, data_layout Layout, El::Device Device>
136 {
137  return new channelwise_softmax_layer(*this);
138 }
139 
140 template <typename TensorDataType, data_layout Layout, El::Device Device>
141 std::string
143 {
144  return "channel-wise softmax";
145 }
146 
147 template <typename TensorDataType, data_layout Layout, El::Device Device>
150  const
151 {
152  return Layout;
153 }
154 
155 template <typename TensorDataType, data_layout Layout, El::Device Device>
158 {
159  return Device;
160 }
161 
162 // =========================================================
163 // Explicit template instantiation
164 // =========================================================
165 
166 #ifndef LBANN_CHANNELWISE_SOFTMAX_LAYER_INSTANTIATE
167 #define PROTO_DEVICE(T, Device) \
168  extern template class channelwise_softmax_layer<T, \
169  data_layout::DATA_PARALLEL, \
170  Device>;
172 #undef PROTO_DEVICE
173 #endif // LBANN_CHANNELWISE_SOFTMAX_LAYER_INSTANTIATE
174 
175 } // namespace lbann
176 
177 #endif // LBANN_LAYERS_REGULARIZERS_CHANNELWISE_SOFTMAX_HPP_INCLUDED
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
bool m_single_dim_mode
If true, only performs softmax on the chosen dimension. Otherwise all dimensions but m_dim will be us...
void get_channel_size_and_stride(El::Int &channel_size, El::Int &channel_stride, El::Int &num_channels) const
channelwise_softmax_layer & operator=(const channelwise_softmax_layer &other)=default
void write_specific_proto(lbann_data::Layer &proto) const final
std::string get_type() const override
Get the layer type&#39;s name.
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
constexpr El::Device Device
Apply softmax to tensor channels.
channelwise_softmax_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
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 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.
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...