Skip to content

Commit 33931a1

Browse files
committed
simplify GPU transformers by bypassing some of the Adapt machinery - coefficient promotion is now allowed
1 parent 68c2038 commit 33931a1

1 file changed

Lines changed: 24 additions & 41 deletions

File tree

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -271,37 +271,6 @@ function DeviceGenericTreeTransformer(
271271
)
272272
end
273273

274-
"""
275-
StorageAdaptor(proto)
276-
277-
`Adapt` adaptor moving arrays onto the same device and array type as `proto`, preserving
278-
their element type. For `proto::CuVector{Float64}` and `array::Vector{Int}`,
279-
the call `adapt(typeof(proto), array)` would force-convert the element type `Int`
280-
to `Float64`, while `adapt(StoreAdaptor(proto), array)` does not.
281-
"""
282-
struct StorageAdaptor{A <: AbstractArray}
283-
proto::A
284-
end
285-
function Adapt.adapt_storage(a::StorageAdaptor, x::AbstractArray)
286-
dst = similar(a.proto, eltype(x), size(x))
287-
isempty(x) && return dst
288-
return copy!(dst, x)
289-
end
290-
291-
function Adapt.adapt_structure(to, t::DeviceUniqueTreeTransformer)
292-
return DeviceUniqueTreeTransformer(
293-
Adapt.adapt(to, t.blocks), Adapt.adapt(to, t.work_offsets), t.nwork
294-
)
295-
end
296-
297-
function Adapt.adapt_structure(to, t::DeviceGenericTreeTransformer)
298-
return DeviceGenericTreeTransformer(
299-
Adapt.adapt(to, t.unique_blocks), Adapt.adapt(to, t.blocks),
300-
Adapt.adapt(to, t.work_offsets), t.nwork, Adapt.adapt(to, t.coeffs),
301-
Adapt.adapt(to, t.structs_dst), Adapt.adapt(to, t.structs_src)
302-
)
303-
end
304-
305274
# Cache the device transformer directly, constructing the cached host version only on a miss.
306275
@cached function TensorKit.treebraider(
307276
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
@@ -336,18 +305,32 @@ function __init__()
336305
return nothing
337306
end
338307

339-
# We have this complicated setup because a naive `adapt` doesn't work.
340-
# Rather we copy everything to GPU-native arrays and have kernels that can work
341-
# with that.
342-
function device_transformer(A, transformer, p)
343-
# Allocations must outlive any GPUArrays allocation-cache scope around the operation.
344-
return GPUArrays.@uncached Adapt.adapt(
345-
StorageAdaptor(A(undef, 0)), _device_transformer(transformer, p)
346-
)
308+
# Allocations must outlive any GPUArrays allocation-cache scope around the operation.
309+
function device_transformer(A, transformer::UniqueTreeTransformer, p)
310+
packed = DeviceUniqueTreeTransformer(transformer, p)
311+
return GPUArrays.@uncached begin
312+
proto = A(undef, 0)
313+
# Metadata and coefficients have different element types; preserve each on upload.
314+
upload(x) = copy!(similar(proto, eltype(x), size(x)), x)
315+
DeviceUniqueTreeTransformer(upload(packed.blocks), upload(packed.work_offsets), packed.nwork)
316+
end
347317
end
348318

349-
_device_transformer(t::UniqueTreeTransformer, p) = DeviceUniqueTreeTransformer(t, p)
350-
_device_transformer(t::GenericTreeTransformer, p) = DeviceGenericTreeTransformer(t, p)
319+
function device_transformer(A, transformer::GenericTreeTransformer, p)
320+
packed = DeviceGenericTreeTransformer(transformer, p)
321+
return GPUArrays.@uncached begin
322+
proto = A(undef, 0)
323+
upload(x) = copy!(similar(proto, eltype(x), size(x)), x)
324+
unique = packed.unique_blocks
325+
device_unique = DeviceUniqueTreeTransformer(
326+
upload(unique.blocks), upload(unique.work_offsets), unique.nwork
327+
)
328+
DeviceGenericTreeTransformer(
329+
device_unique, upload(packed.blocks), upload(packed.work_offsets), packed.nwork,
330+
upload(packed.coeffs), upload(packed.structs_dst), upload(packed.structs_src)
331+
)
332+
end
333+
end
351334

352335
# COV_EXCL_START
353336
# kernels are not reachable by coverage

0 commit comments

Comments
 (0)