xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/flops_util.cc (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 #include "tensorflow/lite/delegates/gpu/common/flops_util.h"
17 
18 namespace tflite {
19 namespace gpu {
20 
GetConvolutionFlops(const BHWC & dst_shape,const OHWI & weights_shape)21 uint64_t GetConvolutionFlops(const BHWC& dst_shape, const OHWI& weights_shape) {
22   uint64_t dst_elements = dst_shape.b * dst_shape.h * dst_shape.w * dst_shape.c;
23   // 2 flops per operation( s = a * b + s);
24   return dst_elements * weights_shape.i * weights_shape.w * weights_shape.h * 2;
25 }
26 
GetConvolutionWinograd4x4To6x6Flops(const BHWC & dst_shape,const OHWI & weights_shape)27 uint64_t GetConvolutionWinograd4x4To6x6Flops(const BHWC& dst_shape,
28                                              const OHWI& weights_shape) {
29   return GetConvolutionFlops(dst_shape, weights_shape) / 4u;
30 }
31 
GetConvolutionTransposedFlops(const BHWC & src_shape,const OHWI & weights_shape)32 uint64_t GetConvolutionTransposedFlops(const BHWC& src_shape,
33                                        const OHWI& weights_shape) {
34   uint64_t elements = src_shape.b * src_shape.h * src_shape.w * weights_shape.o;
35   // 2 flops per operation( s = a * b + s);
36   return elements * weights_shape.i * weights_shape.w * weights_shape.h * 2;
37 }
38 
GetDepthwiseConvolutionFlops(const BHWC & dst_shape,const OHWI & weights_shape)39 uint64_t GetDepthwiseConvolutionFlops(const BHWC& dst_shape,
40                                       const OHWI& weights_shape) {
41   uint64_t dst_elements = dst_shape.b * dst_shape.h * dst_shape.w * dst_shape.c;
42   // 2 flops per operation( s = a * b + s);
43   return dst_elements * weights_shape.w * weights_shape.h * 2;
44 }
45 
GetFullyConnectedFlops(const BHWC & dst_shape,const OHWI & weights_shape)46 uint64_t GetFullyConnectedFlops(const BHWC& dst_shape,
47                                 const OHWI& weights_shape) {
48   uint64_t dst_elements = dst_shape.b * dst_shape.h * dst_shape.w * dst_shape.c;
49   // 2 flops per operation( s = a * b + s);
50   return dst_elements * weights_shape.i * 2;
51 }
52 
53 }  // namespace gpu
54 }  // namespace tflite
55