• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CheckKernels(int kernel_h,int kernel_w)28 absl::Status CheckKernels(int kernel_h, int kernel_w) {
29   if (kernel_h <= 0 || kernel_w <= 0) {
30     return absl::InvalidArgumentError(
31         absl::StrCat("Incorrect kernel values: kernel_height = ", kernel_h,
32                      ", kernel_width = ", kernel_w));
33   }
34   return absl::OkStatus();
35 }
36 
CheckKernelsAndStrides(int kernel_h,int kernel_w,int strides_h,int strides_w)37 absl::Status CheckKernelsAndStrides(int kernel_h, int kernel_w, int strides_h,
38                                     int strides_w) {
39   RETURN_IF_ERROR(CheckKernels(kernel_h, kernel_w));
40   RETURN_IF_ERROR(CheckStrides(strides_h, strides_w));
41   return absl::OkStatus();
42 }
43 
CheckMaxSupportedOpVersion(const TfLiteRegistration * registration,int max_version)44 absl::Status CheckMaxSupportedOpVersion(const TfLiteRegistration* registration,
45                                         int max_version) {
46   const int op_version = registration->version;
47   if (op_version > max_version) {
48     return absl::UnimplementedError(
49         absl::StrCat("Max version supported: ", max_version,
50                      ". Requested version ", op_version, "."));
51   }
52   return absl::OkStatus();
53 }
54 
CheckStrides(int strides_h,int strides_w)55 absl::Status CheckStrides(int strides_h, int strides_w) {
56   if (strides_h <= 0 || strides_w <= 0) {
57     return absl::InvalidArgumentError(
58         absl::StrCat("Incorrect stride values: stride_height = ", strides_h,
59                      ", stride_width = ", strides_w));
60   }
61   return absl::OkStatus();
62 }
63 
CheckTensorIsAvailable(const TfLiteContext * context,const TfLiteNode * tflite_node,int idx)64 absl::Status CheckTensorIsAvailable(const TfLiteContext* context,
65                                     const TfLiteNode* tflite_node, int idx) {
66   // If tensor id is in range, it's guaranteed that it'll be available.
67   if (idx >= tflite_node->inputs->size) {
68     return absl::OutOfRangeError(
69         absl::StrCat("Requested index goes beyond array size: ", idx, " vs ",
70                      idx, tflite_node->inputs->size));
71   }
72   return absl::OkStatus();
73 }
74 
ToHW(int32_t h,int32_t w)75 HW ToHW(int32_t h, int32_t w) { return HW(h > 0 ? h : 1, w > 0 ? w : 1); }
76 
ParsePoolingAttributes(const TfLitePoolParams * tf_options,const BHWC & input_shape,Pooling2DAttributes * attr)77 absl::Status ParsePoolingAttributes(const TfLitePoolParams* tf_options,
78                                     const BHWC& input_shape,
79                                     Pooling2DAttributes* attr) {
80   attr->kernel = ToHW(tf_options->filter_height, tf_options->filter_width);
81   attr->strides = ToHW(tf_options->stride_height, tf_options->stride_width);
82   UpdatePadding(tf_options->padding, input_shape, attr);
83   return absl::OkStatus();
84 }
85 
86 }  // namespace gpu
87 }  // namespace tflite
88