diff --git a/problems/pmpp/histogram_py/reference.py b/problems/pmpp/histogram_py/reference.py index dc7a8792f..18e8b249b 100644 --- a/problems/pmpp/histogram_py/reference.py +++ b/problems/pmpp/histogram_py/reference.py @@ -1,4 +1,4 @@ -from utils import make_match_reference +from utils import verbose_allequal import torch from task import input_t, output_t @@ -11,42 +11,41 @@ def ref_kernel(data: input_t) -> output_t: Returns: Tensor containing bin counts """ - # Fixed range [0, 100] - min_val, max_val = 0, 100 - - # Number of bins is input size / 16 - num_bins = data.shape[0] // 16 - - clipped = torch.clamp(data, min_val, max_val) - - # Scale to bin indices - bin_width = (max_val - min_val) / num_bins - indices = ((clipped - min_val) / bin_width).long() - indices = torch.clamp(indices, 0, num_bins - 1) - # Count values in each bin - return torch.bincount(indices, minlength=num_bins).to(torch.float32) + return torch.bincount(data, minlength=256) -def generate_input(size: int, seed: int) -> input_t: +def generate_input(size: int, contention: float, seed: int) -> input_t: """ Generates random input tensor for histogram. - The number of bins is automatically set to size/16. - + Args: size: Size of the input tensor (must be multiple of 16) + contention: float in [0, 100], specifying the percentage of identical values seed: Random seed Returns: - The input tensor with values in [0, 100] + The input tensor with values in [0, 255] """ gen = torch.Generator(device='cuda') gen.manual_seed(seed) - # Generate integer values between 0 and 100 - data = torch.randint(0, 101, (size,), device='cuda', dtype=torch.int32, generator=gen) - - # Convert to float since the histogram implementation expects float input - return data.float().contiguous() + # Generate integer values between 0 and 256 + data = torch.randint(0, 256, (size,), device='cuda', dtype=torch.uint8, generator=gen) + + # make one value appear quite often, increasing the chance for atomic contention + evil_value = torch.randint(0, 256, (), device='cuda', dtype=torch.uint8, generator=gen) + evil_loc = torch.rand((size,), device='cuda', dtype=torch.float32, generator=gen) < (contention / 100.0) + data[evil_loc] = evil_value + + return data.contiguous() + + +def check_implementation(data, output): + expected = ref_kernel(data) + reasons = verbose_allequal(output, expected) + + if len(reasons) > 0: + return "mismatch found! custom implementation doesn't match reference: " + " ".join(reasons) + return '' -check_implementation = make_match_reference(ref_kernel) diff --git a/problems/pmpp/histogram_py/submission.py b/problems/pmpp/histogram_py/submission.py index 338585f7c..1e62e9a33 100644 --- a/problems/pmpp/histogram_py/submission.py +++ b/problems/pmpp/histogram_py/submission.py @@ -9,19 +9,4 @@ def custom_kernel(data: input_t) -> output_t: Returns: Tensor containing bin counts """ - # Fixed range [0, 100] - min_val, max_val = 0, 100 - - # Number of bins is input size / 16 - num_bins = data.shape[0] // 16 - - # Clip values to range - clipped = torch.clamp(data, min_val, max_val) - - # Scale to bin indices - bin_width = (max_val - min_val) / num_bins - indices = ((clipped - min_val) / bin_width).long() - indices = torch.clamp(indices, 0, num_bins - 1) - - # Count values in each bin - return torch.bincount(indices, minlength=num_bins).to(torch.float32) \ No newline at end of file + return torch.bincount(data, minlength=256) diff --git a/problems/pmpp/histogram_py/task.py b/problems/pmpp/histogram_py/task.py index 61fd33787..807278685 100644 --- a/problems/pmpp/histogram_py/task.py +++ b/problems/pmpp/histogram_py/task.py @@ -6,5 +6,6 @@ class TestSpec(TypedDict): size: int - seed: int + seed: int + contention: int diff --git a/problems/pmpp/histogram_py/task.yml b/problems/pmpp/histogram_py/task.yml index 667ad8ea3..a49c9be93 100644 --- a/problems/pmpp/histogram_py/task.yml +++ b/problems/pmpp/histogram_py/task.yml @@ -21,13 +21,16 @@ config: main: "eval.py" tests: - - {"size": 5120, "seed": 9991} - - {"size": 7840, "seed": 2105} - - {"size": 30080, "seed": 9999} - - {"size": 100000, "seed": 1212} + - {"size": 5120, "seed": 9991, "contention": 10} + - {"size": 7840, "seed": 2105, "contention": 10} + - {"size": 30080, "seed": 9999, "contention": 10} + - {"size": 30080, "seed": 4254, "contention": 90} + - {"size": 100000, "seed": 1212, "contention": 10} benchmarks: - - {"size": 1310720, "seed": 6252} - - {"size": 2621440, "seed": 8841} - - {"size": 5242880, "seed": 6252} - - {"size": 10485760, "seed": 8841} + - {"size": 1310720, "seed": 6252, "contention": 10} + - {"size": 2621440, "seed": 8841, "contention": 10} + - {"size": 2621440, "seed": 3411, "contention": 40} + - {"size": 2621440, "seed": 8753, "contention": 90} + - {"size": 5242880, "seed": 6252, "contention": 10} + - {"size": 10485760, "seed": 8841, "contention": 10} diff --git a/problems/pmpp/utils.py b/problems/pmpp/utils.py index 4d8d4ebef..c3eb2447d 100644 --- a/problems/pmpp/utils.py +++ b/problems/pmpp/utils.py @@ -76,13 +76,43 @@ def verbose_allclose( mismatched_indices = torch.nonzero(mismatched) # Count the number of mismatched elements - num_mismatched = mismatched.sum().item() + num_mismatched = mismatched.count_nonzero().item() - # Check if all elements are close - all_close = num_mismatched == 0 + # Generate detailed information if there are mismatches + if num_mismatched >= 1: + mismatch_details = [f"Number of mismatched elements: {num_mismatched}"] + + for index in mismatched_indices[:max_print]: + i = tuple(index.tolist()) + mismatch_details.append(f"ERROR AT {i}: {received[i]} {expected[i]}") + if num_mismatched > max_print: + mismatch_details.append(f"... and {num_mismatched - max_print} more mismatched elements.") + return mismatch_details + + return [] + + +@torch.no_grad() +def verbose_allequal(received: torch.Tensor, expected: torch.Tensor, max_print: int=5): + """ + Assert that two tensors are element-wise perfectly equal, providing detailed information about mismatches. + + Parameters: + received (torch.Tensor): Tensor we actually got. + expected (torch.Tensor): Tensor we expected to receive. + max_print (int): Maximum number of mismatched elements to print. + + Returns: + Empty string if tensors are equal, otherwise detailed error information + """ + mismatched = torch.not_equal(received, expected) + mismatched_indices = torch.nonzero(mismatched) + + # Count the number of mismatched elements + num_mismatched = mismatched.count_nonzero().item() - # Raise AssertionError with detailed information if there are mismatches - if not all_close and num_mismatched >= 1: + # Generate detailed information if there are mismatches + if num_mismatched >= 1: mismatch_details = [f"Number of mismatched elements: {num_mismatched}"] for index in mismatched_indices[:max_print]: