LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
binary_with_constant.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_INCLUDE_LBANN_OPERATORS_BINARY_WITH_CONSTANT_HPP_INCLUDED
28 #define LBANN_INCLUDE_LBANN_OPERATORS_BINARY_WITH_CONSTANT_HPP_INCLUDED
29 
30 #include "lbann_config.hpp"
31 
34 
35 #include "lbann/proto/operators.pb.h"
36 
50 
51 #include "lbann/proto/operators.pb.h"
52 
53 // These are all single-type operators.
54 
55 #define LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(OP_NAME, \
56  OP_STRING, \
57  NEEDS_PREVACTS) \
58  template <typename DataT, El::Device D> \
59  class OP_NAME##Operator final \
60  : public Cloneable<OP_NAME##Operator<DataT, D>, \
61  ElementwiseOperator<DataT, DataT, D>> \
62  { \
63  using BaseType = Cloneable<OP_NAME##Operator<DataT, D>, \
64  ElementwiseOperator<DataT, DataT, D>>; \
65  using LocalInputTensorType = typename BaseType::LocalInputTensorType; \
66  using LocalOutputTensorType = typename BaseType::LocalOutputTensorType; \
67  using ConstLocalInputTensorType = \
68  typename BaseType::ConstLocalInputTensorType; \
69  using ConstLocalOutputTensorType = \
70  typename BaseType::ConstLocalOutputTensorType; \
71  \
72  public: \
73  OP_NAME##Operator(double constant = 0.) \
74  : m_constant{El::To<DataT>(constant)} \
75  {} \
76  OP_NAME##Operator(OP_NAME##Operator&&) = default; \
77  OP_NAME##Operator(OP_NAME##Operator const&) = default; \
78  OP_NAME##Operator& operator=(OP_NAME##Operator&&) = default; \
79  OP_NAME##Operator& operator=(OP_NAME##Operator const&) = default; \
80  ~OP_NAME##Operator() = default; \
81  std::string get_type() const final { return OP_STRING; } \
82  int get_backprop_requirements() const final \
83  { \
84  return ((NEEDS_PREVACTS) ? (ERROR_SIGNALS | PREV_ACTIVATIONS) \
85  : ERROR_SIGNALS); \
86  } \
87  template <typename ArchiveT> \
88  void serialize(ArchiveT& ar) \
89  { \
90  using OperatorType = ElementwiseOperator<DataT, DataT, D>; \
91  ar(::cereal::make_nvp("ElementwiseOperator", \
92  ::cereal::base_class<OperatorType>(this)), \
93  CEREAL_NVP(m_constant)); \
94  } \
95  DataT get_constant() const noexcept { return m_constant; } \
96  \
97  private: \
98  void \
99  fp_compute_local(std::vector<ConstLocalInputTensorType> inputs, \
100  std::vector<LocalOutputTensorType> outputs) const final; \
101  void bp_compute_local( \
102  std::vector<ConstLocalInputTensorType> inputs, \
103  std::vector<ConstLocalOutputTensorType> grads_wrt_outputs, \
104  std::vector<LocalInputTensorType> grads_wrt_inputs) const final; \
105  void set_proto_params(lbann_data::Operator& msg) const final \
106  { \
107  lbann_data::OP_NAME##Operator op_msg; \
108  op_msg.set_constant(m_constant); \
109  msg.mutable_parameters()->PackFrom(op_msg); \
110  } \
111  void do_fill_description(description& desc) const final \
112  { \
113  std::ostringstream oss; \
114  oss << m_constant; \
115  desc.add("Constant", oss.str()); \
116  } \
117  \
118  private: \
119  DataT m_constant; \
120  }
121 
122 namespace lbann {
123 
124 // x + c -- treated as commutative.
125 LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(AddConstant, "add constant", false);
126 
127 // x + c -- treated as commutative.
128 LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(Scale, "scale", false);
129 
130 // x - C -- yes, could be "plus -C", but so could 7-4 be 7+-4, but
131 // nobody writes that.
133  "subtract constant",
134  false);
135 // C - x -- yes, could be "negative-x plus C", but again, why write
136 // -4+7 when you could just write 7-4...
138  "subtract from constant",
139  false);
140 
141 LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(MaxConstant, "max constant", true);
142 LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(MinConstant, "min constant", true);
143 
145  "equals constant",
146  false);
148  "not equals constant",
149  false);
151  "less-equals constant",
152  false);
154  "less than constant",
155  false);
157  "greater-equals constant",
158  false);
160  "greater than constant",
161  false);
162 
163 } // namespace lbann
164 #endif // LBANN_INCLUDE_LBANN_OPERATORS_BINARY_WITH_CONSTANT_HPP_INCLUDED
LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(AddConstant, "add constant", false)