Skip to content

[MLIR] xla hlo -> lhlo conversion issue when operand is a constant tensor #39895

Closed

Description

@bondhugula

With master branch as of 831a555 (May 26):
The xla hlo to lhlo lowering doesn't convert completely when the operand of an op (in this example below, that of broadcast_in_dim) is a constant tensor. To reproduce, please use:

func @main(%arg0: tensor<4x64x128x3xf32>, %arg1: tensor<5x3x3x10xf32>) {
  %cst = constant  dense<0.000000e+00> : tensor<f32>
  %0 = "xla_hlo.broadcast_in_dim"(%cst) {broadcast_dimensions = dense<[]> : tensor<0xi64>, name = "broadcast.6"} : (tensor<f32>) -> tensor<4x30x42x10xf32>
  return
}

and

$ tf-opt -hlo-legalize-to-lhlo constant-tensor-lowering.mlir 
constant-tensor-lowering.mlir:3:8: error: 'xla_lhlo.broadcast_in_dim' op operand #0 must be memref of floating-point or signless integer or complex-type values, but got 'tensor<f32>'
  %0 = "xla_hlo.broadcast_in_dim"(%cst) {broadcast_dimensions = dense<[]> : tensor<0xi64>, name = "broadcast.6"} : (tensor<f32>) -> tensor<4x30x42x10xf32>
       ^
constant-tensor-lowering.mlir:3:8: note: see current operation: "xla_lhlo.broadcast_in_dim"(%cst, %0) {broadcast_dimensions = dense<[]> : tensor<0xi64>, name = "broadcast.6"} : (tensor<f32>, memref<4x30x42x10xf32>) -> ()

Using -print-ir-after-all reveals the operand for the lhlo broadcast_in_dim op wasn't converted to a memref:

tuple.mlir:4:10: error: 'xla_lhlo.broadcast_in_dim' op operand #0 must be memref of floating-point or signless integer or complex-type values, but got 'tensor<f32>'
    %0 = "xla_hlo.broadcast_in_dim"(%cst) {broadcast_dimensions = dense<[]> : tensor<0xi64>, name = "broadcast.6"} : (tensor<f32>) -> tensor<4x30x42x10xf32>
         ^
tuple.mlir:4:10: note: see current operation: "xla_lhlo.broadcast_in_dim"(%cst, %0) {broadcast_dimensions = dense<[]> : tensor<0xi64>, name = "broadcast.6"} : (tensor<f32>, memref<4x30x42x10xf32>) -> ()
// *** IR Dump After mlir::detail::VerifierPass Failed ***


"module"() ( {
  "func"() ( {
  ^bb0(%arg0: memref<4x64x128x3xf32>, %arg1: memref<5x3x3x10xf32>):  // no predecessors
    %cst = "std.constant"() {name = "constant.5", value = dense<0.000000e+00> : tensor<f32>} : () -> tensor<f32>
    %0 = "std.alloc"() : () -> memref<4x30x42x10xf32>
    "xla_lhlo.broadcast_in_dim"(%cst, %0) {broadcast_dimensions = dense<[]> : tensor<0xi64>, name = "broadcast.6"} : (tensor<f32>, memref<4x30x42x10xf32>) -> ()
    %1 = "std.alloc"() : () -> memref<4x30x42x10xf32>
    "xla_lhlo.convolution"(%arg0, %arg1, %1) {batch_group_count = 1 : i64, dimension_numbers = {input_batch_dimension = 0 : i64, input_feature_dimension = 3 : i64, input_spatial_dimensions = dense<[1, 2]> : tensor<2xi64>, kernel_input_feature_dimension = 2 : i64, kernel_output_feature_dimension = 3 : i64, kernel_spatial_dimensions = dense<[0, 1]> : tensor<2xi64>, output_batch_dimension = 0 : i64, output_feature_dimension = 3 : i64, output_spatial_dimensions = dense<[1, 2]> : tensor<2xi64>}, feature_group_count = 1 : i64, lhs_dilations = dense<1> : tensor<2xi64>, name = "convolution.4", padding = dense<0> : tensor<2x2xi64>, precision_config = ["DEFAULT", "DEFAULT"], rhs_dilations = dense<1> : tensor<2xi64>, window_strides = dense<[2, 3]> : tensor<2xi64>} : (memref<4x64x128x3xf32>, memref<5x3x3x10xf32>, memref<4x30x42x10xf32>) -> ()
    %2 = "std.alloc"() : () -> memref<4x30x42x10xf32>
    "xla_lhlo.maximum"(%0, %1, %2) {name = "maximum.7"} : (memref<4x30x42x10xf32>, memref<4x30x42x10xf32>, memref<4x30x42x10xf32>) -> ()
    "xla_lhlo.terminator"() : () -> ()
  }) {sym_name = "main", type = (memref<4x64x128x3xf32>, memref<5x3x3x10xf32>) -> ()} : () -> ()
  "module_terminator"() : () -> ()
}) : () -> ()

