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.3 kB

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