|
| 1 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +import importlib |
| 3 | +import os |
| 4 | +import site |
| 5 | +from functools import ( |
| 6 | + lru_cache, |
| 7 | +) |
| 8 | +from importlib.machinery import ( |
| 9 | + FileFinder, |
| 10 | +) |
| 11 | +from importlib.util import ( |
| 12 | + find_spec, |
| 13 | +) |
| 14 | +from pathlib import ( |
| 15 | + Path, |
| 16 | +) |
| 17 | +from sysconfig import ( |
| 18 | + get_path, |
| 19 | +) |
| 20 | +from typing import ( |
| 21 | + Optional, |
| 22 | + Union, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +@lru_cache |
| 27 | +def find_paddle() -> tuple[Optional[str], list[str]]: |
| 28 | + """Find PaddlePadle library. |
| 29 | +
|
| 30 | + Tries to find PaddlePadle in the order of: |
| 31 | +
|
| 32 | + 1. Environment variable `PADDLE_ROOT` if set |
| 33 | + 2. The current Python environment. |
| 34 | + 3. user site packages directory if enabled |
| 35 | + 4. system site packages directory (purelib) |
| 36 | +
|
| 37 | + Considering the default PaddlePadle package still uses old CXX11 ABI, we |
| 38 | + cannot install it automatically. |
| 39 | +
|
| 40 | + Returns |
| 41 | + ------- |
| 42 | + str, optional |
| 43 | + PaddlePadle library path if found. |
| 44 | + list of str |
| 45 | + Paddle requirement if not found. Empty if found. |
| 46 | + """ |
| 47 | + if os.environ.get("DP_ENABLE_PADDLE", "0") == "0": |
| 48 | + return None, [] |
| 49 | + requires = [] |
| 50 | + pd_spec = None |
| 51 | + |
| 52 | + if (pd_spec is None or not pd_spec) and os.environ.get("PADDLE_ROOT") is not None: |
| 53 | + site_packages = Path(os.environ.get("PADDLE_ROOT")).parent.absolute() |
| 54 | + pd_spec = FileFinder(str(site_packages)).find_spec("paddle") |
| 55 | + |
| 56 | + # get paddle spec |
| 57 | + # note: isolated build will not work for backend |
| 58 | + if pd_spec is None or not pd_spec: |
| 59 | + pd_spec = find_spec("paddle") |
| 60 | + |
| 61 | + if not pd_spec and site.ENABLE_USER_SITE: |
| 62 | + # first search TF from user site-packages before global site-packages |
| 63 | + site_packages = site.getusersitepackages() |
| 64 | + if site_packages: |
| 65 | + pd_spec = FileFinder(site_packages).find_spec("paddle") |
| 66 | + |
| 67 | + if not pd_spec: |
| 68 | + # purelib gets site-packages path |
| 69 | + site_packages = get_path("purelib") |
| 70 | + if site_packages: |
| 71 | + pd_spec = FileFinder(site_packages).find_spec("paddle") |
| 72 | + |
| 73 | + # get install dir from spec |
| 74 | + try: |
| 75 | + pd_install_dir = pd_spec.submodule_search_locations[0] # type: ignore |
| 76 | + # AttributeError if ft_spec is None |
| 77 | + # TypeError if submodule_search_locations are None |
| 78 | + # IndexError if submodule_search_locations is an empty list |
| 79 | + except (AttributeError, TypeError, IndexError): |
| 80 | + pd_install_dir = None |
| 81 | + requires.extend(get_pd_requirement()["paddle"]) |
| 82 | + return pd_install_dir, requires |
| 83 | + |
| 84 | + |
| 85 | +@lru_cache |
| 86 | +def get_pd_requirement(pd_version: str = "") -> dict: |
| 87 | + """Get PaddlePadle requirement when Paddle is not installed. |
| 88 | +
|
| 89 | + If pd_version is not given and the environment variable `PADDLE_VERSION` is set, use it as the requirement. |
| 90 | +
|
| 91 | + Parameters |
| 92 | + ---------- |
| 93 | + pd_version : str, optional |
| 94 | + Paddle version |
| 95 | +
|
| 96 | + Returns |
| 97 | + ------- |
| 98 | + dict |
| 99 | + PaddlePadle requirement. |
| 100 | + """ |
| 101 | + if pd_version is None: |
| 102 | + return {"paddle": []} |
| 103 | + if pd_version == "": |
| 104 | + pd_version = os.environ.get("PADDLE_VERSION", "") |
| 105 | + |
| 106 | + return { |
| 107 | + "paddle": [ |
| 108 | + "paddlepaddle>=3.0.0b1" if pd_version != "" else "paddlepaddle>=3.0.0b1", |
| 109 | + ], |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +@lru_cache |
| 114 | +def get_pd_version(pd_path: Optional[Union[str, Path]]) -> str: |
| 115 | + """Get Paddle version from a Paddle Python library path. |
| 116 | +
|
| 117 | + Parameters |
| 118 | + ---------- |
| 119 | + pd_path : str or Path |
| 120 | + Paddle Python library path, e.g. "/python3.10/site-packages/paddle/" |
| 121 | +
|
| 122 | + Returns |
| 123 | + ------- |
| 124 | + str |
| 125 | + version |
| 126 | + """ |
| 127 | + if pd_path is None or pd_path == "": |
| 128 | + return "" |
| 129 | + version_file = Path(pd_path) / "version" / "__init__.py" |
| 130 | + spec = importlib.util.spec_from_file_location("paddle.version", version_file) |
| 131 | + module = importlib.util.module_from_spec(spec) |
| 132 | + spec.loader.exec_module(module) |
| 133 | + return module.full_version |
0 commit comments