Skip to content

Mooncake on CUDA issue tracker #702

Open

Description

@Parvm1102

I ran Mooncake on GPU where the test sites for CPU were enabled (Mooncake 0.5.41, CUDA.jl 6.2.1, Flux 0.16.10, Julia 1.12.5).
CPU is fully green, but many fail on GPU. Here are my findings:

Already works on GPU (COO storage): GraphConv, SAGEConv, GINConv, EdgeConv, NNConv, ResGatedGraphConv, CGConv, MEGNetConv, GlobalPool, GlobalAttentionPool, GCNConv with add_self_loops=false, and all the propagate fast paths (copy_xj with +/mean, e_mul_xj, w_mul_xj). The rules in GNNlibMooncakeExt.

Zygote uses ChainRules for non-differentiable functions, Mooncake doesn't, so it the hits GPU code it can't differentiate:

  • add_self_loops for COO does nodes = convert(typeof(s), [1:n;]), i.e. builds a CPU vector and copies it to the GPU. That lands on a CPU→GPU unsafe_copyto! containing try/catch, which Mooncake can't trace (its cross-device copy rule only covers float arrays, and here the destination is an integer index array). It affects all layers with self loops: GCNConv (default), GATConv, GATv2Conv, TransformerConv, AGNNConv, SGConv, TAGConv, TGCN.
  • On :dense graphs, edge_index/to_coo call _findnz_idx, i.e. findall on a CuArray{Bool} (no Mooncake rule), and right after that v = A[nz] indexes with a vector of CartesianIndex (also no rule). This kills every layer on dense storage.

We can add GNNGraphs/ext/GNNGraphsMooncakeExt.jl (Mooncake as weakdep) giving Mooncake the same semantics the ChainRules markers give Zygote:

Mooncake.@zero_derivative Mooncake.DefaultCtx Tuple{typeof(add_self_loops), GNNGraph}
Mooncake.@zero_derivative Mooncake.DefaultCtx Tuple{typeof(GNNGraphs._findnz_idx), Any}
Mooncake.@zero_derivative Mooncake.DefaultCtx Tuple{typeof(to_coo), GNNGraphs.ADJMAT_T}
Mooncake.@zero_derivative Mooncake.DefaultCtx Tuple{typeof(Core.kwcall), NamedTuple, typeof(to_coo), GNNGraphs.ADJMAT_T}

The Core.kwcall rule is required because all in-repo call sites call to_coo with keyword arguments. I validated all of this by defining the rules via type piracy in a test script: with them in place, GCNConv (coo and dense), EdgeConv/dense, ResGatedGraphConv/dense, SGConv, AGNNConv, GMMConv and GatedGraphConv pass on GPU with gradients matching Zygote to ~1e-7.

Two caveats worth recording in the ext:

  • add_self_loops: same caveat as the existing @non_differentiable in transform.jl (the graph carries feature arrays; fine in practice since layers take x explicitly).
  • to_coo: Zygote only skips _findnz_idx and still differentiates A[nz], so it delivers dA for weighted dense adjacencies. A zero-derivative to_coo drops that under Mooncake. If Mooncake gains a CartesianIndex getindex rule upstream (item 4 below), we can drop the to_coo rules and keep full parity.

Upstream Mooncake.jl gaps found (I think I can open some issues on Mooncake regarding this):

  1. Correctness bug: broadcasts over non-contiguous SubArrays of CuArrays silently drop gradients. This is why GatedGraphConv returns wrong gradients with no error on GPU. Flux's GRUCell slices its gates with chunk(Wi*x, 3, dims=1).
  2. No kwcall rule for sum(x::CuArray; dims). It breaks GMMConv and other attention score computation.
  3. Cross-device unsafe_copyto! rule covers float destinations only, not integer.
  4. No getindex(::CuArray, ::Vector{CartesianIndex}) rule.
  5. CuMatrix + CuMatrix → cuBLAS.geam! has no rule (CuRef tangent type missing).
  6. No repeat(::CuArray, ...) rule (hits TGCNCell's repeat(h, 1, g.num_nodes)).
  7. task_local_storage() IdDict fdata mismatch, reached through CUDA's default RNG by dropout.

I will work on these once it gets green flag.

Activity

  1. changed the title [-]Mooncake on CUDA: add GNNGraphsMooncakeExt with non-differentiable rules for graph-structure functions[/-] [+]Mooncake on CUDA issue tracker[/+] on Jul 31, 2026
  2. Parvm1102 commented on Aug 11, 2026

    @Parvm1102
    ContributorAuthor

    Status update after #704, tested on Mooncake 0.5.45 and CUDA.jl 6.2.1.

    Fixed upstream by Mooncake 0.5.45:

    • Item 1 (wrong gradients through broadcasts over views): fixed. GatedGraphConv went from wrong by 0.036 to matching Zygote at 3.7e-9. It is now safe and green on CUDA.
    • Item 7 (IdDict error through CUDA's RNG in dropout): no longer reproduces.

    Fixed by #704:

    • Items 3 and 4 no longer affect us. The extension marks add_self_loops, _findnz_idx and integer _edge_values as zero-derivative, and v = A[nz] in to_coo / _to_coo_graph was rewritten to linear indexing, which Mooncake covers. The final design is smaller than the sketch above: 3 rules, no Core.kwcall rule, and float edge weights stay differentiable under Mooncake. Items 3 and 4 are still real upstream gaps, just not on our code paths anymore.

    Now green on CUDA under Mooncake:

    GCNConv (all configs, coo and dense), SGConv coo, GatedGraphConv, and all dense-storage sites (EdgeConv, NNConv, CGConv, MEGNetConv, ResGatedGraphConv, GNNlib e_mul_xj and w_mul_xj). Gradients match Zygote to 1.5e-8 or better. As a bonus, w_mul_xj on GPU now matches the CPU reference exactly under Mooncake, on the case where Zygote GPU is known broken (broken = true in the msgpass GPU tests).

    Still open, all upstream Mooncake: Item 2, 5, 6. I will report these to Mooncake.

    Remaining repo-side follow-ups: sg_conv only converts dense graphs to COO when edge weights are passed, aligning it with gcn_conv (always convert) would make unweighted dense SGConv green today. And once the upstream items land, enable Mooncake in the :gpu test items, since the harness currently only runs Zygote there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions