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
16#include <stdint.h>
17#include <string.h>
18
19#include "tensorflow/lite/c/common.h"
20#include "tensorflow/lite/core/subgraph.h"
21#include "tensorflow/lite/experimental/resource/resource_variable.h"
22#include "tensorflow/lite/kernels/internal/tensor.h"
23#include "tensorflow/lite/kernels/kernel_util.h"
24
25namespace tflite {
26namespace ops {
27namespace builtin {
28namespace read_variable {
29
30constexpr int kInputVariableId = 0;
31constexpr int kOutputValue = 0;
32
33TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
34 TF_LITE_ENSURE_EQ(context, node->inputs->size, 1);
35 TF_LITE_ENSURE_EQ(context, node->outputs->size, 1);
36
37 const TfLiteTensor* input_resource_id_tensor;
38 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputVariableId,
39 &input_resource_id_tensor));
40 TF_LITE_ENSURE(context, (input_resource_id_tensor->type == kTfLiteResource ||
41 input_resource_id_tensor->type == kTfLiteInt32));
42 TF_LITE_ENSURE_EQ(context, NumElements(input_resource_id_tensor), 1);
43
44 TfLiteTensor* output;
45 TF_LITE_ENSURE_OK(context,
46 GetOutputSafe(context, node, kOutputValue, &output));
47
48 if (output->dims->size == 0) {
49 // Currently there is no good way to differentiate between scalar and
50 // unranked tensor, so we set the tensor's allocation type to dynamic in
51 // both cases.
52 SetTensorToDynamic(output);
53 }
54
55 return kTfLiteOk;
56}
57
58TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
59 Subgraph* subgraph = reinterpret_cast<Subgraph*>(context->impl_);
60
61 const TfLiteTensor* input_resource_id_tensor;
62 TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputVariableId,
63 &input_resource_id_tensor));
64 int resource_id = input_resource_id_tensor->data.i32[0];
65 auto& resources = subgraph->resources();
66 auto* variable = resource::GetResourceVariable(&resources, resource_id);
67 TF_LITE_ENSURE(context, variable != nullptr);
68
69 TfLiteTensor* variable_tensor = variable->GetTensor();
70 TfLiteTensor* output;
71 TF_LITE_ENSURE_OK(context,
72 GetOutputSafe(context, node, kOutputValue, &output));
73
74 TF_LITE_ENSURE_TYPES_EQ(context, variable_tensor->type, output->type);
75 // Only resize the output if the op produces dynamic output.
76 if (IsDynamicTensor(output)) {
77 TF_LITE_ENSURE_OK(context, context->ResizeTensor(
78 context, output,
79 TfLiteIntArrayCopy(variable_tensor->dims)));
80 }
81 memcpy(output->data.raw, variable_tensor->data.raw, output->bytes);
82
83 return kTfLiteOk;
84}
85
86} // namespace read_variable
87
88TfLiteRegistration* Register_READ_VARIABLE() {
89 static TfLiteRegistration r = {nullptr, nullptr, read_variable::Prepare,
90 read_variable::Eval};
91 return &r;
92}
93
94} // namespace builtin
95} // namespace ops
96} // namespace tflite
97