Skip to content

Commit eec6de4

Browse files
Copilotmawad-amd
andauthored
Port cache_modifier, volatile, and other to DeviceContext and Gluon APIs (#471)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mawad-amd <112003944+mawad-amd@users.noreply.github.com>
1 parent 726bfe5 commit eec6de4

4 files changed

Lines changed: 1283 additions & 26 deletions

File tree

iris/experimental/iris_gluon.py

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def _translate(self, ptr, from_rank, to_rank):
374374
return translated_ptr
375375

376376
@gluon.jit
377-
def load(self, pointer, from_rank, mask=None, other=None):
377+
def load(self, pointer, from_rank, mask=None, other=None, cache_modifier=None, volatile=False):
378378
"""
379379
Loads a value from the specified rank's memory location to the current rank.
380380
@@ -383,6 +383,17 @@ def load(self, pointer, from_rank, mask=None, other=None):
383383
from_rank: The rank ID from which to read the data
384384
mask: Optional mask for conditional loading
385385
other: Value to return for masked-out elements. If not provided, the result for masked-out elements is undefined.
386+
cache_modifier (str, optional): Controls cache behavior of the load.
387+
388+
Supported values:
389+
- None: *(default)* — Same as ".ca". Uses cache at all levels (CU, L2, LLC) with LRU policy.
390+
- ".ca": Cache at all levels (CU, L2, LLC) with LRU policy.
391+
- ".cg": Bypasses the CU (L1) cache, streams through L2, and may hit in LLC but the line is not retained or inserted.
392+
- ".cv": Bypasses all GPU caches (CU and L2) and fetches directly from system memory. If data exists in the LLC, it may hit, but is not retained or inserted.
393+
Ensures global coherence by invalidating stale GPU cache lines.
394+
395+
volatile (bool, optional): If True, disables compiler optimizations that
396+
could reorder or eliminate the load. Defaults to False.
386397
387398
Returns:
388399
The loaded value from the target memory location
@@ -392,11 +403,11 @@ def load(self, pointer, from_rank, mask=None, other=None):
392403
>>> data = ctx.load(buffer + offsets, 1, mask=mask)
393404
"""
394405
translated_ptr = self._translate(pointer, self.cur_rank, from_rank)
395-
result = gl.load(translated_ptr, mask=mask, other=other)
406+
result = gl.load(translated_ptr, mask=mask, other=other, cache_modifier=cache_modifier, volatile=volatile)
396407
return result
397408

398409
@gluon.jit
399-
def store(self, pointer, value, to_rank, mask=None):
410+
def store(self, pointer, value, to_rank, mask=None, cache_modifier=None):
400411
"""
401412
Writes data from the current rank to the specified rank's memory location.
402413
@@ -405,16 +416,25 @@ def store(self, pointer, value, to_rank, mask=None):
405416
value: The value to store
406417
to_rank: The rank ID to which the data will be written
407418
mask: Optional mask for conditional storing
419+
cache_modifier (str, optional): Controls cache behavior of the store. Supported values are:
420+
421+
- None: *(default)* — Same as ".wb". Uses write-back caching at all levels (CU, L2, LLC) with LRU policy.
422+
- ".wb": Write-back. Write-allocate on L1 miss, inserted into caches and written back later.
423+
- ".cg": Cache Global. Equivalent to ".wb" — stored through L1 → L2 → LLC under LRU.
424+
- ".cs": Cache Streaming. Bypasses L1, streamed through L2, not retained in LLC.
425+
- ".wt": Write-Through. Bypasses L1 and L2 (coherent cache bypass), may hit in LLC with LRU.
408426
409427
Example:
410428
>>> # Store from current rank to rank 1
411429
>>> ctx.store(buffer + offsets, values, 1, mask=mask)
412430
"""
413431
translated_ptr = self._translate(pointer, self.cur_rank, to_rank)
414-
gl.store(translated_ptr, value, mask=mask)
432+
gl.store(translated_ptr, value, mask=mask, cache_modifier=cache_modifier)
415433

416434
@gluon.jit
417-
def get(self, from_ptr, to_ptr, from_rank, mask=None, other=None):
435+
def get(
436+
self, from_ptr, to_ptr, from_rank, mask=None, other=None, load_cache_modifier=None, store_cache_modifier=None
437+
):
418438
"""
419439
Copies data from the specified rank's memory to the current rank's local memory.
420440
@@ -424,17 +444,31 @@ def get(self, from_ptr, to_ptr, from_rank, mask=None, other=None):
424444
from_rank: The rank ID from which to read the data
425445
mask: Optional mask for conditional operations
426446
other: Value to return for masked-out elements during the load operation. If not provided, the result for masked-out elements is undefined.
447+
load_cache_modifier (str, optional): Controls cache behavior of the load. Supported values are:
448+
- None: *(default)* — Same as ".ca". Uses cache at all levels (CU, L2, LLC) with LRU policy.
449+
- ".ca": Cache at all levels (CU, L2, LLC) with LRU policy.
450+
- ".cg": Bypasses the CU (L1) cache, streams through L2, and may hit in LLC but the line is not retained or inserted.
451+
- ".cv": Bypasses all GPU caches (CU and L2) and fetches directly from system memory. If data exists in the LLC, it may hit, but is not retained or inserted.
452+
453+
store_cache_modifier (str, optional): Controls cache behavior of the store. Supported values are:
454+
- None: *(default)* — Same as ".wb". Uses write-back caching at all levels (CU, L2, LLC) with LRU policy.
455+
- ".wb": Write-back. Write-allocate on L1 miss, inserted into caches and written back later.
456+
- ".cg": Cache Global. Equivalent to ".wb" — stored through L1 → L2 → LLC under LRU.
457+
- ".cs": Cache Streaming. Bypasses L1, streamed through L2, not retained in LLC.
458+
- ".wt": Write-Through. Bypasses L1 and L2 (coherent cache bypass), may hit in LLC with LRU.
427459
428460
Example:
429461
>>> # Copy from rank 1 to current rank's local memory
430462
>>> ctx.get(remote_ptr + offsets, local_ptr + offsets, 1, mask=mask)
431463
"""
432464
translated_from_ptr = self._translate(from_ptr, self.cur_rank, from_rank)
433-
data = gl.load(translated_from_ptr, mask=mask, other=other)
434-
gl.store(to_ptr, data, mask=mask)
465+
data = gl.load(translated_from_ptr, mask=mask, other=other, cache_modifier=load_cache_modifier)
466+
gl.store(to_ptr, data, mask=mask, cache_modifier=store_cache_modifier)
435467

436468
@gluon.jit
437-
def put(self, from_ptr, to_ptr, to_rank, mask=None, other=None):
469+
def put(
470+
self, from_ptr, to_ptr, to_rank, mask=None, other=None, load_cache_modifier=None, store_cache_modifier=None
471+
):
438472
"""
439473
Copies data from the current rank's local memory to the specified rank's memory.
440474
@@ -444,17 +478,39 @@ def put(self, from_ptr, to_ptr, to_rank, mask=None, other=None):
444478
to_rank: The rank ID to which the data will be written
445479
mask: Optional mask for conditional operations
446480
other: Value to return for masked-out elements during the load operation. If not provided, the result for masked-out elements is undefined.
481+
load_cache_modifier (str, optional): Controls cache behavior of the load. Supported values are:
482+
- None: *(default)* — Same as ".ca". Uses cache at all levels (CU, L2, LLC) with LRU policy.
483+
- ".ca": Cache at all levels (CU, L2, LLC) with LRU policy.
484+
- ".cg": Bypasses the CU (L1) cache, streams through L2, and may hit in LLC but the line is not retained or inserted.
485+
- ".cv": Bypasses all GPU caches (CU and L2) and fetches directly from system memory. If data exists in the LLC, it may hit, but is not retained or inserted.
486+
487+
store_cache_modifier (str, optional): Controls cache behavior of the store. Supported values are:
488+
- None: *(default)* — Same as ".wb". Uses write-back caching at all levels (CU, L2, LLC) with LRU policy.
489+
- ".wb": Write-back. Write-allocate on L1 miss, inserted into caches and written back later.
490+
- ".cg": Cache Global. Equivalent to ".wb" — stored through L1 → L2 → LLC under LRU.
491+
- ".cs": Cache Streaming. Bypasses L1, streamed through L2, not retained in LLC.
492+
- ".wt": Write-Through. Bypasses L1 and L2 (coherent cache bypass), may hit in LLC with LRU.
447493
448494
Example:
449495
>>> # Copy from current rank's local memory to rank 1
450496
>>> ctx.put(local_ptr + offsets, remote_ptr + offsets, 1, mask=mask)
451497
"""
452498
translated_to_ptr = self._translate(to_ptr, self.cur_rank, to_rank)
453-
data = gl.load(from_ptr, mask=mask, other=other)
454-
gl.store(translated_to_ptr, data, mask=mask)
499+
data = gl.load(from_ptr, mask=mask, other=other, cache_modifier=load_cache_modifier)
500+
gl.store(translated_to_ptr, data, mask=mask, cache_modifier=store_cache_modifier)
455501

456502
@gluon.jit
457-
def copy(self, src_ptr, dst_ptr, from_rank, to_rank, mask=None, other=None):
503+
def copy(
504+
self,
505+
src_ptr,
506+
dst_ptr,
507+
from_rank,
508+
to_rank,
509+
mask=None,
510+
other=None,
511+
load_cache_modifier=None,
512+
store_cache_modifier=None,
513+
):
458514
"""
459515
Copies data from the specified rank's memory into the destination rank's memory.
460516
@@ -471,6 +527,18 @@ def copy(self, src_ptr, dst_ptr, from_rank, to_rank, mask=None, other=None):
471527
to_rank: The rank ID that will receive the data (destination rank)
472528
mask: Optional mask for conditional operations
473529
other: Value to return for masked-out elements during the load operation. If not provided, the result for masked-out elements is undefined.
530+
load_cache_modifier (str, optional): Controls cache behavior of the load. Supported values are:
531+
- None: *(default)* — Same as ".ca". Uses cache at all levels (CU, L2, LLC) with LRU policy.
532+
- ".ca": Cache at all levels (CU, L2, LLC) with LRU policy.
533+
- ".cg": Bypasses the CU (L1) cache, streams through L2, and may hit in LLC but the line is not retained or inserted.
534+
- ".cv": Bypasses all GPU caches (CU and L2) and fetches directly from system memory. If data exists in the LLC, it may hit, but is not retained or inserted.
535+
536+
store_cache_modifier (str, optional): Controls cache behavior of the store. Supported values are:
537+
- None: *(default)* — Same as ".wb". Uses write-back caching at all levels (CU, L2, LLC) with LRU policy.
538+
- ".wb": Write-back. Write-allocate on L1 miss, inserted into caches and written back later.
539+
- ".cg": Cache Global. Equivalent to ".wb" — stored through L1 → L2 → LLC under LRU.
540+
- ".cs": Cache Streaming. Bypasses L1, streamed through L2, not retained in LLC.
541+
- ".wt": Write-Through. Bypasses L1 and L2 (coherent cache bypass), may hit in LLC with LRU.
474542
475543
Example:
476544
>>> # Copy from rank 1 to rank 0 (current rank must be either 1 or 0)
@@ -492,8 +560,8 @@ def copy(self, src_ptr, dst_ptr, from_rank, to_rank, mask=None, other=None):
492560
translated_src = tl.cast(from_base_byte + src_offset, src_ptr.dtype)
493561
translated_dst = tl.cast(to_base_byte + dst_offset, src_ptr.dtype)
494562

495-
data = gl.load(translated_src, mask=mask, other=other)
496-
gl.store(translated_dst, data, mask=mask)
563+
data = gl.load(translated_src, mask=mask, other=other, cache_modifier=load_cache_modifier)
564+
gl.store(translated_dst, data, mask=mask, cache_modifier=store_cache_modifier)
497565

498566
@gluon.jit
499567
def atomic_add(self, pointer, val, to_rank, mask=None, sem=None, scope=None):

0 commit comments

Comments
 (0)