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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. namespace mindspore {
  25. namespace api {
  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. std::vector<Output> operator()(const std::vector<Input> &inputs) const;
  36. };
  37. template <class T>
  38. class MS_API Cell : public CellBase {
  39. public:
  40. virtual ~Cell() = default;
  41. std::shared_ptr<CellBase> Clone() const override {
  42. return std::make_shared<T>(static_cast<const T&>(*this));
  43. }
  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 Tensor &);
  54. ParameterCell &operator=(const Tensor &);
  55. explicit ParameterCell(Tensor &&);
  56. ParameterCell &operator=(Tensor &&);
  57. Tensor GetTensor() const { return tensor_; }
  58. private:
  59. Tensor 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 {
  75. return std::make_shared<T>(static_cast<const T&>(*this));
  76. }
  77. };
  78. class MS_API InputAndOutput {
  79. public:
  80. InputAndOutput();
  81. ~InputAndOutput() = default;
  82. // no explicit
  83. InputAndOutput(const Tensor &); // NOLINT(runtime/explicit)
  84. InputAndOutput(Tensor &&); // NOLINT(runtime/explicit)
  85. InputAndOutput(const std::shared_ptr<CellBase> &, const std::vector<InputAndOutput> &, int32_t index);
  86. int32_t GetIndex() const { return index_; }
  87. void SetIndex(int32_t index) { index_ = index; }
  88. private:
  89. std::shared_ptr<CellBase> cell_;
  90. std::vector<InputAndOutput> prev_;
  91. int32_t index_;
  92. };
  93. } // namespace api
  94. } // namespace mindspore
  95. #endif // MINDSPORE_INCLUDE_API_CELL_H