LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
channelwise_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_REGULARIZERS_CHANNELWISE_SOFTMAX_IMPL_HPP_INCLUDED
28 #define LBANN_LAYERS_REGULARIZERS_CHANNELWISE_SOFTMAX_IMPL_HPP_INCLUDED
29 
32 
33 namespace lbann {
34 
35 template <typename TensorDataType, data_layout Layout, El::Device Device>
37 {
39  int64_t dims = static_cast<int64_t>(this->get_input_dims().size());
40  if (this->m_dim < -dims || this->m_dim >= dims) {
41  LBANN_ERROR("Dimension ",
42  this->m_dim,
43  " is out of bounds for Channelwise "
44  "Softmax layer on tensor with ",
45  dims,
46  " dimensions.");
47  }
48  if (!this->m_single_dim_mode && this->m_dim != 0 && this->m_dim != -dims &&
49  this->m_dim != (dims - 1) && this->m_dim != -1) {
50  LBANN_ERROR("Channelwise softmax with all dimensions is only supported for "
51  "the first or last tensor dimensions. Got dimension ",
52  this->m_dim);
53  }
54 
55  this->set_output_dims(this->get_input_dims());
56 }
57 
58 template <typename TensorDataType, data_layout Layout, El::Device Device>
60  get_channel_size_and_stride(El::Int& channel_size,
61  El::Int& channel_stride,
62  El::Int& num_channels) const
63 {
64  auto const& input_dims = this->get_input_dims();
65  int dims = static_cast<int>(input_dims.size());
66  int dim = this->m_dim;
67  if (dim < 0) // Handle negative dimensions
68  dim += dims;
69 
70  size_t total_size = 1;
71  for (int i = 0; i < dims; ++i) {
72  total_size *= input_dims[i];
73  }
74 
75  // Definitions:
76  // * Channel size: The channel size being normalized
77  // * Number of channels: The number of normalized sub-tensors
78  // * Channel stride: The number of elements to jump between two channels
79 
80  // Single dimension mode: size = dim size, stride = dim stride
81  if (m_single_dim_mode) {
82  channel_size = input_dims[dim];
83  num_channels = total_size / channel_size;
84  // Assuming contiguous tensors with C stride ordering
85  channel_stride = 1;
86  for (int i = dims - 1; i >= dim; --i) {
87  channel_stride *= input_dims[i];
88  }
89  }
90  else {
91  // All other dimensions mode:
92  // size = total size / dim size
93  channel_size = total_size / input_dims[dim];
94  num_channels = input_dims[dim];
95  // -if dim = first: stride = total size / dim size (product of all other
96  // dims) -if dim = last: stride = dim size
97  if (dim == 0) { // First dimension
98  channel_stride = channel_size;
99  }
100  else { // Last dimension
101  channel_stride = 1;
102  }
103  }
104 }
105 
106 } // namespace lbann
107 
108 #endif // LBANN_LAYERS_REGULARIZERS_CHANNELWISE_SOFTMAX_IMPL_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.
void get_channel_size_and_stride(El::Int &channel_size, El::Int &channel_stride, El::Int &num_channels) const
#define LBANN_ERROR(...)
Definition: exception.hpp:37
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.