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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. class CoderContext;
  32. constexpr int kPrecision = 19;
  33. #define CODE_PARALLEL_FUNC(func) code << "ParallelLaunch(THREAD_POOL_DEFAULT, " << func << ", &args, thread_num);\n"
  34. class OperatorCoder {
  35. public:
  36. OperatorCoder(const std::vector<Tensor *> &in_tensors, const std::vector<Tensor *> &out_tensors,
  37. const Model::Node *node, size_t node_index, Target target)
  38. : input_tensors_(in_tensors),
  39. output_tensors_(out_tensors),
  40. node_(node),
  41. target_(target),
  42. node_index_(node_index) {
  43. allocator_ = MemoryAllocator::GetInstance();
  44. // vectors checked not empty in OpCoderBuilder::build
  45. input_tensor_ = input_tensors_.at(kInputIndex);
  46. output_tensor_ = output_tensors_.at(kOutputIndex);
  47. }
  48. std::string ID() const { return node_->name_; }
  49. void set_input_tensor_indices(const std::vector<uint32_t> &input_indices);
  50. void set_output_tensor_indices(const std::vector<uint32_t> &output_indices);
  51. const std::vector<uint32_t> input_tensor_indices() const;
  52. const std::vector<uint32_t> output_tensor_indices() const;
  53. const std::vector<Tensor *> input_tensors() const;
  54. const std::vector<Tensor *> output_tensors() const;
  55. void AddInputOp(OperatorCoder *op) { input_ops_.push_back(op); }
  56. void AddOutputOp(OperatorCoder *op) { output_ops_.push_back(op); }
  57. const std::vector<OperatorCoder *> input_ops() const { return input_ops_; }
  58. const std::vector<OperatorCoder *> output_ops() const { return output_ops_; }
  59. size_t node_index() const;
  60. void set_parameter(OpParameter *parameter);
  61. const PrimitiveC *primitive() const { return node_->primitive_; }
  62. const Model::Node *node() const { return this->node_; }
  63. void AddInitialParameters(Tensor *parameter) { initial_parameters_.push_back(parameter); }
  64. const std::vector<Tensor *> initial_parameters() const { return initial_parameters_; }
  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. const Model::Node *node_{nullptr};
  74. Target target_{kTargetUnknown};
  75. Tensor *input_tensor_{nullptr};
  76. Tensor *output_tensor_{nullptr};
  77. OpParameter *parameter_{nullptr};
  78. MemoryAllocator *allocator_{nullptr};
  79. std::string thread_num_s_{"1"};
  80. int thread_num_{1};
  81. private:
  82. size_t node_index_{0};
  83. std::vector<uint32_t> input_tensor_indices_;
  84. std::vector<uint32_t> output_tensor_indices_;
  85. std::vector<OperatorCoder *> input_ops_;
  86. std::vector<OperatorCoder *> output_ops_;
  87. std::vector<Tensor *> initial_parameters_;
  88. };
  89. // a template func for normal op_coder creator
  90. template <typename T>
  91. std::unique_ptr<OperatorCoder> CPUOpCoderCreator(const std::vector<Tensor *> &in_tensors,
  92. const std::vector<Tensor *> &out_tensors, const Model::Node *node,
  93. size_t node_index, Target target) {
  94. std::unique_ptr<T> coder = std::make_unique<T>(in_tensors, out_tensors, node, node_index, target);
  95. return coder;
  96. }
  97. } // namespace mindspore::lite::micro
  98. #endif // MINDSPORE_LITE_MICRO_CODER_OPCODER_H_