forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lifting pass for dynamic range quantization
Separating lifting pass from PTQ to ease managing different op coverage. tf.MatMul is supported for now and the coverage will be extended in the follow-up CLs. PiperOrigin-RevId: 448926595
- Loading branch information
1 parent
4a52a36
commit fcc0207
Showing
4 changed files
with
178 additions
and
5 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
89 changes: 89 additions & 0 deletions
89
.../mlir/quantization/tensorflow/passes/lift_quantizable_spots_as_functions_dynamic_range.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,89 @@ | ||
/* Copyright 2022 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 <memory> | ||
#include <utility> | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project | ||
#include "mlir/IR/BuiltinOps.h" // from @llvm-project | ||
#include "mlir/IR/Diagnostics.h" // from @llvm-project | ||
#include "mlir/IR/MLIRContext.h" // from @llvm-project | ||
#include "mlir/IR/PatternMatch.h" // from @llvm-project | ||
#include "mlir/Pass/Pass.h" // from @llvm-project | ||
#include "mlir/Support/LogicalResult.h" // from @llvm-project | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project | ||
#include "tensorflow/compiler/mlir/quantization/tensorflow/utils/lift_as_function_call_utils.h" | ||
#include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h" | ||
#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" | ||
|
||
namespace mlir { | ||
namespace quant { | ||
namespace { | ||
|
||
class LiftQuantizableSpotsAsFunctionsDynamicRangePass | ||
: public PassWrapper<LiftQuantizableSpotsAsFunctionsDynamicRangePass, | ||
OperationPass<ModuleOp>> { | ||
public: | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID( | ||
LiftQuantizableSpotsAsFunctionsDynamicRangePass) | ||
|
||
StringRef getArgument() const final { | ||
// This is the argument used to refer to the pass in | ||
// the textual format (on the commandline for example). | ||
return "quant-lift-quantizable-spots-as-functions-dynamic-range"; | ||
} | ||
|
||
StringRef getDescription() const final { | ||
// This is a brief description of the pass. | ||
return "Replace quantization candidates with composite functions into the " | ||
"module for post-training dynamic range case"; | ||
} | ||
|
||
void getDependentDialects(DialectRegistry ®istry) const override { | ||
registry.insert<TF::TensorFlowDialect>(); | ||
} | ||
|
||
void runOnOperation() override; | ||
}; | ||
|
||
static PassRegistration<LiftQuantizableSpotsAsFunctionsDynamicRangePass> pass; | ||
|
||
#include "tensorflow/compiler/mlir/quantization/tensorflow/passes/lift_quantizable_spots_as_functions_dynamic_range.inc" | ||
|
||
void LiftQuantizableSpotsAsFunctionsDynamicRangePass::runOnOperation() { | ||
MLIRContext *ctx = &getContext(); | ||
RewritePatternSet patterns(ctx); | ||
ModuleOp module = getOperation(); | ||
|
||
populateWithGenerated(patterns); | ||
FrozenRewritePatternSet frozen_patterns(std::move(patterns)); | ||
for (auto func : module.getOps<func::FuncOp>()) { | ||
if (failed(applyPatternsAndFoldGreedily(func, frozen_patterns))) { | ||
func.emitError() | ||
<< "quant-lift-quantizable-spots-as-functions-dynamic-range failed."; | ||
signalPassFailure(); | ||
} | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
std::unique_ptr<OperationPass<ModuleOp>> | ||
CreateLiftQuantizableSpotsAsFunctionsDynamicRangePass() { | ||
return std::make_unique<LiftQuantizableSpotsAsFunctionsDynamicRangePass>(); | ||
} | ||
|
||
} // namespace quant | ||
} // namespace mlir |
33 changes: 33 additions & 0 deletions
33
.../mlir/quantization/tensorflow/passes/lift_quantizable_spots_as_functions_dynamic_range.td
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,33 @@ | ||
/* Copyright 2022 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 "mlir/IR/OpBase.td" | ||
include "mlir/Dialect/Func/IR/FuncOps.td" | ||
include "mlir/Dialect/Arithmetic/IR/ArithmeticOps.td" | ||
include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td" | ||
include "tensorflow/compiler/mlir/quantization/tensorflow/utils/lift_as_function_call_utils.td" | ||
include "tensorflow/compiler/mlir/quantization/tensorflow/passes/utils.td" | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Pattern rules for lifting ops as functions | ||
//===----------------------------------------------------------------------===// | ||
|
||
def LiftMatMul : Pat< | ||
(TF_MatMulOp:$res $a, $b, $transpose_a, $transpose_b), | ||
(LiftAsFunctionCall<"fused_matmul_fn"> | ||
(ArgumentList $a, $b), | ||
(ResultList $res), | ||
(AttributeList $transpose_a, $transpose_b)), | ||
[(IsNotInLiftedFunc $res)], (addBenefit 1)>; |
35 changes: 35 additions & 0 deletions
35
...mlir/quantization/tensorflow/tests/lift_quantizable_spots_as_functions_dynamic_range.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,35 @@ | ||
// Copyright 2022 The TensorFlow Runtime Authors | ||
// | ||
// 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. | ||
|
||
// RUN: tf-quant-opt %s -split-input-file -quant-lift-quantizable-spots-as-functions-dynamic-range | FileCheck %s | ||
|
||
// CHECK-LABEL: float_matmul | ||
func.func @float_matmul(%arg0: tensor<1x12x12x512xf32>) -> (tensor<*xf32>) { | ||
%cst = "tf.Const"() {value = dense<0.000000e+00> : tensor<512x512xf32>} : () -> tensor<512x512xf32> | ||
%out = "tf.MatMul"(%arg0, %cst) { | ||
device = "", transpose_a = false, transpose_b = false | ||
} : (tensor<1x12x12x512xf32>, tensor<512x512xf32>) -> tensor<*xf32> | ||
func.return %out : tensor<*xf32> | ||
|
||
// CHECK: %[[CONST:.*]] = "tf.Const"() {value = dense<0.000000e+00> : tensor<512x512xf32>} : () -> tensor<512x512xf32> | ||
// CHECK: %[[PARTITIONEDCALL:.*]] = "tf.PartitionedCall"(%arg0, %[[CONST]]) | ||
// CHECK-SAME: {_tfl_quant_trait = "fully_quantizable", | ||
// CHECK-SAME: f = @fused_matmul_fn_1} | ||
// CHECK: return %[[PARTITIONEDCALL]] | ||
// CHECK: } | ||
|
||
// CHECK-LABEL: private @fused_matmul_fn_1 | ||
// CHECK-NEXT: %[[OUT:.*]] = "tf.MatMul"(%arg0, %arg1) | ||
// CHECK-NEXT: return %[[OUT]] | ||
} |