lazy_loader.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 inspect
  17. import os
  18. import types
  19. from . import logging
  20. from .flags import FLAGS_json_format_model
  21. def disable_pir_bydefault():
  22. # FLAGS_json_format_model not set
  23. if not FLAGS_json_format_model:
  24. os.environ["FLAGS_json_format_model"] = "0"
  25. os.environ["FLAGS_enable_pir_api"] = "0"
  26. logging.debug("FLAGS_enable_pir_api has been set 0")
  27. class LazyLoader(types.ModuleType):
  28. """Lazily import a module, mainly to avoid pulling in large dependencies."""
  29. def __init__(self, local_name, parent_module_globals, name):
  30. self._local_name = local_name
  31. self._parent_module_globals = parent_module_globals
  32. self._module = None
  33. super(LazyLoader, self).__init__(name)
  34. @property
  35. def loaded(self):
  36. return self._module is not None
  37. def _load(self):
  38. # TODO(gaotingquan): disable PIR using Flag
  39. if self.__name__ == "paddle":
  40. disable_pir_bydefault()
  41. module = importlib.import_module(self.__name__)
  42. self._parent_module_globals[self._local_name] = module
  43. self._module = module
  44. def __getattr__(self, item):
  45. logging.debug("lazy load in : %s", inspect.currentframe().f_back)
  46. if not self.loaded:
  47. # HACK: For circumventing shared library symbol conflicts when
  48. # importing paddlex_hpi
  49. if item in ("__file__",):
  50. raise AttributeError
  51. self._load()
  52. return getattr(self._module, item)
  53. def __dir__(self):
  54. if not self.loaded:
  55. self._load()
  56. return dir(self._module)