squeeze.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright 2020 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/quantization_util.h"
  15. #include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h"
  16. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  17. #include "tensorflow/lite/kernels/kernel_util.h"
  18. #include "tensorflow/lite/kernels/op_macros.h"
  19. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  20. #include "tensorflow/lite/micro/memory_helpers.h"
  21. namespace tflite {
  22. namespace {
  23. struct SqueezeContext {
  24. SqueezeContext(TfLiteContext* context, TfLiteNode* node)
  25. : params(reinterpret_cast<TfLiteSqueezeParams*>(node->builtin_data)),
  26. input(GetInput(context, node, 0)),
  27. output(GetOutput(context, node, 0)) {}
  28. TfLiteSqueezeParams* params;
  29. const TfLiteTensor* const input;
  30. TfLiteTensor* output;
  31. };
  32. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  33. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  34. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  35. SqueezeContext op_context(context, node);
  36. const int input_num_dims = NumDimensions(op_context.input);
  37. const int num_squeeze_dims = op_context.params->num_squeeze_dims;
  38. // Determines number of dimensions of output tensor after squeeze.
  39. const TfLiteIntArray* input_dims = op_context.input->dims;
  40. const TfLiteIntArray* output_dims = op_context.output->dims;
  41. const int* squeeze_dims = op_context.params->squeeze_dims;
  42. constexpr int max_squeeze_dims = 8;
  43. TF_LITE_ENSURE(context, input_num_dims <= max_squeeze_dims);
  44. bool should_squeeze[max_squeeze_dims] = {};
  45. if (num_squeeze_dims == 0) {
  46. for (int idx = 0; idx < input_num_dims; ++idx) {
  47. if (input_dims->data[idx] == 1) {
  48. should_squeeze[idx] = true;
  49. }
  50. }
  51. } else {
  52. for (int idx = 0; idx < num_squeeze_dims; ++idx) {
  53. int current = squeeze_dims[idx] < 0 ? squeeze_dims[idx] + input_num_dims
  54. : squeeze_dims[idx];
  55. TF_LITE_ENSURE(context, current >= 0 && current < input_num_dims &&
  56. input_dims->data[current] == 1);
  57. should_squeeze[current] = true;
  58. }
  59. }
  60. // Ensure output dimensions are big enough.
  61. for (int in_idx = 0, out_idx = 0; in_idx < input_num_dims; ++in_idx) {
  62. if (!should_squeeze[in_idx]) {
  63. TFLITE_CHECK_GE(output_dims->data[out_idx++], input_dims->data[in_idx]);
  64. }
  65. }
  66. return kTfLiteOk;
  67. }
  68. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  69. SqueezeContext op_context(context, node);
  70. if (op_context.input->type == kTfLiteString) {
  71. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  72. TfLiteTypeGetName(op_context.input->type),
  73. op_context.input->type);
  74. return kTfLiteError;
  75. }
  76. TF_LITE_ENSURE_EQ(context, op_context.input->bytes, op_context.output->bytes);
  77. memcpy(op_context.output->data.raw, op_context.input->data.raw,
  78. op_context.input->bytes);
  79. return kTfLiteOk;
  80. }
  81. } // namespace
  82. TfLiteRegistration Register_SQUEEZE() {
  83. return {/*init=*/nullptr,
  84. /*free=*/nullptr,
  85. /*prepare=*/Prepare,
  86. /*invoke=*/Eval,
  87. /*profiling_string=*/nullptr,
  88. /*builtin_code=*/0,
  89. /*custom_name=*/nullptr,
  90. /*version=*/0};
  91. }
  92. } // namespace tflite