You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

graph.cc 3.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "micro/coder/graph.h"
  17. #include <queue>
  18. #include <deque>
  19. #include <string>
  20. #include <memory>
  21. #include <algorithm>
  22. #include <set>
  23. #include "schema/inner/model_generated.h"
  24. #include "src/ops/primitive_c.h"
  25. namespace mindspore::lite::micro {
  26. CoderGraph::~CoderGraph() {
  27. model_->Free();
  28. delete model_;
  29. for (auto &tensor : all_tensors_) {
  30. delete tensor;
  31. }
  32. }
  33. std::vector<lite::Tensor *> CoderGraph::input_tensors() const { return input_tensors_; }
  34. std::vector<lite::Tensor *> CoderGraph::output_tensors() const { return output_tensors_; }
  35. void CoderGraph::InitInputs() {
  36. for (const auto &pair : inputs_map_) {
  37. std::vector<Tensor *> tensors = pair.second;
  38. input_tensors_.insert(input_tensors_.end(), tensors.begin(), tensors.end());
  39. }
  40. // remove duplicate tensors
  41. std::set<lite::Tensor *> unique;
  42. unique.insert(input_tensors_.begin(), input_tensors_.end());
  43. input_tensors_.clear();
  44. input_tensors_.insert(input_tensors_.end(), unique.begin(), unique.end());
  45. }
  46. void CoderGraph::InitOutputs() {
  47. std::transform(output_indices_.begin(), output_indices_.end(), std::back_inserter(output_tensors_),
  48. [&](uint32_t a) { return this->all_tensors_.at(a); });
  49. }
  50. void CoderGraph::SetAllTensors(const std::vector<Tensor *> &all_tensors) {
  51. all_tensors_.insert(all_tensors_.end(), all_tensors.begin(), all_tensors.end());
  52. }
  53. void CoderGraph::SetInputIndices(const std::vector<uint32_t> &input_indices) {
  54. input_indices_.insert(input_indices_.end(), input_indices.begin(), input_indices.end());
  55. }
  56. void CoderGraph::SetOutputIndices(const std::vector<uint32_t> &output_indices) {
  57. output_indices_.insert(output_indices_.end(), output_indices.begin(), output_indices.end());
  58. }
  59. void CoderGraph::AddInputMap(const std::string &node_id, Tensor *input_tensor) {
  60. if (!input_tensor) {
  61. MS_LOG(ERROR) << "input tensor is nullptr, can not added to coder_graph";
  62. return;
  63. }
  64. this->inputs_map_[node_id].emplace_back(input_tensor);
  65. }
  66. void CoderGraph::AddOutputMap(const std::string &node_id, Tensor *output_tensor) {
  67. if (!output_tensor) {
  68. MS_LOG(ERROR) << "output tensor is nullptr, can not added to coder_graph";
  69. return;
  70. }
  71. this->outputs_map_[node_id].emplace_back(output_tensor);
  72. }
  73. std::vector<lite::Tensor *> CoderGraph::all_tensors() const { return this->all_tensors_; }
  74. const std::map<std::string, std::vector<lite::Tensor *>> &CoderGraph::GetOutputsMap() const { return outputs_map_; }
  75. std::vector<uint32_t> CoderGraph::input_indices() const { return this->input_indices_; }
  76. std::vector<uint32_t> CoderGraph::output_indices() const { return this->output_indices_; }
  77. } // namespace mindspore::lite::micro