Skip to content

Commit

Permalink
Add lifting pass for dynamic range quantization
Browse files Browse the repository at this point in the history
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
sngyhan authored and tensorflower-gardener committed May 16, 2022
1 parent 4a52a36 commit fcc0207
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 5 deletions.
26 changes: 21 additions & 5 deletions tensorflow/compiler/mlir/quantization/tensorflow/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ cc_library(
)

td_library(
name = "ptq_td_files",
name = "quant_td_files",
srcs = [
"passes/lift_quantizable_spots_as_functions.td",
"passes/lift_quantizable_spots_as_functions_dynamic_range.td",
"passes/prepare_lifting.td",
"passes/prepare_quantize.td",
"passes/quantize_composite_functions.td",
Expand All @@ -77,7 +78,7 @@ gentbl_cc_library(
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "passes/prepare_lifting.td",
deps = [":ptq_td_files"],
deps = [":quant_td_files"],
)

gentbl_cc_library(
Expand All @@ -90,7 +91,20 @@ gentbl_cc_library(
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "passes/lift_quantizable_spots_as_functions.td",
deps = [":ptq_td_files"],
deps = [":quant_td_files"],
)

gentbl_cc_library(
name = "lift_quantizable_spots_as_functions_dynamic_range_inc_gen",
tbl_outs = [
(
["-gen-rewriters"],
"passes/lift_quantizable_spots_as_functions_dynamic_range.inc",
),
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "passes/lift_quantizable_spots_as_functions_dynamic_range.td",
deps = [":quant_td_files"],
)

gentbl_cc_library(
Expand All @@ -103,7 +117,7 @@ gentbl_cc_library(
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "passes/prepare_quantize.td",
deps = [":ptq_td_files"],
deps = [":quant_td_files"],
)

gentbl_cc_library(
Expand All @@ -116,7 +130,7 @@ gentbl_cc_library(
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "passes/quantize_composite_functions.td",
deps = [":ptq_td_files"],
deps = [":quant_td_files"],
)

cc_library(
Expand All @@ -130,6 +144,8 @@ cc_library(
"passes/issue_ids_of_custom_aggregation_ops.cc",
"passes/lift_quantizable_spots_as_functions.cc",
"passes/lift_quantizable_spots_as_functions.inc",
"passes/lift_quantizable_spots_as_functions_dynamic_range.cc",
"passes/lift_quantizable_spots_as_functions_dynamic_range.inc",
"passes/post_quantize.cc",
"passes/prepare_lifting.cc",
"passes/prepare_lifting.inc",
Expand Down
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 &registry) 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
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)>;
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]]
}

0 comments on commit fcc0207

Please sign in to comment.