• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 #include "mindir_lite_graph.h"
17 #include "mindir_tensor.h"
18 #include "mindir_primitive.h"
19 #include "src/common/log.h"
20 #include "schema/model_generated.h"
21 #include "mindir_memory_manager.h"
22 #include <string>
23 namespace mindspore {
24 namespace lite {
MindIR_LiteGraph_Destroy(LiteGraph ** lite_graph)25 void MindIR_LiteGraph_Destroy(LiteGraph **lite_graph) {
26   if (lite_graph != nullptr && *lite_graph != nullptr) {
27     MS_LOG(INFO) << "start to destroy LiteGraph.";
28     auto graph = *lite_graph;
29     graph->name_.clear();
30     graph->input_indices_.clear();
31     graph->output_indices_.clear();
32     MS_LOG(INFO) << "Destroying  nodes.";
33     // node
34     for (size_t idx = 0; idx < graph->all_nodes_.size(); idx++) {
35       if (graph->all_nodes_[idx] != nullptr) {
36         MindIRMemoryManager::GetInstance()->DeletePrimitive(
37           static_cast<schema::Primitive *>(graph->all_nodes_[idx]->primitive_));
38         delete graph->all_nodes_[idx];
39       }
40     }
41     MS_LOG(INFO) << "Destroying  subgraphs.";
42     // subgraph
43     for (size_t idx = 0; idx < graph->sub_graphs_.size(); idx++) {
44       if (graph->sub_graphs_[idx] != nullptr) {
45         delete graph->sub_graphs_[idx];
46       }
47     }
48     MS_LOG(INFO) << "Destroying  tensors.";
49     // tensor
50     for (size_t idx = 0; idx < graph->all_tensors_.size(); idx++) {
51       if (graph->all_tensors_[idx] != nullptr) {
52         MindIRMemoryManager::GetInstance()->DeleteTensor(static_cast<schema::Tensor *>(graph->all_tensors_[idx]));
53       }
54     }
55     // graph
56     delete graph;
57     *lite_graph = nullptr;
58   } else {
59     MS_LOG(ERROR) << "nnrt_lite_graph is nullptr, can not delete.";
60   }
61 }
62 
MindIR_LiteGraph_GetConstTensorSize(const LiteGraph * lite_graph)63 size_t MindIR_LiteGraph_GetConstTensorSize(const LiteGraph *lite_graph) {
64   if (lite_graph != nullptr) {
65     size_t size = 0;
66     for (auto tensor : lite_graph->all_tensors_) {
67       if (tensor != nullptr) {
68         auto value = static_cast<schema::Tensor *>(tensor);
69         if (value != nullptr) {
70           auto src = value->data();
71           if (src == nullptr) {
72             continue;
73           }
74           size += src->size();
75         }
76       }
77     }
78     MS_LOG(DEBUG) << "lite_graph has " << lite_graph->all_tensors_.size() << "tensors ,const tensor size = " << size;
79     return size;
80   } else {
81     MS_LOG(ERROR) << "lite_graph is nullptr";
82     return 0;
83   }
84 }
85 
86 }  // namespace lite
87 }  // namespace mindspore