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.

tree_modifier.h 5.5 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_CCSRC_MINDDATA_DATASET_ENGINE_TREE_MODIFIER_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_TREE_MODIFIER_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <unordered_map>
  22. #include <utility>
  23. #include <vector>
  24. #include "minddata/dataset/engine/execution_tree.h"
  25. #include "minddata/dataset/engine/tree_adapter.h"
  26. namespace mindspore {
  27. namespace dataset {
  28. class DatasetNode;
  29. /// A pure virtual class to be used as a base for all pipeline modification requests.
  30. class ChangeRequest {
  31. public:
  32. /// Default constructor
  33. ChangeRequest() = default;
  34. /// Pure virtual method. Subclasses should override this function and implement the actual change to the give
  35. /// operator.
  36. /// \param op pointer to the operator that the change will be applied on
  37. /// \return Status return Status code
  38. virtual Status ApplyChange(DatasetOp *op) = 0;
  39. };
  40. using ChangeRequestPtr = std::shared_ptr<ChangeRequest>;
  41. /// ChangeRequest to add n workers to an operator.
  42. class ChangeNumWorkersRequest : public ChangeRequest {
  43. public:
  44. /// Constructor
  45. /// \param num_workers number of workeres to be added to the opertor. Default to 1.
  46. explicit ChangeNumWorkersRequest(int32_t num_workers = 1) : num_workers_(num_workers) {}
  47. virtual ~ChangeNumWorkersRequest() = default;
  48. /// Actual change to add n workers
  49. /// \param op pointer to the operator that the change will be applied on
  50. /// \return Status return Status code
  51. Status ApplyChange(DatasetOp *op) override;
  52. private:
  53. int32_t num_workers_;
  54. };
  55. /// ChangeRequest to change the size of the oupout connector of an operator.
  56. class ResizeConnectorRequest : public ChangeRequest {
  57. public:
  58. /// Constructor
  59. /// \param new_size new queue size.
  60. explicit ResizeConnectorRequest(int32_t new_size) : new_size_(new_size) {}
  61. virtual ~ResizeConnectorRequest() = default;
  62. /// Actual change to resize the output connector of the given operator
  63. /// \param op pointer to the operator that the change will be applied on
  64. /// \return Status return Status code
  65. Status ApplyChange(DatasetOp *op) override {
  66. RETURN_IF_NOT_OK(op->OutputConnector()->Resize(new_size_));
  67. return Status::OK();
  68. }
  69. private:
  70. int32_t new_size_;
  71. };
  72. /// A callback class used by Aututune to queue changes for opertors
  73. class AutotuneCallback : public DSCallback {
  74. public:
  75. AutotuneCallback(int32_t step_size, DatasetOp *op)
  76. : DSCallback(step_size), op_(op), change_request_queue_(std::make_unique<Queue<ChangeRequestPtr>>(10)) {}
  77. virtual ~AutotuneCallback() = default;
  78. Status DSNStepBegin(const CallbackParam &cb_param) override;
  79. Status DSBegin(const CallbackParam &cb_param) override;
  80. Status DSEpochBegin(const CallbackParam &cb_param) override;
  81. Status DSEnd(const CallbackParam &cb_param) override;
  82. Status DSEpochEnd(const CallbackParam &cb_param) override;
  83. Status DSNStepEnd(const CallbackParam &cb_param) override;
  84. bool IsBeginNeeded() override;
  85. bool IsEpochBeginNeeded() override;
  86. bool IsNStepBeginNeeded() override;
  87. bool IsEndNeeded() override;
  88. bool IsEpochEndNeeded() override;
  89. bool IsNStepEndNeeded() override;
  90. /// Push a change request to the queue of the callback.
  91. /// \param change_request Shared pointer to the change request to be pushed to the queue.
  92. /// \return Status return Status code
  93. Status PushChangeRequest(ChangeRequestPtr change_request);
  94. private:
  95. DatasetOp *op_;
  96. std::unique_ptr<Queue<ChangeRequestPtr>> change_request_queue_;
  97. };
  98. /// Main class to handle modification of the ExecutionTree used by AutoTune
  99. class TreeModifier {
  100. // friend with TreeAdapter to access the ExecutionTree
  101. friend TreeAdapter;
  102. public:
  103. /// Constructor to create a TreeModifier given a TreeAdapter
  104. /// \param adapter TreeAdapter
  105. explicit TreeModifier(TreeAdapter *adapter);
  106. /// Constructor to create a TreeModifier given an ExecutionTree
  107. /// \param tree ExecutionTree
  108. explicit TreeModifier(ExecutionTree *tree) : tree_(tree) {
  109. // loop over all ops to create AutotuneCallback and register it.
  110. for (auto itr = tree_->begin(); itr != tree_->end(); ++itr) {
  111. auto cb = std::make_shared<AutotuneCallback>(1, itr.get().get());
  112. itr->AddCallbacks({cb});
  113. callbacks.insert(std::make_pair(itr->id(), cb));
  114. }
  115. }
  116. /// Add changeRequest to the callback associated with the op.
  117. /// \param op_id Operator ID
  118. /// \param change_request Pointer to the change request
  119. /// \return Status return Status code
  120. Status AddChangeRequest(int32_t op_id, ChangeRequestPtr change_request) {
  121. RETURN_IF_NOT_OK(callbacks[op_id]->PushChangeRequest(change_request));
  122. return Status::OK();
  123. }
  124. private:
  125. ExecutionTree *tree_;
  126. std::map<int32_t, std::shared_ptr<AutotuneCallback>> callbacks;
  127. };
  128. } // namespace dataset
  129. } // namespace mindspore
  130. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_TREE_MODIFIER_H_