Skip to content

HLO broadcast_in_dim handling improvements #1583

Closed

Description

@benvanik

Currently the partitioning step is not handling xla_hlo.broadcast_in_dim efficiently. There's a bunch of things that could be improved:

  • No-op broadcasts are not properly folded:
    %116 = "xla_hlo.broadcast_in_dim"(%115) {broadcast_dimensions = dense<[0, 1, 2, 3]> : tensor<4xi64>} : (tensor<1x8x8x64xf32>) -> tensor<1x8x8x64xf32>
  • We should move and duplicate broadcasts down the IR tree as much as possible. For example:
    %116 = "xla_hlo.broadcast_in_dim"(%115) {broadcast_dimensions = dense<[]> : tensor<4xi64>} : (tensor<f32>) -> tensor<1x8x8x64xf32>
    %117 = "xla_hlo.broadcast_in_dim"(%arg4) {broadcast_dimensions = dense<[]> : tensor<0xi64>} : (tensor<f32>) -> tensor<1x8x8x64xf32>
    %118 = xla_hlo.maximum %116, %117 : tensor<1x8x8x64xf32>

should instead be:

    %118 = xla_hlo.maximum %115, %arg4 : tensor<f32>
    %119 = "xla_hlo.broadcast_in_dim"(%118) {broadcast_dimensions = dense<[]> : tensor<0xi64>} : (tensor<f32>) -> tensor<1x8x8x64xf32>

(1 max operation instead of 4096)

  • Broadcasts should always fold into consumer dispatch regions, never producer regions.
    %126 = "xla_hlo.broadcast_in_dim"(%arg5) {broadcast_dimensions = dense<[]> : tensor<0xi64>} : (tensor<f32>) -> tensor<1x4x4x512xf32>
    %127 = "xla_hlo.broadcast_in_dim"(%arg5) {broadcast_dimensions = dense<[]> : tensor<0xi64>} : (tensor<f32>) -> tensor<1x4x4x512xf32>
    %128 = "xla_hlo.broadcast_in_dim"(%arg5) {broadcast_dimensions = dense<[]> : tensor<0xi64>} : (tensor<f32>) -> tensor<1x4x4x512xf32>
    flow.return %125, %126, %127, %128 : tensor<1x4x4x512xf32>, tensor<1x4x4x512xf32>, tensor<1x4x4x512xf32>, tensor<1x4x4x512xf32>

this just causes a ton of transient memory to be required and prevents linalg from performing the broadcast behavior as part of its mapping.

  • Splat constants should be turned into scalar constants + broadcast_in_dim ops:
%cst_22 = constant dense<0.000000e+00> : tensor<2048xf32>

should be

%cst_22_scalar = constant dense<0.000000e+00> : tensor<f32>
%cst_22 = "xla_hlo.broadcast_in_dim"(%cst_22_scalar) {broadcast_dimensions = dense<[]> : tensor<0xi64>} : (tensor<f32>) -> tensor<2048xf32>

(combined with the other improvements above this would dramatically reduce memory required)

Even if linalg can still handle these cases when generating code having them unhandled during IREE partitioning forces a lot of additional transient tensors and data dependencies that otherwise don't need to exist.

Activity

  1. added
    help wanted
    good first issue 🌱
    compiler/dialects
    performance ⚡
    on Apr 20, 2020
  2. added this to the milestone on Apr 20, 2020
  3. benvanik commented on Apr 20, 2020

    @benvanik
    CollaboratorAuthor

    resnet50 after flow partitioning, showing that the broadcasts are pretty much the major performance/memory issue: https://gist.github.com/benvanik/cf8783fc6b2e58737df6b4e1fbc58d34

  4. qqsun8819 commented on May 6, 2020

    @qqsun8819
    Contributor

    I'll take this @benvanik

  5. removed this from the milestone on May 12, 2020
  6. self-assigned this
    on May 16, 2020
  7. benvanik commented on Nov 22, 2020

    @benvanik
    CollaboratorAuthor

    These are important and required but should be done upstream in TensorFlow. For now closing here as it won't be work done as part of the IREE repo (we need to remove the HLOToHLOPreprocessing-esque stuff we already have and work against adding more) and #1354 will decouple us from MHLO entirely.

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

Metadata

Metadata

Assignees

Labels

compiler/dialectsgood first issue 🌱help wantedperformance ⚡

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions