LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
initializer.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_WEIGHTS_INITIALIZER_HPP
28 #define LBANN_WEIGHTS_INITIALIZER_HPP
29 
30 #include "lbann/base.hpp"
33 
34 #include <google/protobuf/message.h>
35 
36 // Forward declarations
37 namespace lbann_data {
38 class Initializer;
39 }
40 namespace lbann {
41 
44  : public Cloneable<HasAbstractFunction<weights_initializer>>
45 {
46 public:
47  weights_initializer() = default;
48  virtual ~weights_initializer() = default;
49 
51  virtual std::string get_type() const = 0;
52 
54  virtual description get_description() const;
55 };
56 
58 template <typename TensorDataType>
60  : public Cloneable<
61  HasAbstractFunction<data_type_weights_initializer<TensorDataType>>,
62  weights_initializer>
63 {
64 public:
66 
69  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
70 
72 
73 public:
75  virtual ~data_type_weights_initializer() = default;
76 
78  std::string get_type() const override { return "data_type_weights"; }
79 
81  virtual void fill(AbsDistMatrixType& matrix) = 0;
82 
84  virtual void write_proto(lbann_data::Initializer& proto) const = 0;
85 };
86 
88 template <typename TensorDataType>
90  : public Cloneable<constant_initializer<TensorDataType>,
91  data_type_weights_initializer<TensorDataType>>
92 {
93 public:
95 
98  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
99 
101 
102 public:
103  constant_initializer(TensorDataType value) : m_value(value) {}
104  std::string get_type() const override { return "constant"; }
105  description get_description() const override;
106  void fill(AbsDistMatrixType& matrix) override;
107 
109  void write_proto(lbann_data::Initializer& init) const final;
110 
111 private:
113  TensorDataType m_value;
114 };
115 
124 template <typename TensorDataType>
126  : public Cloneable<value_initializer<TensorDataType>,
127  data_type_weights_initializer<TensorDataType>>
128 {
129 public:
131 
134  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
135 
137 
138 public:
139  value_initializer(std::vector<TensorDataType> values)
140  : m_values{std::move(values)}
141  {}
142  std::string get_type() const override { return "value"; }
143  void fill(AbsDistMatrixType& matrix) override;
144 
146  void write_proto(lbann_data::Initializer& init) const final;
147 
148 private:
150  std::vector<TensorDataType> m_values;
151 };
152 
158 template <typename TensorDataType>
160  : public Cloneable<numpy_initializer<TensorDataType>,
161  data_type_weights_initializer<TensorDataType>>
162 {
163 public:
165 
168  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
169 
171 
172 public:
173  numpy_initializer(std::string file) : m_file{std::move(file)} {}
174  std::string get_type() const override { return "NumPy"; }
175  void fill(AbsDistMatrixType& matrix) override;
176 
178  void write_proto(lbann_data::Initializer& init) const final;
179 
180 private:
182  std::string m_file;
183 };
184 
186 template <typename TensorDataType>
188  : public Cloneable<uniform_initializer<TensorDataType>,
189  data_type_weights_initializer<TensorDataType>>
190 {
191 public:
193 
196  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
197 
199 
200 public:
201  uniform_initializer(TensorDataType min = El::To<TensorDataType>(0),
202  TensorDataType max = El::To<TensorDataType>(1))
203  : m_min{min}, m_max{max}
204  {}
205  std::string get_type() const override { return "uniform"; }
206  description get_description() const override;
207  void fill(AbsDistMatrixType& matrix) override;
208 
210  void write_proto(lbann_data::Initializer& init) const final;
211 
212 private:
214  TensorDataType m_min;
216  TensorDataType m_max;
217 };
218 
220 template <typename TensorDataType>
222  : public Cloneable<normal_initializer<TensorDataType>,
223  data_type_weights_initializer<TensorDataType>>
224 {
225 public:
227 
230  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
231 
233 
234 public:
236  TensorDataType mean = El::TypeTraits<TensorDataType>::Zero(),
237  TensorDataType standard_deviation = El::TypeTraits<TensorDataType>::One())
238  : m_mean{mean}, m_standard_deviation{standard_deviation}
239  {}
240  std::string get_type() const override { return "normal"; }
241  description get_description() const override;
242  void fill(AbsDistMatrixType& matrix) override;
243 
245  void write_proto(lbann_data::Initializer& init) const final;
246 
247 private:
249  TensorDataType m_mean;
251  TensorDataType m_standard_deviation;
252 };
253 
254 template <typename TensorDataType>
255 std::unique_ptr<weights_initializer>
256 build_constant_initializer_from_pbuf(google::protobuf::Message const& msg);
257 
258 template <typename TensorDataType>
259 std::unique_ptr<weights_initializer>
260 build_value_initializer_from_pbuf(google::protobuf::Message const& msg);
261 
262 template <typename TensorDataType>
263 std::unique_ptr<weights_initializer>
264 build_numpy_initializer_from_pbuf(google::protobuf::Message const& msg);
265 
266 template <typename TensorDataType>
267 std::unique_ptr<weights_initializer>
268 build_uniform_initializer_from_pbuf(google::protobuf::Message const& msg);
269 
270 template <typename TensorDataType>
271 std::unique_ptr<weights_initializer>
272 build_normal_initializer_from_pbuf(google::protobuf::Message const& msg);
273 
274 #ifndef LBANN_INITIALIZER_INSTANTIATE
275 #define PROTO(T) \
276  extern template class data_type_weights_initializer<T>; \
277  extern template class constant_initializer<T>; \
278  extern template class value_initializer<T>; \
279  extern template class numpy_initializer<T>; \
280  extern template class uniform_initializer<T>; \
281  extern template class normal_initializer<T>
282 
283 #define LBANN_INSTANTIATE_CPU_HALF
284 #define LBANN_INSTANTIATE_GPU_HALF
286 #undef PROTO
287 #undef LBANN_INSTANTIATE_CPU_HALF
288 #undef LBANN_INSTANTIATE_GPU_HALF
289 #endif // LBANN_INITIALIZER_INSTANTIATE
290 
291 } // namespace lbann
292 
293 #endif // LBANN_WEIGHTS_INITIALIZER_HPP
std::unique_ptr< weights_initializer > build_uniform_initializer_from_pbuf(google::protobuf::Message const &msg)
Fill weights with values from a NumPy file.
void fill(std::istream &is, google::protobuf::Message &msg)
Fill the protobuf message from a binary stream.
normal_initializer(TensorDataType mean=El::TypeTraits< TensorDataType >::Zero(), TensorDataType standard_deviation=El::TypeTraits< TensorDataType >::One())
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
Inject polymorphic clone functions into hierarchies.
Definition: cloneable.hpp:94
numpy_initializer(std::string file)
std::string get_type() const override
Scheme for initializing weight values.
Definition: initializer.hpp:59
Generates nicely formatted description messages.
Definition: description.hpp:49
value_initializer(std::vector< TensorDataType > values)
std::string get_type() const override
Fill weights with a single constant value.
Definition: initializer.hpp:89
std::unique_ptr< weights_initializer > build_constant_initializer_from_pbuf(google::protobuf::Message const &msg)
uniform_initializer(TensorDataType min=El::To< TensorDataType >(0), TensorDataType max=El::To< TensorDataType >(1))
std::unique_ptr< weights_initializer > build_numpy_initializer_from_pbuf(google::protobuf::Message const &msg)
Fill weights with values from a list.
TensorDataType m_standard_deviation
Scheme for initializing weight values.
Definition: initializer.hpp:43
Draw weights values from a normal random distribution.
std::vector< TensorDataType > m_values
Draw weights values from a uniform random distribution.
std::string get_type() const override
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
Definition: initializer.hpp:98
std::string get_type() const override
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
Definition: initializer.hpp:69
std::unique_ptr< weights_initializer > build_normal_initializer_from_pbuf(google::protobuf::Message const &msg)
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
constant_initializer(TensorDataType value)
std::string get_type() const override
Definition: initializer.hpp:78
std::string get_type() const override
std::unique_ptr< weights_initializer > build_value_initializer_from_pbuf(google::protobuf::Message const &msg)