Repository navigation
Closed
Labels
compiler/dialectsRelating to the IREE compiler dialects (flow, hal, vm)good first issue 🌱Good for newcomershelp wantedExtra attention is neededperformance ⚡Performance/optimization related work across the compiler and runtime
Description
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.
Metadata
Metadata
Assignees
Labels
compiler/dialectsRelating to the IREE compiler dialects (flow, hal, vm)good first issue 🌱Good for newcomershelp wantedExtra attention is neededperformance ⚡Performance/optimization related work across the compiler and runtime
Activity
benvanik commentedon Apr 20, 2020
resnet50 after flow partitioning, showing that the broadcasts are pretty much the major performance/memory issue: https://gist.github.com/benvanik/cf8783fc6b2e58737df6b4e1fbc58d34
qqsun8819 commentedon May 6, 2020
I'll take this @benvanik
benvanik commentedon Nov 22, 2020
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.