Add overloads of _apply_operator to support type conversion of Numbers to ValidVector eltype.#575
Add overloads of _apply_operator to support type conversion of Numbers to ValidVector eltype.#575GongJr0 wants to merge 3 commits intoastroautomata:masterfrom
_apply_operator to support type conversion of Numbers to ValidVector eltype.#575Conversation
Refactor operator application by introducing _apply_operator_values that accepts raw vector values and broadcasts a safe operator over them. Restore previous behavior with a wrapper _apply_operator that maps _get_value and delegates to the new function. Add optimized overloads to handle ValidVector/Number and Number/ValidVector combinations by converting scalars to the vector element type and calling _apply_operator_values. This improves clarity and handles mixed scalar/vector operations more efficiently.
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
|
Fails unit test "ValidVector operations with Union{} return type" due to the snippet: a = ValidVector(Float32[1.0, 2.0], false)
b = 1.0
result2 = apply_operator(*, a, b)
@test result2 isa ValidVector{<:AbstractArray{Float64}}Under previous behavior the operation would resolve to No other functionality seems to be affected but I refrained from updating test cases prior to a review. |
|
Just so I understand the design, is it to automatically convert everything to lower precision when applying operations on Float64,Float32 types? I wonder if the safer thing might be to lower the precision only after the entire template is evaluated |
Not exactly. The intent isn’t to automatically lower precision across the whole expression/template. But whenever a Example where precision gets lowered: x1::ValidVector{Float32} = ...
y1::Float64 = ...
apply_operator(some_op, x, y) # Returns Float32 (eltype of the vector)Example where precision remains 64-bit: x1::ValidVector{Float64}
y1::Float32
apply_operator(some_op, y, x) # Promotes y -> Float64 (preserves eltype)
# before passing to some_op; return type remains Float64This is implemented with the assumption that the |
|
In your example: I don't think we would want this. Julia always promotes types to higher precision in operations so this would be unusual. We probably just want something to convert types at the very end |
Introduced a small refactor to add two overloads of the
_apply_operatorfunction specifically targeting{ValidVector, Number}and{Number, ValidVector}argument pairs. The overloads ensure that a givenNumberis converted to theeltypeof the vector, preventing type upcasting via higer-precisionFloatliterals. The changes address issue #1411 posted inMilesCranmer/PySR.