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.

context.h 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifndef MINDSPORE_LITE_MICRO_CODER_CODER_CONTEXT_H_
  17. #define MINDSPORE_LITE_MICRO_CODER_CODER_CONTEXT_H_
  18. #include <map>
  19. #include <memory>
  20. #include <set>
  21. #include <stack>
  22. #include <string>
  23. #include <utility>
  24. #include <vector>
  25. #include "src/tensor.h"
  26. namespace mindspore::lite::micro {
  27. class CoderContext {
  28. public:
  29. CoderContext();
  30. ~CoderContext() = default;
  31. std::vector<std::string> init_contents() const { return initialContent_; }
  32. void set_code_blocks(const std::vector<std::string> &code_block) { code_blocks_ = code_block; }
  33. std::vector<std::string> code_blocks() const { return code_blocks_; }
  34. void set_inference_blocks(const std::vector<std::string> &inference_blocks) { inference_blocks_ = inference_blocks; }
  35. std::vector<std::string> inference_blocks() const { return inference_blocks_; }
  36. void set_train_blocks(const std::vector<std::string> &train_blocks) { train_blocks_ = train_blocks; }
  37. std::vector<std::string> train_blocks() const { return train_blocks_; }
  38. void set_global_code_blocks(const std::vector<std::string> &global_code_blocks) {
  39. global_code_blocks_ = global_code_blocks;
  40. }
  41. std::vector<std::string> global_code_blocks() const { return global_code_blocks_; }
  42. void set_after_inference_code_blocks(const std::vector<std::string> &after_inference_code_blocks) {
  43. after_inference_code_blocks_ = after_inference_code_blocks;
  44. }
  45. std::vector<std::string> after_inference_code_blocks() const { return after_inference_code_blocks_; }
  46. void set_tensor_map(const std::map<Tensor *, std::string> &tensor_map) {
  47. tensors_map_.insert(tensor_map.begin(), tensor_map.end());
  48. }
  49. std::map<Tensor *, std::string> tensors_map() const { return tensors_map_; }
  50. void set_saved_weights(const std::map<std::string, Tensor *> &saved_weights) { saved_weights_ = saved_weights; }
  51. std::map<std::string, Tensor *> saved_weights() const { return saved_weights_; }
  52. void set_total_buffer_size(size_t size) { total_buffer_size_ = size; }
  53. size_t total_buffer_size() const { return total_buffer_size_; }
  54. void set_graph_inputs(const std::vector<Tensor *> &graph_inputs) { graph_inputs_ = graph_inputs; }
  55. void set_graph_outputs(const std::vector<Tensor *> &graph_outputs) { graph_outputs_ = graph_outputs; }
  56. std::vector<Tensor *> graph_inputs() const { return graph_inputs_; }
  57. std::vector<Tensor *> graph_outputs() const { return graph_outputs_; }
  58. std::string input_name() { return input_name_; }
  59. std::string output_name() { return output_name_; }
  60. std::string buffer_name() { return buffer_name_; }
  61. std::string weight_name() { return weight_name_; }
  62. void AppendCode(const std::string &codeBlock);
  63. void AppendInitCode(const std::string &codeBlock);
  64. std::set<std::string> c_files() const { return c_files_; }
  65. void set_c_files(const std::set<std::string> &files) { c_files_.insert(files.begin(), files.end()); }
  66. std::set<std::string> h_files() const { return h_files_; }
  67. void set_h_files(const std::set<std::string> &files) { h_files_.insert(files.begin(), files.end()); }
  68. std::set<std::string> asm_files() const { return asm_files_; }
  69. void set_asm_files(const std::set<std::string> &files) { asm_files_.insert(files.begin(), files.end()); }
  70. private:
  71. std::vector<Tensor *> graph_inputs_;
  72. std::vector<Tensor *> graph_outputs_;
  73. // primitive const tensors, parsed from model, without packed.
  74. std::map<std::string, Tensor *> saved_weights_;
  75. // all tensors, include parsed from model and packed tensors.
  76. std::map<Tensor *, std::string> tensors_map_;
  77. // workspace's size.
  78. size_t total_buffer_size_{0};
  79. // model's input tensor data's address.
  80. std::string input_name_;
  81. // model's output tensor's address
  82. std::string output_name_;
  83. // the address of workspace, use for inference or train.
  84. std::string buffer_name_;
  85. // model's weight tensors' address.
  86. std::string weight_name_;
  87. // code blocks store the tensor will be packed runtime
  88. std::vector<std::string> initialContent_;
  89. // operator C Lang files list, depended by the net.c. it will be add to CMakeLists.txt
  90. std::set<std::string> c_files_;
  91. // when codegen generate the code for ARM64 OR ARM32, we provide server optimized artimetic used the assembly
  92. // instructions. asm_files store the assembly file names
  93. std::set<std::string> asm_files_;
  94. // operator header files
  95. std::set<std::string> h_files_;
  96. // net.c's content, include the Inference and Training implementation
  97. std::vector<std::string> code_blocks_;
  98. std::vector<std::string> global_code_blocks_;
  99. std::vector<std::string> train_blocks_;
  100. std::vector<std::string> inference_blocks_;
  101. std::vector<std::string> after_inference_code_blocks_;
  102. };
  103. } // namespace mindspore::lite::micro
  104. #endif // MINDSPORE_LITE_MICRO_CODER_CONTEXT_H_