1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_CFG_H_ 17 #define MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_CFG_H_ 18 #include <string> 19 #include <vector> 20 21 namespace mindspore { 22 namespace lite { 23 24 /// \brief MixPrecisionCfg defined for holding mix precision training configuration. 25 class MixPrecisionCfg { 26 public: MixPrecisionCfg()27 MixPrecisionCfg() { 28 this->dynamic_loss_scale_ = false; 29 this->loss_scale_ = 128.0f; 30 this->keep_batchnorm_fp32_ = true; 31 this->num_of_not_nan_iter_th_ = 1000; 32 } MixPrecisionCfg(const MixPrecisionCfg & rhs)33 MixPrecisionCfg(const MixPrecisionCfg &rhs) { 34 this->dynamic_loss_scale_ = rhs.dynamic_loss_scale_; 35 this->loss_scale_ = rhs.loss_scale_; 36 this->keep_batchnorm_fp32_ = rhs.keep_batchnorm_fp32_; 37 this->num_of_not_nan_iter_th_ = rhs.num_of_not_nan_iter_th_; 38 } 39 MixPrecisionCfg &operator=(MixPrecisionCfg const &rhs) = default; 40 bool dynamic_loss_scale_ = false; /**< Enable\disable dynamic loss scale during mix precision training */ 41 float loss_scale_; /**< Initial loss scale factor */ 42 bool keep_batchnorm_fp32_ = true; /**< Keep batch norm in FP32 while training */ 43 uint32_t num_of_not_nan_iter_th_; /**< a threshold for modifying loss scale when dynamic loss scale is enabled */ 44 }; 45 46 /// \brief TrainCfg defined for holding train configuration. 47 class TrainCfg { 48 public: TrainCfg()49 TrainCfg() { 50 this->loss_name_.emplace_back("_loss_fn"); 51 this->loss_name_.emplace_back("SigmoidCrossEntropy"); 52 } TrainCfg(const TrainCfg & rhs)53 TrainCfg(const TrainCfg &rhs) { 54 this->loss_name_ = rhs.loss_name_; 55 this->mix_precision_cfg_ = rhs.mix_precision_cfg_; 56 this->accumulate_gradients_ = rhs.accumulate_gradients_; 57 } 58 TrainCfg &operator=(const TrainCfg &rhs) = default; 59 std::vector<std::string> loss_name_ = {"loss_fct"}; /**< Set part of the name that identify a loss kernel */ 60 MixPrecisionCfg mix_precision_cfg_; /**< Mix precision configuration */ 61 bool accumulate_gradients_ = false; /**< If true gardents are accmulated and can be read by GetGradients */ 62 }; 63 64 } // namespace lite 65 } // namespace mindspore 66 #endif // MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_CFG_H_ 67