|
| 1 | +/* Copyright 2025 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +// This transformation pass propagates QSV information through the model. |
| 17 | + |
| 18 | +#include <memory> |
| 19 | +#include <utility> |
| 20 | + |
| 21 | +#include "mlir/Dialect/Quant/IR/Quant.h" // from @llvm-project |
| 22 | +#include "mlir/Dialect/Quant/IR/QuantTypes.h" // from @llvm-project |
| 23 | +#include "mlir/IR/BuiltinOps.h" // from @llvm-project |
| 24 | +#include "mlir/IR/BuiltinTypeInterfaces.h" // from @llvm-project |
| 25 | +#include "mlir/IR/Diagnostics.h" // from @llvm-project |
| 26 | +#include "mlir/IR/PatternMatch.h" // from @llvm-project |
| 27 | +#include "mlir/Pass/Pass.h" // from @llvm-project |
| 28 | +#include "mlir/Support/LLVM.h" // from @llvm-project |
| 29 | +#include "mlir/Support/LogicalResult.h" // from @llvm-project |
| 30 | +#include "mlir/Support/TypeID.h" // from @llvm-project |
| 31 | +#include "mlir/Transforms/DialectConversion.h" // from @llvm-project |
| 32 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project |
| 33 | +#include "tensorflow/compiler/mlir/lite/transforms/passes.h" // IWYU pragma: keep |
| 34 | + |
| 35 | +namespace mlir { |
| 36 | +namespace TFL { |
| 37 | +namespace { |
| 38 | + |
| 39 | +#define GEN_PASS_DEF_PROPAGATEQSVPASS |
| 40 | +#include "tensorflow/compiler/mlir/lite/transforms/passes.h.inc" |
| 41 | + |
| 42 | +//===----------------------------------------------------------------------===// |
| 43 | +// Pass Definition |
| 44 | +//===----------------------------------------------------------------------===// |
| 45 | + |
| 46 | +struct PropagateQsvPass : public impl::PropagateQsvPassBase<PropagateQsvPass> { |
| 47 | + public: |
| 48 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(PropagateQsvPass) |
| 49 | + |
| 50 | + void runOnOperation() override; |
| 51 | +}; |
| 52 | + |
| 53 | +//===----------------------------------------------------------------------===// |
| 54 | +// Pass Implementation |
| 55 | +//===----------------------------------------------------------------------===// |
| 56 | + |
| 57 | +void PropagateQsvPass::runOnOperation() { |
| 58 | + MLIRContext* ctx = &getContext(); |
| 59 | + mlir::ModuleOp module = getOperation(); |
| 60 | + |
| 61 | + RewritePatternSet patterns(ctx); |
| 62 | + |
| 63 | + // Configure the greedy pattern rewrite driver. |
| 64 | + GreedyRewriteConfig greedy_config; |
| 65 | + greedy_config.enableFolding(false); |
| 66 | + |
| 67 | + // Apply the patterns. |
| 68 | + if (failed( |
| 69 | + applyPatternsGreedily(module, std::move(patterns), greedy_config))) { |
| 70 | + signalPassFailure(); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace |
| 75 | + |
| 76 | +//===----------------------------------------------------------------------===// |
| 77 | +// Pass Creation Function |
| 78 | +//===----------------------------------------------------------------------===// |
| 79 | + |
| 80 | +std::unique_ptr<OperationPass<mlir::ModuleOp>> CreatePropagateQsvPass() { |
| 81 | + return std::make_unique<PropagateQsvPass>(); |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace TFL |
| 85 | +} // namespace mlir |
0 commit comments