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.

op_coder.h 4.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_OPCODER_H_
  17. #define MINDSPORE_LITE_MICRO_CODER_OPCODER_H_
  18. #include <vector>
  19. #include <set>
  20. #include <string>
  21. #include <memory>
  22. #include "coder/context.h"
  23. #include "coder/graph.h"
  24. #include "coder/allocator/allocator.h"
  25. #include "include/errorcode.h"
  26. #include "src/lite_kernel.h"
  27. #include "src/common/version_manager.h"
  28. #include "securec/include/securec.h"
  29. #include "coder/opcoders/op_coder_register.h"
  30. #include "coder/log.h"
  31. namespace mindspore::lite::micro {
  32. constexpr int kPrecision = 19;
  33. class OperatorCoder {
  34. public:
  35. OperatorCoder(const std::vector<Tensor *> &in_tensors, const std::vector<Tensor *> &out_tensors,
  36. const Model::Node *node, size_t node_index, Target target)
  37. : input_tensors_(in_tensors),
  38. output_tensors_(out_tensors),
  39. target_(target),
  40. node_(node),
  41. node_index_(node_index) {
  42. allocator_ = MemoryAllocator::GetInstance();
  43. input_tensor_ = input_tensors_.at(kInputIndex);
  44. output_tensor_ = output_tensors_.at(kOutputIndex);
  45. }
  46. std::string name() const { return node_->name_; }
  47. void set_input_tensor_indices(const std::vector<uint32_t> &input_indices);
  48. void set_output_tensor_indices(const std::vector<uint32_t> &output_indices);
  49. const std::vector<uint32_t> input_tensor_indices() const;
  50. const std::vector<uint32_t> output_tensor_indices() const;
  51. const std::vector<Tensor *> input_tensors() const;
  52. const std::vector<Tensor *> output_tensors() const;
  53. void AddInputOp(OperatorCoder *op) { input_ops_.push_back(op); }
  54. void AddOutputOp(OperatorCoder *op) { output_ops_.push_back(op); }
  55. const std::vector<OperatorCoder *> input_ops() const { return input_ops_; }
  56. const std::vector<OperatorCoder *> output_ops() const { return output_ops_; }
  57. void set_type(int type) { type_ = type; }
  58. const int type() const { return type_; }
  59. size_t node_index() const;
  60. void set_parameter(OpParameter *parameter);
  61. const Model::Node *node() const { return this->node_; }
  62. void AddInitialParameters(Tensor *parameter) { initial_parameters_.push_back(parameter); }
  63. const std::vector<Tensor *> initial_parameters() const { return initial_parameters_; }
  64. void SetSchemaVersion(int schema_version) { schema_version_ = schema_version; }
  65. // context
  66. virtual int Prepare(CoderContext *const context) = 0;
  67. virtual int DoCode(CoderContext *const context) = 0;
  68. virtual ~OperatorCoder();
  69. void set_thread_num(int thread_num);
  70. protected:
  71. std::vector<Tensor *> input_tensors_;
  72. std::vector<Tensor *> output_tensors_;
  73. Target target_{kTargetUnknown};
  74. const Model::Node *node_{nullptr};
  75. Tensor *input_tensor_{nullptr};
  76. Tensor *output_tensor_{nullptr};
  77. OpParameter *parameter_{nullptr};
  78. MemoryAllocator *allocator_{nullptr};
  79. bool support_parallel_{false};
  80. int thread_num_{1};
  81. int schema_version_ = lite::SCHEMA_VERSION::SCHEMA_CUR;
  82. private:
  83. size_t node_index_{0};
  84. std::vector<uint32_t> input_tensor_indices_;
  85. std::vector<uint32_t> output_tensor_indices_;
  86. std::vector<OperatorCoder *> input_ops_;
  87. std::vector<OperatorCoder *> output_ops_;
  88. std::vector<Tensor *> initial_parameters_;
  89. int type_{schema::PrimitiveType_NONE};
  90. };
  91. // a template func for normal op_coder creator
  92. template <typename T>
  93. std::unique_ptr<OperatorCoder> CPUOpCoderCreator(const std::vector<Tensor *> &in_tensors,
  94. const std::vector<Tensor *> &out_tensors, const Model::Node *node,
  95. size_t node_index, Target target, int schema_version) {
  96. if (node == nullptr) {
  97. MS_LOG(ERROR) << "node is null";
  98. return nullptr;
  99. }
  100. std::unique_ptr<T> coder = std::make_unique<T>(in_tensors, out_tensors, node, node_index, target);
  101. if (coder == nullptr) {
  102. return nullptr;
  103. }
  104. coder->SetSchemaVersion(schema_version);
  105. return coder;
  106. }
  107. } // namespace mindspore::lite::micro
  108. #endif // MINDSPORE_LITE_MICRO_CODER_OPCODER_H_