From f692e007bf168f4b89a9ddd6c51b7600b61a4f61 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 20 Aug 2026 17:47:52 +0000 Subject: [PATCH 1/4] Initial plan From 6af44cfeeb53cbdfef7f40b2191273237c78c465 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 20 Aug 2026 17:54:26 +0000 Subject: [PATCH 2/4] Fix derived CuTracedArray construction Co-authored-by: ptiede <5565461+ptiede@users.noreply.github.com> --- ext/ReactantCUDAExt.jl | 15 +++++++++++---- test/integration/cuda.jl | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ext/ReactantCUDAExt.jl b/ext/ReactantCUDAExt.jl index 0b2cd80243..7f868bbcec 100644 --- a/ext/ReactantCUDAExt.jl +++ b/ext/ReactantCUDAExt.jl @@ -59,6 +59,9 @@ struct CuTracedArray{T,N,A,Size} <: DenseArray{T,N} ptr = Base.reinterpret(Core.LLVMPtr{T,CUDA.AS.Global}, Base.pointer_from_objref(xs)) return new(ptr) end + function CuTracedArray{T,N,A,Size}(ptr::Core.LLVMPtr{T,A}) where {T,N,A,Size} + return new(ptr) + end end Reactant.use_overlayed_version(::CuTracedArray) = true @@ -562,15 +565,13 @@ function Base.reinterpret(::Type{T}, a::CuTracedArray{S,N,A}) where {T,S,N,A} err === nothing || throw(err) if sizeof(T) == sizeof(S) # fast case - return CuTracedArray{T,N,A}( - reinterpret(Core.LLVMPtr{T,A}, a.ptr), size(a), a.maxsize - ) + return _derived_array(a, T, size(a)) end isize = size(a) size1 = div(isize[1] * sizeof(S), sizeof(T)) osize = tuple(size1, Base.tail(isize)...) - return CuTracedArray{T,N,A}(reinterpret(Core.LLVMPtr{T,A}, a.ptr), osize, a.maxsize) + return _derived_array(a, T, osize) end ## reshape @@ -589,6 +590,12 @@ function Base.reshape(a::CuTracedArray{T,M,A}, dims::NTuple{N,Int}) where {T,N,M return _derived_array(a, T, dims) end +@inline function _derived_array( + a::CuTracedArray{<:Any,<:Any,A}, ::Type{T}, osize::Dims{N} +) where {T,N,A} + return CuTracedArray{T,N,A,osize}(reinterpret(Core.LLVMPtr{T,A}, a.ptr)) +end + struct ReactantKernelAdaptor end function Adapt.adapt_storage(to::ReactantKernelAdaptor, p::CUDA.CuPtr) diff --git a/test/integration/cuda.jl b/test/integration/cuda.jl index 9ab811a74f..09dad6de6e 100644 --- a/test/integration/cuda.jl +++ b/test/integration/cuda.jl @@ -114,6 +114,25 @@ function sin!(x, y) return nothing end +function reshape_kernel!(out, x) + xr = reshape(x, 2, 2) + @inbounds out[1] = xr[1, 2] + xr[2, 1] + return nothing +end + +function reshape!(out, x) + @cuda blocks = 1 threads = 1 reshape_kernel!(out, x) + return nothing +end + +@testset "Reshape Kernel" begin + x = Reactant.to_rarray([1.0, 2.0, 3.0, 4.0]) + out = Reactant.to_rarray(zeros(Float64, 1)) + + @jit reshape!(out, x) + @test Array(out) == [5.0] +end + @testset "Sin Kernel" begin oA = collect(Float64, 1:1:64) A = Reactant.to_rarray(oA) From 090bc74025243f2225e787ff4bdfaea5a8639c1b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 20 Aug 2026 17:55:00 +0000 Subject: [PATCH 3/4] Tighten CUDA reshape regression test Co-authored-by: ptiede <5565461+ptiede@users.noreply.github.com> --- test/integration/cuda.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/cuda.jl b/test/integration/cuda.jl index 09dad6de6e..8336bc13f1 100644 --- a/test/integration/cuda.jl +++ b/test/integration/cuda.jl @@ -116,7 +116,7 @@ end function reshape_kernel!(out, x) xr = reshape(x, 2, 2) - @inbounds out[1] = xr[1, 2] + xr[2, 1] + @inbounds out[1] = xr[1, 2] return nothing end @@ -130,7 +130,7 @@ end out = Reactant.to_rarray(zeros(Float64, 1)) @jit reshape!(out, x) - @test Array(out) == [5.0] + @test Array(out) == [3.0] end @testset "Sin Kernel" begin From adc7e6b6cfb7398be071a43ce3275673cec1c2aa Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 20 Aug 2026 18:07:43 +0000 Subject: [PATCH 4/4] Use CUDA _derived_array when available Co-authored-by: ptiede <5565461+ptiede@users.noreply.github.com> --- ext/ReactantCUDAExt.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/ReactantCUDAExt.jl b/ext/ReactantCUDAExt.jl index 7f868bbcec..49162c60e4 100644 --- a/ext/ReactantCUDAExt.jl +++ b/ext/ReactantCUDAExt.jl @@ -15,7 +15,12 @@ using Reactant.Ops: @opcall using Enzyme using Adapt: Adapt, adapt using CUDA: CUDA, CuDim, DenseCuArray, unsafe_cached_load -# Compatibility for CUDA v5 and v6 +@static if isdefined(CUDA, :_derived_array) + # Extend CUDA's internal derived-array helper so traced arrays follow the + # same reshape/reinterpret reconstruction path when it is available; the + # CuTracedArray-specific method is defined alongside reshape below. + import CUDA: _derived_array +end const CUVERSION = isdefined(CUDA, :CUDACore) ? 6 : 5 if CUVERSION == 6