-
Notifications
You must be signed in to change notification settings - Fork 41
Support memory sharing in simulation environments #539
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
mawad-amd
wants to merge
9
commits into
main
Choose a base branch
from
muhaawad/mmap
base: main
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.
+279
−21
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c687a55
Replace simulation rank_bools with cross-process shm_open
mawad-amd 3d29562
Apply Ruff auto-fixes
github-actions[bot] f50ea19
Fix shm_open sim mode: keep CPU tensors, wire up peer access
mawad-amd 122b80b
Return CUDA device in sim mode for device check compatibility
mawad-amd e84559a
Register shm mmap with hipHostRegister for Triton pointer check
mawad-amd 6251890
Apply Ruff auto-fixes
github-actions[bot] 1d96924
Add single-kernel message passing example
mawad-amd 1975233
Use gloo backend in sim mode for FFM compatibility
mawad-amd dc9aae7
Use gloo backend always in example 33
mawad-amd 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 |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| #!/usr/bin/env python3 | ||
| # SPDX-License-Identifier: MIT | ||
| # Copyright (c) 2026 Advanced Micro Devices, Inc. All rights reserved. | ||
|
|
||
| """ | ||
| Example: producer-consumer message passing with a single unified kernel. | ||
| Same semantics as example 31 (message_passing) but uses one kernel that | ||
| branches internally on cur_rank. This produces a single kernel dispatch | ||
| per rank, which is required for downstream capture tools (e.g. GUMMi). | ||
| Producer rank writes source data to consumer's buffer and signals via flag. | ||
| Consumer rank spin-waits on flag then reads and doubles the data. | ||
| Requires exactly 2 ranks. | ||
| Run with: | ||
| torchrun --nproc_per_node=2 --standalone example.py [--validate] | ||
| """ | ||
|
|
||
| import argparse | ||
| import os | ||
| import random | ||
|
|
||
| import torch | ||
| import torch.distributed as dist | ||
| import triton | ||
| import triton.language as tl | ||
|
|
||
| import iris | ||
|
|
||
|
|
||
| @triton.jit | ||
| def message_passing_kernel( | ||
| source_buffer, | ||
| target_buffer, | ||
| flag, | ||
| buffer_size, | ||
| cur_rank, | ||
| producer_rank: tl.constexpr, | ||
| consumer_rank: tl.constexpr, | ||
| BLOCK_SIZE: tl.constexpr, | ||
| heap_bases_ptr: tl.tensor, | ||
| ): | ||
| pid = tl.program_id(0) | ||
| block_start = pid * BLOCK_SIZE | ||
| offsets = block_start + tl.arange(0, BLOCK_SIZE) | ||
| mask = offsets < buffer_size | ||
|
|
||
| is_producer = cur_rank == producer_rank | ||
|
|
||
| if is_producer: | ||
| values = iris.load(source_buffer + offsets, producer_rank, producer_rank, heap_bases_ptr, mask=mask) | ||
| iris.store(target_buffer + offsets, values, producer_rank, consumer_rank, heap_bases_ptr, mask=mask) | ||
| iris.atomic_cas(flag + pid, 0, 1, producer_rank, consumer_rank, heap_bases_ptr, sem="release", scope="sys") | ||
| else: | ||
| done = 0 | ||
| while done == 0: | ||
| done = iris.atomic_cas( | ||
| flag + pid, 1, 0, consumer_rank, consumer_rank, heap_bases_ptr, sem="acquire", scope="sys" | ||
| ) | ||
| values = iris.load(target_buffer + offsets, consumer_rank, consumer_rank, heap_bases_ptr, mask=mask) | ||
| values = values * 2 | ||
| iris.store(target_buffer + offsets, values, consumer_rank, consumer_rank, heap_bases_ptr, mask=mask) | ||
|
|
||
|
|
||
| torch.manual_seed(123) | ||
| random.seed(123) | ||
|
|
||
|
|
||
| def torch_dtype_from_str(datatype: str) -> torch.dtype: | ||
| dtype_map = { | ||
| "fp16": torch.float16, | ||
| "fp32": torch.float32, | ||
| "int8": torch.int8, | ||
| "bf16": torch.bfloat16, | ||
| } | ||
| try: | ||
| return dtype_map[datatype] | ||
| except KeyError: | ||
| raise ValueError(f"Unknown datatype: {datatype}") | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Message passing producer-consumer example with single kernel (2 ranks).", | ||
| formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
| ) | ||
| parser.add_argument( | ||
| "-t", | ||
| "--datatype", | ||
| type=str, | ||
| default="fp32", | ||
| choices=["fp16", "fp32", "int8", "bf16"], | ||
| help="Datatype of computation", | ||
| ) | ||
| parser.add_argument("-s", "--buffer_size", type=int, default=4096, help="Buffer size") | ||
| parser.add_argument("-b", "--block_size", type=int, default=512, help="Block size") | ||
| parser.add_argument("--heap_size", type=int, default=1 << 16, help="Iris heap size") | ||
| parser.add_argument("-v", "--validate", action="store_true", help="Validate output against reference") | ||
| return vars(parser.parse_args()) | ||
|
|
||
|
|
||
| def main(): | ||
| args = parse_args() | ||
|
|
||
| local_rank = int(os.environ.get("LOCAL_RANK", 0)) | ||
| torch.cuda.set_device(local_rank) | ||
| dist.init_process_group(backend="gloo") | ||
|
|
||
| ctx = iris.iris(heap_size=args["heap_size"]) | ||
| cur_rank = ctx.get_rank() | ||
| world_size = ctx.get_num_ranks() | ||
|
|
||
| if world_size != 2: | ||
| raise ValueError("This example requires exactly two processes. Use: torchrun --nproc_per_node=2 ...") | ||
|
|
||
| dtype = torch_dtype_from_str(args["datatype"]) | ||
| producer_rank = 0 | ||
| consumer_rank = 1 | ||
|
|
||
| source_buffer = ctx.zeros(args["buffer_size"], device="cuda", dtype=dtype) | ||
| if dtype.is_floating_point: | ||
| destination_buffer = ctx.randn(args["buffer_size"], device="cuda", dtype=dtype) | ||
| else: | ||
| ii = torch.iinfo(dtype) | ||
| destination_buffer = ctx.randint(ii.min, ii.max, (args["buffer_size"],), device="cuda", dtype=dtype) | ||
|
|
||
| n_elements = source_buffer.numel() | ||
| grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),) | ||
| num_blocks = triton.cdiv(n_elements, args["block_size"]) | ||
| flags = ctx.zeros((num_blocks,), device="cuda", dtype=torch.int32) | ||
|
|
||
| heap_bases = ctx.get_heap_bases() | ||
|
|
||
| ctx.info(f"Rank {cur_rank} launching message_passing_kernel.") | ||
| message_passing_kernel[grid]( | ||
| source_buffer, | ||
| destination_buffer, | ||
| flags, | ||
| n_elements, | ||
| cur_rank, | ||
| producer_rank, | ||
| consumer_rank, | ||
| args["block_size"], | ||
| heap_bases, | ||
| ) | ||
|
|
||
| ctx.barrier() | ||
| ctx.info(f"Rank {cur_rank} has finished.") | ||
|
|
||
| if args["validate"]: | ||
| ctx.info("Validating output...") | ||
| if cur_rank == consumer_rank: | ||
| expected = source_buffer * 2 | ||
| if not torch.allclose(destination_buffer, expected, atol=1): | ||
| max_diff = (destination_buffer - expected).abs().max().item() | ||
| ctx.error(f"Validation failed. Max absolute difference: {max_diff}") | ||
| else: | ||
| ctx.info("Validation successful.") | ||
|
|
||
| ctx.barrier() | ||
| dist.destroy_process_group() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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
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.