assign_variable.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 kInputValue = 1;
  28. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  29. TF_LITE_ENSURE_EQ(context, NumInputs(node), 2);
  30. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 0);
  31. // This must be a TfLiteEvalTensor despite this being in Prepare, because
  32. // CreateTensor allocates a temp tensor from the flatbuffer, which does not
  33. // contain the correct ID generated within the VAR_HANDLE op. EvalTensors are
  34. // all allocated during StartModelAllocation which happens before
  35. // init/prepare, and VAR_HANDLE Prepare() references its own op_data in the
  36. // TfLiteEvalTensor, so reading the ID here is valid.
  37. const TfLiteEvalTensor* input_resource_id_tensor =
  38. tflite::micro::GetEvalInput(context, node, kInputVariableId);
  39. TFLITE_DCHECK(input_resource_id_tensor != nullptr);
  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->dims), 1);
  43. tflite::MicroContext* micro_context = tflite::GetMicroContext(context);
  44. TfLiteTensor* input_value =
  45. micro_context->AllocateTempInputTensor(node, kInputValue);
  46. TFLITE_DCHECK(input_value != nullptr);
  47. MicroGraph& graph_info = micro_context->graph();
  48. MicroResourceVariables* resources = graph_info.GetResourceVariables();
  49. TF_LITE_ENSURE_OK(context,
  50. resources->Allocate(input_resource_id_tensor->data.i32[0],
  51. context, input_value));
  52. micro_context->DeallocateTempTfLiteTensor(input_value);
  53. return kTfLiteOk;
  54. }
  55. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  56. const TfLiteEvalTensor* input_id =
  57. tflite::micro::GetEvalInput(context, node, kInputVariableId);
  58. TFLITE_DCHECK(input_id != nullptr);
  59. const TfLiteEvalTensor* input_value =
  60. tflite::micro::GetEvalInput(context, node, kInputValue);
  61. TFLITE_DCHECK(input_value != nullptr);
  62. tflite::MicroContext* micro_context = tflite::GetMicroContext(context);
  63. MicroGraph& graph_info = micro_context->graph();
  64. MicroResourceVariables* resources = graph_info.GetResourceVariables();
  65. if (resources == nullptr) {
  66. MicroPrintf(
  67. "ASSIGN_VARIABLE requires resource variables. Please create "
  68. "ResourceVariables and pass it to the interpreter.");
  69. return kTfLiteError;
  70. }
  71. TF_LITE_ENSURE_OK(context,
  72. resources->Assign(input_id->data.i32[0], input_value));
  73. return kTfLiteOk;
  74. }
  75. } // namespace.
  76. TfLiteRegistration Register_ASSIGN_VARIABLE() {
  77. return tflite::micro::RegisterOp(nullptr, Prepare, Eval);
  78. }
  79. } // namespace tflite