Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[xla] hlo_computation: compact instructions' vector on Cleanup()
tl;dr: this gives a 1.26x compilation time speedup for a large, dense model in XLA:GPU. The largest perf leaf seen in profiles of a large, dense model is related to computing the post order. Surprisingly, it is not the DFS itself what's most expensive; rather, most of the time is spent on scanning through HloComputation::Instructions() to identify DFS roots. The reason this scan becomes expensive as instructions are removed is that the vector holding HloInstructionInfo (introduced in cl/600130708 || openxla/xla@247280ab727) is not shrunk as it flows through the pipeline, making us having to walk through many deleted "tombstone" entries. Here is the histogram of # of tombstones encountered during post order computations for this model: ``` [ 1 - 1,536,345) ****************************** (1,300,248) [1,536,345 - 3,072,690) (2) [3,072,690 - 4,609,034) (364) [4,609,034 - 6,145,378) (10,443) ``` To ameliorate this, this CL shrinks the vector periodically, so far only between passes. This is done by running compaction on the vector during HloComputation::Cleanup(), which is called after every pass. The cost of compaction is made proportional to the number of deleted entries by swapping--if needed--each tombstone with the rightmost (within the vector) non-deleted entry. This brings the number of seen tombstones down significantly: ``` [ 1 - 327,699) ****************************** (937,541) [ 327,699 - 655,396) (308) [ 655,396 - 983,094) (0) [ 983,094 - 1,310,792) (1) ``` Note: we could further improve compaction by calling Cleanup() from some passes, instead of just between passes. However, that would not yield a significant gain; at least for this model, scanning the instructions' vector now takes ~1% of total time (vs. ~17% before). FUTURE_COPYBARA_INTEGRATE_REVIEW=openxla/xla#10503 from pearu:pearu/log1p d35cef4f5fa09482c49edfee709e86c5ca29adde PiperOrigin-RevId: 619057964
- Loading branch information
1 parent
a4d9df4
commit c99f3c4
Showing
17 changed files
with
2,216 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
tensorflow/compiler/mlir/quantization/stablehlo/passes/xla_call_module_to_call.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include <utility> | ||
|
||
#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project | ||
#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project | ||
#include "mlir/IR/MLIRContext.h" // from @llvm-project | ||
#include "mlir/IR/OpDefinition.h" // from @llvm-project | ||
#include "mlir/IR/PatternMatch.h" // from @llvm-project | ||
#include "mlir/IR/SymbolTable.h" // from @llvm-project | ||
#include "mlir/Support/LLVM.h" // from @llvm-project | ||
#include "mlir/Support/LogicalResult.h" // from @llvm-project | ||
#include "mlir/Support/TypeID.h" // from @llvm-project | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project | ||
#include "tensorflow/compiler/mlir/lite/transforms/passes.h" | ||
#include "tensorflow/compiler/mlir/quantization/common/attrs_and_constraints.h" | ||
#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" | ||
|
||
namespace mlir::quant::stablehlo { | ||
|
||
#define GEN_PASS_DEF_XLACALLMODULETOCALLPASS | ||
#include "tensorflow/compiler/mlir/quantization/stablehlo/passes/passes.h.inc" | ||
|
||
namespace { | ||
|
||
// Converts XlaCallModuleOps to func.call. | ||
class XlaCallModuleToCallPass | ||
: public impl::XlaCallModuleToCallPassBase<XlaCallModuleToCallPass> { | ||
public: | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(XlaCallModuleToCallPass) | ||
|
||
explicit XlaCallModuleToCallPass() = default; | ||
|
||
private: | ||
void runOnOperation() override; | ||
}; | ||
|
||
// Converts XlaCallModuleOps to func.call. | ||
class XlaCallModuleOpToCallOp : public OpRewritePattern<TF::XlaCallModuleOp> { | ||
public: | ||
explicit XlaCallModuleOpToCallOp(MLIRContext* context) | ||
: OpRewritePattern<TF::XlaCallModuleOp>(context) {} | ||
|
||
LogicalResult matchAndRewrite(TF::XlaCallModuleOp op, | ||
PatternRewriter& rewriter) const override { | ||
auto module_op = op->getParentOfType<ModuleOp>(); | ||
SymbolTable symbol_table(module_op); | ||
|
||
auto entry_func_op = dyn_cast_or_null<func::FuncOp>( | ||
symbol_table.lookup(GetEntryFunctionName(op))); | ||
if (!entry_func_op) return failure(); | ||
|
||
// Replace the XlaCallModuleOp with a new CallOp. | ||
rewriter.replaceOpWithNewOp<func::CallOp>(op, entry_func_op, op.getArgs()); | ||
return success(); | ||
} | ||
}; | ||
|
||
void XlaCallModuleToCallPass::runOnOperation() { | ||
ModuleOp module_op = getOperation(); | ||
MLIRContext* ctx = module_op.getContext(); | ||
RewritePatternSet patterns(&getContext()); | ||
patterns.add<XlaCallModuleOpToCallOp>(ctx); | ||
if (failed(applyPatternsAndFoldGreedily(module_op, std::move(patterns)))) { | ||
signalPassFailure(); | ||
} | ||
} | ||
|
||
} // namespace | ||
} // namespace mlir::quant::stablehlo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tensorflow/compiler/mlir/quantization/stablehlo/tests/passes/xla_call_module_to_call.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// RUN: stablehlo-quant-opt %s -split-input-file -stablehlo-xla-call-module-to-call | FileCheck %s | ||
|
||
// ----- | ||
|
||
// Tests composite tf.XlaCallModule is converted to func.call. | ||
|
||
module { | ||
// CHECK-LABEL: func.func @main | ||
func.func @main(%arg0: tensor<1x1024xf32>) -> tensor<1x3xf32> { | ||
// CHECK: call @composite_dot_general_fn_1 | ||
// CHECK-SAME: (tensor<1x1024xf32>, tensor<1024x3xf32>) -> tensor<1x3xf32> | ||
// CHECK-NOT: tf.XlaCallModule | ||
%0 = "tf.Const"() <{value = dense<0.5> : tensor<1024x3xf32>}> : () -> tensor<1024x3xf32> | ||
%2 = "tf.XlaCallModule"(%arg0, %0) <{Sout = [#tf_type.shape<1x3>], dim_args_spec = [], disabled_checks = [], has_token_input_output = false, module = "", platforms = [], version = 5 : i64}> {_entry_function = @composite_dot_general_fn_1, _original_entry_function = "composite_dot_general_fn_1", _stablehlo_module_attrs = {}, _tfl_quant_trait = "fully_quantizable", device = ""} : (tensor<1x1024xf32>, tensor<1024x3xf32>) -> tensor<1x3xf32> | ||
return %2 : tensor<1x3xf32> | ||
} | ||
// CHECK-LABEL: func.func private @composite_dot_general_fn_1 | ||
// CHECK-SAME: -> tensor<1x3xf32> | ||
func.func private @composite_dot_general_fn_1(%arg0: tensor<1x1024xf32>, %arg1: tensor<1024x3xf32>) -> tensor<1x3xf32> attributes {_from_xla_call_module} { | ||
%0 = stablehlo.dot_general %arg0, %arg1, contracting_dims = [1] x [0] : (tensor<1x1024xf32>, tensor<1024x3xf32>) -> tensor<1x3xf32> | ||
return %0 : tensor<1x3xf32> | ||
} | ||
} |
Oops, something went wrong.