Skip to content

Reactant cannot compile functions using Rational{Int64} coefficients #2143

Description

@ChrisRackauckas-Claude

Description

Reactant fails to compile functions that use Rational{Int64} values when those values need to be promoted/converted during operations with traced arrays.

MWE

using Reactant
using Reactant: @compile

function multiply_by_rational(x)
    coeff = 1 // 5  # Rational{Int64}
    return x * coeff
end

function multiply_by_float(x)
    coeff = 0.2  # Float64 equivalent of 1//5
    return x * coeff
end

function main()
    # Regular Julia - works fine
    println("=== Regular Julia ===")
    x = [1.0, 2.0, 3.0]
    println("multiply_by_rational: ", multiply_by_rational(x))

    # Reactant with Rational - fails
    println("\n=== Reactant @compile with Rational ===")
    x_reactant = Reactant.to_rarray([1.0, 2.0, 3.0])
    try
        compiled_fn = @compile multiply_by_rational(x_reactant)
        result = compiled_fn(x_reactant)
        println("Result: ", result)
    catch e
        println("Error: ", e)
    end

    # Reactant with Float64 - works
    println("\n=== Reactant @compile with Float64 ===")
    try
        compiled_fn = @compile multiply_by_float(x_reactant)
        result = compiled_fn(x_reactant)
        println("Result: ", result)
    catch e
        println("Error: ", e)
    end
end

main()

Output

=== Regular Julia ===
multiply_by_rational: [0.2, 0.4, 0.6000000000000001]

=== Reactant @compile with Rational ===
Error: FieldError(DataType, :ptr)

=== Reactant @compile with Float64 ===
Result: ConcretePJRTArray{Float64, 1, 1}([0.2, 0.4, 0.6000000000000001])

Impact

This affects packages like OrdinaryDiffEq.jl where Butcher tableaus for ODE solvers (Tsit5, Verner, etc.) use Rational coefficients for exact arithmetic. These Rationals cause compilation failures when trying to use Reactant with ODE solvers.

Possible Solutions

  1. Add an overlay or conversion that automatically converts Rational{Int64} to Float64 when operating with traced types
  2. Support Rational as a traceable type in MLIR
  3. Document the limitation and recommend users avoid Rationals in traced code

Version Info

  • Julia 1.12
  • Reactant.jl 0.2.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions