Skip to content

Conversation

@ptiede
Copy link
Contributor

@ptiede ptiede commented Jan 26, 2026

Thanks for the great package. I use it for a bunch of research. Right now, I am working to adapt my code to use Reactant so I can take advantage of various accelerators. One of Reactant's quirks is that it requires pretty generous type annotations. The base structures are TracedRNumbers <: Number and TracedRArray <: AbstractArray. If I want to use TransformVariables with Reactant. I sadly need to widen some of the type annotations.

This is a draft PR that uses one method to make it compatible with Reactant, but I am very open to suggestions!

There are still some issues with this PR, including the scalar indexing, which will require some additional thought.

@ptiede ptiede marked this pull request as draft January 26, 2026 23:36
@wsmoses
Copy link

wsmoses commented Jan 28, 2026

@tpapp can this get a quick review?

Copy link
Owner

@tpapp tpapp left a comment

Choose a reason for hiding this comment

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

questions, see comments

dimension(::ScalarTransform) = 1

function transform_with(flag::NoLogJac, t::ScalarTransform, x::AbstractVector, index::Int)
function transform_with(flag::NoLogJac, t::ScalarTransform, x::AbstractVector, index)
Copy link
Owner

Choose a reason for hiding this comment

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

I am a bit surprised that you need this, since transform_with is called internally and index is an integer.

Can you explain what the actual type is that you need here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sometimes in Reactant you can get a TracedNumber{Integer} if I loop through this and the array x is a AbstractTraced array.

end

function transform_with(::LogJac, t::ScalarTransform, x::AbstractVector, index::Int)
function transform_with(::LogJac, t::ScalarTransform, x::AbstractVector, index)
Copy link
Owner

Choose a reason for hiding this comment

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

ditto

struct Identity <: ScalarTransform end

transform(::Identity, x::Real) = x
transform(::Identity, x::Number) = x
Copy link
Owner

Choose a reason for hiding this comment

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

In principle I am OK with widening Real here and in other places, the intent is to exclude Complex. It unfortunate that Base does not have an intermediate type for this purpose, but we can use Number.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya I agree it is very sad.

@tpapp
Copy link
Owner

tpapp commented Jan 28, 2026

@ptiede: I am fine with widening, just have some questions. Please continue this PR and I will give feedback.

@wsmoses: yes, if you don't get a response within a few days to improvements feel free to ping me

@ptiede
Copy link
Contributor Author

ptiede commented Jan 28, 2026

There is one final thing. For some of these operations, we will induce scalar indexing. I am not really sure what the best way to deal with that is, because I can't add @allowscalar to the code without impacting performance in the CPU code. I am open to suggestions for how to fix this.

@ptiede
Copy link
Contributor Author

ptiede commented Jan 28, 2026

For example

function TV.transform_with(::TV.LogJac, t::TV.ScalarTransform, x::Reactant.AnyTracedRVector, index)
    return TV.transform_and_logjac(t, @allowscalar x[index])..., index + 1
end

requires the @allowscalar to compile

@ptiede
Copy link
Contributor Author

ptiede commented Jan 28, 2026

I could wrap this in a Reactant extension, but it would result in a lot of code duplication.

@tpapp
Copy link
Owner

tpapp commented Jan 29, 2026

I am not familiar with the internals of Reactant, so this may be a silly suggestion, but that index could be any other type (eg a wrapper) if that would help. The entry point is transform and similar methods, which just call firstindex. Similarly for inverse!.

@tpapp
Copy link
Owner

tpapp commented Jan 29, 2026

But I am open to any other reasonable solution too. Eg have a my_getindex(x, i) method that Reactant can specialize and use that instead.

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.

3 participants