ai-edge-quantizer 0.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. ai_edge_quantizer-0.5.1/LICENSE +201 -0
  2. ai_edge_quantizer-0.5.1/PKG-INFO +225 -0
  3. ai_edge_quantizer-0.5.1/README.md +192 -0
  4. ai_edge_quantizer-0.5.1/VERSION +1 -0
  5. ai_edge_quantizer-0.5.1/ai_edge_quantizer/__init__.py +19 -0
  6. ai_edge_quantizer-0.5.1/ai_edge_quantizer/aeq.py +117 -0
  7. ai_edge_quantizer-0.5.1/ai_edge_quantizer/aeq_test.py +62 -0
  8. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithm_manager.py +410 -0
  9. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithm_manager_api.py +271 -0
  10. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithm_manager_api_test.py +219 -0
  11. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/__init__.py +15 -0
  12. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/nonlinear_quantize/__init__.py +15 -0
  13. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/nonlinear_quantize/float_casting.py +340 -0
  14. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/nonlinear_quantize/float_casting_test.py +788 -0
  15. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/__init__.py +15 -0
  16. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/common_quantize.py +1368 -0
  17. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/common_quantize_test.py +101 -0
  18. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/dequantized_weight_recovery.py +244 -0
  19. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/dequantized_weight_recovery_test.py +238 -0
  20. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/hadamard_rotation.py +455 -0
  21. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/hadamard_rotation_test.py +459 -0
  22. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/mse.py +127 -0
  23. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/mse_test.py +195 -0
  24. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/naive_min_max_quantize.py +238 -0
  25. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/naive_min_max_quantize_test.py +244 -0
  26. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/octav.py +188 -0
  27. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/octav_test.py +240 -0
  28. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/add_test.py +166 -0
  29. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/average_pool_2d_test.py +119 -0
  30. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/batch_matmul_test.py +307 -0
  31. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/broadcast_to_test.py +101 -0
  32. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/concatenation_test.py +117 -0
  33. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/conv2d_test.py +214 -0
  34. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/conv2d_transpose_test.py +222 -0
  35. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/depthwise_conv2d_test.py +209 -0
  36. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/div_test.py +99 -0
  37. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/dynamic_update_slice_test.py +113 -0
  38. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/embedding_lookup_test.py +128 -0
  39. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/equal_test.py +100 -0
  40. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/fully_connected_test.py +167 -0
  41. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/gather_nd_test.py +168 -0
  42. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/gather_test.py +101 -0
  43. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/gelu_test.py +115 -0
  44. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/hard_swish_test.py +96 -0
  45. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/input_output_test.py +307 -0
  46. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/logistic_test.py +115 -0
  47. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/max_pool_2d_test.py +102 -0
  48. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/maximum_test.py +100 -0
  49. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/mean_test.py +117 -0
  50. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/mirror_pad_test.py +101 -0
  51. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/mul_test.py +161 -0
  52. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/not_equal_test.py +100 -0
  53. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/pack_test.py +100 -0
  54. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/pad_test.py +104 -0
  55. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/padv2_test.py +106 -0
  56. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/reduce_min_test.py +101 -0
  57. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/relu_test.py +98 -0
  58. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/reshape_test.py +118 -0
  59. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/resize_bilinear_test.py +104 -0
  60. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/resize_nearest_neighbor_test.py +102 -0
  61. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/rsqrt_test.py +108 -0
  62. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/select_test.py +103 -0
  63. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/select_v2_test.py +113 -0
  64. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/slice_test.py +113 -0
  65. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/softmax_test.py +115 -0
  66. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/space_to_depth_test.py +97 -0
  67. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/split_test.py +119 -0
  68. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/sqrt_test.py +98 -0
  69. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/squared_difference_test.py +102 -0
  70. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/strided_slice_test.py +117 -0
  71. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/sub_test.py +159 -0
  72. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/sum_test.py +111 -0
  73. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/tanh_test.py +115 -0
  74. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/test_utils.py +698 -0
  75. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/transpose_test.py +118 -0
  76. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/op_architecture_tests/unpack_test.py +100 -0
  77. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/uniform_quantize_tensor.py +587 -0
  78. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/uniform_quantize/uniform_quantize_tensor_test.py +519 -0
  79. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/utils/__init__.py +15 -0
  80. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/utils/common_utils.py +1154 -0
  81. ai_edge_quantizer-0.5.1/ai_edge_quantizer/algorithms/utils/common_utils_test.py +514 -0
  82. ai_edge_quantizer-0.5.1/ai_edge_quantizer/calibrator.py +459 -0
  83. ai_edge_quantizer-0.5.1/ai_edge_quantizer/calibrator_test.py +499 -0
  84. ai_edge_quantizer-0.5.1/ai_edge_quantizer/conftest.py +22 -0
  85. ai_edge_quantizer-0.5.1/ai_edge_quantizer/default_policy.py +402 -0
  86. ai_edge_quantizer-0.5.1/ai_edge_quantizer/examples/mnist/quantize_toy_model.py +218 -0
  87. ai_edge_quantizer-0.5.1/ai_edge_quantizer/model_modifier.py +320 -0
  88. ai_edge_quantizer-0.5.1/ai_edge_quantizer/model_modifier_test.py +268 -0
  89. ai_edge_quantizer-0.5.1/ai_edge_quantizer/model_validator.py +396 -0
  90. ai_edge_quantizer-0.5.1/ai_edge_quantizer/model_validator_test.py +410 -0
  91. ai_edge_quantizer-0.5.1/ai_edge_quantizer/params_generator.py +524 -0
  92. ai_edge_quantizer-0.5.1/ai_edge_quantizer/params_generator_test.py +1166 -0
  93. ai_edge_quantizer-0.5.1/ai_edge_quantizer/qtyping.py +591 -0
  94. ai_edge_quantizer-0.5.1/ai_edge_quantizer/quantizer.py +533 -0
  95. ai_edge_quantizer-0.5.1/ai_edge_quantizer/quantizer_test.py +805 -0
  96. ai_edge_quantizer-0.5.1/ai_edge_quantizer/recipe.py +202 -0
  97. ai_edge_quantizer-0.5.1/ai_edge_quantizer/recipe_manager.py +402 -0
  98. ai_edge_quantizer-0.5.1/ai_edge_quantizer/recipe_manager_test.py +992 -0
  99. ai_edge_quantizer-0.5.1/ai_edge_quantizer/recipe_test.py +176 -0
  100. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/add_test.py +134 -0
  101. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/batch_matmul_test.py +107 -0
  102. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/broadcast_to_test.py +69 -0
  103. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/composite_test.py +155 -0
  104. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/concatenation_test.py +90 -0
  105. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/conv2d_transpose_test.py +187 -0
  106. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/depthwise_conv2d_test.py +186 -0
  107. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/div_test.py +69 -0
  108. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/dynamic_update_slice_test.py +120 -0
  109. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/embedding_lookup_test.py +153 -0
  110. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/equal_test.py +68 -0
  111. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/fully_connected_test.py +413 -0
  112. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/gather_nd_test.py +96 -0
  113. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/gather_test.py +67 -0
  114. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/gelu_test.py +93 -0
  115. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/hard_swish_test.py +67 -0
  116. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/input_output_test.py +204 -0
  117. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/logistic_test.py +88 -0
  118. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/max_pool_2d_test.py +69 -0
  119. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/maximum_test.py +69 -0
  120. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/mean_test.py +90 -0
  121. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/mirror_pad_test.py +67 -0
  122. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/mul_test.py +170 -0
  123. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/not_equal_test.py +67 -0
  124. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/pack_test.py +68 -0
  125. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/pad_test.py +74 -0
  126. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/padv2_test.py +67 -0
  127. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/reduce_min_test.py +70 -0
  128. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/relu_test.py +67 -0
  129. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/resize_bilinear_test.py +69 -0
  130. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/resize_nearest_neighbor_test.py +70 -0
  131. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/rsqrt_test.py +92 -0
  132. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/select_test.py +69 -0
  133. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/select_v2_test.py +117 -0
  134. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/slice_test.py +117 -0
  135. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/space_to_depth_test.py +65 -0
  136. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/split_test.py +92 -0
  137. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/sqrt_test.py +70 -0
  138. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/squared_difference_test.py +75 -0
  139. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/strided_slice_test.py +90 -0
  140. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/sub_test.py +134 -0
  141. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/sum_test.py +124 -0
  142. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/tanh_test.py +88 -0
  143. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/transpose_test.py +156 -0
  144. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/end_to_end_tests/unpack_test.py +70 -0
  145. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/mnist_test.py +270 -0
  146. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/padv2_inf_max_pool_2d_test.py +68 -0
  147. ai_edge_quantizer-0.5.1/ai_edge_quantizer/tests/shared_buffer_test.py +356 -0
  148. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformation_instruction_generator.py +852 -0
  149. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformation_instruction_generator_test.py +1515 -0
  150. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformation_performer.py +332 -0
  151. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformation_performer_test.py +521 -0
  152. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/__init__.py +15 -0
  153. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/dequant_insert.py +87 -0
  154. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/dequant_insert_test.py +305 -0
  155. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/duplicate_buffer.py +46 -0
  156. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/duplicate_buffer_test.py +107 -0
  157. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/duplicate_tensor.py +62 -0
  158. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/duplicate_tensor_test.py +132 -0
  159. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/insert_decomposed_hadamard_rotation.py +306 -0
  160. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/insert_decomposed_hadamard_rotation_test.py +244 -0
  161. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/insert_hadamard_rotation.py +186 -0
  162. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/insert_hadamard_rotation_test.py +200 -0
  163. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/quant_insert.py +100 -0
  164. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/quant_insert_test.py +285 -0
  165. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/quantize_tensor.py +200 -0
  166. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/quantize_tensor_test.py +265 -0
  167. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/transformation_utils.py +287 -0
  168. ai_edge_quantizer-0.5.1/ai_edge_quantizer/transformations/transformation_utils_test.py +256 -0
  169. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/__init__.py +15 -0
  170. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/calibration_utils.py +394 -0
  171. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/calibration_utils_test.py +269 -0
  172. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/constrained_ops_utils.py +113 -0
  173. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/constrained_ops_utils_test.py +50 -0
  174. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/progress_utils.py +96 -0
  175. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/progress_utils_test.py +105 -0
  176. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/test_utils.py +238 -0
  177. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/tfl_flatbuffer_utils.py +458 -0
  178. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/tfl_flatbuffer_utils_test.py +220 -0
  179. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/tfl_interpreter_utils.py +471 -0
  180. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/tfl_interpreter_utils_test.py +434 -0
  181. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/validation_utils.py +235 -0
  182. ai_edge_quantizer-0.5.1/ai_edge_quantizer/utils/validation_utils_test.py +167 -0
  183. ai_edge_quantizer-0.5.1/ai_edge_quantizer.egg-info/PKG-INFO +225 -0
  184. ai_edge_quantizer-0.5.1/ai_edge_quantizer.egg-info/SOURCES.txt +188 -0
  185. ai_edge_quantizer-0.5.1/ai_edge_quantizer.egg-info/dependency_links.txt +1 -0
  186. ai_edge_quantizer-0.5.1/ai_edge_quantizer.egg-info/entry_points.txt +2 -0
  187. ai_edge_quantizer-0.5.1/ai_edge_quantizer.egg-info/requires.txt +5 -0
  188. ai_edge_quantizer-0.5.1/ai_edge_quantizer.egg-info/top_level.txt +1 -0
  189. ai_edge_quantizer-0.5.1/pyproject.toml +63 -0
  190. ai_edge_quantizer-0.5.1/setup.cfg +4 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,225 @@
