read_variable.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include <stddef.h>
  13. #include <cstring>
  14. #include "tensorflow/lite/c/builtin_op_data.h"
  15. #include "tensorflow/lite/c/common.h"
  16. #include "tensorflow/lite/kernels/internal/compatibility.h"
  17. #include "tensorflow/lite/kernels/kernel_util.h"
  18. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  19. #include "tensorflow/lite/micro/memory_helpers.h"
  20. #include "tensorflow/lite/micro/micro_error_reporter.h"
  21. #include "tensorflow/lite/micro/micro_graph.h"
  22. #include "tensorflow/lite/micro/micro_resource_variable.h"
  23. #include "tensorflow/lite/schema/schema_generated.h"
  24. namespace tflite {
  25. namespace {
  26. constexpr int kInputVariableId = 0;
  27. constexpr int kOutputValue = 0;
  28. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  29. TFLITE_DCHECK(NumInputs(node) == 1);
  30. TFLITE_DCHECK(NumOutputs(node) == 1);
  31. MicroContext* micro_context = GetMicroContext(context);
  32. TfLiteTensor* input_resource_id_tensor =
  33. micro_context->AllocateTempInputTensor(node, kInputVariableId);
  34. TFLITE_DCHECK(input_resource_id_tensor != nullptr);
  35. TFLITE_DCHECK(input_resource_id_tensor->type == kTfLiteResource);
  36. TFLITE_DCHECK(NumElements(input_resource_id_tensor) == 1);
  37. micro_context->DeallocateTempTfLiteTensor(input_resource_id_tensor);
  38. return kTfLiteOk;
  39. }
  40. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  41. const TfLiteEvalTensor* input_resource_id_tensor =
  42. tflite::micro::GetEvalInput(context, node, kInputVariableId);
  43. TFLITE_DCHECK(input_resource_id_tensor != nullptr);
  44. TfLiteEvalTensor* output_value =
  45. tflite::micro::GetEvalOutput(context, node, kOutputValue);
  46. TFLITE_DCHECK(output_value != nullptr);
  47. tflite::MicroContext* micro_context = tflite::GetMicroContext(context);
  48. MicroGraph& graph_info = micro_context->graph();
  49. MicroResourceVariables* resources = graph_info.GetResourceVariables();
  50. if (resources == nullptr) {
  51. MicroPrintf(
  52. "READ_VARIABLE requires resource variables. Please create "
  53. "ResourceVariables and pass it to the interpreter.");
  54. return kTfLiteError;
  55. }
  56. TF_LITE_ENSURE_OK(
  57. context,
  58. resources->Read(input_resource_id_tensor->data.i32[0], output_value));
  59. return kTfLiteOk;
  60. }
  61. } // namespace.
  62. TfLiteRegistration Register_READ_VARIABLE() {
  63. return tflite::micro::RegisterOp(nullptr, Prepare, Eval);
  64. }
  65. } // namespace tflite