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 kValueTensor = 2;
30
31TfLiteStatus PrepareHashtableImport(TfLiteContext* context, TfLiteNode* node) {
32 TF_LITE_ENSURE_EQ(context, NumInputs(node), 3);
33 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 0);
34
35 const TfLiteTensor* input_resource_id_tensor;
36 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputResourceIdTensor,
37 &input_resource_id_tensor));
38 TF_LITE_ENSURE_EQ(context, input_resource_id_tensor->type, kTfLiteResource);
39 TF_LITE_ENSURE_EQ(context, NumDimensions(input_resource_id_tensor), 1);
40 TF_LITE_ENSURE_EQ(context, SizeOfDimension(input_resource_id_tensor, 0), 1);
41
42 const TfLiteTensor* key_tensor;
43 TF_LITE_ENSURE_OK(context,
44 GetInputSafe(context, node, kKeyTensor, &key_tensor));
45 const TfLiteTensor* value_tensor;
46 TF_LITE_ENSURE_OK(context,
47 GetInputSafe(context, node, kValueTensor, &value_tensor));
48 TF_LITE_ENSURE(context, (key_tensor->type == kTfLiteInt64 &&
49 value_tensor->type == kTfLiteString) ||
50 (key_tensor->type == kTfLiteString &&
51 value_tensor->type == kTfLiteInt64));
52 // TODO(b/144731295): Tensorflow lookup ops support 1-D vector in storing
53 // values.
54 TF_LITE_ENSURE(context, HaveSameShapes(key_tensor, value_tensor));
55 return kTfLiteOk;
56}
57
58TfLiteStatus EvalHashtableImport(TfLiteContext* context, TfLiteNode* node) {
59 const TfLiteTensor* input_resource_id_tensor;
60 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputResourceIdTensor,
61 &input_resource_id_tensor));
62 const int resource_id = input_resource_id_tensor->data.i32[0];
63
64 const TfLiteTensor* key_tensor;
65 TF_LITE_ENSURE_OK(context,
66 GetInputSafe(context, node, kKeyTensor, &key_tensor));
67 const TfLiteTensor* value_tensor;
68 TF_LITE_ENSURE_OK(context,
69 GetInputSafe(context, node, kValueTensor, &value_tensor));
70
71 Subgraph* subgraph = reinterpret_cast<Subgraph*>(context->impl_);
72 auto& resources = subgraph->resources();
73 auto* lookup = resource::GetHashtableResource(&resources, resource_id);
74 TF_LITE_ENSURE(context, lookup != nullptr);
75 TF_LITE_ENSURE_STATUS(
76 lookup->CheckKeyAndValueTypes(context, key_tensor, value_tensor));
77 // The hashtable resource will only be initialized once, attempting to
78 // initialize it multiple times will be a no-op.
79 auto result = lookup->Import(context, key_tensor, value_tensor);
80 return result;
81}
82
83} // namespace hashtable
84
85TfLiteRegistration* Register_HASHTABLE_IMPORT() {
86 static TfLiteRegistration r = {/*init=*/nullptr,
87 /*free=*/nullptr,
88 hashtable::PrepareHashtableImport,
89 hashtable::EvalHashtableImport};
90 return &r;
91}
92
93} // namespace builtin
94} // namespace ops
95} // namespace tflite
96