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.

strategy.h 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright 2019 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_PARALLEL_STRATEGY_H_
  17. #define MINDSPORE_CCSRC_PARALLEL_STRATEGY_H_
  18. #include <cstdint>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include "parallel/status.h"
  24. namespace mindspore {
  25. namespace parallel {
  26. #define MIN_SLICE_NUM 1
  27. using Dimensions = std::vector<int32_t>;
  28. class Strategy;
  29. using StrategyPtr = std::shared_ptr<Strategy>;
  30. class Strategy {
  31. public:
  32. Strategy(int32_t stage, std::vector<Dimensions> inputs) : stage_(stage), inputs_(std::move(inputs)) {}
  33. ~Strategy() = default;
  34. size_t GetInputNumber() const { return inputs_.size(); }
  35. std::vector<Dimensions> GetInputDim() const { return inputs_; }
  36. int32_t GetInputStage() const { return stage_; }
  37. void ExpandInputDimFromOneToTwo() {
  38. if (inputs_.size() == 1) {
  39. inputs_.push_back(inputs_[0]);
  40. }
  41. }
  42. void ResetInputs(const std::vector<Dimensions> &input) { inputs_ = input; }
  43. bool IsEqual(const StrategyPtr &another_stra) {
  44. if (another_stra == nullptr) {
  45. return false;
  46. }
  47. if ((stage_ != another_stra->GetInputStage()) || (inputs_ != another_stra->GetInputDim())) {
  48. return false;
  49. }
  50. return true;
  51. }
  52. private:
  53. const int32_t stage_;
  54. // The size of Dimensions must equal to inputs_ tensor dimension.
  55. std::vector<Dimensions> inputs_;
  56. };
  57. inline StrategyPtr NewStrategy(const int32_t stage, const std::vector<Dimensions> &inputs) {
  58. return std::make_shared<Strategy>(stage, inputs);
  59. }
  60. } // namespace parallel
  61. } // namespace mindspore
  62. #endif // MINDSPORE_CCSRC_PARALLEL_STRATEGY_H_