Skip to content

Commit 1e935c4

Browse files
authored
[PJRT] Fix ninja not found error while re-building the plugin (iree-org#19553)
If we want to build the PJRT CPU plugin, we'll run something like ``` pip install --no-deps -v ./integrations/pjrt/python_packages/iree_cpu_plugin/ ``` It works well in the first run. But if we did some changes, and want to run it the second time, some errors will appear: cmake cannot find the ninja in the first run anymore because it's in a temp build environment and removed after the first build is finished. We can remove the build dir to solve this problem. But it will cause a full rebuild, and is quite annoying : ) Since IREE compiler doesn't have such issue so I checked its build script, and I found that it's solved via the function `maybe_nuke_cmake_cache` in its [setup.py](https://github.com/iree-org/iree/blob/76a7b893e4c62d52eae2c165bdb23952a8589689/compiler/setup.py#L177). So I copy it into setup.py of the PJRT plugin and did some modification: - I think the PJRT plugin doesn't rely on CPython API (although it builds a shared library) so we don't need to pin Python version; - the build dir should be passed via a parameter since we have plugins for different platforms (cpu/cuda/rocm..). Also, I used this chance to add `cmake` to build dependencies, in case of some users don't have cmake installed in the system. ci-exactly: build_packages, test_pjrt Signed-off-by: PragmaTwice <[email protected]>
1 parent c2d408f commit 1e935c4

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

integrations/pjrt/python_packages/_setup_support/iree_pjrt_setup.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,42 @@ def run(self):
121121
self.run_command("build_py")
122122

123123

124+
def maybe_nuke_cmake_cache(cmake_build_dir):
125+
# From run to run under pip, we can end up with different paths to ninja,
126+
# which isn't great and will confuse cmake. Detect if the location of
127+
# ninja changes and force a cache flush.
128+
ninja_path = ""
129+
try:
130+
import ninja
131+
except ModuleNotFoundError:
132+
pass
133+
else:
134+
ninja_path = ninja.__file__
135+
expected_stamp_contents = f"{ninja_path}"
136+
137+
# In order to speed things up on CI and not rebuild everything, we nuke
138+
# the CMakeCache.txt file if the path to ninja changed.
139+
# Ideally, CMake would let us reconfigure this dynamically... but it does
140+
# not (and gets very confused).
141+
NINJA_STAMP_FILE = os.path.join(cmake_build_dir, "ninja_stamp.txt")
142+
if os.path.exists(NINJA_STAMP_FILE):
143+
with open(NINJA_STAMP_FILE, "rt") as f:
144+
actual_stamp_contents = f.read()
145+
if actual_stamp_contents == expected_stamp_contents:
146+
# All good.
147+
return
148+
149+
# Mismatch or not found. Clean it.
150+
cmake_cache_file = os.path.join(cmake_build_dir, "CMakeCache.txt")
151+
if os.path.exists(cmake_cache_file):
152+
print("Removing CMakeCache.txt because Ninja path changed", file=sys.stderr)
153+
os.remove(cmake_cache_file)
154+
155+
# And write.
156+
with open(NINJA_STAMP_FILE, "wt") as f:
157+
f.write(expected_stamp_contents)
158+
159+
124160
class BaseCMakeBuildPy(_build_py):
125161
def run(self):
126162
self.build_default_configuration()
@@ -131,6 +167,7 @@ def run(self):
131167

132168
def build_configuration(self, cmake_build_dir, extra_cmake_args=()):
133169
subprocess.check_call(["cmake", "--version"])
170+
maybe_nuke_cmake_cache(cmake_build_dir)
134171

135172
cfg = os.getenv("IREE_CMAKE_BUILD_TYPE", "Release")
136173

integrations/pjrt/python_packages/iree_cpu_plugin/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ requires = [
33
"setuptools>=42",
44
"wheel",
55
"ninja",
6+
"cmake",
67
]
78
build-backend = "setuptools.build_meta"

integrations/pjrt/python_packages/iree_cuda_plugin/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ requires = [
33
"setuptools>=42",
44
"wheel",
55
"ninja",
6+
"cmake",
67
]
78
build-backend = "setuptools.build_meta"

integrations/pjrt/python_packages/iree_rocm_plugin/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ requires = [
33
"setuptools>=42",
44
"wheel",
55
"ninja",
6+
"cmake",
67
]
78
build-backend = "setuptools.build_meta"

integrations/pjrt/python_packages/iree_vulkan_plugin/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ requires = [
33
"setuptools>=42",
44
"wheel",
55
"ninja",
6+
"cmake",
67
]
78
build-backend = "setuptools.build_meta"

0 commit comments

Comments
 (0)