Skip to content

Conversation

@jumerckx
Copy link
Collaborator

While in my testing traced_type now does the typevar promotion we talked about, it doesn't yet work for make_tracer as the result from traced_types isn't used to guide make_tracer AFAICT.

using Reactant

struct Bar{T}
    b::T
end
struct Foo{T, B<:Bar{T}, AT<:AbstractArray{T}}
    a::AT
    b::B
end

foo = Reactant.to_rarray(Foo(rand(3), Bar(1.)))

Reactant.traced_type(typeof(foo), Val(Reactant.ConcreteToTraced), Union{}, nothing, nothing)
# returns: Foo{Reactant.TracedRNumber{Float64}, Bar{Reactant.TracedRNumber{Float64}}, Reactant.TracedRArray{Float64, 1}}

Reactant.make_tracer(Reactant.OrderedIdDict(), foo, (), Reactant.ConcreteToTraced) # fails
ERROR: AssertionError: Could not recursively make tracer of object of type Foo{Float64, Bar{Float64}, ConcretePJRTArray{Float64, 1, 1, Reactant.Sharding.ShardInfo{Reactant.Sharding.NoSharding, Nothing}}} into Foo{Reactant.TracedRNumber{Float64}, Bar{Reactant.TracedRNumber{Float64}}, Reactant.TracedRArray{Float64, 1}} at field 2 (named b), need object of type Bar{Reactant.TracedRNumber{Float64}} found object of type Bar{Float64} 
Stacktrace:
 [1] make_tracer_unknown(seen::Reactant.OrderedIdDict{…}, prev::Any, path::Any, mode::Reactant.TraceMode; track_numbers::Type, sharding::Any, runtime::Any, kwargs::@Kwargs{})
   @ Reactant ~/Reactant2/src/Tracing.jl:1197
 [2] make_tracer_unknown
   @ ~/Reactant2/src/Tracing.jl:1095 [inlined]
 [3] #make_tracer#139
   @ ~/Reactant2/src/Tracing.jl:1232 [inlined]
 [4] make_tracer(seen::Reactant.OrderedIdDict{Any, Any}, prev::Any, path::Any, mode::Reactant.TraceMode)
   @ Reactant ~/Reactant2/src/Tracing.jl:1222
 [5] top-level scope
   @ ~/Reactant2/make_tracer_debuggin.jl:12
Some type information was truncated. Use `show(err)` to see complete types.

@jumerckx jumerckx requested a review from wsmoses September 18, 2025 08:36
src/Tracing.jl Outdated
# The field is constrained by a TypeVar directly,
# so we don't need to check.
# (The check below would fail if the typevar was promoted as
# we don't get the same result when calling traced_type_inner
Copy link
Member

Choose a reason for hiding this comment

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

don't we still need to check below that the subtype matches, even if constrained? just to confirm we did do the typevar solve correctly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure...
The reason I added this early return is for e.g.:
Foo{Float64, Bar{Float64}, ConcretePJRTArray{Float64, 1}}
Which, with ConcreteToTraced is now converted to:
Foo{TracedRNumber{Float64}, Bar{TracedRNumber{Float64}}, TracedRArray{Float64, 1}}

The field b of the original type is Bar{Float64}. When tracing this, it just returns Bar{Float64} because of lack of constraints. So what should we compare the fieldtype against?

Copy link
Member

Choose a reason for hiding this comment

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

struct T
   d::Array
end

Copy link
Member

@wsmoses wsmoses Sep 24, 2025

Choose a reason for hiding this comment

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

struct T{D<:Array}
   d::D
end


struct T{D<:AbstractArray{Float64}}
   d::D
end

Copy link
Member

Choose a reason for hiding this comment

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

check can be skipped when an unconstrained typevar

  • must be skipped when that typevar is itself a dependency of another type. If this is true in any case, we need the fancy make_tracer
  • may be run when that typevar is itself not a dependency of another type. If this is true in all cases, we don't need the upgrade

Copy link
Member

Choose a reason for hiding this comment

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

case where need to skip checj

struct MyNewArray{FT, N, D <: AbstractArray{FT, N}}
    data::D
    off::FT
end

