|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +// 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. <[email protected]> |
| 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. |
| 25 | +// |
| 26 | +// gradient_clipping .hpp .cpp - Callbacks to clip gradient values in training |
| 27 | +//////////////////////////////////////////////////////////////////////////////// |
| 28 | + |
| 29 | +#ifndef LBANN_CALLBACKS_CALLBACK_GRADIENT_CLIPPING_HPP_INCLUDED |
| 30 | +#define LBANN_CALLBACKS_CALLBACK_GRADIENT_CLIPPING_HPP_INCLUDED |
| 31 | + |
| 32 | +#include <unordered_set> |
| 33 | +#include <utility> |
| 34 | + |
| 35 | +#include "lbann/callbacks/callback.hpp" |
| 36 | + |
| 37 | +namespace lbann { |
| 38 | +namespace callback { |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief Clip gradients whose norm is larger than a user-defined value by |
| 42 | + * dividing them. |
| 43 | + */ |
| 44 | +class clip_gradient_norm : public callback_base |
| 45 | +{ |
| 46 | +public: |
| 47 | + using callback_base::on_backward_prop_end; |
| 48 | + |
| 49 | + /** |
| 50 | + * @param weights Parameters whose gradient to clip, or empty for all |
| 51 | + * @param global_norm Whether to clip according to the norm of all parameters |
| 52 | + * or each one separately |
| 53 | + * @param value Value to clip to |
| 54 | + */ |
| 55 | + clip_gradient_norm(std::vector<std::string> weights, |
| 56 | + bool global_norm = false, |
| 57 | + float value = 1.0f) |
| 58 | + : callback_base(1), |
| 59 | + m_weight_names(std::move(weights)), |
| 60 | + m_global_norm(global_norm), |
| 61 | + m_value(value) |
| 62 | + {} |
| 63 | + clip_gradient_norm(const clip_gradient_norm&) = default; |
| 64 | + clip_gradient_norm& operator=(const clip_gradient_norm&) = default; |
| 65 | + void setup(model* m) override; |
| 66 | + clip_gradient_norm* copy() const override |
| 67 | + { |
| 68 | + return new clip_gradient_norm(*this); |
| 69 | + } |
| 70 | + void on_backward_prop_end(model* m) override; |
| 71 | + std::string name() const override { return "clip gradient norm"; } |
| 72 | + |
| 73 | + /** @name Serialization */ |
| 74 | + ///@{ |
| 75 | + |
| 76 | + /** @brief Store state to archive for checkpoint and restart */ |
| 77 | + template <class Archive> |
| 78 | + void serialize(Archive& ar); |
| 79 | + |
| 80 | + ///@} |
| 81 | + |
| 82 | +private: |
| 83 | + /** Add callback specific data to prototext */ |
| 84 | + void write_specific_proto(lbann_data::Callback& proto) const final; |
| 85 | + |
| 86 | + friend class cereal::access; |
| 87 | + clip_gradient_norm(); |
| 88 | + |
| 89 | + /** @brief Parameter names whose gradients to clip. */ |
| 90 | + std::vector<std::string> m_weight_names; |
| 91 | + |
| 92 | + /** @brief Whether to clip according to the norm of all parameters. */ |
| 93 | + bool m_global_norm; |
| 94 | + |
| 95 | + /** @brief Value to clip to. */ |
| 96 | + float m_value; |
| 97 | + |
| 98 | + /** Weights to update. */ |
| 99 | + std::unordered_set<weights*> m_weights; |
| 100 | +}; |
| 101 | + |
| 102 | +// Builder function |
| 103 | +std::unique_ptr<callback_base> build_clip_gradient_norm_callback_from_pbuf( |
| 104 | + const google::protobuf::Message&, |
| 105 | + std::shared_ptr<lbann_summary> const&); |
| 106 | + |
| 107 | +} // namespace callback |
| 108 | +} // namespace lbann |
| 109 | + |
| 110 | +#endif // LBANN_CALLBACKS_CALLBACK_GRADIENT_CLIPPING_HPP_INCLUDED |
0 commit comments