LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
clamp.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_OPERATORS_MATH_CLAMP_HPP_INCLUDED
28 #define LBANN_OPERATORS_MATH_CLAMP_HPP_INCLUDED
29 
30 #include "lbann_config.hpp"
31 
34 
35 #include "lbann/proto/operators.pb.h"
36 
37 #include <h2/meta/Core.hpp>
38 
39 namespace lbann {
40 
52 template <typename DataT, El::Device D>
53 class ClampOperator final
54  : public Cloneable<ClampOperator<DataT, D>,
55  ElementwiseOperator<DataT, DataT, D>>
56 {
57 #ifdef LBANN_HAS_GPU_FP16
58  using CompareType =
59  h2::meta::IfThenElse<std::is_same_v<DataT, fp16>, float, DataT>;
60 #else
61  using CompareType = DataT;
62 #endif
63 
65 
67  using BaseType =
69 
70  using LocalInputTensorType = typename BaseType::LocalInputTensorType;
71  using LocalOutputTensorType = typename BaseType::LocalOutputTensorType;
73  typename BaseType::ConstLocalInputTensorType;
75  typename BaseType::ConstLocalOutputTensorType;
76 
78 
79 public:
81 
83  ClampOperator(double min, double max)
84  : m_min{El::To<DataT>(min)}, m_max{El::To<DataT>(max)}
85  {
87  }
88  ClampOperator(ClampOperator&&) = default;
89  ClampOperator(ClampOperator const&) = default;
90 
92  ClampOperator& operator=(ClampOperator const&) = default;
93 
94  ~ClampOperator() = default;
95 
97 
98 
100  std::string get_type() const final { return "clamp"; }
101  int get_backprop_requirements() const final
102  {
104  }
105  DataT get_min() const noexcept { return m_min; }
106  DataT get_max() const noexcept { return m_max; }
107 
109 
110 
112  template <typename ArchiveT>
113  void serialize(ArchiveT& ar)
114  {
115  using OperatorType = ElementwiseOperator<DataT, DataT, D>;
116  ar(::cereal::make_nvp("ElementwiseOperator",
117  ::cereal::base_class<OperatorType>(this)),
118  CEREAL_NVP(m_min),
119  CEREAL_NVP(m_max));
120  }
121 
123 
124 protected:
125  friend class cereal::access;
126  ClampOperator() : ClampOperator(El::To<DataT>(0), El::To<DataT>(1)) {}
127 
128 private:
130  void fp_compute_local(std::vector<ConstLocalInputTensorType> input,
131  std::vector<LocalOutputTensorType> output) const final;
132 
134  void bp_compute_local(
135  std::vector<ConstLocalInputTensorType> input,
136  std::vector<ConstLocalOutputTensorType> gradient_wrt_output,
137  std::vector<LocalInputTensorType> gradient_wrt_input) const final;
138 
139  void set_proto_params(lbann_data::Operator& msg) const final
140  {
141  lbann_data::ClampOperator clamp_msg;
142  clamp_msg.set_min(m_min);
143  clamp_msg.set_max(m_max);
144  msg.mutable_parameters()->PackFrom(clamp_msg);
145  }
146 
147  void do_fill_description(description& desc) const final
148  {
149  std::ostringstream oss;
150  oss << "[" << m_min << "," << m_max << "]";
151  desc.add("Range", oss.str());
152  }
153 
154 private:
156  DataT m_min;
158  DataT m_max;
159 }; // class ClampOperator
160 
161 #ifndef LBANN_CLAMP_OPERATOR_INSTANTIATE
162 #define PROTO_DEVICE(T, D) extern template class ClampOperator<T, D>
164 #undef PROTO_DEVICE
165 #endif // LBANN_CLAMP_OPERATOR_INSTANTIATE
166 
167 } // namespace lbann
168 
169 #endif // LBANN_OPERATORS_MATH_CLAMP_HPP_INCLUDED
void bp_compute_local(std::vector< ConstLocalInputTensorType > input, std::vector< ConstLocalOutputTensorType > gradient_wrt_output, std::vector< LocalInputTensorType > gradient_wrt_input) const final
Local backward compute function.
void fp_compute_local(std::vector< ConstLocalInputTensorType > input, std::vector< LocalOutputTensorType > output) const final
Local forward compute function.
Element-wise specific tensor operation sub-class.
int get_backprop_requirements() const final
Definition: clamp.hpp:101
Inject polymorphic clone functions into hierarchies.
Definition: cloneable.hpp:94
ClampOperator(double min, double max)
Definition: clamp.hpp:83
Generates nicely formatted description messages.
Definition: description.hpp:49
Constrain values to a range.
Definition: clamp.hpp:53
typename BaseType::LocalOutputTensorType LocalOutputTensorType
Definition: clamp.hpp:71
DataT get_max() const noexcept
Definition: clamp.hpp:106
#define LBANN_ASSERT(cond)
Definition: exception.hpp:97
typename BaseType::ConstLocalInputTensorType ConstLocalInputTensorType
Definition: clamp.hpp:73
std::string get_type() const final
Definition: clamp.hpp:100
friend class cereal::access
Definition: clamp.hpp:125
void do_fill_description(description &desc) const final
Definition: clamp.hpp:147
void serialize(ArchiveT &ar)
Definition: clamp.hpp:113
typename BaseType::LocalInputTensorType LocalInputTensorType
Definition: clamp.hpp:70
DataT get_min() const noexcept
Definition: clamp.hpp:105
typename BaseType::ConstLocalOutputTensorType ConstLocalOutputTensorType
Definition: clamp.hpp:75
void set_proto_params(lbann_data::Operator &msg) const final
Definition: clamp.hpp:139
~ClampOperator()=default
ClampOperator & operator=(ClampOperator &&)=default