Skip to content

Commit b92a58b

Browse files
leburgelclaude
andcommitted
Share the relative positions of subblock groups between transformers, and trim their other vectors to length
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ff5219f commit b92a58b

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

‎src/tensors/treetransformers.jl‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ end
107107
_cache_structure_size(t) = sizeof(t.structure_src) +
108108
(t.structure_src === t.structure_dst ? 0 : sizeof(t.structure_dst))
109109

110-
# The subblock groups hold their own position, offset and coefficient vectors.
110+
# The subblock groups hold their own offset and coefficient vectors; their relative positions are shared with
111+
# other transformers and counted by the cache of `subblockpositions`.
111112
function _cache_groups_size(t)
112113
bytes = sizeof(t.groups) + sizeof(t.large)
113114
for g in t.groups
114-
bytes += sizeof(g.rel_dst) + sizeof(g.rel_src) + sizeof(g.offset_dst) +
115-
sizeof(g.offset_src) + _cache_payload_size(g.coeff)
115+
bytes += sizeof(g.offset_dst) + sizeof(g.offset_src) + _cache_payload_size(g.coeff)
116116
end
117117
return bytes
118118
end
@@ -250,19 +250,24 @@ function subblockgroups(::Type{T}, data, structure_dst::Vector{StridedStructure{
250250
strides_dst = map((n, s) -> n == 1 ? 0 : s, sz, strides_dst)
251251
strides_src′ = map((n, s) -> n == 1 ? 0 : s, sz, TupleTools.getindices(strides_src, p))
252252
group = get!(groups, (accumulate, (sz, strides_dst, strides_src′))) do
253-
rel_dst, rel_src = Int32[], Int32[]
254-
for I in CartesianIndices(sz)
255-
push!(rel_dst, 1 + sum((Tuple(I) .- 1) .* strides_dst; init = 0))
256-
push!(rel_src, 1 + sum((Tuple(I) .- 1) .* strides_src′; init = 0))
257-
end
253+
rel_dst, rel_src = subblockpositions(sz, strides_dst), subblockpositions(sz, strides_src′)
258254
return SubblockGroup(rel_dst, rel_src, Int32[], Int32[], T[], accumulate)
259255
end
260256
push!(group.offset_dst, offset_dst)
261257
push!(group.offset_src, offset_src)
262258
push!(group.coeff, coeff)
263259
end
264-
return sort!(collect(values(groups)); by = group -> group.accumulate), large
260+
# `copy` drops the spare capacity that `push!` leaves
261+
groups′ = [SubblockGroup(g.rel_dst, g.rel_src, copy(g.offset_dst), copy(g.offset_src), copy(g.coeff), g.accumulate)
262+
for g in values(groups)]
263+
return sort!(groups′; by = group -> group.accumulate), copy(large)
264+
end
265+
266+
# the positions of the entries of a subblock relative to its offset, shared by all groups with the same shape and strides
267+
@cached function subblockpositions(sz::NTuple{N, Int}, strides::NTuple{N, Int})::Vector{Int32} where {N}
268+
return vec(Int32[1 + sum((Tuple(I) .- 1) .* strides; init = 0) for I in CartesianIndices(sz)])
265269
end
270+
MemoizationKit.default_cache_settings(::typeof(subblockpositions)) = (; measure = "bytes", maxsize = 2^28)
266271

267272
# the largest position addressed by the subblocks of `structure`
268273
_lastindex(structure) = maximum(((sz, str, offset),) -> offset + sum((sz .- 1) .* str; init = 0) + 1, structure; init = 0)

0 commit comments

Comments
 (0)