Skip to content

Sequence return types failing in the python bindings. #5320

Closed

Description

@phoenix-meadowlark

MLIR modules bound with the Python runtime bindings currently provide incorrect results when returning multiple values (e.g. return a, b) and when returning sequences (e.g. return (a,) or return (a, b)). This is documented in this colab and below:

class PythonTypesModule(tf.Module):

  @tf.function(input_signature=[])
  def output_tuple_len_0(self):
    return tuple()

  @tf.function(input_signature=[])
  def output_tuple_len_1(self):
    return (1.,)  # Need to explicitly wrap.

  @tf.function(input_signature=[])
  def output_tuple_len_2(self):
    return 1., 2.

  @tf.function(input_signature=[])
  def output_tuple_len_3(self):
    return 1., 2., 3.
module  {
  func @output_tuple_len_0() attributes {
        iree.module.export,
        iree.reflection = {abi = "sip", abiv = 1 : i32, sip = "I1!R1!"}
    } {
    return
  }
  func @output_tuple_len_1() -> tensor<f32> attributes {
        iree.module.export,
        iree.reflection = {abi = "sip", abiv = 1 : i32, sip = "I1!R8!S5!k0_0"}
    } {
    %0 = mhlo.constant dense<1.000000e+00> : tensor<f32>
    return %0 : tensor<f32>
  }
  func @output_tuple_len_2() -> (tensor<f32>, tensor<f32>) attributes {
        iree.module.export,
        iree.reflection = {abi = "sip", abiv = 1 : i32, sip = "I1!R12!S9!k0_0k1_1"}
    } {
    %0 = mhlo.constant dense<1.000000e+00> : tensor<f32>
    %1 = mhlo.constant dense<2.000000e+00> : tensor<f32>
    return %0, %1 : tensor<f32>, tensor<f32>
  }
  func @output_tuple_len_3() -> (tensor<f32>, tensor<f32>, tensor<f32>) attributes {
        iree.module.export,
        iree.reflection = {abi = "sip", abiv = 1 : i32, sip = "I1!R17!S13!k0_0k1_1k2_2"}
    } {
    %0 = mhlo.constant dense<1.000000e+00> : tensor<f32>
    %1 = mhlo.constant dense<2.000000e+00> : tensor<f32>
    %2 = mhlo.constant dense<3.000000e+00> : tensor<f32>
    return %0, %1, %2 : tensor<f32>, tensor<f32>, tensor<f32>
  }
}

Expected vs actual results:

module.output_tuple_len_1()
expected: [array([1.])]
actual:   [None, array([1.])]

module.output_tuple_len_2()
expected: [array([1.]), array([2.])]
actual:   [None, array([2.])]

module.output_tuple_len_3()
expected: [array([1.]), array([2.]), array([3.])]
actual:   [None, array([2.]), None, array([3.])]

IREE itself produces the correct values via iree-run-mlir:

iree/tools/iree-run-mlir \
  -export-all \
  -iree-hal-target-backends=dylib-llvm-aot \
  sequences.mlir

EXEC @output_tuple_len_1
I iree/iree/tools/utils/vm_util.cc:201] result[0]: Buffer<float32[2]>
1xf32=1

EXEC @output_tuple_len_2
I iree/iree/tools/utils/vm_util.cc:201] result[0]: Buffer<float32[2]>
1xf32=1
I iree/iree/tools/utils/vm_util.cc:201] result[1]: Buffer<float32[2]>
1xf32=2

EXEC @output_tuple_len_3
I iree/iree/tools/utils/vm_util.cc:201] result[0]: Buffer<float32[2]>
1xf32=1
I iree/iree/tools/utils/vm_util.cc:201] result[1]: Buffer<float32[2]>
1xf32=2
I iree/iree/tools/utils/vm_util.cc:201] result[2]: Buffer<float32[2]>
1xf32=3

Given this I would guess that there is some error in how the python bindings are interpreting the SIP or with the SIP itself.

Activity

  1. added
    bug 🐞
    bindings/python
    on Apr 5, 2021
  2. stellaraccident commented on Apr 5, 2021

    @stellaraccident
    Collaborator

    Do we have an isolated/minimal repro for this? (I suspect the limitation is unintentional but need to look at something a bit more concrete)

  3. phoenix-meadowlark commented on Apr 5, 2021

    @phoenix-meadowlark
    ContributorAuthor

    I updated the colab and my post above with the tf.Module that I used to generate the MLIR, and changed the outputs of the module so that they more clearly show what is supposed to be the 1st, 2nd and 3rd outputs.

  4. phoenix-meadowlark commented on Apr 6, 2021

    @phoenix-meadowlark
    ContributorAuthor

    The general pattern is:

    return tuple()
    expected = []
    actual   = None
    
    return (0,)
    expected = [0]
    actual   = [None, 0]
    
    return 0, 1
    expected = [0, 1]
    actual   = [None, 1]
    
    return 0, 1, 2
    expected = [0, 1, 2]
    actual   = [None, 1, None, 2]
    
    return 0, 1, 2, 3
    expected = [0, 1, 2, 3]
    actual   = [None, 1, None, 3]
    
    return 0, 1, 2, 3, 4
    expected = [0, 1, 2, 3, 4]
    actual   = [None, 1, None, 3, None, 4]
    
    return 0, 1, 2, 3, 4, 5
    expected = [0, 1, 2, 3, 4, 5]
    actual   = [None, 1, None, 3, None, 5]
  5. added a commit that references this issue on Apr 9, 2021
    3fc84c3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bindings/pythonbug 🐞

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions