1 /* Copyright 2021 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/Func/IR/FuncOps.h"  // from @llvm-project
19 #include "mlir/IR/Attributes.h"  // from @llvm-project
20 #include "mlir/IR/Builders.h"  // from @llvm-project
21 #include "mlir/IR/BuiltinAttributes.h"  // from @llvm-project
22 #include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
23 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
24 #include "mlir/IR/Operation.h"  // from @llvm-project
25 #include "mlir/IR/Visitors.h"  // from @llvm-project
26 #include "mlir/Pass/Pass.h"  // from @llvm-project
27 #include "mlir/Transforms/DialectConversion.h"  // from @llvm-project
28 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
29 #include "tensorflow/compiler/mlir/lite/transforms/passes.h"
30 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
31 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops_n_z.h"
32 
33 namespace mlir {
34 namespace TFL {
35 namespace {
36 #define GEN_PASS_CLASSES
37 #include "tensorflow/compiler/mlir/lite/transforms/passes.h.inc"
38 
39 // The threshold of constant bits to be unfolded (1Mb). If there is a splat
40 // constant with size equal or greater to this threshold, then it will be
41 // unfolded back to a regular `tfl.fill` operation.
42 constexpr int64_t kConstantSizeThresholdInBits = 1e+6;
43 
44 // Pass which will replace large splat constant tensors to `tfl.Fill` op to
45 // reduce the size of the generated flatbuffer model size.
46 class UnfoldLargeSplatConstantPass
47     : public UnfoldLargeSplatConstantPassBase<UnfoldLargeSplatConstantPass> {
48  public:
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(UnfoldLargeSplatConstantPass)49   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(UnfoldLargeSplatConstantPass)
50 
51   void runOnOperation() override {
52     auto module = getOperation();
53 
54     mlir::OpBuilder op_builder(&module.getBodyRegion());
55     module.walk([&](mlir::arith::ConstantOp const_op) {
56       MaybeUnfoldLargeSplatConstant(&op_builder, const_op);
57     });
58   }
59 
60  private:
MaybeUnfoldLargeSplatConstant(mlir::OpBuilder * op_builder,mlir::arith::ConstantOp const_op) const61   void MaybeUnfoldLargeSplatConstant(mlir::OpBuilder* op_builder,
62                                      mlir::arith::ConstantOp const_op) const {
63     auto splat_elements_attr =
64         const_op.getValue().dyn_cast<SplatElementsAttr>();
65     if (!splat_elements_attr) {
66       return;
67     }
68     auto element_type = splat_elements_attr.getType().getElementType();
69     if (!(element_type.isF32() || element_type.isInteger(1) ||
70           element_type.isInteger(32) || element_type.isInteger(64))) {
71       return;
72     }
73     if (splat_elements_attr.getNumElements() *
74             splat_elements_attr.getType().getElementTypeBitWidth() <
75         kConstantSizeThresholdInBits) {
76       return;
77     }
78 
79     op_builder->setInsertionPoint(const_op);
80     mlir::arith::ConstantOp fill_shape =
81         op_builder->create<mlir::arith::ConstantOp>(
82             const_op->getLoc(),
83             DenseIntElementsAttr::get(
84                 RankedTensorType::get({splat_elements_attr.getType().getRank()},
85                                       op_builder->getI64Type()),
86                 splat_elements_attr.getType().getShape()));
87     mlir::arith::ConstantOp fill_value =
88         op_builder->create<mlir::arith::ConstantOp>(
89             const_op->getLoc(),
90             DenseElementsAttr::get(
91                 RankedTensorType::get(
92                     {}, splat_elements_attr.getType().getElementType()),
93                 splat_elements_attr.getSplatValue<Attribute>()));
94     TFL::FillOp fill = op_builder->create<TFL::FillOp>(
95         const_op->getLoc(), splat_elements_attr.getType(), fill_shape,
96         fill_value);
97     const_op->replaceAllUsesWith(fill);
98     const_op->erase();
99   }
100 };
101 
102 }  // namespace
103 
CreateUnfoldLargeSplatConstantPass()104 std::unique_ptr<OperationPass<ModuleOp>> CreateUnfoldLargeSplatConstantPass() {
105   return std::make_unique<UnfoldLargeSplatConstantPass>();
106 }
107 
108 }  // namespace TFL
109 }  // namespace mlir
110