reshape.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright 2017 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 "tensorflow/lite/c/builtin_op_data.h"
  13. #include "tensorflow/lite/c/common.h"
  14. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  15. #include "tensorflow/lite/kernels/kernel_util.h"
  16. #include "tensorflow/lite/kernels/op_macros.h"
  17. namespace tflite {
  18. namespace ops {
  19. namespace micro {
  20. namespace reshape {
  21. constexpr int kInputTensor = 0;
  22. constexpr int kOutputTensor = 0;
  23. TfLiteStatus ReshapeOutput(TfLiteContext* context, TfLiteNode* node) {
  24. const TfLiteTensor* input = GetInput(context, node, kInputTensor);
  25. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  26. // Tensorflow's Reshape allows one of the shape components to have the
  27. // special -1 value, meaning it will be calculated automatically based on the
  28. // input. Here we calculate what that dimension should be so that the number
  29. // of output elements in the same as the number of input elements.
  30. int num_input_elements = NumElements(input);
  31. TfLiteIntArray* output_shape = output->dims;
  32. if (NumInputs(node) == 1 && // Legacy scalar supported with params.
  33. output_shape->size == 1 && output_shape->data[0] == 0) {
  34. // Legacy tflite models use a shape parameter of [0] to indicate scalars,
  35. // so adjust accordingly. TODO(b/111614235): Allow zero-sized buffers during
  36. // toco conversion.
  37. output_shape->size = 0;
  38. }
  39. int num_output_elements = 1;
  40. int stretch_dim = -1;
  41. for (int i = 0; i < output_shape->size; ++i) {
  42. int value = output_shape->data[i];
  43. if (value == -1) {
  44. TF_LITE_ENSURE_EQ(context, stretch_dim, -1);
  45. stretch_dim = i;
  46. } else {
  47. num_output_elements *= value;
  48. }
  49. }
  50. if (stretch_dim != -1) {
  51. output_shape->data[stretch_dim] = num_input_elements / num_output_elements;
  52. num_output_elements *= output_shape->data[stretch_dim];
  53. }
  54. TF_LITE_ENSURE_EQ(context, input->type, output->type);
  55. TF_LITE_ENSURE_EQ(context, num_input_elements, num_output_elements);
  56. return kTfLiteOk;
  57. }
  58. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  59. TF_LITE_ENSURE(context, NumInputs(node) == 1 || NumInputs(node) == 2);
  60. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  61. TF_LITE_ENSURE_EQ(context, ReshapeOutput(context, node), kTfLiteOk);
  62. return kTfLiteOk;
  63. }
  64. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  65. const TfLiteTensor* input = GetInput(context, node, kInputTensor);
  66. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  67. // Do nothing for in-place reshape.
  68. if (input->data.raw != output->data.raw) {
  69. // Otherwise perform reshape with copy.
  70. for (size_t i = 0; i < input->bytes; ++i) {
  71. output->data.raw[i] = input->data.raw[i];
  72. }
  73. }
  74. return kTfLiteOk;
  75. }
  76. } // namespace reshape
  77. TfLiteRegistration* Register_RESHAPE() {
  78. static TfLiteRegistration r = {/*init=*/nullptr,
  79. /*free=*/nullptr,
  80. /*prepare=*/reshape::Prepare,
  81. /*invoke=*/reshape::Eval,
  82. /*profiling_string=*/nullptr,
  83. /*builtin_code=*/0,
  84. /*custom_name=*/nullptr,
  85. /*version=*/0};
  86. return &r;
  87. }
  88. } // namespace micro
  89. } // namespace ops
  90. } // namespace tflite