File size: 19,684 Bytes
42f3769
 
1
2
3
{"prompt": "Write a complete Triton 3.7.1 kernel on Blackwell SM12x that **must use** `tl.atomic_and`.\nDefine exact input shapes/dtypes, include @triton.jit, launcher, masks, and torch.allclose test.\n\nContext:\nCategory: Atomic Ops\nAPI: tl.atomic_and\nSymbol: atomic_and (triton.language)", "response": "```python\nimport torch\nimport triton\nimport triton.language as tl\n\n\n# Exact input contract:\n#   x: contiguous CUDA tensor, shape (37, 1003), dtype=torch.int32\n# Output:\n#   out[col] = bitwise AND of x[:, col], shape (1003,), dtype=torch.int32\nN_ROWS = 37\nN_COLS = 1003\nBLOCK_SIZE = 256\n\n\n@triton.jit\ndef atomic_and_reduce_kernel(\n    x_ptr,\n    out_ptr,\n    n_rows: tl.constexpr,\n    n_cols: tl.constexpr,\n    BLOCK_SIZE: tl.constexpr,\n):\n    row = tl.program_id(axis=0)\n    col_block = tl.program_id(axis=1)\n\n    cols = col_block * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)\n    mask = (row < n_rows) & (cols < n_cols)\n\n    values = tl.load(\n        x_ptr + row * n_cols + cols,\n        mask=mask,\n        other=-1,\n    )\n    tl.atomic_and(out_ptr + cols, values, mask=mask)\n\n\ndef atomic_and_reduce(x: torch.Tensor) -> torch.Tensor:\n    assert x.is_cuda\n    assert x.is_contiguous()\n    assert x.dtype == torch.int32\n    assert x.shape == (N_ROWS, N_COLS)\n\n    # -1 has every int32 bit set, so it is the identity for bitwise AND.\n    out = torch.full(\n        (N_COLS,),\n        -1,\n        dtype=torch.int32,\n        device=x.device,\n    )\n\n    grid = (N_ROWS, triton.cdiv(N_COLS, BLOCK_SIZE))\n    atomic_and_reduce_kernel[grid](\n        x,\n        out,\n        n_rows=N_ROWS,\n        n_cols=N_COLS,\n        BLOCK_SIZE=BLOCK_SIZE,\n        num_warps=4,\n    )\n    return out\n\n\ndef reference_bitwise_and(x: torch.Tensor) -> torch.Tensor:\n    expected = torch.full(\n        (N_COLS,),\n        -1,\n        dtype=torch.int32,\n        device=x.device,\n    )\n    for row in range(N_ROWS):\n        expected = torch.bitwise_and(expected, x[row])\n    return expected\n\n\ndef main() -> None:\n    assert triton.__version__ == \"3.7.1\", (\n        f\"This example requires Triton 3.7.1, found {triton.__version__}\"\n    )\n    assert torch.cuda.is_available(), \"A CUDA GPU is required\"\n\n    capability = torch.cuda.get_device_capability()\n    assert capability[0] == 12, (\n        f\"Blackwell SM12x is required, found compute capability \"\n        f\"{capability[0]}.{capability[1]}\"\n    )\n\n    torch.manual_seed(1234)\n    x = torch.randint(\n        low=0,\n        high=1 << 30,\n        size=(N_ROWS, N_COLS),\n        dtype=torch.int32,\n        device=\"cuda\",\n    )\n\n    # Preserve a known set of bits across every row so the reduction is\n    # nontrivial instead of converging almost entirely to zero.\n    common_bits = torch.tensor(0x15555555, dtype=torch.int32, device=\"cuda\")\n    x = torch.bitwise_or(x, common_bits).contiguous()\n\n    actual = atomic_and_reduce(x)\n    expected = reference_bitwise_and(x)\n\n    torch.cuda.synchronize()\n    assert torch.allclose(actual, expected, rtol=0.0, atol=0.0), (\n        f\"Mismatch: maximum absolute difference \"\n        f\"{(actual.to(torch.int64) - expected.to(torch.int64)).abs().max().item()}\"\n    )\n\n    print(\"SPARKPROOF_TRITON_PASS\")\n\n\nif __name__ == \"__main__\":\n    main()\n```", "provider": "openai", "model": "gpt-4o-mini", "system": "You are a Triton 3.7.1 GPU kernel expert for Blackwell SM12x. Write complete runnable Python with @triton.jit, launcher, and torch.allclose test. Always end with print(\"SPARKPROOF_TRITON_PASS\") after tests pass.", "reasoning": null, "gateway": "yunwu", "api_base": "https://yunwu.ai/v1", "request_url": "https://yunwu.ai/v1/chat/completions", "gateway_model": "gpt-5.6-sol", "openrouter_model": "gpt-5.6-sol", "request_sha256": "8b2d9c399a74a9c7e0031155e0d911c8b539cd2989fdf34acc07cb121e9fb64f", "response_sha256": "86c3f67fcf4da7f6a2acf6b1d2ee2c67fc736d6efcfa4cbc679339e9abfa808c", "metadata": {"finish_reason": "stop", "native_finish_reason": null, "usage": {"prompt_tokens": 1582, "completion_tokens": 1839, "total_tokens": 3421, "prompt_tokens_details": {"cached_tokens": 0, "cache_write_tokens": 0}, "completion_tokens_details": {"reasoning_tokens": 1034}}, "gateway_generation_id": "chatcmpl-49ba0829-a0d7-4933-a1cb-cb2c1f9b00c2", "gateway_response_model": "gpt-5.6-sol", "gateway_reasoning_effort": "xhigh", "gateway_max_tokens": 4096, "gateway_temperature": 0.7, "prompt_meta": {"prompt": "Write a complete Triton 3.7.1 kernel on Blackwell SM12x that **must use** `tl.atomic_and`.\nDefine exact input shapes/dtypes, include @triton.jit, launcher, masks, and torch.allclose test.\n\nContext:\nCategory: Atomic Ops\nAPI: tl.atomic_and\nSymbol: atomic_and (triton.language)", "task_id": "api_tl_atomic_and", "source": "api_doc", "origin": "api_doc", "category": "kernel_write", "target_api": "tl.atomic_and", "doc_chunk_id": "api_tl_atomic_and", "title": "atomic_and", "split": "train"}, "tier": "gold", "gateway_model": "gpt-4o-mini"}, "sparkproof_validation": {"passed": true, "triton_version": "3.7.1", "stages": {"syntax": {"passed": true}, "triton_api": {"passed": true, "details": {"modern": true, "issues": [], "features_used": [], "deprecated_used": []}}, "compile_execute": {"passed": true, "output_tail": "SPARKPROOF_TRITON_PASS\nSPARKPROOF_TRITON_PASS\n"}, "benchmark": {"checks": {"correctness": 1.0, "autotune": 0.0, "block_tiling": 0.0, "launch_grid": 1.0, "blackwell_hint": 1.0}, "composite_score": 0.6}}, "code_sha256": "dfa544dee37ad4b9d4bbe0314f1eee844e50d411a2a4579cedd961b4c0e0225a", "benchmark": {"checks": {"correctness": 1.0, "autotune": 0.0, "block_tiling": 0.0, "launch_grid": 1.0, "blackwell_hint": 1.0}, "composite_score": 0.6}}}
{"prompt": "Your prior Triton 3.7.1 answer failed Blackwell validation.\nFailure: benchmark_below_floor\nTrace tail:\nSPARKPROOF_TRITON_PASS\nSPARKPROOF_TRITON_PASS\n\n\nReturn corrected **complete runnable Python** (kernel + launcher + torch.allclose test).\n\n```python\nimport torch\nimport triton\nimport triton.language as tl\n\n\n@triton.jit\ndef softmax_kernel(\n    out_ptr,\n    in_ptr,\n    in_stride_row,\n    out_stride_row,\n    ncols: tl.constexpr,\n    BLOCK: tl.constexpr,\n):\n    row = tl.program_id(0)\n    cols = tl.arange(0, BLOCK)\n    mask = cols < ncols\n\n    values = tl.load(\n        in_ptr + row * in_stride_row + cols,\n        mask=mask,\n        other=-float(\"inf\"),\n    ).to(tl.float32)\n\n    row_max = tl.max(values, axis=0)\n    numerators = tl.exp(values - row_max)\n    denominator = tl.sum(numerators, axis=0)\n    output = numerators / denominator\n\n    tl.store(\n        out_ptr + row * out_stride_row + cols,\n        output,\n        mask=mask,\n    )\n\n\ndef launch_softmax(x: torch.Tensor) -> torch.Tensor:\n    if x.ndim != 2:\n        raise ValueError(\"launch_softmax expects a 2D tensor\")\n    if not x.is_cuda:\n        raise ValueError(\"launch_softmax expects a CUDA tensor\")\n    if x.dtype not in (torch.float16, torch.bfloat16, torch.float32):\n        raise ValueError(\"supported dtypes are float16, bfloat16, and float32\")\n\n    rows, ncols = x.shape\n    if rows == 0:\n        return torch.empty_like(x)\n    if not 1 <= ncols <= 2048:\n        raise ValueError(\"ncols must be in the range [1, 2048]\")\n\n    x = x.contiguous()\n    out = torch.empty_like(x)\n\n    # BLOCK must always cover the complete row. Autotuning between BLOCK=1024\n    # and BLOCK=2048 is invalid for rows wider than 1024 because autotuning\n    # measures performance, not correctness.\n    block = triton.next_power_of_2(ncols)\n    num_warps = 8 if block >= 2048 else 4\n\n    softmax_kernel[(rows,)](\n        out,\n        x,\n        x.stride(0),\n        out.stride(0),\n        ncols=ncols,\n        BLOCK=block,\n        num_warps=num_warps,\n        num_stages=2,\n    )\n    return out\n\n\ndef test_kernel() -> None:\n    if not torch.cuda.is_available():\n        raise RuntimeError(\"CUDA is required\")\n\n    torch.manual_seed(0)\n\n    shapes = [\n        (8, 1),\n        (8, 127),\n        (8, 1003),\n        (5, 1024),\n        (3, 1537),\n        (2, 2048),\n    ]\n\n    for dtype in (torch.float32, torch.float16, torch.bfloat16):\n        for rows, ncols in shapes:\n            x = torch.randn(\n                (rows, ncols),\n                device=\"cuda\",\n                dtype=dtype,\n            )\n\n            actual = launch_softmax(x)\n            expected = torch.softmax(x, dim=-1)\n\n            if dtype == torch.float32:\n                rtol, atol = 1e-4, 1e-5\n            else:\n                rtol, atol = 2e-3, 2e-3\n\n            assert torch.allclose(actual, expected, rtol=rtol, atol=atol), (\n                f\"softmax mismatch for shape={(rows, ncols)}, dtype={dtype}, \"\n                f\"max_error={(actual.float() - expected.float()).abs().max().item()}\"\n            )\n\n\nif __name__ == \"__main__\":\n    test_kernel()\n    print(\"SPARKPROOF_TRITON_PASS\")\n\n```", "response": "```python\nimport torch\nimport triton\nimport triton.language as tl\n\n\n@triton.jit\ndef softmax_kernel(\n    out_ptr,\n    in_ptr,\n    nrows,\n    ncols: tl.constexpr,\n    BLOCK_N: tl.constexpr,\n    ROWS_PER_PROGRAM: tl.constexpr,\n):\n    program_id = tl.program_id(0)\n\n    row_offsets = (\n        program_id * ROWS_PER_PROGRAM\n        + tl.arange(0, ROWS_PER_PROGRAM)\n    )\n    col_offsets = tl.arange(0, BLOCK_N)\n\n    rows = row_offsets[:, None]\n    cols = col_offsets[None, :]\n    mask = (rows < nrows) & (cols < ncols)\n    offsets = rows * ncols + cols\n\n    values = tl.load(\n        in_ptr + offsets,\n        mask=mask,\n        other=-float(\"inf\"),\n    ).to(tl.float32)\n\n    row_max = tl.max(values, axis=1)\n    numerators = tl.exp(values - row_max[:, None])\n    reciprocal_sum = 1.0 / tl.sum(numerators, axis=1)\n    output = numerators * reciprocal_sum[:, None]\n\n    tl.store(out_ptr + offsets, output, mask=mask)\n\n\ndef _launch_config(rows: int, block_n: int) -> tuple[int, int]:\n    # Batch narrow rows into one program to avoid launching mostly idle CTAs.\n    if block_n <= 32:\n        rows_per_program = 32\n    elif block_n <= 64:\n        rows_per_program = 16\n    elif block_n <= 128:\n        rows_per_program = 8\n    elif block_n <= 256:\n        rows_per_program = 4\n    elif block_n <= 512:\n        rows_per_program = 2\n    else:\n        rows_per_program = 1\n\n    while rows_per_program > rows and rows_per_program > 1:\n        rows_per_program //= 2\n\n    tile_elements = block_n * rows_per_program\n    if block_n >= 2048:\n        num_warps = 8\n    elif tile_elements >= 512:\n        num_warps = 4\n    elif tile_elements >= 128:\n        num_warps = 2\n    else:\n        num_warps = 1\n\n    return rows_per_program, num_warps\n\n\ndef launch_softmax(x: torch.Tensor) -> torch.Tensor:\n    if x.ndim != 2:\n        raise ValueError(\"launch_softmax expects a 2D tensor\")\n    if not x.is_cuda:\n        raise ValueError(\"launch_softmax expects a CUDA tensor\")\n    if x.dtype not in (torch.float16, torch.bfloat16, torch.float32):\n        raise ValueError(\n            \"supported dtypes are float16, bfloat16, and float32\"\n        )\n\n    rows, ncols = x.shape\n    if not 1 <= ncols <= 2048:\n        raise ValueError(\"ncols must be in the range [1, 2048]\")\n    if rows == 0:\n        return torch.empty_like(x)\n\n    x = x.contiguous()\n    out = torch.empty_like(x)\n\n    block_n = triton.next_power_of_2(ncols)\n    rows_per_program, num_warps = _launch_config(rows, block_n)\n    grid = (triton.cdiv(rows, rows_per_program),)\n\n    softmax_kernel[grid](\n        out,\n        x,\n        rows,\n        ncols=ncols,\n        BLOCK_N=block_n,\n        ROWS_PER_PROGRAM=rows_per_program,\n        num_warps=num_warps,\n        num_stages=1,\n    )\n    return out\n\n\ndef test_kernel() -> None:\n    if not torch.cuda.is_available():\n        raise RuntimeError(\"CUDA is required\")\n\n    torch.manual_seed(0)\n\n    shapes = [\n        (8, 1),\n        (37, 31),\n        (65, 64),\n        (129, 127),\n        (67, 255),\n        (35, 511),\n        (17, 1003),\n        (5, 1024),\n        (3, 1537),\n        (2, 2048),\n    ]\n\n    for dtype in (torch.float32, torch.float16, torch.bfloat16):\n        for rows, ncols in shapes:\n            x = torch.randn(\n                (rows, ncols),\n                device=\"cuda\",\n                dtype=dtype,\n            )\n\n            actual = launch_softmax(x)\n            expected = torch.softmax(x, dim=-1)\n\n            if dtype == torch.float32:\n                rtol, atol = 1e-4, 1e-5\n            else:\n                rtol, atol = 2e-3, 2e-3\n\n            max_error = (\n                actual.float() - expected.float()\n            ).abs().max().item()\n\n            assert torch.allclose(\n                actual,\n                expected,\n                rtol=rtol,\n                atol=atol,\n            ), (\n                f\"softmax mismatch for shape={(rows, ncols)}, \"\n                f\"dtype={dtype}, max_error={max_error}\"\n            )\n\n    base = torch.randn(\n        (41, 257 * 2),\n        device=\"cuda\",\n        dtype=torch.float16,\n    )\n    noncontiguous = base[:, ::2]\n    actual = launch_softmax(noncontiguous)\n    expected = torch.softmax(noncontiguous, dim=-1)\n    assert torch.allclose(actual, expected, rtol=2e-3, atol=2e-3)\n\n\nif __name__ == \"__main__\":\n    test_kernel()\n    print(\"SPARKPROOF_TRITON_PASS\")\n```", "provider": "openai", "model": "gpt-4o-mini", "system": "You are a Triton 3.7.1 GPU kernel expert for Blackwell SM12x. Write complete runnable Python with @triton.jit, launcher, and torch.allclose test. Always end with print(\"SPARKPROOF_TRITON_PASS\") after tests pass.", "reasoning": null, "gateway": "yunwu", "api_base": "https://yunwu.ai/v1", "request_url": "https://yunwu.ai/v1/chat/completions", "gateway_model": "gpt-5.6-sol", "openrouter_model": "gpt-5.6-sol", "request_sha256": "cf588910f1dee334178b4b24b33ec89c3c2e58292ab63bddfbc8a0badb2a2a3b", "response_sha256": "6bc4573f4828d65bbdaa6504267c50cee8ad6219db3d312b463e43571e2845dc", "metadata": {"finish_reason": "stop", "native_finish_reason": null, "usage": {"prompt_tokens": 2363, "completion_tokens": 4451, "total_tokens": 6814, "prompt_tokens_details": {"cached_tokens": 0, "cache_write_tokens": 0}, "completion_tokens_details": {"reasoning_tokens": 3248}}, "gateway_generation_id": "chatcmpl-8ec7c592-fabb-411b-8ff9-71f090a95606", "gateway_response_model": "gpt-5.6-sol", "gateway_reasoning_effort": "xhigh", "gateway_max_tokens": 4096, "gateway_temperature": 0.7, "prompt_meta": {"prompt": "The following Triton 3.7.1 kernel fails or is numerically wrong. Diagnose the bug and return corrected, runnable code with a torch.allclose test. End with: print(\"SPARKPROOF_TRITON_PASS\"). Do NOT pass BLOCK= to the kernel launch when @triton.autotune is used.\n\n```python\nimport torch\nimport triton\nimport triton.language as tl\n\n@triton.autotune(configs=[triton.Config({'BLOCK': 1024}, num_warps=4, num_stages=2), triton.Config({'BLOCK': 2048}, num_warps=8, num_stages=2)], key=['ncols'])\n@triton.jit\ndef softmax_kernel(out_ptr, in_ptr, stride_row, ncols, BLOCK: tl.constexpr):\n    row = tl.program_id(0)\n    row_in = in_ptr + row * stride_row\n    cols = tl.arange(0, BLOCK)\n    mask = cols < ncols\n    vals = tl.load(row_in + cols)\n    vals = vals.to(tl.float32)\n    row_max = tl.max(vals, axis=0)\n    vals = vals - row_max\n    num = tl.exp(vals)\n    den = tl.sum(num, axis=0)\n    out = num / den\n    tl.store(out_ptr + row * stride_row + cols, out)\n\ndef launch_softmax(x: torch.Tensor) -> torch.Tensor:\n    rows, cols = x.shape\n    out = torch.empty_like(x)\n    grid = (rows,)\n    softmax_kernel[grid](out, x, x.stride(0), cols)\n    return out\n\ndef test_kernel():\n    x = torch.randn(8, 1003, device='cuda', dtype=torch.float32)\n    out = launch_softmax(x)\n    ref = torch.softmax(x, dim=-1)\n    assert torch.allclose(out, ref, rtol=0.001, atol=0.001)\ntest_kernel()\nprint('SPARKPROOF_TRITON_PASS')\n\n```", "task_id": "mutate_softmax_row_strip_boundary_mask", "source": "mutation", "origin": "mutation", "category": "debugging", "ground_truth_code": "\nimport torch\nimport triton\nimport triton.language as tl\n\n\n@triton.autotune(\n    configs=[\n        triton.Config({\"BLOCK\": 1024}, num_warps=4, num_stages=2),\n        triton.Config({\"BLOCK\": 2048}, num_warps=8, num_stages=2),\n    ],\n    key=[\"ncols\"],\n)\n@triton.jit\ndef softmax_kernel(out_ptr, in_ptr, stride_row, ncols, BLOCK: tl.constexpr):\n    row = tl.program_id(0)\n    row_in = in_ptr + row * stride_row\n    cols = tl.arange(0, BLOCK)\n    mask = cols < ncols\n    vals = tl.load(row_in + cols, mask=mask, other=-float(\"inf\"))\n    vals = vals.to(tl.float32)\n    row_max = tl.max(vals, axis=0)\n    vals = vals - row_max\n    num = tl.exp(vals)\n    den = tl.sum(num, axis=0)\n    out = num / den\n    tl.store(out_ptr + row * stride_row + cols, out, mask=mask)\n\n\ndef launch_softmax(x: torch.Tensor) -> torch.Tensor:\n    rows, cols = x.shape\n    out = torch.empty_like(x)\n    grid = (rows,)\n    softmax_kernel[grid](out, x, x.stride(0), cols)\n    return out\n\n\ndef test_kernel():\n    x = torch.randn(8, 1003, device=\"cuda\", dtype=torch.float32)\n    out = launch_softmax(x)\n    ref = torch.softmax(x, dim=-1)\n    assert torch.allclose(out, ref, rtol=1e-3, atol=1e-3)\n\n\ntest_kernel()\nprint(\"SPARKPROOF_TRITON_PASS\")\n", "broken_code": "import torch\nimport triton\nimport triton.language as tl\n\n@triton.autotune(configs=[triton.Config({'BLOCK': 1024}, num_warps=4, num_stages=2), triton.Config({'BLOCK': 2048}, num_warps=8, num_stages=2)], key=['ncols'])\n@triton.jit\ndef softmax_kernel(out_ptr, in_ptr, stride_row, ncols, BLOCK: tl.constexpr):\n    row = tl.program_id(0)\n    row_in = in_ptr + row * stride_row\n    cols = tl.arange(0, BLOCK)\n    mask = cols < ncols\n    vals = tl.load(row_in + cols)\n    vals = vals.to(tl.float32)\n    row_max = tl.max(vals, axis=0)\n    vals = vals - row_max\n    num = tl.exp(vals)\n    den = tl.sum(num, axis=0)\n    out = num / den\n    tl.store(out_ptr + row * stride_row + cols, out)\n\ndef launch_softmax(x: torch.Tensor) -> torch.Tensor:\n    rows, cols = x.shape\n    out = torch.empty_like(x)\n    grid = (rows,)\n    softmax_kernel[grid](out, x, x.stride(0), cols)\n    return out\n\ndef test_kernel():\n    x = torch.randn(8, 1003, device='cuda', dtype=torch.float32)\n    out = launch_softmax(x)\n    ref = torch.softmax(x, dim=-1)\n    assert torch.allclose(out, ref, rtol=0.001, atol=0.001)\ntest_kernel()\nprint('SPARKPROOF_TRITON_PASS')\n", "mutation_reason": "Removed boundary masks from tl.load/tl.store.", "task_family": "softmax_row", "split": "train"}, "tier": "repair", "gateway_model": "gpt-4o-mini"}, "sparkproof_validation": {"passed": true, "triton_version": "3.7.1", "stages": {"syntax": {"passed": true}, "triton_api": {"passed": true, "details": {"modern": true, "issues": [], "features_used": [], "deprecated_used": []}}, "compile_execute": {"passed": true, "output_tail": "SPARKPROOF_TRITON_PASS\nSPARKPROOF_TRITON_PASS\n"}, "benchmark": {"checks": {"correctness": 1.0, "autotune": 0.0, "block_tiling": 0.0, "launch_grid": 1.0, "blackwell_hint": 0.0}, "composite_score": 0.4}}, "code_sha256": "342365041a5128936f811acbf6861d281ecd4a4401c52a7a23606299f6b0157a", "benchmark": {"checks": {"correctness": 1.0, "autotune": 0.0, "block_tiling": 0.0, "launch_grid": 1.0, "blackwell_hint": 0.0}, "composite_score": 0.4}}}