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.

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