# GPU kernel compilation error

**URL:** <https://forum.modular.com/t/gpu-kernel-compilation-error/1504>\
**Category:** MAX\
**Created:** [May 24, 2025, 9:38am UTC](https://forum.modular.com/t/gpu-kernel-compilation-error/1504 "2025-05-24T09:38:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steepcurve](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@steepcurve](https://forum.modular.com/u/steepcurve)\
**Post date:** [May 24, 2025, 9:38am UTC](https://forum.modular.com/t/gpu-kernel-compilation-error/1504/1 "2025-05-24T09:38:40Z")

</div>

I’m trying to test a GPU kernel, and the code implementation is exactly the same as the content in this link: [https://github.com/modular/modular/blob/main/examples/custom\_ops/kernels/fused\_attention.mojo](https://github.com/modular/modular/blob/main/examples/custom_ops/kernels/fused_attention.mojo). However, I encountered a compilation error, which seems to be a shape mismatch.

```python
import torch

from pathlib import Path
from max.torch import CustomOpLibrary

mojo_kernels = Path( __file__ ).parent / "operations"
op_library = CustomOpLibrary(mojo_kernels)
mojo_fused_attention = op_library.fused_attention_custom[
    {
        "BN": 8,
        "BD": 32,
    }
]

def fused_attention(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor) -> torch.Tensor:
    m, _ = q.shape
    _, n = v.shape
    result = torch.zeros((m, n), dtype=q.dtype, device=q.device)
    mojo_fused_attention(result, q, k, v)
    return result

def main():
    device = "cuda" if torch.cuda.is_available() else "cpu"
    print(f"device: {device}")
    q = torch.randn((40, 128), dtype=torch.float32, device=device)
    k = torch.randn((40, 128), dtype=torch.float32, device=device)
    v = torch.randn((40, 128), dtype=torch.float32, device=device)
    print(fused_attention(q, k, v))
    
if __name__ == " __main__":
    main()

```

```auto
ValueError: Failed to run the MOToMGP pass manager:
open-source/max/max/kernels/src/Mogg/MOGGKernelAPI:1:1: error: failed to run the pass manager for offload functions
/content/modular-practice/custom_op/operations/attentions.mojo:314:4: error: call expansion failed
/content/modular-practice/custom_op/operations/attentions.mojo:314:4: note: function instantiation failed
/content/modular-practice/custom_op/operations/attentions.mojo:361:50: note: call expansion failed
note: call expansion failed
open-source/max/max/kernels/src/layout/layout_tensor.mojo:958:8: note: function instantiation failed
open-source/max/max/kernels/src/layout/layout_tensor.mojo:1027:18: note: call expansion failed
note: constraint failed: _elementwise_binary_with_broadcast requires shape to be the same for tensors of the same rank
open-source/max/max/kernels/src/Mogg/MOGGKernelAPI:1:1: error: Could not elaborate the provided code: failed to run the pass manager
error: The graph compiler tried to JIT compile the provided kernels but failed during elaboration

```

---

<div class="post-metadata">

**Author:** ![steepcurve](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@steepcurve](https://forum.modular.com/u/steepcurve)\
**Post date:** [May 27, 2025, 2:30am UTC](https://forum.modular.com/t/gpu-kernel-compilation-error/1504/2 "2025-05-27T02:30:15Z")

</div>

> [@steepcurve](#):
>
> [modular/examples/custom\_ops/kernels/fused\_attention.mojo at main · modular/modular · GitHub](https://github.com/modular/modular/blob/main/examples/custom_ops/kernels/fused_attention.mojo)

The GPU matrix multiplication implementation in the linked code lacks robustness, and I am currently working on replacing the matmul implementation.

---

<div class="post-metadata">

**Author:** ![Ehsan](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/ehsan/32/142_2.png) [@Ehsan](https://forum.modular.com/u/Ehsan)\
**Post date:** [May 30, 2025, 2:38am UTC](https://forum.modular.com/t/gpu-kernel-compilation-error/1504/3 "2025-05-30T02:38:54Z")

</div>

This one seems to be the Pytorch custom os issue and not the kernel itself. Could you please create an issue [here](https://github.com/modular/modular/issues)?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [November 26, 2025, 2:39am UTC](https://forum.modular.com/t/gpu-kernel-compilation-error/1504/4 "2025-11-26T02:39:09Z")

</div>

Closed on November 26, 2025, 2:39am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
