You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

callback.h 3.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_INCLUDE_API_CALLBACK_CALLBACK_H
  17. #define MINDSPORE_INCLUDE_API_CALLBACK_CALLBACK_H
  18. #include <cstddef>
  19. #include <string>
  20. #include <vector>
  21. #include <memory>
  22. #include "include/api/data_type.h"
  23. #include "include/api/dual_abi_helper.h"
  24. namespace mindspore {
  25. class Model;
  26. class ModelImpl;
  27. class CallbackImpl;
  28. struct TrainCallBackData {
  29. TrainCallBackData(bool train_mode, int epoch, int step, Model *model): train_mode_(train_mode), epoch_(epoch),
  30. step_(step), model_(model) {}
  31. bool train_mode_; /**< training mode of LiteSession object */
  32. unsigned int epoch_; /**< the current training epoch (starts at 0) */
  33. unsigned int step_ = 0; /**< the current step within the epoch */
  34. Model *model_; /**< pointer to the Model object */
  35. };
  36. enum CallbackRetValue : uint32_t {
  37. kContinue = 0,
  38. kStopTraining = 1,
  39. kExit = 2,
  40. kUnknownRetValue = 0xFFFFFFFF
  41. };
  42. class TrainCallBack {
  43. public:
  44. virtual ~TrainCallBack() = default;
  45. /// \brief This method is called once before the network executing
  46. ///
  47. /// \param[in] cb_data info about current execution
  48. virtual void Begin(const TrainCallBackData &cb_data) {}
  49. /// \brief This method is called once following the network execution
  50. ///
  51. /// \param[in] cb_data info about current execution
  52. virtual void End(const TrainCallBackData &cb_data) {}
  53. /// \brief This method is called at the beginning of each epoch
  54. ///
  55. /// \param[in] cb_data info about current execution
  56. virtual void EpochBegin(const TrainCallBackData &cb_data) {}
  57. /// \brief This method is called after the run of each epoch
  58. ///
  59. /// \param[in] cb_data info about current execution
  60. ///
  61. /// \return indication if to continue in the train loop:
  62. /// RET_CONTINUE -- continue training
  63. /// RET_STOP_TRAINING -- stop training (e.g., due to achieved accuracy)
  64. /// RET_EXIT -- Exit training (due to error of some sort)
  65. virtual CallbackRetValue EpochEnd(const TrainCallBackData &cb_data) { return kContinue; }
  66. /// \brief This method is called at the beginning of each step
  67. ///
  68. /// \param[in] cb_data info about current execution
  69. virtual void StepBegin(const TrainCallBackData &cb_data) {}
  70. /// \brief This method is called after each step is ran
  71. ///
  72. /// \param[in] cb_data info about current execution
  73. virtual void StepEnd(const TrainCallBackData &cb_data) {}
  74. protected:
  75. friend class Model;
  76. friend class ModelImpl;
  77. CallbackImpl* callback_impl_ = nullptr;
  78. };
  79. } // namespace mindspore
  80. #endif // MINDSPORE_INCLUDE_API_CALLBACK_CALLBACK_H