Skip to content

Commit ee184a9

Browse files
lkdvosclaude
andcommitted
Make the JET analysis clean
No behavioural changes; each hunk silences a report that JET cannot see through. Inference cannot narrow a `local` binding that is only assigned inside a branch, so replace the sentinel-by-undefinedness idiom with real sentinel values: `@cached`'s `hasparams`/`typed` flags become `nothing` checks on `params`/`typeex`, `planar_trace`'s `local i, j` becomes `i = j = 0`, and `_construct_braidingtensors!` tracks `V1`/`V2` as `nothing` rather than via `foundV1`/`foundV2`. `OneOrNoneIterator`'s default `state = true` is a `Bool`, but a recursive call passes an `Int`, so `if state` is reported as a non-boolean context; `isone` accepts both and keeps the `Bool` default. Untyped kernel arguments that inference can only narrow to a union get annotated, so the union split no longer reaches branches with no method: `_trace_permute!`, the three `tensoroperations` pullback primals, `_braid!` and `_transpose!` (`Union{AbstractTensorMap, TensorMapSpace}`), and the `UniqueTreeTransformer`/`GenericTreeTransformer` constructors (`::HomSpace`), whose only caller `TreeTransformer` always passes a `HomSpace`. Likewise `parenttype(::Type{Union{}})`, which `typeintersect` manufactures and which otherwise reaches no method at all. `fusiontensor` used `TupleTools.prod(d)` on a value inferred as `Union{Tuple, NamedTuple}`; `reshape(F, :, sz[end])` computes the same leading dimension without the union split. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 953f669 commit ee184a9

10 files changed

Lines changed: 33 additions & 31 deletions

File tree

‎src/auxiliary/caches.jl‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,16 @@ macro cached(ex)
5959
error("cached macro can only be used on function definitions")
6060
fcall = ex.args[1]
6161
if Meta.isexpr(fcall, :where)
62-
hasparams = true
6362
params = fcall.args[2:end]
6463
fcall = fcall.args[1]
6564
else
66-
hasparams = false
65+
params = nothing
6766
end
6867
if Meta.isexpr(fcall, :(::))
69-
typed = true
7068
typeex = fcall.args[2]
7169
fcall = fcall.args[1]
7270
else
73-
typed = false
71+
typeex = nothing
7472
end
7573
Meta.isexpr(fcall, :call) ||
7674
error("cached macro can only be used on function definitions")
@@ -91,14 +89,14 @@ macro cached(ex)
9189
# actual implenetation, with underscore name
9290
_fname = Symbol(:_, fname)
9391
_fcall = Expr(:call, _fname, fargs...)
94-
if hasparams
92+
if !isnothing(params)
9593
_fcall = Expr(:where, _fcall, params...)
9694
end
9795
_fex = Expr(:function, _fcall, _fbody)
9896

9997
# implementation that chooses the cache style
10098
newfcall = fcall
101-
if hasparams
99+
if !isnothing(params)
102100
newfcall = Expr(:where, newfcall, params...)
103101
end
104102
cachestylevar = gensym(:cachestyle)
@@ -112,11 +110,11 @@ macro cached(ex)
112110

