1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#ifndef TENSORFLOW_CONTRIB_LITE_TOCO_GRAPH_TRANSFORMATIONS_REMOVE_TRIVIAL_PASSTHROUGH_H_
16#define TENSORFLOW_CONTRIB_LITE_TOCO_GRAPH_TRANSFORMATIONS_REMOVE_TRIVIAL_PASSTHROUGH_H_
17
18#include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
19#include "tensorflow/contrib/lite/toco/model.h"
20
21namespace toco {
22
23// A "passthrough op" is an op that satisfies the following conditions:
24//   1. One of its inputs is (per the semantics of that op) its "main input"
25//      for some notion of "main input" that is operator-specific; for example,
26//      for a Reshape op, the main input is the array being reshaped, not the
27//      other input which gives the new shape.
28//   2. It has exactly one output.
29//   3. It forwards exactly its main input to its single output.
30//
31// Examples include:
32//   1. TensorFlow Identity ops. (Have one input).
33//   2. TensorFlow Reshape ops when the input and output shapes agree.
34//   3. Any binary operator, one of whose two inputs is a constant and is the
35//      neutral value for that operation. For example, a binary Add operator
36//      where one of its inputs is a constant array filled with zeros.
37//
38// A passthrough op is "trivial" and can be removed when it is possible to
39// discard either its main input or output array, rerouting any
40// edge involving it to the other of these two arrays.
41//
42// It is only possible to discard such an array if it is not explicitly
43// designated as a global input/output array of the graph, e.g. the model's
44// input arrays, output arrays, and any array involved in a RNN back-edge
45// specified by the model.
46//
47// This function does not check that the given operator is a passthrough op:
48// that's the responsibility of the caller.
49// Given that it is a passthrough op, this function checks whether it is trivial
50// and then discards it and returns true, or, if it's not trivial (if neither
51// the input nor the output may be discarded), returns false.
52bool RemoveTrivialPassthroughOp(GraphTransformation* transformation,
53                                Model* model, std::size_t op_index);
54
55}  // namespace toco
56
57#endif  // TENSORFLOW_CONTRIB_LITE_TOCO_GRAPH_TRANSFORMATIONS_REMOVE_TRIVIAL_PASSTHROUGH_H_
58