# Big PDESystem exceeds maximal parameter space of CUDA kernels

**URL:** <https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047>\
**Category:** Modelling & Simulations\
**Tags:** question, cuda, sciml\
**Created:** [July 1, 2023, 9:30am UTC](https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047 "2023-07-01T09:30:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drsk/32/46545_2.png) [@drsk](https://discourse.julialang.org/u/drsk)\
**Post date:** [July 1, 2023, 9:30am UTC](https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047/1 "2023-07-01T09:30:26Z")

</div>

I’m using `NeuralPDE.jl` to find critical points of a geometric flow equation on the four-torus. This ends up being a fairly big system, with 18 variables in total. I think the size of the system makes the CUDA compiler generate kernels that exceed the maximal parameter space, i.e. the issue described here: [Guard against exceeding maximum kernel parameter size · Issue #32 · JuliaGPU/CUDA.jl · GitHub](https://github.com/JuliaGPU/CUDA.jl/issues/32). Is there a way to avoid this, maybe a parameter for the CUDA compilation? Or is this a bug in the way the kernel gets assembled in the first place from the `PDESystem`?

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [July 3, 2023, 7:47am UTC](https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047/2 "2023-07-03T07:47:38Z")

</div>

> [@drsk](#):
>
> Is there a way to avoid this, maybe a parameter for the CUDA compilation?

There’s a suggestion in that issue: Pass the large argument as a one-element array.

---

<div class="post-metadata">

**Author:** ![drsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drsk/32/46545_2.png) [@drsk](https://discourse.julialang.org/u/drsk)\
**Post date:** [July 3, 2023, 8:32am UTC](https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047/3 "2023-07-03T08:32:09Z")

</div>

Thanks for the answer @maleadt ! I did see the suggestion, but I don’t know how I can control the generated CUDA code. Is there a way I can mangle with it after the automatic generation? Or are you suggesting finding whatever is generating these huge tuple arguments and patching that library?

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [July 4, 2023, 5:48am UTC](https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047/4 "2023-07-04T05:48:13Z")

</div>

> [@drsk](#):
>
> Or are you suggesting finding whatever is generating these huge tuple arguments and patching that library?

Yes. You can’t just patch up the generated code, as changing the argument from a value to reference type changes the calling convention (i.e. requires you to pass a pointer to memory, e.g., by using an Array).

---

<div class="post-metadata">

**Author:** ![drsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drsk/32/46545_2.png) [@drsk](https://discourse.julialang.org/u/drsk)\
**Post date:** [July 4, 2023, 7:26am UTC](https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047/5 "2023-07-04T07:26:08Z")

</div>

That makes sense. I can see that `Lux.jl` is creating these named tuples, but it could also be that `ComponentArrays.jl` introduces the problem. The offending line of code is

```julia
[Lux.setup(Random.default_rng(), c)[1] |> ComponentArray |> gpu .|> Float64 for c in chains]

```

I’ll try to pinpoint it. I’m a bit surprised that I’m the first one to hit this problem, as this seems to be a fairly common way of using `Lux.jl` parameters on the GPU.

---

<div class="post-metadata">

**Author:** ![drsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drsk/32/46545_2.png) [@drsk](https://discourse.julialang.org/u/drsk)\
**Post date:** [July 5, 2023, 1:20pm UTC](https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047/6 "2023-07-05T13:20:08Z")

</div>

This was caused by a more complicated PDE equation, which made NeuralPDE.jl compile a fairly complicated loss function. I simplified the constraints by reformulating them into several smaller ones. This solves the problem for me. I’m not sure yet if I’m paying a performance penalty for this though.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 5, 2023, 1:27pm UTC](https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047/7 "2023-07-05T13:27:29Z")

</div>

worth opening an issue to track

---

<div class="post-metadata">

**Author:** ![drsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drsk/32/46545_2.png) [@drsk](https://discourse.julialang.org/u/drsk)\
**Post date:** [July 6, 2023, 6:48am UTC](https://discourse.julialang.org/t/big-pdesystem-exceeds-maximal-parameter-space-of-cuda-kernels/101047/8 "2023-07-06T06:48:06Z")

</div>

@ChrisRackauckas I think this is essentially a CUDA.jl and not so much a NeuralPDE.jl problem. The issue is tracked here [Guard against exceeding maximum kernel parameter size · Issue #32 · JuliaGPU/CUDA.jl · GitHub](https://github.com/JuliaGPU/CUDA.jl/issues/32).