This is nothing specific to broadcast_in_dim (happens with say xla_hlo.add as well). This is likely a missing conversion for std.constant that needs to be completed?

Activity

  1. Saduf2019 commented on May 27, 2020

    @Saduf2019
    Contributor

    @bondhugula
    Please share simple stand alone code for us to replicate the issue faced.

  2. added
    stat:awaiting response
    on May 27, 2020
  3. bondhugula commented on May 27, 2020

    @bondhugula
    ContributorAuthor

    @bondhugula
    Please share simple stand alone code for us to replicate the issue faced.

    But that's exactly what I provided above - it's a 2-line MLIR standalone test input. Did you expect something else?

  4. added and removed
    stat:awaiting response
    on May 28, 2020
  5. assigned and unassigned on May 28, 2020
  6. bondhugula commented on May 29, 2020

    @bondhugula
    ContributorAuthor

    @r4nt Please assign this issue to me - I'll add the missing rewrites needed to get this working. @MaheshRavishankar @joker-eph for visibility and their information.

  7. MaheshRavishankar commented on May 29, 2020

    @MaheshRavishankar

    THanks Uday! But is the expectation to have xla_hlo.const and I think there is a conversion from xla_hlo.const to xla_lhlo.const. Then the conversion to memref does work. I am not sure there is an expectation to mix dialects as in your example. But I dont see any reason not to have such a pattern, but maybe you could reuse some of the conversion from hlo -> lhlo for std -> lhlo conversion.

  8. bondhugula commented on May 29, 2020

    @bondhugula
    ContributorAuthor

    THanks Uday! But is the expectation to have xla_hlo.const and I think there is a conversion from xla_hlo.const to xla_lhlo.const. Then the conversion to memref does work. I am not sure there is an expectation to mix dialects as in your example. But I dont see any reason not to have such a pattern, but maybe you could reuse some of the conversion from hlo -> lhlo for std -> lhlo conversion.

    Dumping an XLA protobuf and using tf-mlir-translate -hlo-to-mlir-hlo actually yields such an input (std.constant instead of an xla_hlo.const). xla_hlo.const -> xla_lhlo.const would work, but the -hlo-legalize-to-lhlo pass could potentially be presented with such an input in other ways, and so making -hlo-to-mlir-hlo generate hlo.const isn't a real solution. The std.constant in these cases is isomorphic to xla_hlo.const (generates the same result type), and so as you point out, we could reuse the conversion pattern hlo -> lhlo. Since this is really "a terminal conversion" (tensor operands of some ops get their values from std.constants) and that we can't generate constant memrefs from std.constant, I guess it's fine to have this conversion pattern live in hlo_legalize_to_lhlo.

  9. 19 remaining items

  10. joker-eph commented on Jun 9, 2020

    @joker-eph
    Contributor

    Also, I am fine with adding this pattern from std.const to hlo.const

    This makes sense to me as well: seems like a good application of transitive lowering here, isn't it?

  11. bondhugula commented on Jun 9, 2020

    @bondhugula
    ContributorAuthor

    Also, I am fine with adding this pattern from std.const to hlo.const

    This makes sense to me as well: seems like a good application of transitive lowering here, isn't it?

    But where would you have this pattern live? I'm completely fine with this as long as this pattern can run as part of -hlo-legalize-to-lhlo. If not, it's weirder to have to run a separate pass do std to hlo, and I don't see how this solves the original issue. I would then prefer having std.const to lhlo.const in the hlo to lhlo conversion which was the original proposal.

  12. joker-eph commented on Jun 10, 2020

    @joker-eph
    Contributor

    If not, it's weirder to have to run a separate pass do std to hlo, and I don't see how this solves the original issue

    Why doesn't it solve the original issue? If you're pipeline is always including -std-to-hlo before -hlo-to-lhlo shouldn't this just work then?

    Note that in practice in a real pipeline, one would include all the patterns to legalize to HLO, include the STD->HLO, and legalize there as a single pass before running the LHLO buffer alloc.

  13. bondhugula commented on Jun 10, 2020

    @bondhugula
    ContributorAuthor

    If not, it's weirder to have to run a separate pass do std to hlo, and I don't see how this solves the original issue

    Why doesn't it solve the original issue? If you're pipeline is always including -std-to-hlo before -hlo-to-lhlo shouldn't this just work then?

    The original issue was of making the -hlo to lhlo pass deal with this input.

    Note that in practice in a real pipeline, one would include all the patterns to legalize to HLO, include the STD->HLO, and legalize there as a single pass before running the LHLO buffer alloc.

    A new pass std to hlo just for this?! What other conversions do you foresee that to have? The conversion looks backwards intuitively.

  14. joker-eph commented on Jun 10, 2020

    @joker-eph
    Contributor

    The original issue was of making the -hlo to lhlo pass deal with this input.

    Such passes are mostly testing passes, they can't support the world. This is the case with most passes that are loading A->B conversion pattern right now. If you want A+C->B you need another pass loading the right set of patterns. I don't think there is anything specific to this pass right now.

    The conversion looks backwards intuitively.

    Why? A lot of std dialect op can be seen as the same level of abstraction as HLO, using the existing XLA compiler to target TPU from a tensor-level computation involving std ops could be useful to someone for example.
    Conversion don't need to be one-way: for example we have a conversion from TF->HLO and from HLO->TF.

  15. bondhugula commented on Jun 10, 2020

    @bondhugula
    ContributorAuthor

    The original issue was of making the -hlo to lhlo pass deal with this input.

    Such passes are mostly testing passes, they can't support the world. This is the case with most passes that are loading A->B conversion pattern right now. If you want A+C->B you need another pass loading the right set of patterns. I don't think there is anything specific to this pass right now.

    The conversion looks backwards intuitively.

    Why? A lot of std dialect op can be seen as the same level of abstraction as HLO, using the existing XLA compiler to target TPU from a tensor-level computation involving std ops could be useful to someone for example.
    Conversion don't need to be one-way: for example we have a conversion from TF->HLO and from HLO->TF.

    TF -> HLO and HLO -> TF is a different scenario - I assume you have many conversions there. Creating one full pass and file for a std.const to hlo.const sounds needless, and more importantly, getting back to the main question: what's the issue with having std.const to hlo.const in hlo to lhlo? It's just an intermediate step and would anyway converge at lhlo.const. Why shouldn't one have such trivial intermediate patterns for canonicalization or to facilitate / trigger other conversions towards your legal target?

  16. bondhugula commented on Aug 13, 2020

    @bondhugula
    ContributorAuthor

    Bringing this back up: where should the conversion for std.const to hlo.const live? If we do this as a conversion pattern in HLO to LHLO, wouldn't it fail? (since the xla_hlo op generated out of the pattern would have been marked as illegal for the conversion target, making the dialect conversion reset state). Could one add an op rewrite pattern as part of a dialect conversion? I completely forget whether it would make sense (in spite of having reviewed a recent revision updating the documentation of pattern rewriting and dialect conversion).

    @joker-eph @sherhut

  17. bhack commented on May 31, 2021

    @bhack
    Contributor

    What is the status of this?

  18. tilakrayal commented on Jul 31, 2024

    @tilakrayal
    Contributor

    Hi,

    Thank you for opening this issue. Since this issue has been open for a long time, the code/debug information for this issue may not be relevant with the current state of the code base.

    The Tensorflow team is constantly improving the framework by fixing bugs and adding new features. We suggest you try the latest TensorFlow version with the latest compatible hardware configuration which could potentially resolve the issue. If you are still facing the issue, please create a new GitHub issue with your latest findings, with all the debugging information which could help us investigate.

    Please follow the release notes to stay up to date with the latest developments which are happening in the Tensorflow space.

  19. added
    stat:awaiting response
    on Jul 31, 2024
  20. github-actions commented on Aug 8, 2024

    @github-actions

    This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.

  21. added
    stale
    on Aug 8, 2024
  22. github-actions commented on Aug 16, 2024

    @github-actions

    This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.

  23. google-ml-butler commented on Aug 16, 2024

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

comp:xlastalestat:awaiting responsetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions