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.

cell.h 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Copyright 2020 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_INCLUDE_API_CELL_H
  17. #define MINDSPORE_INCLUDE_API_CELL_H
  18. #include <string>
  19. #include <vector>
  20. #include <map>
  21. #include <memory>
  22. #include "include/api/status.h"
  23. #include "include/api/types.h"
  24. #include "include/api/graph.h"
  25. namespace mindspore {
  26. class InputAndOutput;
  27. class Context;
  28. using Input = InputAndOutput;
  29. using Output = InputAndOutput;
  30. class MS_API CellBase {
  31. public:
  32. CellBase() = default;
  33. virtual ~CellBase() = default;
  34. virtual std::vector<Output> Construct(const std::vector<Input> &inputs) { return {}; }
  35. virtual std::shared_ptr<CellBase> Clone() const = 0;
  36. virtual Status Run(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs) { return kSuccess; }
  37. std::vector<Output> operator()(const std::vector<Input> &inputs) const;
  38. };
  39. template <class T>
  40. class MS_API Cell : public CellBase {
  41. public:
  42. virtual ~Cell() = default;
  43. std::shared_ptr<CellBase> Clone() const override { return std::make_shared<T>(static_cast<const T &>(*this)); }
  44. };
  45. class MS_API ParameterCell final : public Cell<ParameterCell> {
  46. public:
  47. ParameterCell() = default;
  48. ~ParameterCell() override = default;
  49. ParameterCell(const ParameterCell &);
  50. ParameterCell &operator=(const ParameterCell &);
  51. ParameterCell(ParameterCell &&);
  52. ParameterCell &operator=(ParameterCell &&);
  53. explicit ParameterCell(const MSTensor &);
  54. ParameterCell &operator=(const MSTensor &);
  55. explicit ParameterCell(MSTensor &&);
  56. ParameterCell &operator=(MSTensor &&);
  57. MSTensor GetTensor() const { return tensor_; }
  58. private:
  59. MSTensor tensor_;
  60. };
  61. class MS_API OpCellBase : public CellBase {
  62. public:
  63. explicit OpCellBase(const std::string &name) : name_(name) {}
  64. ~OpCellBase() override = default;
  65. const std::string &GetOpType() const { return name_; }
  66. protected:
  67. std::string name_;
  68. };
  69. template <class T>
  70. class MS_API OpCell : public OpCellBase, public std::enable_shared_from_this<T> {
  71. public:
  72. explicit OpCell(const std::string &name) : OpCellBase(name) {}
  73. ~OpCell() override = default;
  74. std::shared_ptr<CellBase> Clone() const override { return std::make_shared<T>(static_cast<const T &>(*this)); }
  75. };
  76. class MS_API GraphCell final : public Cell<GraphCell> {
  77. public:
  78. class GraphImpl;
  79. GraphCell() = default;
  80. ~GraphCell() override = default;
  81. explicit GraphCell(const Graph &);
  82. explicit GraphCell(Graph &&);
  83. explicit GraphCell(const std::shared_ptr<Graph> &);
  84. void SetContext(const std::shared_ptr<Context> &context);
  85. const std::shared_ptr<Graph> &GetGraph() const { return graph_; }
  86. Status Run(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs) override;
  87. std::vector<MSTensor> GetInputs();
  88. std::vector<MSTensor> GetOutputs();
  89. private:
  90. friend class Model;
  91. friend class ModelImpl;
  92. Status Load(uint32_t device_id);
  93. std::shared_ptr<Graph> graph_;
  94. std::shared_ptr<GraphImpl> executor_;
  95. };
  96. class MS_API InputAndOutput {
  97. public:
  98. InputAndOutput();
  99. ~InputAndOutput() = default;
  100. // no explicit
  101. InputAndOutput(const MSTensor &); // NOLINT(runtime/explicit)
  102. InputAndOutput(MSTensor &&); // NOLINT(runtime/explicit)
  103. InputAndOutput(const std::shared_ptr<CellBase> &, const std::vector<InputAndOutput> &, int32_t index);
  104. int32_t GetIndex() const { return index_; }
  105. void SetIndex(int32_t index) { index_ = index; }
  106. private:
  107. std::shared_ptr<CellBase> cell_;
  108. std::vector<InputAndOutput> prev_;
  109. int32_t index_;
  110. };
  111. } // namespace mindspore
  112. #endif // MINDSPORE_INCLUDE_API_CELL_H