1 /** 2 * Copyright 2020-2023 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_COMMON_SOMAS_SOMAS_TENSOR_H 18 #define MINDSPORE_CCSRC_BACKEND_COMMON_SOMAS_SOMAS_TENSOR_H 19 20 #include <memory> 21 #include <set> 22 #include <vector> 23 #include <string> 24 #include "utils/hash_map.h" 25 #include "backend/common/somas/somas_solver_pre.h" 26 27 namespace mindspore { 28 namespace somas { 29 // Lifetime type 30 struct Lifetime { 31 size_t start_; 32 size_t end_; 33 start_Lifetime34 explicit Lifetime(size_t start = 0, size_t end = 0) : start_(start), end_(end) {} 35 }; 36 using lifetime_t = struct Lifetime; 37 38 // Tensor type 39 enum TensorType { 40 kCommon, 41 kWorkspace, 42 kOutputOnly, 43 kGraphOutput, 44 kGraphInput, 45 kSummaryInput, 46 kUnion, 47 kControl, 48 kUnknown 49 }; 50 51 enum LifeLongType { 52 kLifeLongNone, // life time is from tensor start to tensor end 53 kLifeLongGraphAll, // life time is from graph start to graph end 54 kLifeLongGraphStart, // life time is from graph start to tensor end 55 kLifeLongGraphEnd // life time is from tensor start to graph end 56 }; 57 58 class BACKEND_EXPORT SomasTensor { 59 public: 60 size_t aligned_size_{0}; 61 LifeLongType lifelong_value_; 62 63 bool contiguous_; 64 bool is_peak_; 65 size_t can_reuse_peak_mem_; 66 bool is_graph_output_; 67 68 lifetime_t lifetime_; 69 TensorType type_; 70 71 size_t offset_{0}; 72 73 std::set<size_t> destination_nodes_; 74 vector<size_t> consumer_list_; 75 76 // Constructors/Destructors 77 explicit SomasTensor(size_t id, size_t source_node_id, size_t source_stream_id, size_t ori_size, size_t aligned_size, 78 LifeLongType lifelong_value = kLifeLongNone); 79 SomasTensor(const SomasTensor &) = delete; 80 SomasTensor &operator=(const SomasTensor &) = delete; 81 ~SomasTensor() = default; 82 83 // Accessors GetId()84 const size_t &GetId() const { return id_; } GetSourceNodeId()85 size_t GetSourceNodeId() const { return source_node_id_; } GetSourceStreamId()86 size_t GetSourceStreamId() const { return source_stream_id_; } GetOriginalSize()87 const size_t &GetOriginalSize() const { return original_size_; } GetAlignedSize()88 const size_t &GetAlignedSize() const { return aligned_size_; } IsLifelong()89 bool IsLifelong() const { return lifelong_value_ == kLifeLongGraphAll; } IsOutputOnly()90 bool IsOutputOnly() const { return type_ == kOutputOnly; } GetOffset()91 size_t GetOffset() const { return offset_; } IsSemiLifelongStart()92 bool IsSemiLifelongStart() const { return lifelong_value_ == kLifeLongGraphStart; } IsSemiLifelongEnd()93 bool IsSemiLifelongEnd() const { return lifelong_value_ == kLifeLongGraphEnd; } 94 string GetTypeString(); 95 string GetLifelongString(); 96 // Computing functions SetOffset()97 void SetOffset() { 98 if (aligned_size_ != 0) { 99 offset_ = solver_tensor_desc_->offset_; 100 } 101 } 102 SomasSolverTensorDescPtr GetSolverTensorDesc(); 103 104 private: 105 const size_t id_{0}; 106 const size_t source_node_id_; 107 const size_t source_stream_id_; 108 const size_t original_size_{0}; 109 110 SomasSolverTensorDescPtr solver_tensor_desc_; 111 }; 112 using SomasTensorPtr = std::shared_ptr<SomasTensor>; 113 } // namespace somas 114 } // namespace mindspore 115 116 #endif // MINDSPORE_CCSRC_BACKEND_COMMON_SOMAS_SOMAS_TENSOR_H 117