xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/transformations/fuse_mul_to_conv.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 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 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TRANSFORMATIONS_FUSE_MUL_TO_CONV_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TRANSFORMATIONS_FUSE_MUL_TO_CONV_H_
18 
19 #include <memory>
20 
21 #include "tensorflow/lite/delegates/gpu/common/model_transformer.h"
22 #include "tensorflow/lite/delegates/gpu/common/operations.h"
23 
24 namespace tflite {
25 namespace gpu {
26 
27 // Fuse Multiply Scalar or Multiply Broadcast after Convolution(Convolution2D,
28 // DepthWise, TransposedConvolution, FullyConnected) into weights and biases of
29 // convolution.
30 std::unique_ptr<SequenceTransformation> NewMergeConvolutionWithMul();
31 
32 // Fuse Multiply Scalar or Multiply Broadcast before Convolution(Convolution2D,
33 // DepthWise, TransposedConvolution, FullyConnected) into weights and biases of
34 // convolution.
35 std::unique_ptr<SequenceTransformation> NewMergeMulWithConvolution();
36 
37 // Modify Convolution2DAttributes so that after making convolution with
38 // modified attributes we will have the same result as convolution
39 // with old attributes and following multiply operation.
40 void FuseConvolution2DWithMultiply(const ElementwiseAttributes& mul_attr,
41                                    Convolution2DAttributes* attr);
42 
43 // Modify DepthwiseConvolution2DAttributes so that after making depth wise
44 // convolution with modified attributes we will have the same result as depth
45 // wise convolution with old attributes and following multiply operation.
46 void FuseDepthwiseConvolution2DWithMultiply(
47     const ElementwiseAttributes& mul_attr,
48     DepthwiseConvolution2DAttributes* attr);
49 
50 // Modify ConvolutionTransposedAttributes so that after making convolution
51 // transposed with modified attributes we will have the same result as
52 // convolution transposed with old attributes and following multiply operation.
53 void FuseConvolutionTransposedWithMultiply(
54     const ElementwiseAttributes& mul_attr,
55     ConvolutionTransposedAttributes* attr);
56 
57 // Modify FullyConnectedAttributes so that after making fully connected with
58 // modified attributes we will have the same result as fully connected
59 // with old attributes and following multiply operation.
60 void FuseFullyConnectedWithMultiply(const ElementwiseAttributes& mul_attr,
61                                     FullyConnectedAttributes* attr);
62 
63 // Modify Convolution2DAttributes so that after making convolution with
64 // modified attributes we will have the same result as multiply operation and
65 // convolution with old attributes
66 void FuseMultiplyWithConvolution2D(const ElementwiseAttributes& mul_attr,
67                                    Convolution2DAttributes* attr);
68 
69 // Modify DepthwiseConvolution2DAttributes so that after making depth wise
70 // convolution with modified attributes we will have the same result as multiply
71 // operation and depth wise convolution with old attributes
72 void FuseMultiplyWithDepthwiseConvolution2D(
73     const ElementwiseAttributes& mul_attr,
74     DepthwiseConvolution2DAttributes* attr);
75 
76 // Modify ConvolutionTransposedAttributes so that after making convolution
77 // transposed with modified attributes we will have the same result as multiply
78 // operation and convolution transposed with old attributes
79 void FuseMultiplyWithConvolutionTransposed(
80     const ElementwiseAttributes& mul_attr,
81     ConvolutionTransposedAttributes* attr);
82 
83 // Modify FullyConnectedAttributes so that after making fully connected
84 // with modified attributes we will have the same result as multiply
85 // operation and fully connected with old attributes
86 void FuseMultiplyWithFullyConnected(const ElementwiseAttributes& mul_attr,
87                                     FullyConnectedAttributes* attr);
88 
89 }  // namespace gpu
90 }  // namespace tflite
91 
92 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TRANSFORMATIONS_FUSE_MUL_TO_CONV_H_
93