Skip to content

iree-xla-legalize-tf miscompiles tf.IsNan --> dense<false> with static shapes (and correctly compiles with dynamic shapes) #4061

Closed

Description

@phoenix-meadowlark

iree-xla-legalize-tf miscompiles tf.IsNan -> dense<false> when static shapes are provided. This means that tf.math.is_nan only passes when compiled with dynamic dims.

Minimal reproducer

Static (failing)

// is_nan.mlir
module {
  func @"is_nan"(%arg0: tensor<1xf32>) -> tensor<1xi1> {
    %0 = "tf.IsNan"(%arg0) : (tensor<1xf32>) -> tensor<1xi1>
    return %0 : tensor<1xi1>
  }
}
bazel run integrations/tensorflow/compiler:iree-tf-opt -- \
  -iree-xla-legalize-tf \
  is_nan.mlir
module  {
  func @is_nan(%arg0: tensor<1xf32>) -> tensor<1xi1> {
    %0 = mhlo.constant dense<false> : tensor<1xi1>
    return %0 : tensor<1xi1>
  }
}

Dynamic (passing)

// is_nan_dynamic.mlir
module {
  func @"is_nan_dynamic"(%arg0: tensor<?xf32>) -> tensor<?xi1> {
    %0 = "tf.IsNan"(%arg0) : (tensor<?xf32>) -> tensor<?xi1>
    return %0 : tensor<?xi1>
  }
}
bazel run integrations/tensorflow/compiler:iree-tf-opt -- \
  -iree-xla-legalize-tf \
  is_nan_dynamic.mlir
module  {
  func @is_nan_dynamic(%arg0: tensor<?xf32>) -> tensor<?xi1> {
    %0 = shape.shape_of %arg0 : tensor<?xf32> -> tensor<?xindex>
    %1 = shape.shape_of %arg0 : tensor<?xf32> -> tensor<?xindex>
    %2 = shape.cstr_broadcastable %0, %1 : tensor<?xindex>, tensor<?xindex>
    %3 = shape.assuming %2 -> (tensor<?xi1>) {
      %4 = shape.shape_of %arg0 : tensor<?xf32> -> tensor<?xindex>
      %5 = shape.shape_of %arg0 : tensor<?xf32> -> tensor<?xindex>
      %6 = shape.broadcast %4, %5 : tensor<?xindex>, tensor<?xindex> -> tensor<?xindex>
      %7 = tensor_cast %6 : tensor<?xindex> to tensor<1xindex>
      %8 = "mhlo.dynamic_broadcast_in_dim"(%arg0, %7) {broadcast_dimensions = dense<0> : tensor<1xi64>} : (tensor<?xf32>, tensor<1xindex>) -> tensor<?xf32>
      %9 = "mhlo.dynamic_broadcast_in_dim"(%arg0, %7) {broadcast_dimensions = dense<0> : tensor<1xi64>} : (tensor<?xf32>, tensor<1xindex>) -> tensor<?xf32>
      %10 = "mhlo.compare"(%8, %9) {comparison_direction = "NE"} : (tensor<?xf32>, tensor<?xf32>) -> tensor<?xi1>
      shape.assuming_yield %10 : tensor<?xi1>
    }
    return %3 : tensor<?xi1>
  }
}

Partial conversions in iree-xla-legalize-tf

func @is_nan(%arg0: tensor<1xf32>) -> tensor<1xi1> {
  %0 = "tf.IsNan"(%arg0) : (tensor<1xf32>) -> tensor<1xi1>
  return %0 : tensor<1xi1>
}
func @is_nan(%arg0: tensor<1xf32>) -> tensor<1xi1> {
  %0 = "tf.NotEqual"(%arg0, %arg0) {incompatible_shape_error = true} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xi1>
  return %0 : tensor<1xi1>
}
func @is_nan(%arg0: tensor<1xf32>) -> tensor<1xi1> {
  %0 = "mhlo.compare"(%arg0, %arg0) {comparison_direction = "NE"} : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xi1>
  return %0 : tensor<1xi1>
}
func @is_nan(%arg0: tensor<1xf32>) -> tensor<1xi1> {
  %0 = mhlo.constant dense<false> : tensor<1xi1>
  return %0 : tensor<1xi1>
}

It looks like comparison by SSA value in this folder violates IEEE float semantics for NaN != NaN. Dynamic shapes would have also miscompiled if a CSE pass was ran before iree-xla-legalize-tf halted.

Activity

  1. added
    bug 🐞
    integrations/tensorflow
    on Dec 2, 2020
  2. changed the title [-]tf.math.is_nan miscompiles with static shapes, but passes with dynamic shapes[/-] [+]iree-xla-legalize-tf miscompiles tf.math.is_nan with static shapes (and correctly compiles with dynamic shapes)[/+] on Dec 3, 2020
  3. changed the title [-]iree-xla-legalize-tf miscompiles tf.math.is_nan with static shapes (and correctly compiles with dynamic shapes)[/-] [+]iree-xla-legalize-tf miscompiles tf.IsNan --> dense<false> with static shapes (and correctly compiles with dynamic shapes)[/+] on Dec 3, 2020
  4. added a commit that references this issue on Dec 8, 2020
    a49b97b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug 🐞integrations/tensorflow

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions