flatbuffer_utils.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "tensorflow/lite/micro/flatbuffer_utils.h"
  13. namespace tflite {
  14. FlexbufferWrapper::FlexbufferWrapper(const uint8_t* buffer, size_t size)
  15. : flexbuffers::Vector(flexbuffers::GetRoot(buffer, size).AsVector()) {}
  16. int64_t FlexbufferWrapper::ElementAsInt64(size_t i) const {
  17. const uint8_t* elem = data_ + i * byte_width_;
  18. return ::flexbuffers::ReadInt64(elem, byte_width_);
  19. }
  20. uint64_t FlexbufferWrapper::ElementAsUInt64(size_t i) const {
  21. const uint8_t* elem = data_ + i * byte_width_;
  22. return ::flexbuffers::ReadUInt64(elem, byte_width_);
  23. }
  24. int32_t FlexbufferWrapper::ElementAsInt32(size_t i) const {
  25. return static_cast<int32_t>(ElementAsInt64(i));
  26. }
  27. bool FlexbufferWrapper::ElementAsBool(size_t i) const {
  28. return static_cast<bool>(ElementAsUInt64(i));
  29. }
  30. double FlexbufferWrapper::ElementAsDouble(size_t i) const {
  31. const uint8_t* elem = data_ + i * byte_width_;
  32. return ::flexbuffers::ReadDouble(elem, byte_width_);
  33. }
  34. float FlexbufferWrapper::ElementAsFloat(size_t i) const {
  35. return static_cast<float>(FlexbufferWrapper::ElementAsDouble(i));
  36. }
  37. // TODO(b/192589496): Ops must always be there. Remove this function when fixed
  38. uint32_t NumSubgraphOperators(const SubGraph* subgraph) {
  39. if (subgraph->operators() != nullptr) {
  40. return subgraph->operators()->size();
  41. } else {
  42. return 0;
  43. }
  44. }
  45. // TODO(b/192589496): Ops must always be there. Remove this function when fixed
  46. uint32_t NumSubgraphOperators(const Model* model, int subgraph_idx) {
  47. const SubGraph* subgraph = model->subgraphs()->Get(subgraph_idx);
  48. return NumSubgraphOperators(subgraph);
  49. }
  50. TfLiteIntArray* FlatBufferVectorToTfLiteTypeArray(
  51. const flatbuffers::Vector<int32_t>* flatbuffer_array) {
  52. // On little-endian machines, TfLiteIntArray happens to have the same memory
  53. // layout as flatbuffers:Vector<int32_t>, so we can reinterpret_cast the
  54. // flatbuffer vector and avoid a copy and malloc.
  55. // TODO(b/188459715): audit this usage of const_cast.
  56. return const_cast<TfLiteIntArray*>(
  57. reinterpret_cast<const TfLiteIntArray*>(flatbuffer_array));
  58. }
  59. TfLiteFloatArray* FlatBufferVectorToTfLiteTypeArray(
  60. const flatbuffers::Vector<float>* flatbuffer_array) {
  61. // On little-endian machines, TfLiteFloatArray happens to have the same memory
  62. // layout as flatbuffers:Vector<float>, so we can reinterpret_cast the
  63. // flatbuffer vector and avoid a copy and malloc.
  64. // TODO(b/188459715): audit this usage of const_cast.
  65. return const_cast<TfLiteFloatArray*>(
  66. reinterpret_cast<const TfLiteFloatArray*>(flatbuffer_array));
  67. }
  68. } // namespace tflite