LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
instance_norm.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_INSTANCE_NORM_HPP_INCLUDED
28 #define LBANN_LAYERS_REGULARIZERS_INSTANCE_NORM_HPP_INCLUDED
29 
32 #include "lbann/proto/layers.pb.h"
33 
34 namespace lbann {
35 
51 template <typename TensorDataType, data_layout Layout, El::Device Device>
52 class instance_norm_layer : public data_type_layer<TensorDataType>
53 {
54  static_assert(Layout == data_layout::DATA_PARALLEL,
55  "instance norm layer only supports data parallel layout");
56 
57 public:
61  instance_norm_layer(TensorDataType epsilon = El::To<TensorDataType>(1e-5));
62 
63  instance_norm_layer(const instance_norm_layer& other) = default;
64  instance_norm_layer& operator=(const instance_norm_layer& other) = default;
65  instance_norm_layer* copy() const override;
66 
67  std::string get_type() const override;
68  data_layout get_data_layout() const override;
69  El::Device get_device_allocation() const override;
70  description get_description() const override;
71  bool can_run_inplace() const override { return true; }
72  int get_backprop_requirements() const override
73  {
75  }
76 
78 
80  template <typename ArchiveT>
81  void serialize(ArchiveT& ar);
82 
84 
85 protected:
87  void write_specific_proto(lbann_data::Layer& proto) const final;
88 
89  void setup_dims() override;
90 
91  void fp_compute() override;
92  void bp_compute() override;
93 
94 private:
96  TensorDataType m_epsilon;
97 
99  El::Matrix<TensorDataType, Device> m_workspace;
100 };
101 
102 // Builder function
103 LBANN_DEFINE_LAYER_BUILDER(instance_norm);
104 
105 // =========================================================
106 // Implementation
107 // =========================================================
108 
109 template <typename T, data_layout L, El::Device D>
111  lbann_data::Layer& proto) const
112 {
113  proto.set_datatype(proto::ProtoDataType<T>);
114  auto* msg = proto.mutable_instance_norm();
115  msg->mutable_epsilon()->set_value(m_epsilon);
116 }
117 
118 template <typename TensorDataType, data_layout Layout, El::Device Device>
120  TensorDataType epsilon)
121  : data_type_layer<TensorDataType>(nullptr), m_epsilon{epsilon}
122 {}
123 
124 template <typename TensorDataType, data_layout Layout, El::Device Device>
127 {
128  return new instance_norm_layer(*this);
129 }
130 
131 template <typename TensorDataType, data_layout Layout, El::Device Device>
132 std::string
134 {
135  return "instance norm";
136 }
137 
138 template <typename TensorDataType, data_layout Layout, El::Device Device>
141 {
142  return Layout;
143 }
144 
145 template <typename TensorDataType, data_layout Layout, El::Device Device>
148  const
149 {
150  return Device;
151 }
152 
153 template <typename TensorDataType, data_layout Layout, El::Device Device>
156 {
158  desc.add("Epsilon", m_epsilon);
159  return desc;
160 }
161 
162 template <typename TensorDataType, data_layout Layout, El::Device Device>
164 {
166  this->set_output_dims(this->get_input_dims());
167 }
168 
169 // =========================================================
170 // Explicit template instantiation
171 // =========================================================
172 
173 #ifndef LBANN_INSTANCE_NORM_LAYER_INSTANTIATE
174 #define PROTO_DEVICE(T, Device) \
175  extern template class instance_norm_layer<T, \
176  data_layout::DATA_PARALLEL, \
177  Device>;
179 #undef PROTO_DEVICE
180 #endif // LBANN_INSTANCE_NORM_LAYER_INSTANTIATE
181 
182 } // namespace lbann
183 
184 #endif // LBANN_LAYERS_REGULARIZERS_INSTANCE_NORM_HPP_INCLUDED
void write_specific_proto(lbann_data::Layer &proto) const final
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.
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
instance_norm_layer & operator=(const instance_norm_layer &other)=default
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.
std::vector< int > get_input_dims(size_t input_index=0) const
Get input tensor dimensions.
Generates nicely formatted description messages.
Definition: description.hpp:49
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.
virtual description get_description() const
Human-readable description.
constexpr El::Device Device
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
instance_norm_layer(TensorDataType epsilon=El::To< TensorDataType >(1e-5))
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
El::Matrix< TensorDataType, Device > m_workspace
std::string get_type() const override
Get the layer type&#39;s name.
Normalize over data channels.
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
description get_description() const override
Human-readable description.
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
instance_norm_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
LBANN_DEFINE_LAYER_BUILDER(elu)
void serialize(ArchiveT &ar)
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...