-
Notifications
You must be signed in to change notification settings - Fork 173
Add lock over cache dir #2622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add lock over cache dir #2622
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
570e041
Add lock over cache dir
mawad-amd 621577b
Use double quote
mawad-amd 802cb3f
Apply lint change
mawad-amd f85e7a2
Add parallel build test
mawad-amd 9117ab9
Update test/python/jit_compilation.py
mawad-amd 9b1b882
Update test/python/jit_compilation.py
mawad-amd f5c9dbc
Update test/python/jit_compilation.py
mawad-amd 2249f29
Format the test
mawad-amd 613a007
Remove all files except the lock
mawad-amd 4b19595
Merge branch 'main' into muhaawad/process-safe-jit
mawad-amd d94dd75
Avoid racing over creating the file (Thanks Jeff)
mawad-amd 9ffcfd2
Add debug printfs
mawad-amd c89d9ad
Format the test file
mawad-amd e924d98
Set the max number of parallel jobs to 6 on Phoenix
mawad-amd 67dbfc3
Add concurrency test group
mawad-amd 33f6cb0
Merge branch 'main' into muhaawad/process-safe-jit
mawad-amd e2c4380
Update test/python-concurrency/jit_parallel_compilation.py
mawad-amd 89a526c
Use `--filter-out`
mawad-amd fd9a96c
Add pytest substitution
mawad-amd 15279c9
Merge branch 'main' into muhaawad/process-safe-jit
mawad-amd bf11ec4
Merge branch 'main' into muhaawad/process-safe-jit
jgmelber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,7 @@ jobs: | |
|
|
||
| ninja install | ||
| ninja check-aie | ||
| ninja check-aie-concurrency | ||
| popd | ||
|
|
||
| build-quick-setup: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| # This file is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
| # See https://llvm.org/LICENSE.txt for license information. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| # | ||
| # (c) Copyright 2025 AMD Inc. | ||
|
|
||
| # RUN: %run_on_npu1% %pytest %s | ||
| # RUN: %run_on_npu2% %pytest %s | ||
|
|
||
| import pytest | ||
| import numpy as np | ||
| import os | ||
| import tempfile | ||
| import subprocess | ||
| import sys | ||
|
|
||
|
|
||
| def test_parallel_compilation_subprocess(): | ||
| """ | ||
| Test parallel JIT compilation using subprocesses. | ||
| This test spawns multiple processes that compile the same kernel concurrently | ||
| to ensure the file locking mechanism works correctly. | ||
| """ | ||
|
|
||
| # Create a temporary cache directory for this test | ||
| with tempfile.TemporaryDirectory() as temp_cache_dir: | ||
| # Create a simple test script that does JIT compilation | ||
| test_script = """ | ||
| import sys | ||
| import numpy as np | ||
| import aie.iron as iron | ||
| from aie.iron import ObjectFifo, Program, Runtime, Worker | ||
| from aie.iron.placers import SequentialPlacer | ||
| from aie.iron.controlflow import range_ | ||
|
|
||
| @iron.jit(is_placed=False) | ||
| def simple_add(input0, input1, output): | ||
| if input0.shape != input1.shape: | ||
| raise ValueError(f"Input shapes are not equal ({input0.shape} != {input1.shape}).") | ||
| if input0.shape != output.shape: | ||
| raise ValueError(f"Input and output shapes are not equal ({input0.shape} != {output.shape}).") | ||
| if len(np.shape(input0)) != 1: | ||
| raise ValueError("Function only supports vectors.") | ||
| num_elements = np.size(input0) | ||
| n = 16 | ||
| if num_elements % n != 0: | ||
| raise ValueError(f"Number of elements ({num_elements}) must be a multiple of {n}.") | ||
| N_div_n = num_elements // n | ||
|
|
||
| if input0.dtype != input1.dtype: | ||
| raise ValueError(f"Input data types are not the same ({input0.dtype} != {input1.dtype}).") | ||
| if input0.dtype != output.dtype: | ||
| raise ValueError(f"Input and output data types are not the same ({input0.dtype} != {output.dtype}).") | ||
| dtype = input0.dtype | ||
|
|
||
| # Define tensor types | ||
| tensor_ty = np.ndarray[(num_elements,), np.dtype[dtype]] | ||
| tile_ty = np.ndarray[(n,), np.dtype[dtype]] | ||
|
|
||
| # AIE-array data movement with object fifos | ||
| of_in1 = ObjectFifo(tile_ty, name="in1") | ||
| of_in2 = ObjectFifo(tile_ty, name="in2") | ||
| of_out = ObjectFifo(tile_ty, name="out") | ||
|
|
||
| # Define a task that will run on a compute tile | ||
| def core_body(of_in1, of_in2, of_out): | ||
| # Number of sub-vector "tile" iterations | ||
| for _ in range_(N_div_n): | ||
| elem_in1 = of_in1.acquire(1) | ||
| elem_in2 = of_in2.acquire(1) | ||
| elem_out = of_out.acquire(1) | ||
| for i in range_(n): | ||
| elem_out[i] = elem_in1[i] + elem_in2[i] | ||
| of_in1.release(1) | ||
| of_in2.release(1) | ||
| of_out.release(1) | ||
|
|
||
| # Create a worker to run the task on a compute tile | ||
| worker = Worker(core_body, fn_args=[of_in1.cons(), of_in2.cons(), of_out.prod()]) | ||
|
|
||
| # Runtime operations to move data to/from the AIE-array | ||
| rt = Runtime() | ||
| with rt.sequence(tensor_ty, tensor_ty, tensor_ty) as (A, B, C): | ||
| rt.start(worker) | ||
| rt.fill(of_in1.prod(), A) | ||
| rt.fill(of_in2.prod(), B) | ||
| rt.drain(of_out.cons(), C, wait=True) | ||
|
|
||
| # Place program components (assign them resources on the device) and generate an MLIR module | ||
| return Program(iron.get_current_device(), rt).resolve_program(SequentialPlacer()) | ||
|
|
||
| # Test the compilation | ||
| try: | ||
| num_elements = 16 | ||
| dtype = np.int32 | ||
| input0 = iron.randint(1, 100, (num_elements,), dtype=dtype, device="npu") | ||
| input1 = iron.randint(1, 100, (num_elements,), dtype=dtype, device="npu") | ||
| output = iron.zeros_like(input0) | ||
|
|
||
| # This should trigger JIT compilation and cache access | ||
| simple_add(input0, input1, output) | ||
| print("SUCCESS") | ||
| except Exception as e: | ||
| print(f"ERROR: {type(e).__name__}: {str(e)}") | ||
| sys.exit(1) | ||
| """ | ||
|
|
||
| # Write the test script to a temporary file | ||
| script_path = os.path.join(temp_cache_dir, "test_compilation.py") | ||
| with open(script_path, "w") as f: | ||
| f.write(test_script) | ||
|
|
||
| # Run multiple subprocesses concurrently | ||
| num_processes = 5 | ||
| processes = [] | ||
|
|
||
| for i in range(num_processes): | ||
| env = os.environ.copy() | ||
| env["IRON_CACHE_HOME"] = temp_cache_dir | ||
| process = subprocess.Popen( | ||
| [sys.executable, script_path], | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| text=True, | ||
| env=env, | ||
| ) | ||
| processes.append(process) | ||
|
|
||
| # Wait for all processes to complete and collect return codes | ||
| return_codes = [] | ||
| process_outputs = [] | ||
|
|
||
| for i, process in enumerate(processes): | ||
| stdout, stderr = process.communicate() | ||
| return_codes.append(process.returncode) | ||
| process_outputs.append((stdout, stderr)) | ||
|
|
||
| # Print output for each process for debugging | ||
| print(f"\n=== Process {i} (return code: {process.returncode}) ===") | ||
| print(f"STDOUT:\n{stdout}") | ||
| print(f"STDERR:\n{stderr}") | ||
| print("=" * 50) | ||
|
|
||
| # Count successful processes (return code 0) | ||
| successful_processes = sum(1 for code in return_codes if code == 0) | ||
|
|
||
| # Verify that all processes completed | ||
| assert len(return_codes) == num_processes, "All processes should complete" | ||
|
|
||
| # Check if any concurrent compilation failed | ||
| if successful_processes < num_processes: | ||
| # Create detailed error message with all process outputs | ||
| error_msg = ( | ||
| f"Only {successful_processes}/{num_processes} processes succeeded\n\n" | ||
| ) | ||
| error_msg += "Process details:\n" | ||
|
|
||
| for i, (return_code, (stdout, stderr)) in enumerate( | ||
| zip(return_codes, process_outputs) | ||
| ): | ||
| status = "SUCCESS" if return_code == 0 else "FAILED" | ||
| error_msg += f"\nProcess {i}: {status} (return code: {return_code})\n" | ||
| if stdout: | ||
| error_msg += f" STDOUT: {stdout.strip()}\n" | ||
| if stderr: | ||
| error_msg += f" STDERR: {stderr.strip()}\n" | ||
|
|
||
| pytest.fail(error_msg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # This file is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
| # See https://llvm.org/LICENSE.txt for license information. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| # | ||
| # (c) Copyright 2025 AMD Inc. | ||
|
|
||
| # Concurrency tests need to control their own parallelism, so we disable | ||
| # inter-test concurrency for this directory | ||
| config.parallelism_group = "concurrency" | ||
|
|
||
| # Add pytest substitution | ||
| config.substitutions.append(("%pytest", "pytest -rA")) | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? I think we'd rather know if it fails and why.
From man(2) flock:
Keeping in mind
lock_fileis presumably the only file descriptor we have in the current process of the lock file, the explicit unlock here is redundant with thelock_file.close()in the next line, which will also release the lock.