EnzymeAD / Reactant.jl

MIT License
65 stars 6 forks source link

Inconsistent result for `iszero` for booleans #277

Open avik-pal opened 2 hours ago

avik-pal commented 2 hours ago
julia> x_ra = Reactant.to_rarray(false; track_numbers=(Number,))
ConcreteRNumber{Bool}(false)

julia> for i in 1:1000
           @show @jit(iszero(x_ra))
       end
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(false)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(false)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)
#= REPL[87]:2 =# @jit(iszero(x_ra)) = ConcreteRNumber{Bool}(true)

Strange part is that it changes results with each compilation. If we compile and re-run the results are consistent

avik-pal commented 2 hours ago

The HLO seems fine:

Module:
module attributes {transform.with_named_sequence} {
  func.func @main(%arg0: tensor<i1>) -> tensor<i1> {
    %c = stablehlo.constant dense<true> : tensor<i1>
    %0 = stablehlo.compare  EQ, %arg0, %c : (tensor<i1>, tensor<i1>) -> tensor<i1>
    return %0 : tensor<i1>
  }
}

With the patch it is

Module:
module attributes {transform.with_named_sequence} {
  func.func @main(%arg0: tensor<i1>) -> tensor<i1> {
    %0 = stablehlo.not %arg0 : tensor<i1>
    return %0 : tensor<i1>
  }
}