Skip to content

Optimize to_rarray to avoid recursion for simple inputs #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Tracing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,7 @@ end
@inline function to_rarray(@nospecialize(x))
return make_tracer(OrderedIdDict(), x, (), Reactant.ArrayToConcrete)
end

to_rarray(x::Number) = x # TODO: should this be a `ConcreteRArray{_,0}`?
to_rarray(x::ConcreteRArray) = x
to_rarray(x::AbstractArray{<:Number}) = ConcreteRArray(x)
Copy link
Member

Choose a reason for hiding this comment

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

cc @avik-pal can we make a union type of all the primitive types that Reactant supports conversion into (e.g. int, float, complex float, etc). We should then use this instead of Number here

Copy link
Collaborator

Choose a reason for hiding this comment

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

union over https://mlir.llvm.org/docs/Dialects/Builtin/#types? (atleast the ones that are in base)

Copy link
Member

Choose a reason for hiding this comment

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

These ones specificaily:

@inline primitive_type(::Type{Bool}) = 1

We can then also amend

if mode == ArrayToConcrete && eltype(RT) <: AbstractFloat
to be is a subtype of Reactant.PrimitiveTypes

Copy link
Collaborator

Choose a reason for hiding this comment

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

Added in #161

Copy link
Member

Choose a reason for hiding this comment

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

@gdalle I think we're just waiting for this to get addressed, then lgtm

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's merged but we still shouldn't do to_rarray(x::Number) = x because the semantics won't work out

1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
Lux = "b2108857-7c20-44ae-9111-449ecde12c47"
LuxLib = "82251201-b29d-42c6-8e01-566dec8acb11"
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
Expand Down
11 changes: 10 additions & 1 deletion test/tracing.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Reactant
using Reactant: traced_type, ConcreteRArray, TracedRArray, ConcreteToTraced
using Reactant: to_rarray, traced_type, ConcreteRArray, TracedRArray, ConcreteToTraced
using Test
using JET: @test_opt

@testset "Tracing" begin
@testset "trace_type" begin
Expand Down Expand Up @@ -100,4 +101,12 @@ using Test
end
end
end
@testset "to_rarray" begin
@test to_rarray(1.0) isa Float64
@test to_rarray([1.0]) isa ConcreteRArray{Float64,1}
@test to_rarray(ConcreteRArray([1.0])) isa ConcreteRArray{Float64,1}
@test_opt to_rarray(1.0)
@test_opt to_rarray([1.0])
@test_opt to_rarray(ConcreteRArray([1.0]))
end
end
Loading