xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tosa/transforms/dequantize_tfl_softmax.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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 #include <utility>
17 
18 #include "mlir/Dialect/Quant/QuantTypes.h"  // from @llvm-project
19 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
20 #include "mlir/Pass/Pass.h"  // from @llvm-project
21 #include "mlir/Support/LogicalResult.h"  // from @llvm-project
22 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"  // from @llvm-project
23 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
24 #include "tensorflow/compiler/mlir/tosa/transforms/passes.h"
25 
26 #define PASS_NAME "tosa-dequantize_tfl-softmax"
27 #define DEBUG_TYPE PASS_NAME
28 
29 namespace mlir {
30 namespace tosa {
31 namespace {
32 
33 class TosaDequantizeTFLSoftmax
34     : public TosaDequantizeTFLSoftmaxPassBase<TosaDequantizeTFLSoftmax> {
35  public:
TosaDequantizeTFLSoftmax()36   explicit TosaDequantizeTFLSoftmax() {}
37   void runOnOperation() override;
38 };
39 
40 struct TosaDequantizeTFLSoftmaxPattern : public RewritePattern {
TosaDequantizeTFLSoftmaxPatternmlir::tosa::__anone5c6d2300111::TosaDequantizeTFLSoftmaxPattern41   explicit TosaDequantizeTFLSoftmaxPattern(MLIRContext* context)
42       : RewritePattern(TFL::SoftmaxOp::getOperationName(), 1, context) {}
43   LogicalResult matchAndRewrite(Operation* op,
44                                 PatternRewriter& rewriter) const override;
45 };
46 
matchAndRewrite(Operation * op,PatternRewriter & rewriter) const47 LogicalResult TosaDequantizeTFLSoftmaxPattern::matchAndRewrite(
48     Operation* op, PatternRewriter& rewriter) const {
49   TFL::SoftmaxOp tfl_softmax_op = cast<TFL::SoftmaxOp>(op);
50   RankedTensorType input_type =
51       tfl_softmax_op.input().getType().cast<RankedTensorType>();
52   if (!input_type.getElementType().isa<mlir::quant::QuantizedType>()) {
53     return failure();
54   }
55   Location loc = tfl_softmax_op.getLoc();
56   RankedTensorType dequantized_input_type =
57       RankedTensorType::get(input_type.getShape(), rewriter.getF32Type());
58   Value dequantized_input = rewriter.create<TFL::DequantizeOp>(
59       loc, dequantized_input_type, tfl_softmax_op.input());
60   Value dequantized_softmax_output = rewriter.create<TFL::SoftmaxOp>(
61       loc, dequantized_input_type, dequantized_input, tfl_softmax_op.beta());
62   Type qtype = tfl_softmax_op.output().getType();
63   rewriter.replaceOpWithNewOp<TFL::QuantizeOp>(tfl_softmax_op, qtype,
64                                                dequantized_softmax_output,
65                                                mlir::TypeAttr::get(qtype));
66   return success();
67 }
68 
runOnOperation()69 void TosaDequantizeTFLSoftmax::runOnOperation() {
70   RewritePatternSet patterns(&getContext());
71   patterns.add<TosaDequantizeTFLSoftmaxPattern>(&getContext());
72   if (failed(
73           applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)))) {
74     signalPassFailure();
75   }
76 }
77 
78 }  // anonymous namespace
79 
createDequantizeTFLSoftmaxPass()80 std::unique_ptr<OperationPass<func::FuncOp>> createDequantizeTFLSoftmaxPass() {
81   return std::make_unique<TosaDequantizeTFLSoftmax>();
82 }
83 
84 }  // namespace tosa
85 
86 }  // namespace mlir
87