• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 "tensorflow/compiler/mlir/tensorflow/transforms/tf_data_optimization.h"
17 
18 #include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
19 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
20 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_types.h"
21 
22 namespace mlir {
23 namespace TF {
24 
25 namespace {
26 
27 struct FuseParallelMapAndBatch : public OpRewritePattern<BatchDatasetV2Op> {
28   using OpRewritePattern<BatchDatasetV2Op>::OpRewritePattern;
29 
matchAndRewritemlir::TF::__anon267fdae50111::FuseParallelMapAndBatch30   LogicalResult matchAndRewrite(BatchDatasetV2Op op,
31                                 PatternRewriter &rewriter) const override {
32     auto batchInputDataset = op.input_dataset();
33 
34     ParallelMapDatasetOp batchInputOp = dyn_cast_or_null<ParallelMapDatasetOp>(
35         batchInputDataset.getDefiningOp());
36     if (!batchInputOp) return failure();
37 
38     // The type of the `num_parallel_calls` argument in ParallelMapDataset
39     // and MapAndBatchDataset is different (int32 and int64 respectively)
40     auto num_parallel_calls_op = rewriter.create<CastOp>(
41         op.getLoc(), UnrankedTensorType::get(rewriter.getIntegerType(64)),
42         batchInputOp.num_parallel_calls(), rewriter.getBoolAttr(false));
43 
44     if (op.metadata() != batchInputOp.metadata()) {
45       return failure();
46     }
47 
48     auto fused_op = rewriter.create<MapAndBatchDatasetOp>(
49         op.getLoc(), op.getType(), batchInputOp.input_dataset(),
50         batchInputOp.other_arguments(), op.batch_size(),
51         num_parallel_calls_op.y(), op.drop_remainder(), batchInputOp.f(),
52         op.output_types(), op.output_shapes(),
53         batchInputOp.preserve_cardinality(), op.metadata());
54     rewriter.replaceOp(op, {fused_op.handle()});
55     return failure();
56   }
57 };
58 
59 #include "tensorflow/compiler/mlir/tensorflow/transforms/generated_tf_data_optimization.inc"
60 }  // namespace
61 
PopulateTFDataOptimizationPatterns(MLIRContext * context,RewritePatternSet * patterns)62 void PopulateTFDataOptimizationPatterns(MLIRContext *context,
63                                         RewritePatternSet *patterns) {
64   patterns->add<FuseParallelMapAndBatch>(context);
65   populateWithGenerated(*patterns);
66 }
67 
68 }  // namespace TF
69 }  // namespace mlir
70