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 kOutputTensor = 0;
29
30TfLiteStatus PrepareHashtableSize(TfLiteContext* context, TfLiteNode* node) {
31 TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
32 TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
33
34 const TfLiteTensor* input_resource_id_tensor;
35 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputResourceIdTensor,
36 &input_resource_id_tensor));
37 TF_LITE_ENSURE_EQ(context, input_resource_id_tensor->type, kTfLiteResource);
38 TF_LITE_ENSURE_EQ(context, NumDimensions(input_resource_id_tensor), 1);
39 TF_LITE_ENSURE_EQ(context, SizeOfDimension(input_resource_id_tensor, 0), 1);
40
41 TfLiteTensor* output_tensor;
42 TF_LITE_ENSURE_OK(
43 context, GetOutputSafe(context, node, kOutputTensor, &output_tensor));
44 TF_LITE_ENSURE_EQ(context, output_tensor->type, kTfLiteInt64);
45 TfLiteIntArray* outputSize = TfLiteIntArrayCreate(1);
46 outputSize->data[0] = 1;
47 return context->ResizeTensor(context, output_tensor, outputSize);
48}
49
50TfLiteStatus EvalHashtableSize(TfLiteContext* context, TfLiteNode* node) {
51 const TfLiteTensor* input_resource_id_tensor;
52 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputResourceIdTensor,
53 &input_resource_id_tensor));
54 int resource_id = input_resource_id_tensor->data.i32[0];
55
56 TfLiteTensor* output_tensor;
57 TF_LITE_ENSURE_OK(
58 context, GetOutputSafe(context, node, kOutputTensor, &output_tensor));
59 auto* output_data = GetTensorData<std::int64_t>(output_tensor);
60
61 Subgraph* subgraph = reinterpret_cast<Subgraph*>(context->impl_);
62 auto& resources = subgraph->resources();
63 auto* lookup = resource::GetHashtableResource(&resources, resource_id);
64 TF_LITE_ENSURE(context, lookup != nullptr);
65
66 output_data[0] = lookup->Size();
67 return kTfLiteOk;
68}
69
70} // namespace hashtable
71
72TfLiteRegistration* Register_HASHTABLE_SIZE() {
73 static TfLiteRegistration r = {/*init=*/nullptr,
74 /*free=*/nullptr,
75 hashtable::PrepareHashtableSize,
76 hashtable::EvalHashtableSize};
77 return &r;
78}
79
80} // namespace builtin
81} // namespace ops
82} // namespace tflite
83