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<std::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<std::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 std::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<std::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 std::string& msg,
107 const GraphTransformationsSet& transformations);
108
RunGraphTransformations(Model * model,const std::string & msg,const GraphTransformationsSet & transformations)109 inline void RunGraphTransformations(
110 Model* model, const std::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(ConvertMatrixSetDiagV2OrV3ToV1)126 DECLARE_GRAPH_TRANSFORMATION(ConvertMatrixSetDiagV2OrV3ToV1)
127 DECLARE_GRAPH_TRANSFORMATION(ConvertMatrixDiagV2OrV3ToV1)
128 DECLARE_GRAPH_TRANSFORMATION(ConvertPureConvToDepthwise)
129 DECLARE_GRAPH_TRANSFORMATION(ConvertReorderAxes)
130 DECLARE_GRAPH_TRANSFORMATION(ConvertSqueezeToReshape)
131 DECLARE_GRAPH_TRANSFORMATION(ConvertTrivialAddNToAdd)
132 DECLARE_GRAPH_TRANSFORMATION(ConvertTrivialPackToReshape)
133 DECLARE_GRAPH_TRANSFORMATION(ConvertTrivialTileToConcat)
134 DECLARE_GRAPH_TRANSFORMATION(ConvertTrivialTransposeToReshape)
135 DECLARE_GRAPH_TRANSFORMATION(EnsureBiasVectors)
136 DECLARE_GRAPH_TRANSFORMATION(FuseActivationFunctions)
137 DECLARE_GRAPH_TRANSFORMATION(FuseBinaryIntoFollowingAffine)
138 DECLARE_GRAPH_TRANSFORMATION(FuseBinaryIntoPrecedingAffine)
139 DECLARE_GRAPH_TRANSFORMATION(FuseBroadcastIntoFollowingBinary)
140 DECLARE_GRAPH_TRANSFORMATION(GroupBidirectionalSequenceLstm)
141 DECLARE_GRAPH_TRANSFORMATION(GroupBidirectionalSequenceRnn)
142 DECLARE_GRAPH_TRANSFORMATION(GroupDynamicBidirectionalSequenceLstm)
143 DECLARE_GRAPH_TRANSFORMATION(GroupDynamicBidirectionalSequenceRnn)
144 DECLARE_GRAPH_TRANSFORMATION(IdentifyL2Normalization)
145 DECLARE_GRAPH_TRANSFORMATION(IdentifyL2Pool)
146 DECLARE_GRAPH_TRANSFORMATION(IdentifyLstmCell)
147 DECLARE_GRAPH_TRANSFORMATION(IdentifyHardSwish)
148 DECLARE_GRAPH_TRANSFORMATION(SplitLstmCellInputs)
149 DECLARE_GRAPH_TRANSFORMATION(MergeLstmCellInputs)
150 DECLARE_GRAPH_TRANSFORMATION(MergeReshapeIntoPrecedingTranspose)
151 DECLARE_GRAPH_TRANSFORMATION(IdentifyRelu1)
152 DECLARE_GRAPH_TRANSFORMATION(IdentifyPRelu)
153 DECLARE_GRAPH_TRANSFORMATION(MakeInitialDequantizeOperator)
154 DECLARE_GRAPH_TRANSFORMATION(MoveBinaryOperatorBeforeReshape)
155 DECLARE_GRAPH_TRANSFORMATION(PropagateActivationFunctionIntoConstants)
156 DECLARE_GRAPH_TRANSFORMATION(PropagateArrayDataTypes)
157 DECLARE_GRAPH_TRANSFORMATION(PropagateFakeQuantNumBits)
158 DECLARE_GRAPH_TRANSFORMATION(PropagateFixedSizes)
159 DECLARE_GRAPH_TRANSFORMATION(HardcodeMinMax)
160 DECLARE_GRAPH_TRANSFORMATION(Quantize)
161 DECLARE_GRAPH_TRANSFORMATION(RemoveFinalDequantizeOp)
162 DECLARE_GRAPH_TRANSFORMATION(RemoveSuccessiveTranspose)
163 DECLARE_GRAPH_TRANSFORMATION(RemoveTensorFlowAssert)
164 DECLARE_GRAPH_TRANSFORMATION(RemoveTensorFlowIdentity)
165 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialBinaryOperator)
166 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialConcatenation)
167 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialConcatenationInput)
168 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialFakeQuant)
169 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialSlice)
170 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialQuantizedActivationFunc)
171 DECLARE_GRAPH_TRANSFORMATION(RemoveTrivialQuantizedMinMax)
172 DECLARE_GRAPH_TRANSFORMATION(RemoveUnusedOp)
173 DECLARE_GRAPH_TRANSFORMATION(ResolveBatchNormalization)
174 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantBinaryOperator)
175 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantUnaryOperator)
176 DECLARE_GRAPH_TRANSFORMATION(CreateIm2colArrays)
177 DECLARE_GRAPH_TRANSFORMATION(DropIm2colArrays)
178 DECLARE_GRAPH_TRANSFORMATION(ReadArrayMinmaxAndNarrowRangeFromFakeQuant)
179 DECLARE_GRAPH_TRANSFORMATION(ReorderElementwiseUnary)
180 DECLARE_GRAPH_TRANSFORMATION(ReorderReshapeTranspose)
181 DECLARE_GRAPH_TRANSFORMATION(ResolveReorderAxes)
182 DECLARE_GRAPH_TRANSFORMATION(ResolveTensorFlowConcat)
183 DECLARE_GRAPH_TRANSFORMATION(ResolveTensorFlowMatMul)
184 DECLARE_GRAPH_TRANSFORMATION(ResolveTensorFlowMerge)
185 DECLARE_GRAPH_TRANSFORMATION(ResolveSqueezeAttributes)
186 DECLARE_GRAPH_TRANSFORMATION(ResolveTensorFlowSwitch)
187 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantConcatenation)
188 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantReshape)
189 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantTranspose)
190 DECLARE_GRAPH_TRANSFORMATION(DropFakeQuant)
191 DECLARE_GRAPH_TRANSFORMATION(UnfuseActivationFunctions)
192 DECLARE_GRAPH_TRANSFORMATION(UnrollBatchMatMul)
193 DECLARE_GRAPH_TRANSFORMATION(ResolveSpaceToBatchNDAttributes)
194 DECLARE_GRAPH_TRANSFORMATION(ResolveBatchToSpaceNDAttributes)
195 DECLARE_GRAPH_TRANSFORMATION(ResolvePadAttributes)
196 DECLARE_GRAPH_TRANSFORMATION(ResolvePadV2Attributes)
197 DECLARE_GRAPH_TRANSFORMATION(ResolveReduceAttributes)
198 DECLARE_GRAPH_TRANSFORMATION(ResolveReshapeAttributes)
199 DECLARE_GRAPH_TRANSFORMATION(ResolveSliceAttributes)
200 DECLARE_GRAPH_TRANSFORMATION(ResolveStridedSliceAttributes)
201 DECLARE_GRAPH_TRANSFORMATION(ResolveTransposeAttributes)
202 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantPack)
203 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantRandomUniform)
204 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantRange)
205 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantShapeOrRank)
206 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantSlice)
207 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantStridedSlice)
208 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantFill)
209 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantGather)
210 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantSelect)
211 DECLARE_GRAPH_TRANSFORMATION(ResolveConstantTile)
212 DECLARE_GRAPH_TRANSFORMATION(ResolveMultiplyByZero)
213 DECLARE_GRAPH_TRANSFORMATION(Dequantize)
214 DECLARE_GRAPH_TRANSFORMATION(UnpartitionEmbeddingLookup)
215 DECLARE_GRAPH_TRANSFORMATION(ShuffleFCWeights)
216 DECLARE_GRAPH_TRANSFORMATION(ResolveFakeQuantArgsFromVars)
217 DECLARE_GRAPH_TRANSFORMATION(ResolveGatherAttributes)
218 DECLARE_GRAPH_TRANSFORMATION(IdentifyNearestUpsample)
219
220 class PropagateDefaultMinMax : public GraphTransformation {
221 public:
222 ::tensorflow::Status Run(Model* model, std::size_t op_index,
223 bool* modified) override;
224 const char* Name() const override { return "PropagateDefaultMinMax"; }
225
226 bool has_any_ranges_defined() const { return !type_ranges_.empty(); }
227 void DefineTypeRange(ArrayDataType data_type, double min, double max) {
228 MinMax minmax;
229 minmax.min = min;
230 minmax.max = max;
231 type_ranges_.emplace_back(data_type, minmax);
232 }
233
234 private:
235 bool SetArrayMinMax(const std::string& array_name, Array* array);
236 std::vector<std::pair<ArrayDataType, MinMax>> type_ranges_;
237 };
238
239 class RemoveTrivialReshape : public GraphTransformation {
240 public:
241 ::tensorflow::Status Run(Model* model, std::size_t op_index,
242 bool* modified) override;
Name()243 const char* Name() const override { return "RemoveTrivialReshape"; }
treat_expand_dims_as_trivial()244 bool treat_expand_dims_as_trivial() const {
245 return treat_expand_dims_as_trivial_;
246 }
set_treat_expand_dims_as_trivial(bool val)247 void set_treat_expand_dims_as_trivial(bool val) {
248 treat_expand_dims_as_trivial_ = val;
249 }
250
251 private:
252 bool treat_expand_dims_as_trivial_ = false;
253 };
254
255 class ResolveConstantFakeQuant : public GraphTransformation {
256 public:
257 ::tensorflow::Status Run(Model* model, std::size_t op_index,
258 bool* modified) override;
Name()259 const char* Name() const override { return "ResolveConstantFakeQuant"; }
260
261 // True if the num_bits should adjust the final data type.
propagate_fake_quant_num_bits()262 bool propagate_fake_quant_num_bits() const {
263 return propagate_fake_quant_num_bits_;
264 }
set_propagate_fake_quant_num_bits(bool val)265 void set_propagate_fake_quant_num_bits(bool val) {
266 propagate_fake_quant_num_bits_ = val;
267 }
268
269 private:
270 bool propagate_fake_quant_num_bits_ = false;
271 };
272
273 class EnsureUint8WeightsSafeForFastInt8Kernels : public GraphTransformation {
274 public:
275 ::tensorflow::Status Run(Model* model, std::size_t op_index,
276 bool* modified) override;
Name()277 const char* Name() const override {
278 return "EnsureUint8WeightsSafeForFastInt8Kernels";
279 }
allow_nudging_weights()280 bool allow_nudging_weights() const { return allow_nudging_weights_; }
set_allow_nudging_weights(bool val)281 void set_allow_nudging_weights(bool val) { allow_nudging_weights_ = val; }
282
has_default_ranges_flag()283 bool has_default_ranges_flag() const { return has_default_ranges_flag_; }
set_has_default_ranges_flag(bool val)284 void set_has_default_ranges_flag(bool val) { has_default_ranges_flag_ = val; }
285
286 private:
287 bool allow_nudging_weights_ = false;
288 bool has_default_ranges_flag_ = false;
289 };
290
291 class IdentifyDilatedConv : public GraphTransformation {
292 public:
293 ::tensorflow::Status Run(Model* model, std::size_t op_index,
294 bool* modified) override;
Name()295 const char* Name() const override { return "IdentifyDilatedConv"; }
identify_depthwise_conv()296 bool identify_depthwise_conv() const { return identify_depthwise_conv_; }
set_identify_depthwise_conv(bool val)297 void set_identify_depthwise_conv(bool val) { identify_depthwise_conv_ = val; }
298
299 private:
300 bool identify_depthwise_conv_ = true;
301 };
302
303 #undef DECLARE_GRAPH_TRANSFORMATION
304
305 } // end namespace toco
306
307 #endif // TENSORFLOW_LITE_TOCO_GRAPH_TRANSFORMATIONS_GRAPH_TRANSFORMATIONS_H_
308