From 94b6e150774606ed294b7f96e440e0cf710939b1 Mon Sep 17 00:00:00 2001 From: shukebeta Date: Fri, 13 Nov 2026 00:52:39 +1300 Subject: [PATCH 1/2] Support Adapt and copy for WaveletOp WaveletOp now returns a WaveletOpImpl that keeps the shape without singleton dimensions, the wavelet and the host arrays of the transform. Adapt.adapt and copy rebuild the operator, its closures and host arrays from the shape and wavelet with the target storage type. The transform still runs on the CPU; non-Vector storage copies input and output through the host arrays, as before. Co-Authored-By: Claude Opus 5.5 --- docs/src/literate/howtos/gpu.jl | 3 +- .../LinearOperatorWaveletExt.jl | 1 + ext/LinearOperatorWaveletExt/WaveletOp.jl | 46 +++++++++++++++++-- test/testOperators.jl | 22 +++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/docs/src/literate/howtos/gpu.jl b/docs/src/literate/howtos/gpu.jl index 27830cb..ab4e5cf 100644 --- a/docs/src/literate/howtos/gpu.jl +++ b/docs/src/literate/howtos/gpu.jl @@ -23,8 +23,9 @@ LinearOperators.storage_type(fop) == typeof(storage) # Which backends are supported depends on the operator and the target. The following operators implement Adapt: # * `FFTOp`, `NFFTOp` (including its Toeplitz normal operator) and `RadonOp` # * `GradientOp`, `SamplingOp` and `WeightingOp` +# * `WaveletOp`. Its transform runs on the CPU; on another backend, input and output are copied through host arrays. # * `DiagOp`, `ProdOp`, `RowBlockOp` and `NormalOp`, and the normal operators of `DiagOp`, `ProdOp` and `RowBlockOp`. These are built from other operators and adapt them, so the target must also be supported by each of those operators. -# Other operators, such as `WaveletOp`, `DCTOp` and `DSTOp`, do not implement Adapt. +# Other operators, such as `DCTOp` and `DSTOp`, do not implement Adapt. # The target is the backing array type without element-type parameters, such as `Array`, `CuArray` or `ROCArray`. # LinearOperatorCollection does not support element-typed targets such as `Array{ComplexF64}` or `CuArray{Float32}`, even when the element type matches the operator. diff --git a/ext/LinearOperatorWaveletExt/LinearOperatorWaveletExt.jl b/ext/LinearOperatorWaveletExt/LinearOperatorWaveletExt.jl index a5b5139..68221f0 100644 --- a/ext/LinearOperatorWaveletExt/LinearOperatorWaveletExt.jl +++ b/ext/LinearOperatorWaveletExt/LinearOperatorWaveletExt.jl @@ -1,6 +1,7 @@ module LinearOperatorWaveletExt using LinearOperatorCollection, Wavelets +using Adapt include("WaveletOp.jl") diff --git a/ext/LinearOperatorWaveletExt/WaveletOp.jl b/ext/LinearOperatorWaveletExt/WaveletOp.jl index 6e238db..2421804 100644 --- a/ext/LinearOperatorWaveletExt/WaveletOp.jl +++ b/ext/LinearOperatorWaveletExt/WaveletOp.jl @@ -1,23 +1,61 @@ """ - WaveletOp(shape, wt=wavelet(WT.db2)) + WaveletOp(T::Type; shape::Tuple, wt=wavelet(WT.db2), S = Vector{T}) -returns a `ẀaveletOp <: AbstractLinearOperator`, which performs a Wavelet transform on +returns a `WaveletOp <: AbstractLinearOperator`, which performs a Wavelet transform on a given input array. +`Adapt.adapt(target, op)` and `copy(op)` rebuild the operator, with new temporary arrays, +from its shape and wavelet. The transform always runs on the CPU: with another storage type +than `Vector`, input and output are copied through host arrays. See +[Moving an Operator to Another Backend](@ref) for the supported targets. + # Arguments * `shape` - size of the Array to transform * (`wt=wavelet(WT.db2)`) - Wavelet to apply +* (`S = Vector{T}`) - storage type of the operator """ function LinearOperatorCollection.WaveletOp(::Type{T}; shape::Tuple, wt=wavelet(WT.db2), S = Vector{T}) where T <: Number - shape = filter(x-> x != 1, shape) # Drop dimension with 1 + shape = map(Int, filter(x-> x != 1, shape)) # Drop dimension with 1 tmp = Array{T}(undef, shape...) tmpRes = Array{T}(undef, shape...) - return LinearOperator(T, prod(shape), prod(shape), false, false + op = LinearOperator(T, prod(shape), prod(shape), false, false , (res,x)->prodwt!(res, x, wt, tmp, tmpRes) , nothing , (res,x)->ctprodwt!(res, x, wt, tmp, tmpRes), S = S) + return WaveletOpImpl(op, shape, wt, tmp, tmpRes) +end + +struct WaveletOpImpl{T, vecT <: AbstractVector, O <: AbstractLinearOperator{T}, N, W} <: WaveletOp{T} + op::O + # shape without the dimensions of size 1 + shape::NTuple{N, Int} + wt::W + # host arrays the transform reads from and writes to + tmp::Array{T, N} + tmpRes::Array{T, N} + function WaveletOpImpl(op::O, shape::NTuple{N, Int}, wt::W, tmp::Array{T, N}, tmpRes::Array{T, N}) where {T, O <: AbstractLinearOperator{T}, N, W} + return new{T, LinearOperators.storage_type(op), O, N, W}(op, shape, wt, tmp, tmpRes) + end +end + +function Base.getproperty(op::WaveletOpImpl, field::Symbol) + if in(field, (:op, :shape, :wt, :tmp, :tmpRes)) + return getfield(op, field) + else + return getproperty(getfield(op, :op), field) + end +end +Base.setproperty!(op::WaveletOpImpl, field::Symbol, value) = setproperty!(op.op, field, value) + +LinearOperators.storage_type(::WaveletOpImpl{T, vecT}) where {T, vecT} = vecT + +Base.copy(op::WaveletOpImpl) = WaveletOp(eltype(op); shape = op.shape, wt = op.wt, S = LinearOperators.storage_type(op)) + +function Adapt.adapt_structure(to, op::WaveletOpImpl) + target_storage = LinearOperatorCollection._adapt_target_storage(to, op.Mv, "WaveletOp") + return WaveletOp(eltype(op); shape = op.shape, wt = op.wt, S = typeof(target_storage)) end prodwt!(res::Vector{T}, x::Vector{T}, wt, tmp::Array{T, D}, tmpRes::Array{T, D}) where {T, D} = dwt!(reshape(res,size(tmpRes)), reshape(x,size(tmpRes)), wt) diff --git a/test/testOperators.jl b/test/testOperators.jl index a2e06b8..4c146af 100644 --- a/test/testOperators.jl +++ b/test/testOperators.jl @@ -268,6 +268,28 @@ function testWavelet(M=64,N=60;arrayType = Array) x_reco = reshape( adjoint(WOp)*x_wavelet, M, N) @test norm(x_reco - x) / norm(x) ≈ 0 atol=0.001 + + # Adapt and copy rebuild the shape without singleton dimensions, the wavelet and the host arrays + source = WaveletOp(Float64, shape=(M,1,N), wt=wavelet(WT.haar)) + @test createLinearOperator(WaveletOp{Float64}; shape=(M,N)) isa WaveletOp{Float64} + y = source*vec(x) + z = adjoint(source)*y + tmp = copy(rand!(source.tmp)) + target = Adapt.adapt(arrayType, source) + copied = copy(target) + back = Adapt.adapt(Array, copied) + @test source.tmp == tmp + for (op, S) in ((target, arrayType), (copied, arrayType), (source, Array), (back, Array), (target, arrayType)) + check_storage(op, S) + @test op isa WaveletOp{Float64} + @test size(op) == (M*N, M*N) && op.shape == (M,N) && op.wt === source.wt + @test Array(op*S(vec(x))) ≈ y + @test Array(adjoint(op)*S(y)) ≈ z + end + ops = (source, target, copied, back) + for field in (:op, :tmp, :tmpRes) + @test allunique(map(op -> objectid(getproperty(op, field)), ops)) + end end # test FourierOperators From a1875ef81d9a2b66c90d5d16296c426f9f2604a6 Mon Sep 17 00:00:00 2001 From: shukebeta Date: Fri, 13 Nov 2026 01:01:31 +1300 Subject: [PATCH 2/2] Run the WaveletOp Adapt test on Metal in Float32 The Adapt and copy checks move into testWaveletAdapt with an element-type argument. testWavelet calls it with Float64 as before, and test/gpu/metal.jl loads the testOperators.jl helpers and calls it with Float32. Co-Authored-By: Claude Opus 5.5 --- test/gpu/metal.jl | 7 +++++-- test/testOperators.jl | 17 +++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/test/gpu/metal.jl b/test/gpu/metal.jl index 32adc19..2fe4a74 100644 --- a/test/gpu/metal.jl +++ b/test/gpu/metal.jl @@ -13,13 +13,16 @@ using NFFT using RadonKA # Metal has no Float64, so runtests.jl cannot run here. This runs the Float32 and ComplexF32 -# nested operators, including SamplingOp ∘ FFTOp, moved to MtlArray and compared with the CPU. +# nested operators, including SamplingOp ∘ FFTOp, and a Float32 WaveletOp moved to MtlArray +# and compared with the CPU. -# Load the test helpers without the testsets of the file +# Load the test helpers without the testsets of the files include(ex -> Meta.isexpr(ex, :macrocall) ? nothing : ex, joinpath(@__DIR__(), "..", "testAdapt.jl")) +include(ex -> Meta.isexpr(ex, :macrocall) ? nothing : ex, joinpath(@__DIR__(), "..", "testOperators.jl")) @testset "LinearOperatorCollection on Metal" begin N = 16 @testset "ProdOp, NormalOpImpl, and ProdNormalOp recurse" check_nested_prod_adapt(MtlArray, N) @testset "SamplingOp ∘ FFTOp" check_sampling_fft_adapt(MtlArray, N) + @testset "WaveletOp" testWaveletAdapt(64, 60, Float32; arrayType = MtlArray) end diff --git a/test/testOperators.jl b/test/testOperators.jl index 4c146af..ea26a68 100644 --- a/test/testOperators.jl +++ b/test/testOperators.jl @@ -269,10 +269,15 @@ function testWavelet(M=64,N=60;arrayType = Array) @test norm(x_reco - x) / norm(x) ≈ 0 atol=0.001 - # Adapt and copy rebuild the shape without singleton dimensions, the wavelet and the host arrays - source = WaveletOp(Float64, shape=(M,1,N), wt=wavelet(WT.haar)) - @test createLinearOperator(WaveletOp{Float64}; shape=(M,N)) isa WaveletOp{Float64} - y = source*vec(x) + testWaveletAdapt(M, N, Float64; arrayType) +end + +# Adapt and copy rebuild the shape without singleton dimensions, the wavelet and the host arrays +function testWaveletAdapt(M, N, T; arrayType = Array) + x = rand(T, M*N) + source = WaveletOp(T, shape=(M,1,N), wt=wavelet(WT.haar)) + @test createLinearOperator(WaveletOp{T}; shape=(M,N)) isa WaveletOp{T} + y = source*x z = adjoint(source)*y tmp = copy(rand!(source.tmp)) target = Adapt.adapt(arrayType, source) @@ -281,9 +286,9 @@ function testWavelet(M=64,N=60;arrayType = Array) @test source.tmp == tmp for (op, S) in ((target, arrayType), (copied, arrayType), (source, Array), (back, Array), (target, arrayType)) check_storage(op, S) - @test op isa WaveletOp{Float64} + @test op isa WaveletOp{T} @test size(op) == (M*N, M*N) && op.shape == (M,N) && op.wt === source.wt - @test Array(op*S(vec(x))) ≈ y + @test Array(op*S(x)) ≈ y @test Array(adjoint(op)*S(y)) ≈ z end ops = (source, target, copied, back)