aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/toco/graph_transformations/remove_trivial_passthrough.h
blob: b72c85c0e577ffe6d53c89bf35236192771efde2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef THIRD_PARTY_TENSORFLOW_CONTRIB_LITE_TOCO_GRAPH_TRANSFORMATIONS_REMOVE_TRIVIAL_PASSTHROUGH_H_
#define THIRD_PARTY_TENSORFLOW_CONTRIB_LITE_TOCO_GRAPH_TRANSFORMATIONS_REMOVE_TRIVIAL_PASSTHROUGH_H_

#include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
#include "tensorflow/contrib/lite/toco/model.h"

namespace toco {

// A "passthrough op" is an op that satisfies the following conditions:
//   1. It has at most one non-constant input (it may have other constant
//   inputs).
//   2. It has exactly one output.
//   3. It forwards exactly its single non-constant input to its single output.
//
// Examples include:
//   1. TensorFlow Identity ops. (Have one input).
//   2. TensorFlow Reshape ops when the input and output shapes agree.
//   3. Any binary operator, one of whose two inputs is a constant and is the
//      neutral value for that operation. For example, a binary Add operator
//      where one of its inputs is a constant array filled with zeros.
//
// A passthrough op is "trivial" and can be removed when it is possible to
// discard either its single non-constant input or output array, rerouting any
// edge involving it to the other of these two arrays.
//
// It is only possible to discard such an array if it is not explicitly
// designated as a global input/output array of the graph, e.g. the model's
// input arrays, output arrays, and any array involved in a RNN back-edge
// specified by the model.
//
// This function does not check that the given operator is a passthrough op:
// that's the responsibility of the caller.
// Given that it is a passthrough op, this function checks whether it is trivial
// and then discards it and returns true, or, if it's not trivial (if neither
// the input nor the output may be discarded), returns false.
bool RemoveTrivialPassthroughOp(GraphTransformation* transformation,
                                Model* model, std::size_t op_index);

}  // namespace toco

#endif  // THIRD_PARTY_TENSORFLOW_CONTRIB_LITE_TOCO_GRAPH_TRANSFORMATIONS_REMOVE_TRIVIAL_PASSTHROUGH_H_