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

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