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
# 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?

test/tracing.jl Outdated
struct Bar{T}
b::T
end
struct Foo{T, B<:Bar{T}, AT<:AbstractArray{T}}
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
struct Foo{T, B<:Bar{T}, AT<:AbstractArray{T}}
struct Foo{T,B<:Bar{T},AT<:AbstractArray{T}}

test/tracing.jl Outdated
a::AT
b::B
end
@test Reactant.apply_type_with_promotion(Foo, [Float64, Bar{Float64}, Reactant.TracedRArray{Float64, 1}]) == Foo{
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
@test Reactant.apply_type_with_promotion(Foo, [Float64, Bar{Float64}, Reactant.TracedRArray{Float64, 1}]) == Foo{
@test Reactant.apply_type_with_promotion(
Foo, [Float64, Bar{Float64}, Reactant.TracedRArray{Float64,1}]
) == Foo{

test/tracing.jl Outdated
@test Reactant.apply_type_with_promotion(Foo, [Float64, Bar{Float64}, Reactant.TracedRArray{Float64, 1}]) == Foo{
TracedRNumber{Float64},
Bar{TracedRNumber{Float64}},
Reactant.TracedRArray{Float64, 1},
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
Reactant.TracedRArray{Float64, 1},
Reactant.TracedRArray{Float64,1},

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