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 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_FRONTEND_PARALLEL_STRATEGY_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_STRATEGY_H_
  18. #include <cstdint>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include "frontend/parallel/device_matrix.h"
  24. #include "frontend/parallel/status.h"
  25. namespace mindspore {
  26. namespace parallel {
  27. #define MIN_SLICE_NUM 1
  28. using Dimensions = Shape;
  29. using Strategys = std::vector<Dimensions>;
  30. class Strategy;
  31. using StrategyPtr = std::shared_ptr<Strategy>;
  32. class Strategy {
  33. public:
  34. Strategy(int32_t stage, Strategys inputs)
  35. : stage_(stage), inputs_(std::move(inputs)), internal_size_(0), internal_stragies_() {}
  36. Strategy(const Strategy &another_stra) : stage_(another_stra.GetInputStage()) {
  37. inputs_ = another_stra.GetInputDim();
  38. internal_size_ = another_stra.GetInternalSize();
  39. if (internal_size_ != 0) {
  40. internal_stragies_ = another_stra.GetInternalStrategies();
  41. } else {
  42. internal_stragies_ = {};
  43. }
  44. }
  45. ~Strategy() = default;
  46. size_t GetInputNumber() const { return inputs_.size(); }
  47. Strategys GetInputDim() const { return inputs_; }
  48. int32_t GetInputStage() const { return stage_; }
  49. void ExpandInputDimFromOneToTwo() {
  50. if (inputs_.size() == 1) {
  51. inputs_.push_back(inputs_[0]);
  52. }
  53. }
  54. void ResetInputs(const Strategys &input) { inputs_ = input; }
  55. std::vector<StrategyPtr> GetInternalStrategies() const { return internal_stragies_; }
  56. size_t GetInternalSize() const { return internal_size_; }
  57. // TODO(Xiaoda): need fix for adapting 'CoverStrategy'
  58. bool IsEqual(const StrategyPtr &another_stra) {
  59. if (another_stra == nullptr) {
  60. return false;
  61. }
  62. if ((stage_ != another_stra->GetInputStage()) || (inputs_ != another_stra->GetInputDim())) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. // Include 'another_stra' into this strategy
  68. void CoverStrategy(const StrategyPtr &another_stra) {
  69. internal_stragies_.push_back(another_stra);
  70. internal_size_++;
  71. }
  72. private:
  73. const int32_t stage_;
  74. // The size of Dimensions must equal to inputs_ tensor dimension.
  75. Strategys inputs_;
  76. size_t internal_size_ = 0;
  77. std::vector<StrategyPtr> internal_stragies_;
  78. };
  79. inline StrategyPtr NewStrategy(const int32_t stage, const Strategys &inputs) {
  80. return std::make_shared<Strategy>(stage, inputs);
  81. }
  82. } // namespace parallel
  83. } // namespace mindspore
  84. #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_STRATEGY_H_