LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
sgd.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_OPTIMIZERS_SGD_HPP_INCLUDED
28 #define LBANN_OPTIMIZERS_SGD_HPP_INCLUDED
29 
30 #include "lbann/io/persist.hpp"
32 #include "lbann/proto/optimizers.pb.h"
33 
34 namespace lbann {
35 
40 template <typename TensorDataType>
41 class sgd
42  : public Cloneable<sgd<TensorDataType>, data_type_optimizer<TensorDataType>>
43 {
44  using BaseType =
46 
47 public:
49 
52  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
53 
55  using OptimizerType = data_type_optimizer<TensorDataType>;
56 
59 
61 
62 public:
64 
66  sgd(TensorDataType learning_rate,
67  TensorDataType momentum = 0,
68  bool nesterov = false);
69  sgd(const sgd& other);
70  sgd& operator=(const sgd& other);
71  ~sgd() override = default;
73 
75 
78  template <class ArchiveT>
79  void serialize(ArchiveT& ar);
80 
82 
83 
86  std::string get_type() const override { return "SGD"; }
88  description get_description() const override;
89 
91 
93 
98  TensorDataType get_momentum() const noexcept { return m_momentum; }
102  void set_momentum(TensorDataType momentum) { m_momentum = momentum; }
103 
105  bool using_nesterov() const noexcept { return m_nesterov; }
107  void set_nesterov(bool nesterov) { m_nesterov = nesterov; }
108 
110  const AbsDistMatrixType& get_velocity() const;
113 
115 
116 
118  using OptimizerType::setup;
119  void setup(WeightsType* w = nullptr) override;
120 
122 
124  void write_proto(lbann_data::Optimizer& opt) const final;
125 
126 protected:
131  sgd() : sgd(El::To<TensorDataType>(0.f), El::To<TensorDataType>(0.f), false)
132  {}
133 
135  void step_compute(AbsDistMatrixType& values,
136  const AbsDistMatrixType& gradient) override;
137 
138 private:
142  TensorDataType m_momentum;
148  std::unique_ptr<AbsDistMatrixType> m_velocity;
149 
152  const AbsDistMatrixType& gradient);
153 #ifdef LBANN_HAS_GPU
154 
155  void momentum_step_gpu(AbsDistMatrixType& values,
156  const AbsDistMatrixType& gradient);
157 #endif // LBANN_HAS_GPU
158 
159  friend class cereal::access;
160 };
161 
162 template <typename TensorDataType>
163 std::unique_ptr<optimizer>
164 build_sgd_optimizer_from_pbuf(google::protobuf::Message const&);
165 
166 } // namespace lbann
167 
168 #endif // LBANN_OPTIMIZERS_SGD_HPP_INCLUDED
void serialize(ArchiveT &ar)
Serialize to the archive.
Definition: sgd_impl.hpp:37
std::unique_ptr< optimizer > build_sgd_optimizer_from_pbuf(google::protobuf::Message const &)
Inject polymorphic clone functions into hierarchies.
Definition: cloneable.hpp:94
friend class cereal::access
Definition: sgd.hpp:159
~sgd() override=default
void setup(weights *w) override
Must be called before training.
TensorDataType m_momentum
Decay rate for gradient accumulation.
Definition: sgd.hpp:142
description get_description() const override
void set_momentum(TensorDataType momentum)
Decay rate for gradient accumulation.
Definition: sgd.hpp:102
void setup(WeightsType *w=nullptr) override
Generates nicely formatted description messages.
Definition: description.hpp:49
std::unique_ptr< AbsDistMatrixType > m_velocity
Accumulated gradients.
Definition: sgd.hpp:148
void momentum_step_cpu(AbsDistMatrixType &values, const AbsDistMatrixType &gradient)
std::string get_type() const override
Definition: sgd.hpp:86
void step_compute(AbsDistMatrixType &values, const AbsDistMatrixType &gradient) override
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
Definition: sgd.hpp:52
TensorDataType get_momentum() const noexcept
Decay rate for gradient accumulation.
Definition: sgd.hpp:98
void write_proto(lbann_data::Optimizer &opt) const final
sgd & operator=(const sgd &other)
bool using_nesterov() const noexcept
Definition: sgd.hpp:105
void set_nesterov(bool nesterov)
Definition: sgd.hpp:107
bool m_nesterov
Definition: sgd.hpp:144
sgd()
Default constructor.
Definition: sgd.hpp:131
const AbsDistMatrixType & get_velocity() const
Stochastic gradient descent optimizer.
Definition: sgd.hpp:41