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.

model_impl.h 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_CCSRC_CXX_API_MODEL_MODEL_IMPL_H
  17. #define MINDSPORE_CCSRC_CXX_API_MODEL_MODEL_IMPL_H
  18. #include <functional>
  19. #include <map>
  20. #include <string>
  21. #include <vector>
  22. #include <memory>
  23. #include <utility>
  24. #include "include/api/context.h"
  25. #include "include/api/model.h"
  26. #include "include/api/graph.h"
  27. #include "cxx_api/graph/graph_data.h"
  28. #include "utils/utils.h"
  29. #include "ir/func_graph.h"
  30. namespace mindspore {
  31. class ModelImpl {
  32. public:
  33. ModelImpl() = default;
  34. virtual ~ModelImpl() = default;
  35. virtual Status Build() = 0;
  36. virtual Status Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims) = 0;
  37. virtual Status Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs);
  38. virtual Status PredictWithPreprocess(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs);
  39. virtual std::vector<MSTensor> GetInputs() = 0;
  40. virtual std::vector<MSTensor> GetOutputs() = 0;
  41. virtual bool CheckModelSupport(enum ModelType model_type) { return false; }
  42. virtual Status Preprocess(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs);
  43. virtual bool HasPreprocess();
  44. protected:
  45. FuncGraphPtr GetFuncGraph() const {
  46. if (graph_->ModelType() != ModelType::kMindIR) {
  47. return nullptr;
  48. }
  49. auto graph_data = graph_->graph_data_;
  50. MS_EXCEPTION_IF_NULL(graph_data);
  51. return graph_data->GetFuncGraph();
  52. }
  53. std::shared_ptr<Graph> graph_ = nullptr;
  54. std::shared_ptr<GraphCell> graph_cell_ = nullptr;
  55. std::shared_ptr<Context> model_context_ = nullptr;
  56. private:
  57. friend class Model;
  58. void SetGraph(const std::shared_ptr<Graph> &graph) { graph_ = graph; }
  59. void SetContext(const std::shared_ptr<Context> &model_context) {
  60. if (model_context != nullptr) {
  61. model_context_ = std::make_shared<Context>(*model_context);
  62. }
  63. }
  64. };
  65. } // namespace mindspore
  66. #endif // MINDSPORE_CCSRC_CXX_API_MODEL_MODEL_IMPL_H