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.

resource.h 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright 2019 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_PIPELINE_RESOURCE_H_
  17. #define MINDSPORE_CCSRC_PIPELINE_RESOURCE_H_
  18. #include <iostream>
  19. #include <vector>
  20. #include <string>
  21. #include <unordered_map>
  22. #include <memory>
  23. #include "pybind11/pybind11.h"
  24. #include "pybind11/stl.h"
  25. #include "utils/any.h"
  26. #include "utils/profile.h"
  27. #include "ir/manager.h"
  28. #include "pipeline/static_analysis/prim.h"
  29. #include "pipeline/static_analysis/static_analysis.h"
  30. #include "./common.h"
  31. namespace mindspore {
  32. namespace pipeline {
  33. namespace py = pybind11;
  34. const char kBackend[] = "backend";
  35. const char kStepParallelGraph[] = "step_parallel";
  36. const char kOutput[] = "output";
  37. class InferenceResource;
  38. using MethodMap = std::unordered_map<int, std::unordered_map<std::string, Any>>;
  39. MethodMap &GetMethodMap();
  40. class ResourceBase {
  41. public:
  42. ResourceBase() { manager_ = MakeManager(); }
  43. virtual ~ResourceBase() = default;
  44. FuncGraphManagerPtr manager() { return manager_; }
  45. // set a manager defined outside which will not manage the graphs.
  46. void set_manager(const FuncGraphManagerPtr &manager) { manager_ = manager; }
  47. std::unordered_map<std::string, Any> &results() { return results_; }
  48. void SetResult(const std::string &key, const Any &value) { results_[key] = value; }
  49. Any GetResult(const std::string &key) {
  50. if (results_.count(key) == 0) {
  51. MS_LOG(EXCEPTION) << "this key is not in resource list:" << key;
  52. }
  53. return results_[key];
  54. }
  55. bool HasResult(const std::string &key) const { return results_.count(key) != 0; }
  56. std::unordered_map<std::string, Any> results_;
  57. protected:
  58. FuncGraphManagerPtr manager_;
  59. };
  60. using ResourceBasePtr = std::shared_ptr<pipeline::ResourceBase>;
  61. class Resource : public ResourceBase {
  62. public:
  63. explicit Resource(const py::object &obj = py::none());
  64. ~Resource() override;
  65. abstract::AnalysisEnginePtr engine() { return engine_; }
  66. static bool IsTypeInMethodMap(const TypeId &type);
  67. static Any GetMethodPtr(const TypeId &type, const std::string &name);
  68. const py::object &input() const { return input_; }
  69. FuncGraphPtr func_graph() const { return func_graph_; }
  70. void set_func_graph(const FuncGraphPtr &func_graph) { func_graph_ = func_graph; }
  71. const abstract::AbstractBasePtrList &args_spec() const { return args_spec_; }
  72. void set_args_spec(const abstract::AbstractBasePtrList &args_spec) { args_spec_ = args_spec; }
  73. // Reclaim resource and clear the cache.
  74. // ExecutorPy::Compile() can be called multiple times, so cache
  75. // should be cleared.
  76. void Clean();
  77. private:
  78. abstract::AnalysisEnginePtr engine_;
  79. FuncGraphPtr func_graph_;
  80. abstract::AbstractBasePtrList args_spec_;
  81. py::object input_;
  82. bool is_cleaned_;
  83. };
  84. using ResourcePtr = std::shared_ptr<pipeline::Resource>;
  85. } // namespace pipeline
  86. } // namespace mindspore
  87. #endif // MINDSPORE_CCSRC_PIPELINE_RESOURCE_H_