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

Conversation

gdalle
Copy link

@gdalle gdalle commented Oct 1, 2024

Fixes #138 by adding methods for to_rarray on ::Number, ::AbstractArray{<:Number} and ::ConcreteRArray. Adds tests with JET to ensure type stability (which did not hold before).

Question: what should happen to a number going through to_rarray? If it keeps the same type, how can it be traced through the function?


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

@mofeing
Copy link
Collaborator

mofeing commented Oct 1, 2024

Now that Adapt is a strong dependency, shouldn't we just overload adapt? I don't get the different between to_rarray and adapt.

@wsmoses
Copy link
Member

wsmoses commented Oct 1, 2024

Now that Adapt is a strong dependency, shouldn't we just overload adapt? I don't get the different between to_rarray and adapt.

Well to_rarray predates adapt as a strong dep [which we might remove if we can work around later], but also are we sure that adapt correctly deals with recursive data structures?

@avik-pal
Copy link
Collaborator

avik-pal commented Oct 1, 2024

Well to_rarray predates adapt as a strong dep [which we might remove if we can work around later], but also are we sure that adapt correctly deals with recursive data structures?

Not really. You need to opt-in for every custom structure

@mofeing
Copy link
Collaborator

mofeing commented Oct 1, 2024

Well to_rarray predates adapt as a strong dep [which we might remove if we can work around later], but also are we sure that adapt correctly deals with recursive data structures?

Not really. You need to opt-in for every custom structure

But then, we could maybe overload Adapt.adapt_structure(::Type{ConcreteRArray}, x) to do what to_rarray is doing right now.

@mofeing
Copy link
Collaborator

mofeing commented Oct 1, 2024

Now that Adapt is a strong dependency, shouldn't we just overload adapt? I don't get the different between to_rarray and adapt.

Well to_rarray predates adapt as a strong dep [which we might remove if we can work around later], ...

Yeah, but we introduced to_rarray just because we didn't want Adapt as a strong dep xD

@gdalle
Copy link
Author

gdalle commented Oct 7, 2024

What should we do about this PR?

@mofeing
Copy link
Collaborator

mofeing commented Oct 8, 2024

What should we do about this PR?

I guess we can talk about this on Wednesday. Would you mind putting it in the agenda?

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.

Shortcuts for to_rarray
4 participants