1 /** 2 * Copyright 2020 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 17 #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_TENSOR_H_ 18 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_TENSOR_H_ 19 20 #include <memory> 21 #include <set> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "backend/optimizer/somas/somas_node.h" 26 #include "backend/optimizer/somas/somas_solver_pre.h" 27 #include "backend/optimizer/somas/somas_stream.h" 28 29 namespace mindspore { 30 namespace somas { 31 class SomasNode; 32 class SomasStream; 33 34 // Lifetime type 35 struct Lifetime { 36 size_t start_; 37 size_t end_; 38 start_Lifetime39 explicit Lifetime(size_t start = 0, size_t end = 0) : start_(start), end_(end) {} 40 }; 41 using lifetime_t = struct Lifetime; 42 43 // Tensor type 44 enum TensorType { 45 kCommon, 46 kOutputOnly, 47 kWorkspace, 48 kGetNextOutput, 49 kSummaryInput, 50 kRefNodeInput, 51 kRefNodeOutput, 52 kUnknown 53 }; 54 55 enum LifeLongType { 56 kLifeLongNone, // life time is from tensor start to tensor end 57 kLifeLongGraphAll, // life time is from graph start to graph end 58 kLifeLongGraphStart, // life time is from graph start to tensor end 59 kLifeLongGraphEnd // life time is from tensor start to graph end 60 }; 61 62 using SomasNodePtr = std::shared_ptr<SomasNode>; 63 using SomasStreamPtr = std::shared_ptr<SomasStream>; 64 65 class SomasTensor { 66 public: 67 using SomasTensorPtr = std::shared_ptr<SomasTensor>; 68 69 size_t aligned_size_{0}; 70 LifeLongType lifelong_value_; 71 72 bool between_streams_; 73 bool contiguous_; 74 75 lifetime_t lifetime_; 76 TensorType type_; 77 78 size_t offset_{0}; 79 80 std::set<SomasNodePtr> destinations_; 81 std::set<SomasStreamPtr> destinationStreams_; 82 unordered_map<SomasStreamPtr, SomasNodePtr> max_destinations_; 83 84 // Constructors/Destructors 85 explicit SomasTensor(size_t id, SomasNodePtr source_node, SomasStreamPtr source_stream, size_t real_size, 86 LifeLongType lifelong_value = kLifeLongNone); 87 SomasTensor(const SomasTensor &) = delete; 88 SomasTensor &operator=(const SomasTensor &) = delete; 89 ~SomasTensor() = default; 90 91 // Accessors GetId()92 const size_t &GetId() { return id_; } GetSourceNode()93 SomasNodePtr GetSourceNode() const { return source_node_; } GetSourceStream()94 SomasStreamPtr GetSourceStream() const { return source_stream_; } GetOriginalSize()95 const size_t &GetOriginalSize() { return original_size_; } GetAlignedSize()96 const size_t &GetAlignedSize() { return aligned_size_; } GetNumConstraints()97 const size_t &GetNumConstraints() { return num_constraints_; } IsLifelong()98 bool IsLifelong() { return lifelong_value_ == kLifeLongGraphAll; } IsWorkspace()99 bool IsWorkspace() { return type_ == kWorkspace; } IsOutputOnly()100 bool IsOutputOnly() { return type_ == kOutputOnly; } GetOffset()101 size_t GetOffset() { return offset_; } IsBetweenStreams()102 bool IsBetweenStreams() { return between_streams_; } IsSemiLifelongStart()103 bool IsSemiLifelongStart() { return lifelong_value_ == kLifeLongGraphStart; } IsSemiLifelongEnd()104 bool IsSemiLifelongEnd() { return lifelong_value_ == kLifeLongGraphEnd; } IsRefOverlap()105 bool IsRefOverlap() { return ref_overlap_; } 106 107 // Computing functions SetOffset()108 void SetOffset() { 109 if (aligned_size_ != 0) { 110 offset_ = solver_tensor_desc_->offset_; 111 } 112 } 113 SomasSolverTensorDescPtr GetSolverTensorDesc(); 114 void ComputeMaxDestinationId(); 115 116 private: 117 bool ref_overlap_; 118 size_t num_constraints_{0}; 119 unordered_map<SomasStreamPtr, size_t> max_destination_id_; 120 const size_t id_{0}; 121 const SomasNodePtr source_node_; 122 SomasStreamPtr const source_stream_; 123 const size_t original_size_{0}; 124 125 SomasSolverTensorDescPtr solver_tensor_desc_; 126 }; 127 } // namespace somas 128 } // namespace mindspore 129 130 #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_TENSOR_H_ 131