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 #include "backend/common/somas/somas_tensor.h"
18 #include <map>
19 #include <string>
20
21 namespace mindspore {
22 namespace somas {
23 std::map<somas::TensorType, std::string> tensor_type_name_map = {
24 {kCommon, "Common"}, {kWorkspace, "Workspace"},
25 {kOutputOnly, "OutputOnly"}, {kGraphOutput, "GraphOutput"},
26 {kGraphInput, "GraphInput"}, {kSummaryInput, "SummaryInput"},
27 {kUnion, "Union"}, {kControl, "Control"},
28 {kUnknown, "Unknown"}};
29
30 std::map<LifeLongType, std::string> life_long_name_map = {{kLifeLongNone, "LifeLongNone"},
31 {kLifeLongGraphAll, "LifeLongGraphAll"},
32 {kLifeLongGraphStart, "LifeLongGraphStart"},
33 {kLifeLongGraphEnd, "LifeLongGraphEnd"}};
34
SomasTensor(size_t id,size_t source_node_id,size_t source_stream_id,size_t ori_size,size_t aligned_size,LifeLongType lifelong_value)35 SomasTensor::SomasTensor(size_t id, size_t source_node_id, size_t source_stream_id, size_t ori_size,
36 size_t aligned_size, LifeLongType lifelong_value)
37 : aligned_size_(aligned_size),
38 lifelong_value_(lifelong_value),
39 contiguous_(false),
40 is_peak_(false),
41 can_reuse_peak_mem_(0),
42 is_graph_output_(false),
43 type_(kUnknown),
44 offset_(0),
45 id_(id),
46 source_node_id_(source_node_id),
47 source_stream_id_(source_stream_id),
48 original_size_(ori_size) {
49 solver_tensor_desc_ = std::make_shared<SomasSolverTensorDesc>(id_, aligned_size_, offset_, false);
50 }
51
GetSolverTensorDesc()52 SomasSolverTensorDescPtr SomasTensor::GetSolverTensorDesc() {
53 if (contiguous_) {
54 solver_tensor_desc_->Update(id_, aligned_size_, offset_, can_reuse_peak_mem_, is_graph_output_, false);
55 } else {
56 solver_tensor_desc_->Update(id_, aligned_size_, offset_, can_reuse_peak_mem_, is_graph_output_,
57 lifelong_value_ == kLifeLongGraphAll);
58 }
59 if (aligned_size_ == 0) { // ignore zero-size tensors for solver
60 return nullptr;
61 } else {
62 return solver_tensor_desc_;
63 }
64 }
65
GetTypeString()66 std::string SomasTensor::GetTypeString() { return tensor_type_name_map[type_]; }
67
GetLifelongString()68 std::string SomasTensor::GetLifelongString() { return life_long_name_map[lifelong_value_]; }
69 } // namespace somas
70 } // namespace mindspore
71