lazy_loader.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # Code copied from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/util/lazy_loader.py
  15. import importlib
  16. import types
  17. import os
  18. from . import logging
  19. from .flags import FLAGS_json_format_model
  20. def disable_pir_bydefault():
  21. # FLAGS_json_format_model not set
  22. if not FLAGS_json_format_model:
  23. os.environ["FLAGS_json_format_model"] = "0"
  24. os.environ["FLAGS_enable_pir_api"] = "0"
  25. logging.debug("FLAGS_enable_pir_api has been set 0")
  26. class LazyLoader(types.ModuleType):
  27. """Lazily import a module, mainly to avoid pulling in large dependencies."""
  28. def __init__(self, local_name, parent_module_globals, name):
  29. self._local_name = local_name
  30. self._parent_module_globals = parent_module_globals
  31. self._module = None
  32. super(LazyLoader, self).__init__(name)
  33. @property
  34. def loaded(self):
  35. return self._module is not None
  36. def _load(self):
  37. # TODO(gaotingquan): disable PIR using Flag
  38. if self.__name__ == "paddle":
  39. disable_pir_bydefault()
  40. module = importlib.import_module(self.__name__)
  41. self._parent_module_globals[self._local_name] = module
  42. self._module = module
  43. def __getattr__(self, item):
  44. if not self.loaded:
  45. # HACK: For circumventing shared library symbol conflicts when
  46. # importing paddlex_hpi
  47. if item in ("__file__",):
  48. raise AttributeError
  49. self._load()
  50. return getattr(self._module, item)
  51. def __dir__(self):
  52. if not self.loaded:
  53. self._load()
  54. return dir(self._module)