LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
utils/dnn_lib/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 #ifndef LBANN_UTILS_DNN_LIB_SOFTMAX_HPP
27 #define LBANN_UTILS_DNN_LIB_SOFTMAX_HPP
28 
29 #include "lbann_config.hpp"
30 
31 #if defined LBANN_HAS_CUDNN
33 #elif defined LBANN_HAS_MIOPEN
35 #elif defined LBANN_HAS_GPU && !defined LBANN_HAS_ONEDNN_GPU
36 static_assert(false,
37  "GPU support detected but no valid DNN library implementation. ");
38 #endif // LBANN_HAS_CUDNN
39 
40 #if defined LBANN_HAS_ONEDNN
42 #endif // LBANN_HAS_ONEDNN
43 
45 
47 
48 namespace lbann {
49 namespace dnn_lib {
50 
51 template <typename ScalarT, typename TensorDescT, typename DataT, El::Device D>
52 void softmax_forward(ScalarT const& alpha_in,
53  TensorDescT const& xDesc,
54  El::Matrix<DataT, D> const& x,
55  ScalarT const& beta_in,
56  TensorDescT const& yDesc,
57  El::Matrix<DataT, D>& y,
58  El::SyncInfo<D> const& si,
59  softmax_mode mode,
61 {
62  using backend = typename TensorDescT::backend_type;
63  static_assert(backend::device == D, "Mismatched device identifiers.");
64  backend::softmax_forward(alpha_in,
65  xDesc,
66  x,
67  beta_in,
68  yDesc,
69  y,
70  si,
71  mode,
72  alg);
73 }
74 
75 template <typename ScalarT, typename TensorDescT, typename DataT, El::Device D>
76 void softmax_forward(ScalarT const& alpha_in,
77  TensorDescT const& xDesc,
78  El::Matrix<DataT, D> const& x,
79  ScalarT const& beta_in,
80  TensorDescT const& yDesc,
81  El::Matrix<DataT, D>& y,
82  softmax_mode mode,
84 {
85  auto multisync = El::MakeMultiSync(get_sync_info(y), get_sync_info(x));
86  softmax_forward(alpha_in,
87  xDesc,
88  x,
89  beta_in,
90  yDesc,
91  y,
92  force(multisync),
93  mode,
94  alg);
95 }
96 
97 template <typename ScalarT, typename TensorDescT, typename DataT, El::Device D>
98 void softmax_backward(ScalarT const& alpha_in,
99  TensorDescT const& yDesc,
100  El::Matrix<DataT, D> const& y,
101  TensorDescT const& dyDesc,
102  El::Matrix<DataT, D> const& dy,
103  ScalarT const& beta_in,
104  TensorDescT const& dxDesc,
105  El::Matrix<DataT, D>& dx,
106  El::SyncInfo<D> const& si,
107  softmax_mode mode,
109 {
110  // Short-circuit if we can
111  if (y.IsEmpty())
112  return;
113 
114  using backend = typename TensorDescT::backend_type;
115  static_assert(backend::device == D, "Mismatched device identifiers.");
116  backend::softmax_backward(alpha_in,
117  yDesc,
118  y,
119  dyDesc,
120  dy,
121  beta_in,
122  dxDesc,
123  dx,
124  si,
125  mode,
126  alg);
127 }
128 
129 template <typename ScalarT, typename TensorDescT, typename DataT, El::Device D>
130 void softmax_backward(ScalarT const& alpha_in,
131  TensorDescT const& yDesc,
132  El::Matrix<DataT, D> const& y,
133  TensorDescT const& dyDesc,
134  El::Matrix<DataT, D> const& dy,
135  ScalarT const& beta_in,
136  TensorDescT const& dxDesc,
137  El::Matrix<DataT, D>& dx,
138  softmax_mode mode,
140 {
141  auto multisync =
142  El::MakeMultiSync(get_sync_info(dx), get_sync_info(y), get_sync_info(dy));
143  softmax_backward(alpha_in,
144  yDesc,
145  y,
146  dyDesc,
147  dy,
148  beta_in,
149  dxDesc,
150  dx,
151  force(multisync),
152  mode,
153  alg);
154 }
155 } // namespace dnn_lib
156 } // namespace lbann
157 #endif // LBANN_UTILS_DNN_LIB_SOFTMAX_HPP
void softmax_backward(ScalarT const &alpha_in, TensorDescT const &yDesc, El::Matrix< DataT, D > const &y, TensorDescT const &dyDesc, El::Matrix< DataT, D > const &dy, ScalarT const &beta_in, TensorDescT const &dxDesc, El::Matrix< DataT, D > &dx, El::SyncInfo< D > const &si, softmax_mode mode, softmax_alg alg=softmax_alg::ACCURATE)
void softmax_forward(ScalarT const &alpha_in, TensorDescT const &xDesc, El::Matrix< DataT, D > const &x, ScalarT const &beta_in, TensorDescT const &yDesc, El::Matrix< DataT, D > &y, El::SyncInfo< D > const &si, softmax_mode mode, softmax_alg alg=softmax_alg::ACCURATE)
void softmax_backward(ScalarT const &alpha_in, TensorDescT const &yDesc, El::Matrix< DataT, D > const &y, TensorDescT const &dyDesc, El::Matrix< DataT, D > const &dy, ScalarT const &beta_in, TensorDescT const &dxDesc, El::Matrix< DataT, D > &dx, softmax_mode mode, softmax_alg alg=softmax_alg::ACCURATE)
softmax_alg
Internal LBANN names for supported softmax algorithms.
Definition: dnn_enums.hpp:110
auto force(El::MultiSync< D, Ds... > const &x) -> El::SyncInfo< D > const &
Force the MultiSync to the master SyncInfo.
El::SyncInfo< D > get_sync_info(El::Matrix< TensorDataType, D > const &m) noexcept
Get a SyncInfo from an Matrix.
softmax_mode
Which tensor dimensions to apply softmax over.
Definition: dnn_enums.hpp:87
void softmax_forward(ScalarT const &alpha_in, TensorDescT const &xDesc, El::Matrix< DataT, D > const &x, ScalarT const &beta_in, TensorDescT const &yDesc, El::Matrix< DataT, D > &y, softmax_mode mode, softmax_alg alg=softmax_alg::ACCURATE)