Skip to content

Commit ff5219f

Browse files
leburgelclaude
andcommitted
Store the positions of subblock groups as Int32
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6438de1 commit ff5219f

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

‎src/tensors/indexmanipulations.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ end
810810
function _add_subblockgroup!(A, B, op, group::SubblockGroup, terms, α, β)
811811
(; rel_dst, rel_src, offset_dst, offset_src, coeff) = group
812812
@inbounds for n in terms
813-
o₁, o₂, a = offset_dst[n], offset_src[n], α * coeff[n]
813+
o₁, o₂, a = Int(offset_dst[n]), Int(offset_src[n]), α * coeff[n]
814814
if group.accumulate
815815
@simd for k in eachindex(rel_dst)
816816
A[o₁ + rel_dst[k]] += a * op(B[o₂ + rel_src[k]])

‎src/tensors/treetransformers.jl‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ strides are permuted such that both refer to the same entry. Term `n` then compu
4444
`dst[offset_dst[n] .+ rel_dst] = α * coeff[n] * src[offset_src[n] .+ rel_src] + b * dst[offset_dst[n] .+ rel_dst]`,
4545
with `b = β` for the first term into every destination subblock and `b = 1` for the groups of
4646
further recoupling terms, which have `accumulate` set and are applied after all others.
47-
The terms are sorted by `offset_dst`.
47+
The terms are sorted by `offset_dst`. Offsets and relative positions are `Int32`; tensors with more
48+
than `typemax(Int32)` entries are not grouped.
4849
"""
4950
struct SubblockGroup{T}
50-
rel_dst::Vector{Int}
51-
rel_src::Vector{Int}
52-
offset_dst::Vector{Int}
53-
offset_src::Vector{Int}
51+
rel_dst::Vector{Int32}
52+
rel_src::Vector{Int32}
53+
offset_dst::Vector{Int32}
54+
offset_src::Vector{Int32}
5455
coeff::Vector{T}
5556
accumulate::Bool
5657
end
@@ -229,6 +230,9 @@ accumulating groups last, and return them together with the positions in `data`
229230
function subblockgroups(::Type{T}, data, structure_dst::Vector{StridedStructure{N}}, structure_src, p) where {T, N}
230231
terms = Tuple{T, Int, Int, Bool}[]
231232
large = Int[]
233+
# positions are stored as `Int32`
234+
(_lastindex(structure_dst) > typemax(Int32) || _lastindex(structure_src) > typemax(Int32)) &&
235+
return SubblockGroup{T}[], collect(eachindex(data))
232236
for (n, (U, inds_dst, inds_src)) in enumerate(data)
233237
if prod(first(structure_dst[first(inds_dst)])) > SUBBLOCKGROUP_MAXLENGTH
234238
push!(large, n)
@@ -246,12 +250,12 @@ function subblockgroups(::Type{T}, data, structure_dst::Vector{StridedStructure{
246250
strides_dst = map((n, s) -> n == 1 ? 0 : s, sz, strides_dst)
247251
strides_src′ = map((n, s) -> n == 1 ? 0 : s, sz, TupleTools.getindices(strides_src, p))
248252
group = get!(groups, (accumulate, (sz, strides_dst, strides_src′))) do
249-
rel_dst, rel_src = Int[], Int[]
253+
rel_dst, rel_src = Int32[], Int32[]
250254
for I in CartesianIndices(sz)
251255
push!(rel_dst, 1 + sum((Tuple(I) .- 1) .* strides_dst; init = 0))
252256
push!(rel_src, 1 + sum((Tuple(I) .- 1) .* strides_src′; init = 0))
253257
end
254-
return SubblockGroup(rel_dst, rel_src, Int[], Int[], T[], accumulate)
258+
return SubblockGroup(rel_dst, rel_src, Int32[], Int32[], T[], accumulate)
255259
end
256260
push!(group.offset_dst, offset_dst)
257261
push!(group.offset_src, offset_src)
@@ -260,6 +264,9 @@ function subblockgroups(::Type{T}, data, structure_dst::Vector{StridedStructure{
260264
return sort!(collect(values(groups)); by = group -> group.accumulate), large
261265
end
262266

267+
# the largest position addressed by the subblocks of `structure`
268+
_lastindex(structure) = maximum(((sz, str, offset),) -> offset + sum((sz .- 1) .* str; init = 0) + 1, structure; init = 0)
269+
263270
_push_terms!(terms, coeff::Number, idst::Int, isrc::Int) = push!(terms, (coeff, idst, isrc, false))
264271
function _push_terms!(terms, U::AbstractMatrix, inds_dst, inds_src)
265272
for (i, idst) in enumerate(inds_dst)

0 commit comments

Comments
 (0)