-
Notifications
You must be signed in to change notification settings - Fork 52
compilation: run a rule's independent commands concurrently #186
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
Open
atassis
wants to merge
1
commit into
amd:devel
Choose a base branch
from
atassis:pr/parallel-rule-commands
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |||||||||||
| import shutil | ||||||||||||
| import zlib | ||||||||||||
| import logging | ||||||||||||
| import concurrent.futures | ||||||||||||
| import subprocess | ||||||||||||
| import importlib.util | ||||||||||||
| from dataclasses import dataclass, field | ||||||||||||
|
|
@@ -103,14 +104,46 @@ def plan( | |||||||||||
| return [(rule, commands)] + plan(rules, graph, _seen_unavailable=unavailable) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def execute(plan_steps: list[tuple[CompilationRule, list[CompilationCommand]]]) -> None: | ||||||||||||
| for rule, commands in plan_steps: | ||||||||||||
| logging.debug(f"Applying rule: {rule.__class__.__name__}") | ||||||||||||
| def _execute_rule(rule: CompilationRule, commands: list[CompilationCommand]) -> None: | ||||||||||||
| """Run one rule's commands, concurrently when the rule says they are independent. | ||||||||||||
|
|
||||||||||||
| A rule that sets `commands_are_independent` emits one command per artifact in | ||||||||||||
| its worklist, with no artifact depending on another's output -- kernel object | ||||||||||||
| compiles are the case that matters, and they were costing the SUM of their | ||||||||||||
| walls (7.9 s of a 17.1 s encoder-MHA build for two kernels). | ||||||||||||
|
|
||||||||||||
| Bounded by cores: each kernel compile is a single-threaded clang peaking near | ||||||||||||
| 205 MB of RSS. Set IRON_COMPILE_JOBS to override. | ||||||||||||
| """ | ||||||||||||
| if not getattr(rule, "commands_are_independent", False) or len(commands) < 2: | ||||||||||||
| for command in commands: | ||||||||||||
| logging.debug(f" Executing command: {command}") | ||||||||||||
| success = command.run() | ||||||||||||
| if not success: | ||||||||||||
| if not command.run(): | ||||||||||||
| raise RuntimeError(f"Command failed: {command}") | ||||||||||||
| return | ||||||||||||
|
|
||||||||||||
| try: | ||||||||||||
| jobs = int(os.environ.get("IRON_COMPILE_JOBS", "0")) | ||||||||||||
| except ValueError: | ||||||||||||
| jobs = 0 | ||||||||||||
| if jobs <= 0: | ||||||||||||
| jobs = os.cpu_count() or 1 | ||||||||||||
| jobs = min(jobs, len(commands)) | ||||||||||||
| logging.debug(f" Executing {len(commands)} independent commands on {jobs} threads") | ||||||||||||
|
|
||||||||||||
| with concurrent.futures.ThreadPoolExecutor(max_workers=jobs) as pool: | ||||||||||||
| results = list(pool.map(lambda c: (c, c.run()), commands)) | ||||||||||||
| # Report the first failure only after every sibling has finished, so a | ||||||||||||
| # failing command cannot leave half-written outputs behind a raised error. | ||||||||||||
| for command, success in results: | ||||||||||||
| if not success: | ||||||||||||
| raise RuntimeError(f"Command failed: {command}") | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def execute(plan_steps: list[tuple[CompilationRule, list[CompilationCommand]]]) -> None: | ||||||||||||
| for rule, commands in plan_steps: | ||||||||||||
| logging.debug(f"Applying rule: {rule.__class__.__name__}") | ||||||||||||
| _execute_rule(rule, commands) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def compile( | ||||||||||||
|
|
@@ -471,6 +504,12 @@ def __repr__(self) -> str: | |||||||||||
| class CompilationRule(ABC): | ||||||||||||
| """A compilation rule is applied to a artifact graph, producing compilation commands and a transformed artifact graph.""" | ||||||||||||
|
|
||||||||||||
| #: Set by a rule whose `compile()` emits one command per worklist artifact, | ||||||||||||
| #: none of them consuming another's output. `execute` may then run them | ||||||||||||
| #: concurrently. Default off: a rule that batches dependent steps into one | ||||||||||||
| #: application must keep its order, and only the rule knows which it is. | ||||||||||||
| commands_are_independent: bool = False | ||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| @abstractmethod | ||||||||||||
| def matches(self, artifact: CompilationArtifactGraph) -> bool: | ||||||||||||
| """Return true if this rule can be applied to any artifact in the artifact graph.""" | ||||||||||||
|
|
@@ -752,6 +791,10 @@ def _find_working_tool(name, peano_dir, mlir_aie_dir): | |||||||||||
| class KernelCompilationRule(CompilationRule): | ||||||||||||
| """Compile KernelObjectArtifacts using Peano (clang++) or xchesscc.""" | ||||||||||||
|
|
||||||||||||
| # One command per KernelObjectArtifact, each reading its own source and | ||||||||||||
| # writing its own object. | ||||||||||||
| commands_are_independent = True | ||||||||||||
|
|
||||||||||||
| def __init__(self, peano_dir, mlir_aie_dir, use_chess=False, *args, **kwargs): | ||||||||||||
| self.peano_dir = peano_dir | ||||||||||||
| self.mlir_aie_dir = mlir_aie_dir | ||||||||||||
|
|
||||||||||||
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.
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.