This function tries to apply the param types to the wrapper type.
When there's a constraint conflict, it tries to resolve it by promoting the conflicting types. The new param type is then propagated in any param type that depends on it.
"""
function apply_type_with_promotion(wrapper, params, relevant_typevars=typevar_dict(wrapper))
Copy link
Member

Choose a reason for hiding this comment

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

can we add some tests?

src/Tracing.jl Outdated
function collect_tvars_in_type!(dependencies, @nospecialize(t))
if t isa TypeVar
push!(dependencies, t)
return
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
return
return nothing

src/Tracing.jl Outdated
return T
end

@debug "traced_type_inner: Processing type with field changes" T=T subTys=subTys
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@debug "traced_type_inner: Processing type with field changes" T=T subTys=subTys
@debug "traced_type_inner: Processing type with field changes" T = T subTys = subTys

src/Tracing.jl Outdated
wrapped_cifrt_array = T <: AbstractArray && ancestor(T) <: ConcreteIFRTArray
wrapped_tracedarray = T <: AbstractArray && ancestor(T) <: TracedRArray

@debug "wrapped flags" wrapped_cpjrt_array=wrapped_cpjrt_array wrapped_cifrt_array=wrapped_cifrt_array wrapped_tracedarray=wrapped_tracedarray
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@debug "wrapped flags" wrapped_cpjrt_array=wrapped_cpjrt_array wrapped_cifrt_array=wrapped_cifrt_array wrapped_tracedarray=wrapped_tracedarray
@debug "wrapped flags" wrapped_cpjrt_array = wrapped_cpjrt_array wrapped_cifrt_array =
wrapped_cifrt_array wrapped_tracedarray = wrapped_tracedarray

src/Tracing.jl Outdated
@debug "wrapped flags" wrapped_cpjrt_array=wrapped_cpjrt_array wrapped_cifrt_array=wrapped_cifrt_array wrapped_tracedarray=wrapped_tracedarray

subParms = []
@debug "Tracing type parameters" num_params=length(T.parameters) T_parameters=T.parameters
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@debug "Tracing type parameters" num_params=length(T.parameters) T_parameters=T.parameters
@debug "Tracing type parameters" num_params = length(T.parameters) T_parameters =
T.parameters

src/Tracing.jl Outdated
end
end

@debug "Built subParms" subParms=subParms
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@debug "Built subParms" subParms=subParms
@debug "Built subParms" subParms = subParms

src/Tracing.jl Outdated
return TT2
end
else
@debug "Field count mismatch" fieldcount_T=fieldcount(T) fieldcount_TT2=fieldcount(TT2)
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@debug "Field count mismatch" fieldcount_T=fieldcount(T) fieldcount_TT2=fieldcount(TT2)
@debug "Field count mismatch" fieldcount_T = fieldcount(T) fieldcount_TT2 = fieldcount(
TT2
)

src/Tracing.jl Outdated
xi2 = Core.Typeof(xi2)((newpath,), xi2.mlir_data)
seen[xi2] = xi2
changed = true
elseif !ismutabletype(FT) && !ismutabletype(Core.Typeof(xi2)) && fieldcount(FT) == fieldcount(Core.Typeof(xi2))
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
elseif !ismutabletype(FT) && !ismutabletype(Core.Typeof(xi2)) && fieldcount(FT) == fieldcount(Core.Typeof(xi2))
elseif !ismutabletype(FT) &&
!ismutabletype(Core.Typeof(xi2)) &&
fieldcount(FT) == fieldcount(Core.Typeof(xi2))

src/Tracing.jl Outdated
val_wrapped = ft_j(val_j)
# Correct the path for the wrapped scalar
sub_path = append_path(newpath, j)
val_wrapped = Core.Typeof(val_wrapped)((sub_path,), val_wrapped.mlir_data)
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
val_wrapped = Core.Typeof(val_wrapped)((sub_path,), val_wrapped.mlir_data)
val_wrapped = Core.Typeof(val_wrapped)(
(sub_path,), val_wrapped.mlir_data
)

src/Tracing.jl Outdated
end

if success
xi2 = ccall(:jl_new_structv, Any, (Any, Ptr{Any}, UInt32), FT, flds_sub, fieldcount(FT))
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
xi2 = ccall(:jl_new_structv, Any, (Any, Ptr{Any}, UInt32), FT, flds_sub, fieldcount(FT))
xi2 = ccall(
:jl_new_structv,
Any,
(Any, Ptr{Any}, UInt32),
FT,
flds_sub,
fieldcount(FT),
)

test/tracing.jl Outdated
Comment on lines 264 to 268
) == (Foo{
TracedRNumber{Float64},
Bar{TracedRNumber{Float64}},
Reactant.TracedRArray{Float64,1},
}, [true, true, false])

This comment was marked as outdated.

@jumerckx jumerckx marked this pull request as ready for review November 29, 2025 18:21
@jumerckx
Copy link
Collaborator Author

I added a bunch of debug statements that seem handy to keep around.
However, they clutter CI. Should I just get rid of them, or look for a solution?

@jumerckx jumerckx requested a review from wsmoses December 1, 2025 16:52
src/Tracing.jl Outdated
return T
end

@debug "traced_type_inner: Processing type with field changes" T = T subTys = subTys
Copy link
Member

Choose a reason for hiding this comment

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

I would probably get rid of the debug statements here, if no longer necessary

@wsmoses wsmoses requested a review from giordano December 1, 2025 19:14
@wsmoses
Copy link
Member

wsmoses commented Dec 1, 2025

@giordano adding you as a reviewer.

this should remedy many of the custom type annotations weve historically needed for oceananigans and fixedsizearray

@wsmoses wsmoses requested a review from avik-pal December 1, 2025 19:14
@jumerckx
Copy link
Collaborator Author

jumerckx commented Dec 1, 2025

Not sure how I should fix the CI docs failure. It's an internal method but still useful to have a docstring imo.

@giordano
Copy link
Member

giordano commented Dec 2, 2025

Add it to https://enzymead.github.io/Reactant.jl/stable/api/api#Internal-utils

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.

4 participants