xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/string_comparisons.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 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_STRING_COMPARISONS_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_STRING_COMPARISONS_H_
17 
18 #include "tensorflow/lite/c/common.h"
19 #include "tensorflow/lite/kernels/internal/common.h"
20 #include "tensorflow/lite/kernels/internal/reference/comparisons.h"
21 #include "tensorflow/lite/kernels/internal/types.h"
22 #include "tensorflow/lite/string_util.h"
23 
24 namespace tflite {
25 
26 namespace reference_ops {
27 
StringRefEqualFn(const StringRef & lhs,const StringRef & rhs)28 inline bool StringRefEqualFn(const StringRef& lhs, const StringRef& rhs) {
29   if (lhs.len != rhs.len) return false;
30   for (int i = 0; i < lhs.len; ++i) {
31     if (lhs.str[i] != rhs.str[i]) return false;
32   }
33   return true;
34 }
35 
StringRefNotEqualFn(const StringRef & lhs,const StringRef & rhs)36 inline bool StringRefNotEqualFn(const StringRef& lhs, const StringRef& rhs) {
37   return !StringRefEqualFn(lhs, rhs);
38 }
39 
ComparisonStringImpl(bool (* F)(const StringRef &,const StringRef &),const RuntimeShape & input1_shape,const TfLiteTensor * input1,const RuntimeShape & input2_shape,const TfLiteTensor * input2,const RuntimeShape & output_shape,bool * output_data)40 inline void ComparisonStringImpl(bool (*F)(const StringRef&, const StringRef&),
41                                  const RuntimeShape& input1_shape,
42                                  const TfLiteTensor* input1,
43                                  const RuntimeShape& input2_shape,
44                                  const TfLiteTensor* input2,
45                                  const RuntimeShape& output_shape,
46                                  bool* output_data) {
47   const int64_t flatsize =
48       MatchingFlatSize(input1_shape, input2_shape, output_shape);
49   for (int64_t i = 0; i < flatsize; ++i) {
50     const auto lhs = GetString(input1, i);
51     const auto rhs = GetString(input2, i);
52     output_data[i] = F(lhs, rhs);
53   }
54 }
55 
BroadcastComparison4DSlowStringImpl(bool (* F)(const StringRef &,const StringRef &),const RuntimeShape & unextended_input1_shape,const TfLiteTensor * input1,const RuntimeShape & unextended_input2_shape,const TfLiteTensor * input2,const RuntimeShape & unextended_output_shape,bool * output_data)56 inline void BroadcastComparison4DSlowStringImpl(
57     bool (*F)(const StringRef&, const StringRef&),
58     const RuntimeShape& unextended_input1_shape, const TfLiteTensor* input1,
59     const RuntimeShape& unextended_input2_shape, const TfLiteTensor* input2,
60     const RuntimeShape& unextended_output_shape, bool* output_data) {
61   const BroadcastComparison4DSlowCommon dims =
62       BroadcastComparison4DSlowPreprocess(unextended_input1_shape,
63                                           unextended_input2_shape,
64                                           unextended_output_shape);
65 
66   for (int b = 0; b < dims.output_shape.Dims(0); ++b) {
67     for (int y = 0; y < dims.output_shape.Dims(1); ++y) {
68       for (int x = 0; x < dims.output_shape.Dims(2); ++x) {
69         for (int c = 0; c < dims.output_shape.Dims(3); ++c) {
70           const auto lhs =
71               GetString(input1, SubscriptToIndex(dims.desc1, b, y, x, c));
72           const auto rhs =
73               GetString(input2, SubscriptToIndex(dims.desc2, b, y, x, c));
74           output_data[Offset(dims.output_shape, b, y, x, c)] = F(lhs, rhs);
75         }
76       }
77     }
78   }
79 }
80 
81 }  // namespace reference_ops
82 }  // namespace tflite
83 
84 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_STRING_COMPARISONS_H_
85