1 /* Copyright 2017 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 #ifndef TENSORFLOW_LITE_TOCO_GRAPH_TRANSFORMATIONS_GRAPH_TRANSFORMATIONS_H_
16 #define TENSORFLOW_LITE_TOCO_GRAPH_TRANSFORMATIONS_GRAPH_TRANSFORMATIONS_H_
17
18 #include <cstddef>
19 #include <initializer_list>
20 #include <unordered_set>
21 #include <vector>
22
23 #include "tensorflow/lite/toco/model.h"
24 #include "tensorflow/lite/toco/toco_port.h"
25
26 namespace toco {
27
28 class GraphTransformation {
29 public:
30 virtual ::tensorflow::Status Run(Model* model, std::size_t op_index,
31 bool* modified) = 0;
32 virtual const char* Name() const = 0;
~GraphTransformation()33 virtual ~GraphTransformation() {}
34 // Returns the list of messages that this graph transformation
35 // generated since ClearMessages() was called.
Messages()36 const std::vector<string>& Messages() const { return messages_; }
37 // Clears the list of messages; should be called after every
38 // run of this graph transformation.
ClearMessages()39 void ClearMessages() { return messages_.clear(); }
40 // Adds a message; normally only called by the graph transformation
41 // itself during its run (this function could be protected).
42 template <typename... Args>
AddMessageF(const char * format,const Args &...args)43 void AddMessageF(const char* format, const Args&... args) {
44 return messages_.push_back(toco::port::StringF(format, args...));
45 }
46
47 protected:
GraphTransformation()48 GraphTransformation() {}
49
50 // List of messages generated by this graph transformation.
51 std::vector<string> messages_;
52
53 private:
54 GraphTransformation(const GraphTransformation& other) = delete;
55 GraphTransformation(const GraphTransformation&& other) = delete;
56 };
57
58 class GraphTransformationsSet {
59 public:
60 // The choice of a container with fully-specified iteration order
61 // ensures that graph transformations are always run in the same order,
62 // which avoids having toco randomly fail or produce different results
63 // depending on the toolchain. Ideally success/results should be independent
64 // of the order in which graph transformations are run, but that's
65 // unfortunately not currently guaranteed to be the case.
66 using TransformationsContainer =
67 std::vector<std::unique_ptr<GraphTransformation>>;
68
GraphTransformationsSet()69 GraphTransformationsSet() {}
GraphTransformationsSet(const std::initializer_list<GraphTransformation * > transformations)70 GraphTransformationsSet(
71 const std::initializer_list<GraphTransformation*> transformations) {
72 for (GraphTransformation* t : transformations) {
73 Add(t);
74 }
75 }
Add(GraphTransformation * transformation)76 void Add(GraphTransformation* transformation) {
77 const string& name = transformation->Name();
78 CHECK(!names_.count(name));
79 names_.insert(name);
80 transformations_.emplace_back(transformation);
81 }
begin()82 TransformationsContainer::const_iterator begin() const {
83 return transformations_.begin();
84 }
end()85 TransformationsContainer::const_iterator end() const {
86 return transformations_.end();
87 }
empty()88 bool empty() const { return transformations_.empty(); }
89
90 private:
91 GraphTransformationsSet(const GraphTransformationsSet& other) = delete;
92 GraphTransformationsSet(const GraphTransformationsSet&& other) = delete;
93 std::vector<std::unique_ptr<GraphTransformation>> transformations_;
94 // Names of transformations in the set. Only used to guard against dupes.
95 std::unordered_set<string> names_;
96 };
97
98 // Run the given list of graph transformations on the model.
99 // The message is only for logging purposes.
100 // The transformations is a rvalue reference, indicating that
101 // nothing else will use these pointers. The user is supposed to
102 // construct GraphTransformation objects by using 'new', pass us
103 // the resulting raw pointers, and this RunGraphTransformations
104 // takes care of delete'ing these pointers.
105 tensorflow::Status RunGraphTransformationsWithStatus(
106 Model* model, const string& msg,
107 const GraphTransformationsSet& transformations);
108
RunGraphTransformations(Model * model,const string & msg,const GraphTransformationsSet & transformations)109 inline void RunGraphTransformations(
110 Model* model, const string& msg,
111 const GraphTransformationsSet& transformations) {
112 auto s = RunGraphTransformationsWithStatus(model, msg, transformations);
113 CHECK(s.ok()) << s.error_message();
114 }
115
116 #define DECLARE_GRAPH_TRANSFORMATION(GTName) \
117 class GTName : public GraphTransformation { \
118 public: \
119 ::tensorflow::Status Run(Model* model, std::size_t op_index, \
120 bool* modified) override; \
121 const char* Name() const override { return #GTName; } \
122 };
123
124 // List of all graph transformations
125 DECLARE_GRAPH_TRANSFORMATION(ConvertExpandDimsToReshape)
DECLARE_GRAPH_TRANSFORMATION(ConvertPureConvToDepthwise)126 DECLARE_GRAPH_TRANSFORMATION(ConvertPureConvToDepthwise)
127 DECLARE_GRAPH_TRANSFORMATION(ConvertSqueezeToReshape)
128 DECLARE_GRAPH_TRANSFORMATION(ConvertTrivialAddNToAdd)
129 DECLARE_GRAPH_TRANSFORMATION(ConvertTrivialPackToReshape)
130 DECLARE_GRAPH_TRANSFORMATION(ConvertTrivialTileToConcat)
131 DECLARE_GRAPH_TRANSFORMATION(ConvertTrivialTransposeToReshape)
132 DECLARE_GRAPH_TRANSFORMATION(ConvertReorderAxes)
133 DECLARE_GRAPH_TRANSFORMATION(EnsureBiasVectors)
134 DECLARE_GRAPH_TRANSFORMATION(FuseActivationFunctions)
135 DECLARE_GRAPH_TRANSFORMATION(FuseBinaryIntoFollowingAffine)
136 DECLARE_GRAPH_TRANSFORMATION(FuseBinaryIntoPrecedingAffine)
137 DECLARE_GRAPH_TRANSFORMATION(FuseBroadcastIntoFollowingBinary)
138 DECLARE_GRAPH_TRANSFORMATION(GroupBidirectionalSequenceLstm)
139 DECLARE_GRAPH_TRANSFORMATION(GroupBidirectionalSequenceRnn)
140 DECLARE_GRAPH_TRANSFORMATION(GroupDynamicBidirectionalSequenceLstm)
141 DECLARE_GRAPH_TRANSFORMATION(GroupDynamicBidirectionalSequenceRnn)
142 DECLARE_GRAPH_TRANSFORMATION(IdentifyL2Normalization)
143 DECLARE_GRAPH_TRANSFORMATION(IdentifyL2Pool)
144 DECLARE_GRAPH_TRANSFORMATION(IdentifyLstmCell)
145 DECLARE_GRAPH_TRANSFORMATION(SplitLstmCellInputs)
146 DECLARE_GRAPH_TRANSFORMATION(MergeLstmCellInputs)
147 DECLARE_GRAPH_TRANSFORMATION(MergeReshapeIntoPrecedingTranspose)
148 DECLARE_GRAPH_TRANSFORMATION(IdentifyRelu1)
149 DECLARE_GRAPH_TRANSFORMATION(IdentifyPRelu)
150 DECLARE_GRAPH_TRANSFORMATION(MakeInitialDequantizeOperator)
151 DECLARE_GRAPH_TRANSFORMATION(MoveBinaryOperatorBeforeReshape)
152 DECLARE_GRAPH_TRANSFORMATION(PropagateActivationFunctionIntoConstants)
153 DECLARE_GRAPH_TRANSFORMATION(PropagateArrayDataTypes)
154 DECLARE_GRAPH_TRANSFORMATION(PropagateFakeQuantNumBits)
155 DECLARE_GRAPH_TRANSFORMATION(PropagateFixedSizes)
156 DECLARE_GRAPH_TRANSFORMATION(HardcodeMinMax)
157 DECLARE_GRAPH_TRANSFORMATION(Quantize)
158 DECLARE_GRAPH_TRANSFORMATION(RemoveFinalDequantizeOp)
159 DECLARE_GRAPH_TRANSFORMATION(RemoveTensorFlowAssert)
160 DECLARE_GRAPH_TRANSFORMATION(RemoveTensorFlowIdentity)
161 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialBinaryOperator)
162 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialConcatenation)
163 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialConcatenationInput)
164 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialFakeQuant)
165 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialSlice)
166 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialQuantizedActivationFunc)
167 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialQuantizedMinMax)
168 DECLARE_GRAPH_TRANSFORMATION(RemoveUnusedOp)
169 DECLARE_GRAPH_TRANSFORMATION(ResolveBatchNormalization)
170 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantBinaryOperator)
171 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantUnaryOperator)
172 DECLARE_GRAPH_TRANSFORMATION(CreateIm2colArrays)
173 DECLARE_GRAPH_TRANSFORMATION(DropIm2colArrays)
174 DECLARE_GRAPH_TRANSFORMATION(ReadArrayMinmaxAndNarrowRangeFromFakeQuant)
175 DECLARE_GRAPH_TRANSFORMATION(ReorderElementwiseUnary)
176 DECLARE_GRAPH_TRANSFORMATION(ReorderReshapeTranspose)
177 DECLARE_GRAPH_TRANSFORMATION(ResolveReorderAxes)
178 DECLARE_GRAPH_TRANSFORMATION(ResolveTensorFlowConcat)
179 DECLARE_GRAPH_TRANSFORMATION(ResolveTensorFlowMatMul)
180 DECLARE_GRAPH_TRANSFORMATION(ResolveTensorFlowMerge)
181 DECLARE_GRAPH_TRANSFORMATION(ResolveSqueezeAttributes)
182 DECLARE_GRAPH_TRANSFORMATION(ResolveTensorFlowSwitch)
183 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantConcatenation)
184 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantReshape)
185 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantTranspose)
186 DECLARE_GRAPH_TRANSFORMATION(DropFakeQuant)
187 DECLARE_GRAPH_TRANSFORMATION(UnfuseActivationFunctions)
188 DECLARE_GRAPH_TRANSFORMATION(UnrollBatchMatMul)
189 DECLARE_GRAPH_TRANSFORMATION(ResolveSpaceToBatchNDAttributes)
190 DECLARE_GRAPH_TRANSFORMATION(ResolveBatchToSpaceNDAttributes)
191 DECLARE_GRAPH_TRANSFORMATION(ResolvePadAttributes)
192 DECLARE_GRAPH_TRANSFORMATION(ResolvePadV2Attributes)
193 DECLARE_GRAPH_TRANSFORMATION(ResolveReduceAttributes)
194 DECLARE_GRAPH_TRANSFORMATION(ResolveReshapeAttributes)
195 DECLARE_GRAPH_TRANSFORMATION(ResolveSliceAttributes)
196 DECLARE_GRAPH_TRANSFORMATION(ResolveStridedSliceAttributes)
197 DECLARE_GRAPH_TRANSFORMATION(ResolveTransposeAttributes)
198 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantPack)
199 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantRandomUniform)
200 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantRange)
201 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantShapeOrRank)
202 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantSlice)
203 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantStridedSlice)
204 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantFill)
205 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantGather)
206 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantSelect)
207 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantTile)
208 DECLARE_GRAPH_TRANSFORMATION(ResolveMultiplyByZero)
209 DECLARE_GRAPH_TRANSFORMATION(Dequantize)
210 DECLARE_GRAPH_TRANSFORMATION(UnpartitionEmbeddingLookup)
211 DECLARE_GRAPH_TRANSFORMATION(ShuffleFCWeights)
212 DECLARE_GRAPH_TRANSFORMATION(ResolveFakeQuantArgsFromVars)
213 DECLARE_GRAPH_TRANSFORMATION(ResolveGatherAttributes)
214
215 class PropagateDefaultMinMax : public GraphTransformation {
216 public:
217 ::tensorflow::Status Run(Model* model, std::size_t op_index,
218 bool* modified) override;
219 const char* Name() const override { return "PropagateDefaultMinMax"; }
220
221 bool has_any_ranges_defined() const { return !type_ranges_.empty(); }
222 void DefineTypeRange(ArrayDataType data_type, double min, double max) {
223 MinMax minmax;
224 minmax.min = min;
225 minmax.max = max;
226 type_ranges_.emplace_back(data_type, minmax);
227 }
228
229 private:
230 bool SetArrayMinMax(const string& array_name, Array* array);
231 std::vector<std::pair<ArrayDataType, MinMax>> type_ranges_;
232 };
233
234 class RemoveTrivialReshape : public GraphTransformation {
235 public:
236 ::tensorflow::Status Run(Model* model, std::size_t op_index,
237 bool* modified) override;
Name()238 const char* Name() const override { return "RemoveTrivialReshape"; }
treat_expand_dims_as_trivial()239 bool treat_expand_dims_as_trivial() const {
240 return treat_expand_dims_as_trivial_;
241 }
set_treat_expand_dims_as_trivial(bool val)242 void set_treat_expand_dims_as_trivial(bool val) {
243 treat_expand_dims_as_trivial_ = val;
244 }
245
246 private:
247 bool treat_expand_dims_as_trivial_ = false;
248 };
249
250 class ResolveConstantFakeQuant : public GraphTransformation {
251 public:
252 ::tensorflow::Status Run(Model* model, std::size_t op_index,
253 bool* modified) override;
Name()254 const char* Name() const override { return "ResolveConstantFakeQuant"; }
255
256 // True if the num_bits should adjust the final data type.
propagate_fake_quant_num_bits()257 bool propagate_fake_quant_num_bits() const {
258 return propagate_fake_quant_num_bits_;
259 }
set_propagate_fake_quant_num_bits(bool val)260 void set_propagate_fake_quant_num_bits(bool val) {
261 propagate_fake_quant_num_bits_ = val;
262 }
263
264 private:
265 bool propagate_fake_quant_num_bits_ = false;
266 };
267
268 class EnsureUint8WeightsSafeForFastInt8Kernels : public GraphTransformation {
269 public:
270 ::tensorflow::Status Run(Model* model, std::size_t op_index,
271 bool* modified) override;
Name()272 const char* Name() const override {
273 return "EnsureUint8WeightsSafeForFastInt8Kernels";
274 }
allow_nudging_weights()275 bool allow_nudging_weights() const { return allow_nudging_weights_; }
set_allow_nudging_weights(bool val)276 void set_allow_nudging_weights(bool val) { allow_nudging_weights_ = val; }
277
has_default_ranges_flag()278 bool has_default_ranges_flag() const { return has_default_ranges_flag_; }
set_has_default_ranges_flag(bool val)279 void set_has_default_ranges_flag(bool val) { has_default_ranges_flag_ = val; }
280
281 private:
282 bool allow_nudging_weights_ = false;
283 bool has_default_ranges_flag_ = false;
284 };
285
286 class IdentifyDilatedConv : public GraphTransformation {
287 public:
288 ::tensorflow::Status Run(Model* model, std::size_t op_index,
289 bool* modified) override;
Name()290 const char* Name() const override { return "IdentifyDilatedConv"; }
identify_depthwise_conv()291 bool identify_depthwise_conv() const { return identify_depthwise_conv_; }
set_identify_depthwise_conv(bool val)292 void set_identify_depthwise_conv(bool val) { identify_depthwise_conv_ = val; }
293
294 private:
295 bool identify_depthwise_conv_ = true;
296 };
297
298 #undef DECLARE_GRAPH_TRANSFORMATION
299
300 } // end namespace toco
301
302 #endif // TENSORFLOW_LITE_TOCO_GRAPH_TRANSFORMATIONS_GRAPH_TRANSFORMATIONS_H_
303