LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
softmax_impl.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_SOFTMAX_IMPL_HPP_INCLUDED
28 #define LBANN_LAYERS_ACTIVATIONS_SOFTMAX_IMPL_HPP_INCLUDED
29 
31 
32 #ifdef LBANN_HAS_DISTCONV
34 #endif // LBANN_HAS_DISTCONV
35 
36 namespace lbann {
37 
38 template <typename T, data_layout L, El::Device D>
40  lbann_data::Layer& proto) const
41 {
42  proto.set_datatype(proto::ProtoDataType<T>);
43  auto* msg = proto.mutable_softmax();
44  switch (m_mode) {
46  msg->set_softmax_mode("instance");
47  break;
49  msg->set_softmax_mode("channel");
50  break;
51  default:
52  msg->set_softmax_mode("invalid");
53  }
54 }
55 
56 #ifdef LBANN_HAS_DISTCONV
57 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
58 softmax_distconv_adapter<TensorDataType, T_layout, Dev>&
60 {
61  return const_cast<softmax_distconv_adapter<TensorDataType, T_layout, Dev>&>(
62  static_cast<const softmax_layer<TensorDataType, T_layout, Dev>&>(*this)
63  .get_distconv_adapter());
64 }
65 
66 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
67 const softmax_distconv_adapter<TensorDataType, T_layout, Dev>&
69 {
70  return dynamic_cast<
71  const softmax_distconv_adapter<TensorDataType, T_layout, Dev>&>(
73 }
74 
75 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
76 void softmax_distconv_adapter<TensorDataType, T_layout, Dev>::
77  setup_distributions(tensor_overlap_constraints& constraints)
78 {
80  // No overlap supported yet
81  for (auto& d : this->m_prev_activations_dists) {
82  d.clear_overlap();
83  constraints.mark_updated(d);
84  constraints.mark_invariant(d);
85  }
86  for (auto& d : this->m_activations_dists) {
87  d.clear_overlap();
88  constraints.mark_updated(d);
89  constraints.mark_invariant(d);
90  }
91  for (auto& d : this->m_prev_error_signals_dists) {
92  d.clear_overlap();
93  constraints.mark_updated(d);
94  constraints.mark_invariant(d);
95  }
96  for (auto& d : this->m_error_signals_dists) {
97  d.clear_overlap();
98  constraints.mark_updated(d);
99  constraints.mark_invariant(d);
100  }
101 }
102 
103 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
104 void softmax_distconv_adapter<TensorDataType, T_layout, Dev>::setup_layer(
105  size_t workspace_capacity)
106 {
107  auto& l =
108  dynamic_cast<softmax_layer<TensorDataType, T_layout, Dev>&>(this->layer());
109  m_softmax = std::make_unique<dc::Softmax>(dc::get_backend());
110  auto mode = l.m_mode == softmax_mode::INSTANCE
111  ? ::distconv::SoftmaxMode::INSTANCE
112  : ::distconv::SoftmaxMode::CHANNEL;
113  m_softmax->setup(this->get_prev_activations(), mode);
114 }
115 #endif // LBANN_HAS_DISTCONV
116 
117 } // namespace lbann
118 
119 #endif // LBANN_LAYERS_ACTIVATIONS_SOFTMAX_IMPL_HPP_INCLUDED
Position-wise softmax.
void write_specific_proto(lbann_data::Layer &proto) const final
void mark_updated(const dc::Dist &d)
virtual void setup(size_t max_mini_batch_size, const std::vector< El::Grid *> &grids)
Setup layer members.
Sample-wise softmax.
virtual void setup_distributions(tensor_overlap_constraints &constraints)
void mark_invariant(const dc::Dist &d)