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 2.8 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 GraphCell final : public Cell<GraphCell> {
  46. public:
  47. class GraphImpl;
  48. GraphCell() = default;
  49. ~GraphCell() override = default;
  50. explicit GraphCell(const Graph &);
  51. explicit GraphCell(Graph &&);
  52. explicit GraphCell(const std::shared_ptr<Graph> &);
  53. void SetContext(const std::shared_ptr<Context> &context);
  54. const std::shared_ptr<Graph> &GetGraph() const { return graph_; }
  55. Status Run(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs) override;
  56. std::vector<MSTensor> GetInputs();
  57. std::vector<MSTensor> GetOutputs();
  58. Status Load(uint32_t device_id);
  59. private:
  60. friend class Model;
  61. std::shared_ptr<Graph> graph_;
  62. std::shared_ptr<GraphImpl> executor_;
  63. };
  64. class MS_API InputAndOutput {
  65. public:
  66. InputAndOutput();
  67. ~InputAndOutput() = default;
  68. InputAndOutput(const std::shared_ptr<CellBase> &, const std::vector<InputAndOutput> &, int32_t index);
  69. int32_t GetIndex() const { return index_; }
  70. void SetIndex(int32_t index) { index_ = index; }
  71. private:
  72. std::shared_ptr<CellBase> cell_;
  73. std::vector<InputAndOutput> prev_;
  74. int32_t index_;
  75. };
  76. } // namespace mindspore
  77. #endif // MINDSPORE_INCLUDE_API_CELL_H