• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "backend/optimizer/somas/somas_tensor.h"
18 
19 #include "backend/optimizer/somas/somas_node.h"
20 #include "backend/optimizer/somas/somas_stream.h"
21 #include "backend/optimizer/somas/somas.h"
22 
23 namespace mindspore {
24 namespace somas {
SomasTensor(size_t id,SomasNodePtr source_node,SomasStreamPtr source_stream,size_t real_size,LifeLongType lifelong_value)25 SomasTensor::SomasTensor(size_t id, SomasNodePtr source_node, SomasStreamPtr source_stream, size_t real_size,
26                          LifeLongType lifelong_value)
27     : lifelong_value_(lifelong_value),
28       type_(kUnknown),
29       offset_(0),
30       id_(id),
31       source_node_(std::move(source_node)),
32       source_stream_(std::move(source_stream)),
33       original_size_(real_size) {
34   const size_t alignment = 512;
35   const size_t alignment_complement = 31;
36   aligned_size_ = (real_size > 0) ? (real_size + alignment + alignment_complement) / alignment * alignment : 0;
37 
38   solver_tensor_desc_ = std::make_shared<SomasSolverTensorDesc>(id_, aligned_size_, offset_, false);
39 
40   ref_overlap_ = false;
41   between_streams_ = false;
42   contiguous_ = false;
43   num_constraints_ = 0;
44 }
45 
GetSolverTensorDesc()46 SomasSolverTensorDescPtr SomasTensor::GetSolverTensorDesc() {
47   if (contiguous_) {
48     solver_tensor_desc_->Update(id_, aligned_size_, offset_, false, num_constraints_);
49   } else {
50     solver_tensor_desc_->Update(id_, aligned_size_, offset_, lifelong_value_ == kLifeLongGraphAll, num_constraints_);
51   }
52   if (aligned_size_ == 0) {  // ignore zero-size tensors for solver
53     return nullptr;
54   } else {
55     return solver_tensor_desc_;
56   }
57 }
58 
ComputeMaxDestinationId()59 void SomasTensor::ComputeMaxDestinationId() {
60   for (const auto &node : destinations_) {
61     MS_EXCEPTION_IF_NULL(node);
62     if (node->GetId() > max_destination_id_[node->GetStream()]) {
63       max_destination_id_[node->GetStream()] = node->GetId();
64       max_destinations_[node->GetStream()] = node;
65     }
66   }
67 }
68 }  // namespace somas
69 }  // namespace mindspore
70