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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // vectors checked not empty in OpCoderBuilder::build
  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. size_t node_index() const;
  58. void set_parameter(OpParameter *parameter);
  59. const Model::Node *node() const { return this->node_; }
  60. void AddInitialParameters(Tensor *parameter) { initial_parameters_.push_back(parameter); }
  61. const std::vector<Tensor *> initial_parameters() const { return initial_parameters_; }
  62. // context
  63. virtual int Prepare(CoderContext *const context) = 0;
  64. virtual int DoCode(CoderContext *const context) = 0;
  65. virtual ~OperatorCoder();
  66. void set_thread_num(int thread_num);
  67. protected:
  68. std::vector<Tensor *> input_tensors_;
  69. std::vector<Tensor *> output_tensors_;
  70. Target target_{kTargetUnknown};
  71. const Model::Node *node_{nullptr};
  72. Tensor *input_tensor_{nullptr};
  73. Tensor *output_tensor_{nullptr};
  74. OpParameter *parameter_{nullptr};
  75. MemoryAllocator *allocator_{nullptr};
  76. bool support_parallel_{false};
  77. int thread_num_{1};
  78. private:
  79. size_t node_index_{0};
  80. std::vector<uint32_t> input_tensor_indices_;
  81. std::vector<uint32_t> output_tensor_indices_;
  82. std::vector<OperatorCoder *> input_ops_;
  83. std::vector<OperatorCoder *> output_ops_;
  84. std::vector<Tensor *> initial_parameters_;
  85. };
  86. // a template func for normal op_coder creator
  87. template <typename T>
  88. std::unique_ptr<OperatorCoder> CPUOpCoderCreator(const std::vector<Tensor *> &in_tensors,
  89. const std::vector<Tensor *> &out_tensors, const Model::Node *node,
  90. size_t node_index, Target target) {
  91. if (node == nullptr) {
  92. MS_LOG(ERROR) << "node is null";
  93. return nullptr;
  94. }
  95. std::unique_ptr<T> coder = std::make_unique<T>(in_tensors, out_tensors, node, node_index, target);
  96. return coder;
  97. }
  98. } // namespace mindspore::lite::micro
  99. #endif // MINDSPORE_LITE_MICRO_CODER_OPCODER_H_