113111
# nocache implementation
114112
fnocachecall = Expr(:call, fname, fargs..., :(::NoCache))
115-
if hasparams
113+
if !isnothing(params)
116114
fnocachecall = Expr(:where, fnocachecall, params...)
117115
end
118116
fnocachebody = :(@timeit_debug GLOBAL_TIMER $misslabel $(Expr(:call, _fname, fargnames...)))
119-
if typed
117+
if !isnothing(typeex)
120118
T = gensym(:T)
121119
fnocachebody = Expr(:block, Expr(:(=), T, typeex), Expr(:(::), fnocachebody, T))
122120
end
@@ -125,7 +123,7 @@ macro cached(ex)
125123
# tasklocal cache implementation
126124
Dvar = gensym(:D)
127125
flocalcachecall = Expr(:call, fname, fargs..., :(::TaskLocalCache{$Dvar}))
128-
if hasparams
126+
if !isnothing(params)
129127
flocalcachecall = Expr(:where, flocalcachecall, params..., Dvar)
130128
else
131129
flocalcachecall = Expr(:where, flocalcachecall, Dvar)
@@ -148,7 +146,7 @@ macro cached(ex)
148146
return @timeit_debug GLOBAL_TIMER $misslabel $_fname($(fargnames...))
149147
end
150148
)
151-
if typed
149+
if !isnothing(typeex)
152150
T = gensym(:T)
153151
flocalcachebody = Expr(
154152
:block,
@@ -169,12 +167,12 @@ macro cached(ex)
169167

170168
# # global cache implementation
171169
fglobalcachecall = Expr(:call, fname, fargs..., :(::GlobalLRUCache))
172-
if hasparams
170+
if !isnothing(params)
173171
fglobalcachecall = Expr(:where, fglobalcachecall, params...)
174172
end
175173
globalcachename = Symbol(:GLOBAL_, uppercase(string(fname)), :_CACHE)
176174
getglobalcachex = Expr(:(=), cachevar, globalcachename)
177-
if typed
175+
if !isnothing(typeex)
178176
T = gensym(:T)
179177
fglobalcachebody = Expr(
180178
:block,

‎src/auxiliary/iterators.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ struct OneOrNoneIterator{T}
44
end
55

66
function Base.iterate(it::OneOrNoneIterator, state = true)
7-
if state && it.cond
7+
if isone(state) && it.cond
88
return (it.first, false)
99
else
1010
return nothing

‎src/fusiontrees/duality_manipulations.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ function planar_trace(f::FusionTree, (q₁, q₂)::Index2Tuple)
657657
# We thus handle the total trace recursively, by first looking for and
658658
# tracing away neighbouring pairs.
659659
k = 1
660-
local i, j
660+
i = j = 0
661661
while k <= length(q₁)
662662
if mod1(q₁[k] + 1, length(f)) == q₂[k]
663663
i = q₁[k]

‎src/fusiontrees/fusiontrees.jl‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ function fusiontensor((f₁, f₂)::FusionTreePair)
338338
d1 = TupleTools.front(sz1)
339339
d2 = TupleTools.front(sz2)
340340
return reshape(
341-
reshape(F₁, TupleTools.prod(d1), sz1[end]) *
342-
reshape(F₂, TupleTools.prod(d2), sz2[end])', (d1..., d2...)
341+
reshape(F₁, :, sz1[end]) * reshape(F₂, :, sz2[end])', (d1..., d2...)
343342
)
344343
end
345344
fusiontensor(src::FusionTreeBlock) = sum(fusiontensor, fusiontrees(src))

‎src/planar/preprocessors.jl‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,18 @@ function _construct_braidingtensors!(ex, preargs, indexmap, non_braiding) # ex i
180180
i1a, i2a, = rightind
181181
end
182182

183-
foundV1, foundV2 = false, false
183+
V1 = V2 = nothing
184184
if haskey(indexmap, i1a)
185185
V1 = indexmap[i1a]
186-
foundV1 = true
187186
elseif haskey(indexmap, i1b)
188187
V1 = Expr(:call, :dual, indexmap[i1b])
189-
foundV1 = true
190188
end
191189
if haskey(indexmap, i2a)
192190
V2 = indexmap[i2a]
193-
foundV2 = true
194191
elseif haskey(indexmap, i2b)
195192
V2 = Expr(:call, :dual, indexmap[i2b])
196-
foundV2 = true
197193
end
198-
if foundV1 && foundV2
194+
if !isnothing(V1) && !isnothing(V2)
199195
s = gensym(:τ)
200196
storageex = Expr(:call, GlobalRef(TensorKit, :promote_storagetype), non_braiding...)
201197
braidingex = Expr(:call, GlobalRef(TensorKit, :braidingtensortype), V1, V2, storageex)

‎src/pullbacks/tensoroperations.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function blas_contract_pullback_ΔA!(
2-
ΔA, ΔC, A, pA, B, pB, pAB, α, backend, allocator
2+
ΔA, ΔC, A::AbstractTensorMap, pA, B::AbstractTensorMap, pB, pAB, α, backend, allocator
33
)
44
ipAB = invperm(linearize(pAB))
55
pΔC = TO.repartition(ipAB, TO.numout(pA))
@@ -23,7 +23,7 @@ function blas_contract_pullback_ΔA!(
2323
end
2424

2525
function blas_contract_pullback_ΔB!(
26-
ΔB, ΔC, A, pA, B, pB, pAB, α, backend, allocator
26+
ΔB, ΔC, A::AbstractTensorMap, pA, B::AbstractTensorMap, pB, pAB, α, backend, allocator
2727
)
2828
ipAB = invperm(linearize(pAB))
2929
pΔC = TO.repartition(ipAB, TO.numout(pA))
@@ -48,7 +48,7 @@ function blas_contract_pullback_ΔB!(
4848
end
4949

5050
function trace_permute_pullback_ΔA!(
51-
ΔA, ΔC, A, p, q, α, backend
51+
ΔA, ΔC, A::AbstractTensorMap, p, q, α, backend
5252
)
5353
ip = invperm((linearize(p)..., q[1]..., q[2]...))
5454
pdA = TO.repartition(ip, numout(A))

‎src/tensors/adjoint.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ end
1313
Base.parent(t::AdjointTensorMap) = t.parent
1414
parenttype(t::AdjointTensorMap) = parenttype(typeof(t))
1515
parenttype(::Type{AdjointTensorMap{T, S, N₁, N₂, TT}}) where {T, S, N₁, N₂, TT} = TT
16+
parenttype(::Type{Union{}}) = throw(MethodError(parenttype, (Union{},)))
1617

1718
# Constructor: construct from taking adjoint of a tensor
1819
Base.adjoint(t::AdjointTensorMap) = parent(t)

‎src/tensors/indexmanipulations.jl‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ function _dense_transform!(tdst, tsrc, p::Index2Tuple, conjsrc::Bool, α, β, ba
602602
end
603603

604604
@propagate_inbounds function _braid!(
605-
tdst, tsrc, p::Index2Tuple, conjsrc::Bool, levels::IndexTuple, α, β, backend, allocator
605+
tdst::AbstractTensorMap, tsrc::AbstractTensorMap, p::Index2Tuple, conjsrc::Bool,
606+
levels::IndexTuple, α, β, backend, allocator
606607
)
607608
@boundscheck spacecheck_transform(permute, tdst, tsrc, p, conjsrc)
608609
all(has_array_view, (tdst, tsrc)) && return _dense_transform!(tdst, tsrc, p, conjsrc, α, β, backend, allocator)
@@ -612,7 +613,8 @@ end
612613

613614
# counterpart of `_braid!` for `transpose!`; the cyclicity of `p` is checked by the caller
614615
@propagate_inbounds function _transpose!(
615-
tdst, tsrc, p::Index2Tuple, conjsrc::Bool, α, β, backend, allocator
616+
tdst::AbstractTensorMap, tsrc::AbstractTensorMap, p::Index2Tuple, conjsrc::Bool,
617+
α, β, backend, allocator
616618
)
617619
@boundscheck spacecheck_transform(permute, tdst, tsrc, p, conjsrc)
618620
all(has_array_view, (tdst, tsrc)) && return _dense_transform!(tdst, tsrc, p, conjsrc, α, β, backend, allocator)

‎src/tensors/tensoroperations.jl‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ function trace_permute!(
236236
return tdst
237237
end
238238

239-
function _trace_permute!(::UniqueFusion, tdst, tsrc, (p₁, p₂), (q₁, q₂), α, β, backend)
239+
function _trace_permute!(
240+
::UniqueFusion, tdst::AbstractTensorMap, tsrc::AbstractTensorMap,
241+
(p₁, p₂), (q₁, q₂), α, β, backend
242+
)
240243
scale!(tdst, β)
241244
r₁, r₂ = (p₁..., q₁...), (p₂..., q₂...)
242245
N₁, N₂ = length(p₁), length(p₂)
@@ -261,7 +264,10 @@ function _trace_permute!(::UniqueFusion, tdst, tsrc, (p₁, p₂), (q₁, q₂),
261264
return tdst
262265
end
263266

264-
function _trace_permute!(::FusionStyle, tdst, tsrc, (p₁, p₂), (q₁, q₂), α, β, backend)
267+
function _trace_permute!(
268+
::FusionStyle, tdst::AbstractTensorMap, tsrc::AbstractTensorMap,
269+
(p₁, p₂), (q₁, q₂), α, β, backend
270+
)
265271
scale!(tdst, β)
266272
r₁, r₂ = (p₁..., q₁...), (p₂..., q₂...)
267273
N₁, N₂ = length(p₁), length(p₂)

‎src/tensors/treetransformers.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct GenericTreeTransformer{T, N} <: TreeTransformer
4747
structure_src::Vector{StridedStructure{N}}
4848
end
4949

50-
function UniqueTreeTransformer(transform, p, Vdst, Vsrc, conjsrc::Bool)
50+
function UniqueTreeTransformer(transform, p, Vdst::HomSpace, Vsrc::HomSpace, conjsrc::Bool)
5151
t₀ = Base.time()
5252

5353
spacecheck_transform(permute, Vdst, Vsrc, p, conjsrc)
@@ -72,7 +72,7 @@ function UniqueTreeTransformer(transform, p, Vdst, Vsrc, conjsrc::Bool)
7272
return transformer
7373
end
7474

75-
function GenericTreeTransformer(transform, p, Vdst, Vsrc, conjsrc::Bool)
75+
function GenericTreeTransformer(transform, p, Vdst::HomSpace, Vsrc::HomSpace, conjsrc::Bool)
7676
t₀ = Base.time()
7777
spacecheck_transform(permute, Vdst, Vsrc, p, conjsrc)
7878
# the fusion blocks that are transformed are those of the adjoint space for a conjugated source

0 commit comments

Comments
 (0)