Skip to content

More tweaks#375

Draft
kshyatt wants to merge 5 commits intomainfrom
ksh/cuda_tweaks
Draft

More tweaks#375
kshyatt wants to merge 5 commits intomainfrom
ksh/cuda_tweaks

Conversation

@kshyatt
Copy link
Copy Markdown
Member

@kshyatt kshyatt commented Feb 18, 2026

Needed to get more MPSKit examples working

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 26, 2026

Codecov Report

❌ Patch coverage is 69.56522% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/tensors/abstracttensor.jl 50.00% 3 Missing ⚠️
src/tensors/braidingtensor.jl 0.00% 2 Missing ⚠️
ext/TensorKitCUDAExt/cutensormap.jl 91.66% 1 Missing ⚠️
src/auxiliary/auxiliary.jl 66.66% 1 Missing ⚠️
Files with missing lines Coverage Δ
ext/TensorKitCUDAExt/TensorKitCUDAExt.jl 100.00% <ø> (ø)
src/tensors/treetransformers.jl 96.22% <ø> (ø)
ext/TensorKitCUDAExt/cutensormap.jl 75.94% <91.66%> (+1.97%) ⬆️
src/auxiliary/auxiliary.jl 92.98% <66.66%> (-1.67%) ⬇️
src/tensors/braidingtensor.jl 67.46% <0.00%> (-0.83%) ⬇️
src/tensors/abstracttensor.jl 55.22% <50.00%> (+0.33%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kshyatt kshyatt marked this pull request as draft February 27, 2026 11:14
@kshyatt
Copy link
Copy Markdown
Member Author

kshyatt commented Feb 27, 2026

Let's make this a draft too to cut down on CI thrash

@kshyatt kshyatt force-pushed the ksh/cuda_tweaks branch 2 times, most recently from f5857b3 to 32e182d Compare March 12, 2026 12:36
@lkdvos lkdvos mentioned this pull request Mar 26, 2026
Copy link
Copy Markdown
Member

@lkdvos lkdvos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comment throughout, there are some things that I am not entirely convinced by but the rest looks great, thanks for working through all of this!

For the similarstoragetype(tensor, storagetype) calls that you added, this seems like something we should probably discuss over a separate PR, and it would be great if we could consolidate this one to get the remainder of the fixes in.
Would you be up for splitting these two things, and then getting this merged?

The same kind of holds for some of the comments I made too, if we can just postpone the things that are not obvious, but already get the other parts in, that would probably be helpful.

(Note that I am very much aware that none of this is your fault and this PR has lived for too long so the design shifts a bit, for which I do apologize!)

Comment on lines +181 to +188
function TensorKit.allocate_buffers(
tdst::CuTensorMap, tsrc::CuTensorMap, transformer::TensorKit.GenericTreeTransformer
)
sz = TensorKit.buffersize(transformer)
# force zeros to ensure the buffers are empty
# otherwise memory re-use can fill them with garbage data
return CUDA.zeros(eltype(tdst.data), sz), CUDA.zeros(eltype(tsrc.data), sz)
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slightly confusing to me, the zeros shouldn't be necessary (in fact, the implementation reuses the start of the buffer for each of the different blocks anyways), so I would have guessed that the similar(tsrc.data, sz) calls should be sufficient and correctly allocate device arrays here?

Mb = storagetype(T.b)
return promote_storagetype(Ma, Mb)
return promote_storagetype(T.a, T.b)
elseif eltype(T) isa Union
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to better support BlockTensorMap? Do we ever have tensors with union scalartypes?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's for the block case. I don't think we can have scalar unions?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit weird to support that here, since for generic AbstractTensorMap eltype would return a Number, which is why I asked about the scalartype thing. Maybe we can just copy this definition and overload in BlockTensorKit for AbstractBlockTensorMap?

Comment on lines +163 to +179
function TensorKit._add_general_kernel_nonthreaded!(
tdst::CuTensorMap, tsrc::CuTensorMap, p, transformer::TensorKit.GenericTreeTransformer, α, β, backend...
)
# preallocate buffers
buffers = TensorKit.allocate_buffers(tdst, tsrc, transformer)

for subtransformer in transformer.data
# Special case without intermediate buffers whenever there is only a single block
if length(subtransformer[1]) == 1
TensorKit._add_transform_single!(tdst, tsrc, p, subtransformer, α, β, backend...)
else
cu_subtransformer = tuple(CUDA.adapt(CuArray, subtransformer[1]), subtransformer[2:end]...)
TensorKit._add_transform_multi!(tdst, tsrc, p, cu_subtransformer, buffers, α, β, backend...)
end
end
return nothing
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the only change here is to promote the unitary basis transformation into a CuArray, which probably makes more sense to just support at the mul callsite (which I think @kshyatt already fixed, so this might no longer be required?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove it and see! 😈

Comment on lines +20 to +23
function TensorKit.blocktype(::Type{<:CuTensorMap{T, S}}) where {T, S}
return CuMatrix{T, CUDA.DeviceMemory}
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function TensorKit.blocktype(::Type{<:CuTensorMap{T, S}}) where {T, S}
return CuMatrix{T, CUDA.DeviceMemory}
end

I think this is now more properly addressed through type inference.

Comment on lines +108 to +110
function similarstoragetype(::Type{TT}, ::Type{T}) where {TT <: AbstractTensorMap, T <: Number}
return similarstoragetype(storagetype(TT), T)
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a formatting change right?

twistB = false
end

TTC = storagetype(C)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this effectively means that we are deciding to promote inputs to the storagetype of the output. I'm not sure if I am fully convinced that we should solve this automatically at all, since I think that is also inconsistent with how regular matrices work (same for adding):

julia> CUDA.rand(2, 2) * rand(Float32, 2, 2)
ERROR: Scalar indexing is disallowed.

I do think that this might be the right approach, and requiring explicit conversions in the cases of mixed inputs seems like the right call to me. (Even though I can see how that is annoying for MPSKit 😉 )

@kshyatt
Copy link
Copy Markdown
Member Author

kshyatt commented Mar 31, 2026

It's completely fine!! This has stayed open as I work through adding more tests for MPSKit, so I think we can pare off the simpler stuff we agree on, and then discuss things that are more contentious.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 31, 2026

Your PR no longer requires formatting changes. Thank you for your contribution!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants