flatbuffer_utils.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. } // namespace tflite