• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/StandardOps/IR/Ops.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 
37 // The threshold of constant bits to be unfolded (1Mb). If there is a splat
38 // constant with size equal or greater to this threshold, then it will be
39 // unfolded back to a regular `tfl.fill` operation.
40 constexpr int64_t kConstantSizeThresholdInBits = 1e+6;
41 
42 // Pass which will replace large splat constant tensors to `tfl.Fill` op to
43 // reduce the size of the generated flatbuffer model size.
44 class UnfoldLargeSplatConstant
45     : public PassWrapper<UnfoldLargeSplatConstant, OperationPass<ModuleOp>> {
46  public:
getDependentDialects(DialectRegistry & registry) const47   void getDependentDialects(DialectRegistry& registry) const override {
48     registry.insert<TFL::TensorFlowLiteDialect>();
49   }
50 
getArgument() const51   StringRef getArgument() const final {
52     // This is the argument used to refer to the pass in
53     // the textual format (on the commandline for example).
54     return "tfl-unfold-large-splat-constant";
55   }
getDescription() const56   StringRef getDescription() const final {
57     // This is a brief description of the pass.
58     return "Unfold large splat constant tensors";
59   }
60 
runOnOperation()61   void runOnOperation() override {
62     auto module = getOperation();
63 
64     mlir::OpBuilder op_builder(&module.body());
65     module.walk([&](mlir::ConstantOp const_op) {
66       MaybeUnfoldLargeSplatConstant(&op_builder, const_op);
67     });
68   }
69 
70  private:
MaybeUnfoldLargeSplatConstant(mlir::OpBuilder * op_builder,mlir::ConstantOp const_op) const71   void MaybeUnfoldLargeSplatConstant(mlir::OpBuilder* op_builder,
72                                      mlir::ConstantOp const_op) const {
73     auto splat_elements_attr = const_op.value().dyn_cast<SplatElementsAttr>();
74     if (!splat_elements_attr) {
75       return;
76     }
77     auto element_type = splat_elements_attr.getType().getElementType();
78     if (!(element_type.isF32() || element_type.isInteger(1) ||
79           element_type.isInteger(32) || element_type.isInteger(64))) {
80       return;
81     }
82     if (splat_elements_attr.getNumElements() *
83             splat_elements_attr.getType().getElementTypeBitWidth() <
84         kConstantSizeThresholdInBits) {
85       return;
86     }
87 
88     op_builder->setInsertionPoint(const_op);
89     mlir::ConstantOp fill_shape = op_builder->create<mlir::ConstantOp>(
90         const_op->getLoc(),
91         DenseIntElementsAttr::get(
92             RankedTensorType::get({splat_elements_attr.getType().getRank()},
93                                   op_builder->getI64Type()),
94             splat_elements_attr.getType().getShape()));
95     mlir::ConstantOp fill_value = op_builder->create<mlir::ConstantOp>(
96         const_op->getLoc(),
97         DenseElementsAttr::get(
98             RankedTensorType::get(
99                 {}, splat_elements_attr.getType().getElementType()),
100             splat_elements_attr.getSplatValue()));
101     TFL::FillOp fill = op_builder->create<TFL::FillOp>(
102         const_op->getLoc(), splat_elements_attr.getType(), fill_shape,
103         fill_value);
104     const_op->replaceAllUsesWith(fill);
105     const_op->erase();
106   }
107 };
108 
109 }  // namespace
110 
CreateUnfoldLargeSplatConstantPass()111 std::unique_ptr<OperationPass<ModuleOp>> CreateUnfoldLargeSplatConstantPass() {
112   return std::make_unique<UnfoldLargeSplatConstant>();
113 }
114 
115 static PassRegistration<UnfoldLargeSplatConstant> pass;
116 
117 }  // namespace TFL
118 }  // namespace mlir
119