-
Notifications
You must be signed in to change notification settings - Fork 74k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a runtime option to lower bound the number of batch threads.
PiperOrigin-RevId: 626118642
- Loading branch information
1 parent
762375d
commit 45f30ac
Showing
9 changed files
with
171 additions
and
2 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
53 changes: 53 additions & 0 deletions
53
tensorflow/compiler/mlir/tfrt/tests/lower_bound_batch_threads.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,53 @@ | ||
// RUN: tf-tfrt-opt -split-input-file -tfrt-lower-bound-batch-threads="tfrt-min-num-batch-threads=2" %s | FileCheck %s --dump-input=always | ||
|
||
// ----- | ||
|
||
// The num_batch_threads is lowered bound to 2 from the original attribute of 1 | ||
|
||
// CHECK-LABEL: func private @batched_function | ||
func.func private @batched_function(%arg0: tensor<1x3xf32>) -> tensor<1x3xf32> { | ||
%2 = "tf.Identity"(%arg0) : (tensor<1x3xf32>) -> tensor<1x3xf32> | ||
func.return %2 : tensor<1x3xf32> | ||
} | ||
|
||
// CHECK-LABEL: func @main | ||
func.func @main(%arg0: tensor<1x3xf32>) -> tensor<*xf32> { | ||
// CHECK: "tf.BatchFunction" | ||
// CHECK-SAME: allowed_batch_sizes = [6] | ||
// CHECK-SAME: batch_timeout_micros = 100000 : i64 | ||
// CHECK-SAME: batching_queue = "" | ||
// CHECK-SAME: container = "" | ||
// CHECK-SAME: enable_large_batch_splitting = false | ||
// CHECK-SAME: max_batch_size = 6 : i64 | ||
// CHECK-SAME: max_enqueued_batches = 10 : i64 | ||
// CHECK-SAME: num_batch_threads = 2 : i64 | ||
// CHECK-SAME: shared_name = "batch/" | ||
%1 = "tf.BatchFunction"(%arg0) {allowed_batch_sizes = [6], batch_timeout_micros = 100000 : i64, batching_queue = "", container = "", device = "/device:CPU:0", enable_large_batch_splitting = false, f = @batched_function, max_batch_size = 6 : i64, max_enqueued_batches = 10 : i64, num_batch_threads = 1 : i64, operandSegmentSizes = array<i32: 1, 0>, shared_name = "batch/"} : (tensor<1x3xf32>) -> tensor<*xf32> | ||
func.return %1 : tensor<*xf32> | ||
} | ||
|
||
// ----- | ||
|
||
// The num_batch_threads remains 3 (the same as the original attribute) | ||
|
||
// CHECK-LABEL: func private @batched_function | ||
func.func private @batched_function(%arg0: tensor<1x3xf32>) -> tensor<1x3xf32> { | ||
%2 = "tf.Identity"(%arg0) : (tensor<1x3xf32>) -> tensor<1x3xf32> | ||
func.return %2 : tensor<1x3xf32> | ||
} | ||
|
||
// CHECK-LABEL: func @main | ||
func.func @main(%arg0: tensor<1x3xf32>) -> tensor<*xf32> { | ||
// CHECK: "tf.BatchFunction" | ||
// CHECK-SAME: allowed_batch_sizes = [6] | ||
// CHECK-SAME: batch_timeout_micros = 100000 : i64 | ||
// CHECK-SAME: batching_queue = "" | ||
// CHECK-SAME: container = "" | ||
// CHECK-SAME: enable_large_batch_splitting = false | ||
// CHECK-SAME: max_batch_size = 6 : i64 | ||
// CHECK-SAME: max_enqueued_batches = 10 : i64 | ||
// CHECK-SAME: num_batch_threads = 3 : i64 | ||
// CHECK-SAME: shared_name = "batch/" | ||
%1 = "tf.BatchFunction"(%arg0) {allowed_batch_sizes = [6], batch_timeout_micros = 100000 : i64, batching_queue = "", container = "", device = "/device:CPU:0", enable_large_batch_splitting = false, f = @batched_function, max_batch_size = 6 : i64, max_enqueued_batches = 10 : i64, num_batch_threads = 3 : i64, operandSegmentSizes = array<i32: 1, 0>, shared_name = "batch/"} : (tensor<1x3xf32>) -> tensor<*xf32> | ||
func.return %1 : tensor<*xf32> | ||
} |
93 changes: 93 additions & 0 deletions
93
tensorflow/compiler/mlir/tfrt/transforms/lower_bound_batch_threads.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,93 @@ | ||
/* 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 <algorithm> | ||
#include <cstdint> | ||
#include <memory> | ||
|
||
#include "llvm/ADT/APInt.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "mlir/IR/BuiltinOps.h" // from @llvm-project | ||
#include "mlir/Pass/Pass.h" // from @llvm-project | ||
#include "mlir/Pass/PassRegistry.h" // from @llvm-project | ||
#include "mlir/Support/TypeID.h" // from @llvm-project | ||
#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" | ||
#include "tensorflow/compiler/mlir/tfrt/transforms/passes.h" | ||
|
||
namespace tensorflow { | ||
namespace tfrt_compiler { | ||
namespace { | ||
|
||
class LowerBoundBatchThreadsPass | ||
: public mlir::PassWrapper<LowerBoundBatchThreadsPass, | ||
mlir::OperationPass<mlir::ModuleOp>> { | ||
public: | ||
explicit LowerBoundBatchThreadsPass(uint64_t min_num_batch_threads) | ||
: mlir::PassWrapper<LowerBoundBatchThreadsPass, | ||
mlir::OperationPass<mlir::ModuleOp>>() { | ||
min_num_batch_threads_ = min_num_batch_threads; | ||
} | ||
LowerBoundBatchThreadsPass() | ||
: mlir::PassWrapper<LowerBoundBatchThreadsPass, | ||
mlir::OperationPass<mlir::ModuleOp>>() {} | ||
LowerBoundBatchThreadsPass(const LowerBoundBatchThreadsPass& other) | ||
: mlir::PassWrapper<LowerBoundBatchThreadsPass, | ||
mlir::OperationPass<mlir::ModuleOp>>(other) {} | ||
|
||
LowerBoundBatchThreadsPass& operator=( | ||
const LowerBoundBatchThreadsPass& other) = delete; | ||
|
||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LowerBoundBatchThreadsPass) | ||
|
||
private: | ||
llvm::StringRef getArgument() const final { | ||
return "tfrt-lower-bound-batch-threads"; | ||
} | ||
|
||
llvm::StringRef getDescription() const final { | ||
return "Lower bound batch threads for batch ops."; | ||
} | ||
|
||
void runOnOperation() override { | ||
if (min_num_batch_threads_ > 0) { | ||
mlir::ModuleOp module = getOperation(); | ||
module.walk([&](mlir::TF::BatchFunctionOp batch_op) { | ||
int64_t num_batch_threads = batch_op.getNumBatchThreads(); | ||
num_batch_threads = | ||
std::max(num_batch_threads, min_num_batch_threads_.getValue()); | ||
batch_op.setNumBatchThreads(num_batch_threads); | ||
}); | ||
} | ||
} | ||
|
||
protected: | ||
mlir::Pass::Option<int64_t> min_num_batch_threads_{ | ||
*this, "tfrt-min-num-batch-threads", llvm::cl::init(1), | ||
llvm::cl::desc("Minimum number of batch threads")}; | ||
; | ||
}; | ||
|
||
} // namespace | ||
|
||
std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>> | ||
CreateLowerBoundBatchThreadsPass(int64_t min_num_batch_threads) { | ||
return std::make_unique<LowerBoundBatchThreadsPass>(min_num_batch_threads); | ||
} | ||
|
||
static mlir::PassRegistration<LowerBoundBatchThreadsPass> register_pass; | ||
|
||
} // namespace tfrt_compiler | ||
} // namespace tensorflow |
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
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