test_utils.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright 2018 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 TENSORFLOW_LITE_MICRO_TESTING_TEST_UTILS_H_
  13. #define TENSORFLOW_LITE_MICRO_TESTING_TEST_UTILS_H_
  14. #include <cmath>
  15. #include <cstdint>
  16. #include <limits>
  17. #include "tensorflow/lite/c/common.h"
  18. #include "tensorflow/lite/core/api/tensor_utils.h"
  19. #include "tensorflow/lite/micro/micro_utils.h"
  20. #include "tensorflow/lite/micro/test_helpers.h"
  21. #include "tensorflow/lite/micro/testing/micro_test.h"
  22. namespace tflite {
  23. namespace testing {
  24. // Note: These methods are deprecated, do not use. See b/141332970.
  25. // Derives the quantization range max from scaling factor and zero point.
  26. template <typename T>
  27. inline float MaxFromZeroPointScale(const int zero_point, const float scale) {
  28. return (std::numeric_limits<T>::max() - zero_point) * scale;
  29. }
  30. // Derives the quantization range min from scaling factor and zero point.
  31. template <typename T>
  32. inline float MinFromZeroPointScale(const int zero_point, const float scale) {
  33. return (std::numeric_limits<T>::min() - zero_point) * scale;
  34. }
  35. // Derives the quantization scaling factor from a min and max range.
  36. template <typename T>
  37. inline float ScaleFromMinMax(const float min, const float max) {
  38. return (max - min) /
  39. static_cast<float>((std::numeric_limits<T>::max() * 1.0) -
  40. std::numeric_limits<T>::min());
  41. }
  42. // Derives the quantization zero point from a min and max range.
  43. template <typename T>
  44. inline int ZeroPointFromMinMax(const float min, const float max) {
  45. return static_cast<int>(std::numeric_limits<T>::min()) +
  46. static_cast<int>(-min / ScaleFromMinMax<T>(min, max) + 0.5f);
  47. }
  48. // Converts a float value into an unsigned eight-bit quantized value.
  49. uint8_t F2Q(float value, float min, float max);
  50. // Converts a float value into a signed eight-bit quantized value.
  51. int8_t F2QS(const float value, const float min, const float max);
  52. // Converts a float value into a signed thirty-two-bit quantized value. Note
  53. // that values close to max int and min int may see significant error due to
  54. // a lack of floating point granularity for large values.
  55. int32_t F2Q32(const float value, const float scale);
  56. // TODO(b/141330728): Move this method elsewhere as part clean up.
  57. void PopulateContext(TfLiteTensor* tensors, int tensors_size,
  58. ErrorReporter* error_reporter, TfLiteContext* context);
  59. TfLiteTensor CreateQuantizedTensor(const uint8_t* data, TfLiteIntArray* dims,
  60. float min, float max,
  61. bool is_variable = false);
  62. TfLiteTensor CreateQuantizedTensor(const int8_t* data, TfLiteIntArray* dims,
  63. float min, float max,
  64. bool is_variable = false);
  65. TfLiteTensor CreateQuantizedTensor(float* data, uint8_t* quantized_data,
  66. TfLiteIntArray* dims,
  67. bool is_variable = false);
  68. TfLiteTensor CreateQuantizedTensor(float* data, int8_t* quantized_data,
  69. TfLiteIntArray* dims,
  70. bool is_variable = false);
  71. TfLiteTensor CreateQuantizedTensor(float* data, int16_t* quantized_data,
  72. TfLiteIntArray* dims,
  73. bool is_variable = false);
  74. TfLiteTensor CreateQuantized32Tensor(const int32_t* data, TfLiteIntArray* dims,
  75. float scale, bool is_variable = false);
  76. template <typename input_type = int32_t,
  77. TfLiteType tensor_input_type = kTfLiteInt32>
  78. inline TfLiteTensor CreateTensor(const input_type* data, TfLiteIntArray* dims,
  79. bool is_variable = false) {
  80. TfLiteTensor result;
  81. result.type = tensor_input_type;
  82. result.data.raw = reinterpret_cast<char*>(const_cast<input_type*>(data));
  83. result.dims = dims;
  84. result.allocation_type = kTfLiteMemNone;
  85. result.bytes = ElementCount(*dims) * sizeof(input_type);
  86. result.is_variable = is_variable;
  87. return result;
  88. }
  89. } // namespace testing
  90. } // namespace tflite
  91. #endif // TENSORFLOW_LITE_MICRO_TESTING_TEST_UTILS_H_