xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/operation_parser.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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/operation_parser.h"
17 
18 #include <cstdint>
19 
20 #include "absl/strings/str_cat.h"
21 #include "tensorflow/lite/c/common.h"
22 #include "tensorflow/lite/delegates/gpu/common/shape.h"
23 #include "tensorflow/lite/delegates/gpu/common/status.h"
24 
25 namespace tflite {
26 namespace gpu {
27 
CheckMaxSupportedOpVersion(const TfLiteRegistration * registration,int max_version)28 absl::Status CheckMaxSupportedOpVersion(const TfLiteRegistration* registration,
29                                         int max_version) {
30   const int op_version = registration->version;
31   if (op_version > max_version) {
32     return absl::UnimplementedError(
33         absl::StrCat("Max version supported: ", max_version,
34                      ". Requested version ", op_version, "."));
35   }
36   return absl::OkStatus();
37 }
38 
ToHW(int32_t h,int32_t w)39 HW ToHW(int32_t h, int32_t w) { return HW(h > 0 ? h : 1, w > 0 ? w : 1); }
40 
ParsePoolingAttributes(const TfLitePoolParams * tf_options,const BHWC & input_shape,Pooling2DAttributes * attr)41 absl::Status ParsePoolingAttributes(const TfLitePoolParams* tf_options,
42                                     const BHWC& input_shape,
43                                     Pooling2DAttributes* attr) {
44   attr->kernel = ToHW(tf_options->filter_height, tf_options->filter_width);
45   attr->strides = ToHW(tf_options->stride_height, tf_options->stride_width);
46   UpdatePadding(tf_options->padding, input_shape, attr);
47   return absl::OkStatus();
48 }
49 
50 }  // namespace gpu
51 }  // namespace tflite
52