这是indexloc提供的服务,不要输入任何密码
Skip to content

Add declarative quantization optimization patterns. #97467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tensorflow/compiler/mlir/lite/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ td_library(
"transforms/optimize_patterns.td",
"transforms/post_quantize_patterns.td",
"transforms/prepare_patterns.td",
"transforms/quantization/strict_quantize_patterns.td",
"transforms/quantize_by_converter_patterns.td",
"transforms/quantize_patterns.td",
"transforms/tensorlist_patterns.td",
Expand Down Expand Up @@ -233,6 +234,15 @@ gentbl_cc_library(
deps = [":tensorflow_lite_patterns_td_files"],
)

gentbl_cc_library(
name = "tensorflow_lite_strict_quantize_inc_gen",
compatible_with = get_compatible_with_portable(),
tbl_outs = {"transforms/quantization/generated_strict_quantize.inc": ["-gen-rewriters"]},
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "transforms/quantization/strict_quantize_patterns.td",
deps = [":tensorflow_lite_patterns_td_files"],
)

gentbl_cc_library(
name = "tensorflow_lite_quantize_by_converter_inc_gen",
compatible_with = get_compatible_with_portable(),
Expand Down Expand Up @@ -1375,6 +1385,7 @@ cc_library(
":tensorflow_lite_post_quantize_inc_gen",
":tensorflow_lite_quantize_by_converter_inc_gen",
":tensorflow_lite_quantize_inc_gen",
":tensorflow_lite_strict_quantize_inc_gen",
":validators",
"//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib",
"//tensorflow/compiler/mlir/lite/quantization/common/quantization_lib:quantization_config",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Copyright 2025 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.
==============================================================================*/

// Provides patterns for quantization following the framework annotations.

include "mlir/IR/OpBase.td"
include "mlir/IR/PatternBase.td"
include "mlir/Dialect/Arith/IR/ArithOps.td"
include "mlir/Dialect/Func/IR/FuncOps.td"
include "mlir/IR/CommonTypeConstraints.td"
include "tensorflow/compiler/mlir/lite/quantization/common/quantization_lib/quantization.td"
include "tensorflow/compiler/mlir/lite/ir/tfl_ops.td"

def HasSameType : Constraint<CPred<[{$0.getType() == $1.getType()}]>>;

def HasOneUse : Constraint<CPred<"$0.hasOneUse()">>;

def IsNotFromSameScalesOp : Constraint<CPred<
"!$0.getDefiningOp() || !llvm::isa<mlir::TFL::SameScalesOpInterface>($0.getDefiningOp())">>;

def RemoveNoOpQ: Pat<(TFL_QuantizeOp:$out $in, $qt),
(replaceWithValue $in),
[(HasSameType $in, $out)]>;

// Change DQ->Q with different types to a requant.
def FuseDqQToRequant: Pat<(TFL_QuantizeOp:$out (TFL_DequantizeOp:$mid $in), $qt),
(TFL_QuantizeOp $in, $qt),
[(HasOneUse $mid)]>;

// Squash repeating quantize ops.
// Unless the preceding op needs is strict about its scale requirements in which
// case, we keep the first quantize to be fused later and let the second
// quantize be(come) a requant op.
def FuseQQToRequant: Pat<(TFL_QuantizeOp (TFL_QuantizeOp:$mid $in, $_), $qt2),
(TFL_QuantizeOp $in, $qt2),
[(HasOneUse $mid), (IsNotFromSameScalesOp $in)]>;