-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BACKEND] Add Address Sanitizer Pass (#5127)
Add address sanitizer pass to LLVM pass pipeline. Known limitations for this PR: - Currently only the AMD backend is supported - Source code line support not implemented here, coming in follow up patch
- Loading branch information
Showing
11 changed files
with
153 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,33 @@ | ||
import torch | ||
import triton | ||
import triton.language as tl | ||
|
||
size = 4096 | ||
x = torch.rand(size, device='cuda') | ||
y = torch.rand(size, device='cuda') | ||
output = torch.empty_like(x) | ||
n_elements = output.numel() | ||
grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']), ) | ||
|
||
|
||
@triton.jit | ||
def add_kernel( | ||
x_ptr, | ||
y_ptr, | ||
output_ptr, | ||
n_elements, | ||
BLOCK_SIZE: tl.constexpr, | ||
): | ||
pid = tl.program_id(axis=0) | ||
block_start = pid * BLOCK_SIZE | ||
#Set access to go out of bounds for ASAN test | ||
offsets = block_start + tl.arange(0, BLOCK_SIZE) + 1 | ||
x = tl.load(x_ptr + offsets) | ||
y = tl.load(y_ptr + offsets) | ||
output = x + y | ||
tl.store(output_ptr + offsets, output) | ||
|
||
|
||
pgm = add_kernel[grid](x, y, output, n_elements, BLOCK_SIZE=1024) | ||
amdgcn = pgm.asm['amdgcn'] | ||
print(amdgcn) |
This file contains 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,34 @@ | ||
import os | ||
import subprocess | ||
|
||
import triton | ||
|
||
|
||
def is_hip(): | ||
return triton.runtime.driver.active.get_current_target().backend == "hip" | ||
|
||
|
||
def test_address_sanitizer(): | ||
if not is_hip(): | ||
return #not supported on NV backend | ||
|
||
# It is recommended to disable various memory caching strategies both within the ROCm stack and PyTorch | ||
# This will give the address sanitizer the best chance at finding the memory fault where it originates, | ||
# otherwise it could be masked by writing past the end of a cached block within a larger allocation. | ||
os.environ["HSA_DISABLE_FRAGMENT_ALLOCATOR"] = "1" | ||
os.environ["AMD_PYTORCH_NO_CUDA_MEMORY_CACHING"] = "1" | ||
os.environ["PYTORCH_NO_HIP_MEMORY_CACHING"] = "1" | ||
os.environ["TRITON_ENABLE_ASAN"] = "1" | ||
|
||
# HSA_XNACK here is required to set the xnack+ setting for the GPU at runtime. | ||
# If it is not set and the default xnack setting of the system is xnack- | ||
# a runtime error something like "No kernel image found" will occur. The system | ||
# xnack setting can be found through rocminfo. xnack+ is required for ASAN. | ||
# More information about xnack in general can be found here: | ||
# https://llvm.org/docs/AMDGPUUsage.html#target-features | ||
# https://rocm.docs.amd.com/en/docs-6.1.0/conceptual/gpu-memory.html | ||
os.environ["HSA_XNACK"] = "1" | ||
|
||
out = subprocess.Popen(["python", "address_sanitizer_helper.py"], stderr=subprocess.PIPE, stdout=subprocess.PIPE) | ||
assert "Begin function __asan_report" in out.stdout.read().decode() | ||
assert "heap-buffer-overflow" in out.stderr.read().decode() |
This file contains 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 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
Binary file not shown.
This file contains 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