1
+ Metadata-Version: 2.4
2
+ Name: ai-edge-quantizer
3
+ Version: 0.5.1
4
+ Summary: A quantizer for advanced developers to quantize converted AI Edge models.
5
+ License-Expression: Apache-2.0
6
+ Project-URL: Homepage, https://github.com/google-ai-edge/ai-edge-quantizer
7
+ Keywords: On-Device ML,AI,Google,TFLite,Quantization,LLMs,GenAI
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Intended Audience :: Education
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3 :: Only
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Scientific/Engineering
19
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
20
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
+ Classifier: Topic :: Software Development
22
+ Classifier: Topic :: Software Development :: Libraries
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Requires-Python: >=3.10
25
+ Description-Content-Type: text/markdown
26
+ License-File: LICENSE
27
+ Requires-Dist: absl-py
28
+ Requires-Dist: immutabledict
29
+ Requires-Dist: numpy
30
+ Requires-Dist: ml_dtypes
31
+ Requires-Dist: ai-edge-litert==2.1.2
32
+ Dynamic: license-file
33
+
34
+ # AI Edge Quantizer
35
+
36
+ A quantizer for advanced developers to quantize converted LiteRT models. It aims to
37
+ facilitate advanced users to strive for optimal performance on resource
38
+ demanding models (e.g., GenAI models).
39
+
40
+ ## Build Status
41
+
42
+ Build Type | Status |
43
+ ----------- | --------------|
44
+ Unit Tests (Linux) | [![](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_unittests.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_unittests.yml) |
45
+ Nightly Release | [![](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_release.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_release.yml) |
46
+ Nightly Colab | [![](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_colabs.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_colabs.yml) |
47
+
48
+ ## Installation
49
+
50
+ ### Requirements and Dependencies
51
+
52
+ * Python versions: 3.10, 3.11, 3.12, 3.13
53
+ * Operating system: Linux, MacOS
54
+ * TensorFlow: [![tf-nightly](https://img.shields.io/badge/tf--nightly-latest-blue)](https://pypi.org/project/tf-nightly/)
55
+
56
+ ### Install
57
+
58
+ Nightly PyPi package:
59
+
60
+ ```bash
61
+ pip install ai-edge-quantizer-nightly
62
+ ```
63
+
64
+ ## API Usage
65
+
66
+ The quantizer requires two inputs:
67
+
68
+ 1. An unquantized source LiteRT model (with FP32 data type in the FlatBuffer format with `.tflite` extension)
69
+ 2. A quantization recipe (details below)
70
+
71
+ and outputs a quantized LiteRT model that's ready for deployment on edge devices.
72
+
73
+ ### Basic Usage
74
+
75
+ In a nutshell, the quantizer works according to the following steps:
76
+
77
+ 1. Instantiate a `Quantizer` class. This is the entry point to the quantizer's functionalities that the user accesses.
78
+ 2. Load a desired quantization recipe (details in subsection).
79
+ 3. Quantize (and save) the model. This is where most of the quantizer's internal logic works.
80
+
81
+ ```python
82
+ qt = quantizer.Quantizer("path/to/input/tflite")
83
+ qt.load_quantization_recipe(recipe.dynamic_wi8_afp32())
84
+ qt.quantize().export_model("/path/to/output/tflite")
85
+ ```
86
+
87
+ Please see the [getting started colab](colabs/getting_started.ipynb) for the simplest quick start guide on those 3 steps, and the [selective quantization colab](colabs/selective_quantization_isnet.ipynb) with more details on advanced features.
88
+
89
+ #### LiteRT Model
90
+
91
+ Please refer to the [LiteRT documentation](https://ai.google.dev/edge/litert) for ways to generate LiteRT models from Jax, PyTorch and TensorFlow. The input source model should be an FP32 (unquantized) model in the FlatBuffer format with `.tflite` extension.
92
+
93
+ #### Quantization Recipe
94
+
95
+ The user needs to specify a quantization recipe using AI Edge Quantizer's API to
96
+ apply to the source model. The quantization recipe encodes all information on how
97
+ a model is to be quantized, such as number of bits, data type, symmetry, scope
98
+ name, etc.
99
+
100
+ Essentially, a quantization recipe is defined as a collection of commands of the
101
+ following type:
102
+
103
+ _“Apply **Quantization Algorithm X** on **Operator Y** under **Scope Z** with **ConfigN**”._
104
+
105
+ For example:
106
+
107
+ _\"**Uniformly quantize** the **FullyConnected op** under scope **'dense1/'** with **INT8 symmetric with Dynamic Quantization**"._
108
+
109
+ All the unspecified ops will be kept as FP32 (unquantized). The scope of an operator in TFLite is defined as the output tensor name of the op, which preserves the hierarchical model information from the source model (e.g., scope in TF). The best way to obtain scope name is by visualizing the model with [Model Explorer](https://ai.google.dev/edge/model-explorer).
110
+
111
+ Currently, there are three ways to quantize an operator:
112
+
113
+ * **dynamic quantization (recommended)**: weights are quantized while activations
114
+ remain in a float format and are not processed by AI Edge Quantizer (AEQ). The
115
+ runtime kernel handles the on-the-fly quantization of these activations, as
116
+ identified by `compute_precision=integer` and `explicit_dequantize=False`.
117
+ * Pros: reduced model size and memory usage. Latency improvement due to integer computation. No sample data requirement (calibration).
118
+ * Cons: on-the-fly quantization of activation tensors can affect model quality. Not supported in all hardware (e.g., some GPU and NPU).
119
+
120
+ * **weight only quantization**: only model weights are quantized, not activations. The
121
+ actual operation (op) computation remains in float. The quantized weight is
122
+ explicitly dequantized before being fed into the op, by inserting a dequantize
123
+ op between the quantized weight and the consuming op. To enable this,
124
+ `compute_precision` will be set to `float` and `explicit_dequantize` to `True`.
125
+ * Pros: reduced model size and memory usage. No sample data requirement (calibration). Usually has the best model quality.
126
+ * Cons: no latency benefit (may be worse) due to float computation with explicit dequantization.
127
+
128
+ * **static quantization**: both weights and activations are quantized. This requires a calibration phase to estimate quantization parameters of runtime tensors (activations).
129
+ * Pros: reduced model size, memory usage, and latency.
130
+ * Cons: requires sample data for calibration. Imposing static quantization parameters (derived from calibration) on runtime tensors can compromise quality.
131
+
132
+ Generally, we recommend dynamic quantization for CPU/GPU deployment and static
133
+ quantization for NPU deployment.
134
+
135
+ We include commonly used recipes in [recipe.py](ai_edge_quantizer/recipe.py). This is demonstrated in the [getting started colab](colabs/getting_started.ipynb) example. Advanced users can build their own recipe through the quantizer API.
136
+
137
+ #### Deployment
138
+ Please refer to the [LiteRT deployment documentation](https://ai.google.dev/edge/litert/inference) for ways to deploy a quantized LiteRT model.
139
+
140
+ ### Advanced Recipes
141
+
142
+ There are many ways the user can configure and customize the quantization recipe beyond using a template in [recipe.py](ai_edge_quantizer/recipe.py). For example, the user can configure the recipe to achieve these features:
143
+
144
+ * Selective quantization (exclude selected ops from being quantized)
145
+ * Flexible mixed scheme quantization (mixture of different precision, compute precision, scope, op, config, etc)
146
+ * 4-bit weight quantization
147
+
148
+ The [selective quantization colab](colabs/selective_quantization_isnet.ipynb) shows some of these more advanced features.
149
+
150
+ For specifics of the recipe schema, please refer to the `OpQuantizationRecipe` in [recipe_manager.py].
151
+
152
+ For advanced usage involving mixed quantization, the following API may be useful:
153
+
154
+ * Use `Quantizer:load_quantization_recipe()` in [quantizer.py](ai_edge_quantizer/quantizer.py) to load a custom recipe.
155
+ * Use `Quantizer:update_quantization_recipe()` in [quantizer.py](ai_edge_quantizer/quantizer.py) to extend or override specific parts of the recipe.
156
+
157
+ ### Operator coverage
158
+
159
+ The table below outlines the allowed configurations for available recipes.
160
+
161
+ | | | | | | | | | | |
162
+ | --- | --- | --- | --- | --- | --- |--- |--- |--- |--- |
163
+ | **Config** | | DYNAMIC_WI8_AFP32 | DYNAMIC_WI4_AFP32 | STATIC_WI8_AI16 | STATIC_WI4_AI16 | STATIC_WI8_AI8 | STATIC_WI4_AI8 | WEIGHTONLY_WI8_AFP32 | WEIGHTONLY_WI4_AFP32 |
164
+ |activation| num\_bits | None | None | 16 | 16 | 8 | 8 | None | None |
165
+ | | symmetric |None | None | TRUE | TRUE | [TRUE, FALSE] | [TRUE, FALSE] | None | None |
166
+ | | granularity |None | None | TENSORWISE | TENSORWISE | TENSORWISE | TENSORWISE | None | None |
167
+ | | dtype| None | None |INT | INT | INT | INT | None | None |
168
+ | weight | num\_bits | 8 | 4 | 8 | 4 | 8 | 4 | 8 | 4 |
169
+ | | symmetric | TRUE | TRUE | TRUE | TRUE | TRUE | TRUE | [TRUE, FALSE] | [TRUE, FALSE] |
170
+ | | granularity | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] |
171
+ | | dtype | INT | INT | INT | INT | INT | INT | INT | INT |
172
+ | explicit\_dequantize | | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE |
173
+ | compute\_precision || INTEGER | INTEGER | INTEGER | INTEGER | INTEGER | INTEGER | FLOAT | FLOAT |
174
+
175
+
176
+ **Operators Supporting Quantization**
177
+
178
+ | | | | | | | | | |
179
+ | --- | --- | --- | --- | --- | --- |--- |--- |--- |
180
+ | **Config** | DYNAMIC_WI8_AFP32 | DYNAMIC_WI4_AFP32 | STATIC_WI8_AI16 | STATIC_WI4_AI16 | STATIC_WI8_AI8 | STATIC_WI4_AI8 | WEIGHTONLY_WI8_AFP32 | WEIGHTONLY_WI4_AFP32 |
181
+ |FULLY_CONNECTED |<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|
182
+ |CONV_2D |<div align="center"> &check; </div>| |<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>| |
183
+ |BATCH_MATMUL |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |
184
+ |EMBEDDING_LOOKUP |<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>| |<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>| |
185
+ |DEPTHWISE_CONV_2D|<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |
186
+ |AVERAGE_POOL_2D | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
187
+ |RESHAPE | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
188
+ |SOFTMAX | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
189
+ |TANH | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
190
+ |TRANSPOSE | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
191
+ |GELU | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
192
+ |ADD | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
193
+ |CONV_2D_TRANSPOSE|<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
194
+ |SUB | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
195
+ |MUL | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
196
+ |MEAN | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
197
+ |RSQRT | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
198
+ |CONCATENATION | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
199
+ |STRIDED_SLICE | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
200
+ |SPLIT | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
201
+ |LOGISTIC | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
202
+ |SLICE | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
203
+ |SELECT | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
204
+ |SELECT_V2 | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
205
+ |SUM | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
206
+ |PAD | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
207
+ |PADV2 | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
208
+ |MIRROR_PAD | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
209
+ |SQUARED_DIFFERENCE | | | | |<div align="center"> &check; </div>| | | |
210
+ |MAX_POOL_2D | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
211
+ |RESIZE_BILINEAR | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
212
+ |RESIZE_NEAREST_NEIGHBOR| | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
213
+ |GATHER_ND | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
214
+ |PACK | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
215
+ |UNPACK | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
216
+ |DIV | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
217
+ |SQRT | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
218
+ |GATHER | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
219
+ |HARD_SWISH | | | | |<div align="center"> &check; </div>| | | |
220
+ |MAXIMUM | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
221
+ |REDUCE_MIN | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
222
+ |EQUAL | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
223
+ |NOT_EQUAL | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
224
+ |SPACE_TO_DEPTH | | | | |<div align="center"> &check; </div>| | | |
225
+ |RELU | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
@@ -0,0 +1,192 @@
1
+ # AI Edge Quantizer
2
+
3
+ A quantizer for advanced developers to quantize converted LiteRT models. It aims to
4
+ facilitate advanced users to strive for optimal performance on resource
5
+ demanding models (e.g., GenAI models).
6
+
7
+ ## Build Status
8
+
9
+ Build Type | Status |
10
+ ----------- | --------------|
11
+ Unit Tests (Linux) | [![](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_unittests.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_unittests.yml) |
12
+ Nightly Release | [![](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_release.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_release.yml) |
13
+ Nightly Colab | [![](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_colabs.yml/badge.svg?branch=main)](https://github.com/google-ai-edge/ai-edge-quantizer/actions/workflows/nightly_colabs.yml) |
14
+
15
+ ## Installation
16
+
17
+ ### Requirements and Dependencies
18
+
19
+ * Python versions: 3.10, 3.11, 3.12, 3.13
20
+ * Operating system: Linux, MacOS
21
+ * TensorFlow: [![tf-nightly](https://img.shields.io/badge/tf--nightly-latest-blue)](https://pypi.org/project/tf-nightly/)
22
+
23
+ ### Install
24
+
25
+ Nightly PyPi package:
26
+
27
+ ```bash
28
+ pip install ai-edge-quantizer-nightly
29
+ ```
30
+
31
+ ## API Usage
32
+
33
+ The quantizer requires two inputs:
34
+
35
+ 1. An unquantized source LiteRT model (with FP32 data type in the FlatBuffer format with `.tflite` extension)
36
+ 2. A quantization recipe (details below)
37
+
38
+ and outputs a quantized LiteRT model that's ready for deployment on edge devices.
39
+
40
+ ### Basic Usage
41
+
42
+ In a nutshell, the quantizer works according to the following steps:
43
+
44
+ 1. Instantiate a `Quantizer` class. This is the entry point to the quantizer's functionalities that the user accesses.
45
+ 2. Load a desired quantization recipe (details in subsection).
46
+ 3. Quantize (and save) the model. This is where most of the quantizer's internal logic works.
47
+
48
+ ```python
49
+ qt = quantizer.Quantizer("path/to/input/tflite")
50
+ qt.load_quantization_recipe(recipe.dynamic_wi8_afp32())
51
+ qt.quantize().export_model("/path/to/output/tflite")
52
+ ```
53
+
54
+ Please see the [getting started colab](colabs/getting_started.ipynb) for the simplest quick start guide on those 3 steps, and the [selective quantization colab](colabs/selective_quantization_isnet.ipynb) with more details on advanced features.
55
+
56
+ #### LiteRT Model
57
+
58
+ Please refer to the [LiteRT documentation](https://ai.google.dev/edge/litert) for ways to generate LiteRT models from Jax, PyTorch and TensorFlow. The input source model should be an FP32 (unquantized) model in the FlatBuffer format with `.tflite` extension.
59
+
60
+ #### Quantization Recipe
61
+
62
+ The user needs to specify a quantization recipe using AI Edge Quantizer's API to
63
+ apply to the source model. The quantization recipe encodes all information on how
64
+ a model is to be quantized, such as number of bits, data type, symmetry, scope
65
+ name, etc.
66
+
67
+ Essentially, a quantization recipe is defined as a collection of commands of the
68
+ following type:
69
+
70
+ _“Apply **Quantization Algorithm X** on **Operator Y** under **Scope Z** with **ConfigN**”._
71
+
72
+ For example:
73
+
74
+ _\"**Uniformly quantize** the **FullyConnected op** under scope **'dense1/'** with **INT8 symmetric with Dynamic Quantization**"._
75
+
76
+ All the unspecified ops will be kept as FP32 (unquantized). The scope of an operator in TFLite is defined as the output tensor name of the op, which preserves the hierarchical model information from the source model (e.g., scope in TF). The best way to obtain scope name is by visualizing the model with [Model Explorer](https://ai.google.dev/edge/model-explorer).
77
+
78
+ Currently, there are three ways to quantize an operator:
79
+
80
+ * **dynamic quantization (recommended)**: weights are quantized while activations
81
+ remain in a float format and are not processed by AI Edge Quantizer (AEQ). The
82
+ runtime kernel handles the on-the-fly quantization of these activations, as
83
+ identified by `compute_precision=integer` and `explicit_dequantize=False`.
84
+ * Pros: reduced model size and memory usage. Latency improvement due to integer computation. No sample data requirement (calibration).
85
+ * Cons: on-the-fly quantization of activation tensors can affect model quality. Not supported in all hardware (e.g., some GPU and NPU).
86
+
87
+ * **weight only quantization**: only model weights are quantized, not activations. The
88
+ actual operation (op) computation remains in float. The quantized weight is
89
+ explicitly dequantized before being fed into the op, by inserting a dequantize
90
+ op between the quantized weight and the consuming op. To enable this,
91
+ `compute_precision` will be set to `float` and `explicit_dequantize` to `True`.
92
+ * Pros: reduced model size and memory usage. No sample data requirement (calibration). Usually has the best model quality.
93
+ * Cons: no latency benefit (may be worse) due to float computation with explicit dequantization.
94
+
95
+ * **static quantization**: both weights and activations are quantized. This requires a calibration phase to estimate quantization parameters of runtime tensors (activations).
96
+ * Pros: reduced model size, memory usage, and latency.
97
+ * Cons: requires sample data for calibration. Imposing static quantization parameters (derived from calibration) on runtime tensors can compromise quality.
98
+
99
+ Generally, we recommend dynamic quantization for CPU/GPU deployment and static
100
+ quantization for NPU deployment.
101
+
102
+ We include commonly used recipes in [recipe.py](ai_edge_quantizer/recipe.py). This is demonstrated in the [getting started colab](colabs/getting_started.ipynb) example. Advanced users can build their own recipe through the quantizer API.
103
+
104
+ #### Deployment
105
+ Please refer to the [LiteRT deployment documentation](https://ai.google.dev/edge/litert/inference) for ways to deploy a quantized LiteRT model.
106
+
107
+ ### Advanced Recipes
108
+
109
+ There are many ways the user can configure and customize the quantization recipe beyond using a template in [recipe.py](ai_edge_quantizer/recipe.py). For example, the user can configure the recipe to achieve these features:
110
+
111
+ * Selective quantization (exclude selected ops from being quantized)
112
+ * Flexible mixed scheme quantization (mixture of different precision, compute precision, scope, op, config, etc)
113
+ * 4-bit weight quantization
114
+
115
+ The [selective quantization colab](colabs/selective_quantization_isnet.ipynb) shows some of these more advanced features.
116
+
117
+ For specifics of the recipe schema, please refer to the `OpQuantizationRecipe` in [recipe_manager.py].
118
+
119
+ For advanced usage involving mixed quantization, the following API may be useful:
120
+
121
+ * Use `Quantizer:load_quantization_recipe()` in [quantizer.py](ai_edge_quantizer/quantizer.py) to load a custom recipe.
122
+ * Use `Quantizer:update_quantization_recipe()` in [quantizer.py](ai_edge_quantizer/quantizer.py) to extend or override specific parts of the recipe.
123
+
124
+ ### Operator coverage
125
+
126
+ The table below outlines the allowed configurations for available recipes.
127
+
128
+ | | | | | | | | | | |
129
+ | --- | --- | --- | --- | --- | --- |--- |--- |--- |--- |
130
+ | **Config** | | DYNAMIC_WI8_AFP32 | DYNAMIC_WI4_AFP32 | STATIC_WI8_AI16 | STATIC_WI4_AI16 | STATIC_WI8_AI8 | STATIC_WI4_AI8 | WEIGHTONLY_WI8_AFP32 | WEIGHTONLY_WI4_AFP32 |
131
+ |activation| num\_bits | None | None | 16 | 16 | 8 | 8 | None | None |
132
+ | | symmetric |None | None | TRUE | TRUE | [TRUE, FALSE] | [TRUE, FALSE] | None | None |
133
+ | | granularity |None | None | TENSORWISE | TENSORWISE | TENSORWISE | TENSORWISE | None | None |
134
+ | | dtype| None | None |INT | INT | INT | INT | None | None |
135
+ | weight | num\_bits | 8 | 4 | 8 | 4 | 8 | 4 | 8 | 4 |
136
+ | | symmetric | TRUE | TRUE | TRUE | TRUE | TRUE | TRUE | [TRUE, FALSE] | [TRUE, FALSE] |
137
+ | | granularity | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] | \[CHANNELWISE, TENSORWISE\] |
138
+ | | dtype | INT | INT | INT | INT | INT | INT | INT | INT |
139
+ | explicit\_dequantize | | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE |
140
+ | compute\_precision || INTEGER | INTEGER | INTEGER | INTEGER | INTEGER | INTEGER | FLOAT | FLOAT |
141
+
142
+
143
+ **Operators Supporting Quantization**
144
+
145
+ | | | | | | | | | |
146
+ | --- | --- | --- | --- | --- | --- |--- |--- |--- |
147
+ | **Config** | DYNAMIC_WI8_AFP32 | DYNAMIC_WI4_AFP32 | STATIC_WI8_AI16 | STATIC_WI4_AI16 | STATIC_WI8_AI8 | STATIC_WI4_AI8 | WEIGHTONLY_WI8_AFP32 | WEIGHTONLY_WI4_AFP32 |
148
+ |FULLY_CONNECTED |<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|
149
+ |CONV_2D |<div align="center"> &check; </div>| |<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>| |
150
+ |BATCH_MATMUL |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |
151
+ |EMBEDDING_LOOKUP |<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>| |<div align="center"> &check; </div>|<div align="center"> &check; </div>|<div align="center"> &check; </div>| |
152
+ |DEPTHWISE_CONV_2D|<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |
153
+ |AVERAGE_POOL_2D | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
154
+ |RESHAPE | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
155
+ |SOFTMAX | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
156
+ |TANH | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
157
+ |TRANSPOSE | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
158
+ |GELU | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
159
+ |ADD | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
160
+ |CONV_2D_TRANSPOSE|<div align="center"> &check; </div>| |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
161
+ |SUB | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
162
+ |MUL | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
163
+ |MEAN | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
164
+ |RSQRT | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
165
+ |CONCATENATION | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
166
+ |STRIDED_SLICE | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
167
+ |SPLIT | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
168
+ |LOGISTIC | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
169
+ |SLICE | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
170
+ |SELECT | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
171
+ |SELECT_V2 | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
172
+ |SUM | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
173
+ |PAD | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
174
+ |PADV2 | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
175
+ |MIRROR_PAD | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
176
+ |SQUARED_DIFFERENCE | | | | |<div align="center"> &check; </div>| | | |
177
+ |MAX_POOL_2D | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
178
+ |RESIZE_BILINEAR | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
179
+ |RESIZE_NEAREST_NEIGHBOR| | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
180
+ |GATHER_ND | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
181
+ |PACK | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
182
+ |UNPACK | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
183
+ |DIV | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
184
+ |SQRT | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
185
+ |GATHER | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
186
+ |HARD_SWISH | | | | |<div align="center"> &check; </div>| | | |
187
+ |MAXIMUM | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
188
+ |REDUCE_MIN | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
189
+ |EQUAL | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
190
+ |NOT_EQUAL | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
191
+ |SPACE_TO_DEPTH | | | | |<div align="center"> &check; </div>| | | |
192
+ |RELU | | |<div align="center"> &check; </div>| |<div align="center"> &check; </div>| | | |
@@ -0,0 +1 @@
1
+ 0.5.1