flatbuffer_utils.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #ifndef THIRD_PARTY_TFLITE_MICRO_TENSORFLOW_LITE_MICRO_FLATBUFFER_UTILS_H_
  13. #define THIRD_PARTY_TFLITE_MICRO_TENSORFLOW_LITE_MICRO_FLATBUFFER_UTILS_H_
  14. #include "flatbuffers/flatbuffers.h"
  15. #include "flatbuffers/flexbuffers.h"
  16. #include "tensorflow/lite/schema/schema_generated.h"
  17. namespace tflite {
  18. // Kernels use flexbuffers::Map to pack their init parameters in a tflite file,
  19. // with the parameter names as map keys and the parameter values as the
  20. // corresponding map values.
  21. // Accessing the map values using the flexbuffers:Map class is inline heavy,
  22. // which can cause the code size to bloat beyond what's reasonable for a micro
  23. // application. Use this class instead, when possible.
  24. // FlexbufferWrapper takes advantage of the following properties of
  25. // flexbuffers::Map:
  26. // 1. It can be viewed as a flexbuffers::Vector of the values.
  27. // 2. The values in the vector are ordered alphabetically by their keys.
  28. // 3. All integer and Boolean values are stored as 64-bit numbers.
  29. // 4. All floating point values are stored as double precision numbers.
  30. // The properties are mentioned in the flexbuffers docs, but we rely on
  31. // a unit test to catch design changes.
  32. class FlexbufferWrapper : public flexbuffers::Vector {
  33. public:
  34. // Construct with a serialized flexbuffer 'buffer' of 'size' bytes
  35. explicit FlexbufferWrapper(const uint8_t* buffer, size_t size);
  36. int64_t ElementAsInt64(size_t i) const;
  37. uint64_t ElementAsUInt64(size_t i) const;
  38. int32_t ElementAsInt32(size_t i) const;
  39. bool ElementAsBool(size_t i) const;
  40. double ElementAsDouble(size_t i) const;
  41. float ElementAsFloat(size_t i) const;
  42. };
  43. // Return the number of operators in a subgraph tflite
  44. uint32_t NumSubgraphOperators(const SubGraph* subgraph);
  45. uint32_t NumSubgraphOperators(const Model* model, int subgraph_idx);
  46. } // namespace tflite
  47. #endif // THIRD_PARTY_TFLITE_MICRO_TENSORFLOW_LITE_MICRO_FLATBUFFER_UTILS_H_