1/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include "tensorflow/lite/c/common.h"
16#include "tensorflow/lite/core/subgraph.h"
17#include "tensorflow/lite/experimental/resource/lookup_interfaces.h"
18#include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
19#include "tensorflow/lite/kernels/kernel_util.h"
20
21namespace tflite {
22namespace ops {
23namespace builtin {
24
25namespace hashtable {
26
27constexpr int kInputResourceIdTensor = 0;
28constexpr int kKeyTensor = 1;
29constexpr int kDefaultValueTensor = 2;
30constexpr int kOutputTensor = 0;
31
32TfLiteStatus PrepareHashtableFind(TfLiteContext* context, TfLiteNode* node) {
33 TF_LITE_ENSURE_EQ(context, NumInputs(node), 3);
34 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
35
36 const TfLiteTensor* input_resource_id_tensor;
37 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputResourceIdTensor,
38 &input_resource_id_tensor));
39 TF_LITE_ENSURE_EQ(context, input_resource_id_tensor->type, kTfLiteResource);
40 TF_LITE_ENSURE_EQ(context, NumDimensions(input_resource_id_tensor), 1);
41 TF_LITE_ENSURE_EQ(context, SizeOfDimension(input_resource_id_tensor, 0), 1);
42
43 const TfLiteTensor* default_value_tensor;
44 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kDefaultValueTensor,
45 &default_value_tensor));
46
47 const TfLiteTensor* key_tensor;
48 TF_LITE_ENSURE_OK(context,
49 GetInputSafe(context, node, kKeyTensor, &key_tensor));
50 TfLiteTensor* output_tensor;
51 TF_LITE_ENSURE_OK(
52 context, GetOutputSafe(context, node, kOutputTensor, &output_tensor));
53 TF_LITE_ENSURE_EQ(context, default_value_tensor->type, output_tensor->type);
54 TF_LITE_ENSURE(context, (key_tensor->type == kTfLiteInt64 &&
55 output_tensor->type == kTfLiteString) ||
56 (key_tensor->type == kTfLiteString &&
57 output_tensor->type == kTfLiteInt64));
58 return context->ResizeTensor(context, output_tensor,
59 TfLiteIntArrayCopy(key_tensor->dims));
60}
61
62TfLiteStatus EvalHashtableFind(TfLiteContext* context, TfLiteNode* node) {
63 const TfLiteTensor* input_resource_id_tensor;
64 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputResourceIdTensor,
65 &input_resource_id_tensor));
66 int resource_id = input_resource_id_tensor->data.i32[0];
67
68 const TfLiteTensor* key_tensor;
69 TF_LITE_ENSURE_OK(context,
70 GetInputSafe(context, node, kKeyTensor, &key_tensor));
71 const TfLiteTensor* default_value_tensor;
72 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kDefaultValueTensor,
73 &default_value_tensor));
74 TfLiteTensor* output_tensor;
75 TF_LITE_ENSURE_OK(context, GetOutputSafe(context, node, 0, &output_tensor));
76
77 Subgraph* subgraph = reinterpret_cast<Subgraph*>(context->impl_);
78 auto& resources = subgraph->resources();
79 auto* lookup = resource::GetHashtableResource(&resources, resource_id);
80 TF_LITE_ENSURE(context, lookup != nullptr);
81 TF_LITE_ENSURE_STATUS(
82 lookup->CheckKeyAndValueTypes(context, key_tensor, output_tensor));
83 auto result =
84 lookup->Lookup(context, key_tensor, output_tensor, default_value_tensor);
85 return result;
86}
87
88} // namespace hashtable
89
90TfLiteRegistration* Register_HASHTABLE_FIND() {
91 static TfLiteRegistration r = {/*init=*/nullptr,
92 /*free=*/nullptr,
93 hashtable::PrepareHashtableFind,
94 hashtable::EvalHashtableFind};
95 return &r;
96}
97
98} // namespace builtin
99} // namespace ops
100} // namespace tflite
101