Repository navigation
Closed
Labels
Description
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.mlirmodule {
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.mlirmodule {
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
[-]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)[/+][-]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)[/+]Enable passing e2e tests (#4066)
phoenix-meadowlark commentedon Dec 4, 2020
Fixing this in https://github.com/tensorflow/mlir-hlo.
Remove fold of `mhlo.compare(%arg0, %arg0)` for floating types.
Remove fold of `mhlo.compare(%arg0, %arg0)` for floating types.
Enable tf.is_nan test with static shapes (#4129)