diff --git a/tests/data/tflm/tanh_int8_8f6596d.json b/tests/data/tflm/tanh_int8_8f6596d.json new file mode 100644 index 00000000..1c00d208 --- /dev/null +++ b/tests/data/tflm/tanh_int8_8f6596d.json @@ -0,0 +1,1929 @@ +{ + "provenance": { + "tflm_repo": "AdaWorldAPI/tflite-micro", + "tflm_checkout_head": "5f64e4ad1763fbe53f40534c39aabe22037a6e79", + "tflm_upstream_commit": "8f6596d99898deaac36ebd8ff8bf1b9754fd2cd1", + "tensorflow_tree_diff_lines_vs_upstream": 0, + "kernel": "tflite::Register_TANH() via tflite::micro::KernelRunner", + "kernel_sources": { + "tensorflow/lite/micro/kernels/tanh.cc": "822f3d1c21e6c7b1736271e3d269b85c9d51316b7016f4c9373f1a208a25592f", + "tensorflow/lite/micro/kernels/internal/reference/integer_ops/tanh.h": "9372db064ab5130f12d62638d055f4cef4d220aac4ccf40471d6053eec162fbb", + "tensorflow/lite/micro/kernels/internal/common.h": "2742b4738307c5647bc1da0c77db125faa315b0fd90933f2a370f89d94c9c06e", + "tensorflow/lite/micro/kernels/internal/fixedpoint.h": "c19cb30b01ae9452c434140371502a7891bcc07a85f3d30a262e0444a11dc189" + }, + "library": "gen/linux_x86_64_default_gcc/lib/libtensorflow-microlite.a, built by: make -f tensorflow/lite/micro/tools/make/Makefile test_kernel_tanh_test (TFLM's own tanh tests: 3 passed)", + "compiler": "g++ (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0", + "compile_flags": "TFLM Makefile defaults for linux_x86_64_default_gcc (-std=c++17 -Os -fno-rtti -fno-exceptions -DKERNELS_OPTIMIZED_FOR_SPEED -DTF_LITE_DISABLE_X86_NEON ...)", + "third_party_deps": { + "flatbuffers": { + "repo": "https://github.com/google/flatbuffers", + "ref": "v25.9.23", + "commit": "187240970746d00bbd26b0f5873ed54d2477f9f3" + }, + "kissfft": { + "repo": "https://github.com/mborgerding/kissfft", + "ref": "v130", + "commit": "918336beac80868ad3011cb283063bb224e3e5da" + }, + "eyalroz_printf": { + "repo": "https://github.com/eyalroz/printf", + "ref": "f8ed5a9bd9fa8384430973465e94aa14c925872d", + "commit": "f8ed5a9bd9fa8384430973465e94aa14c925872d" + } + }, + "third_party_note": "installed from git at the listed tag/commit because GitHub archive zips return 403 through this proxy; the archive md5 in download_dependencies.py was therefore not verified. None of the three is on the TANH int8 code path.", + "harness": "ndarray tools/tflm_oracle/tanh_int8_oracle.cc", + "harness_sha256": "5880995d82490fc84f927afd23519350f3998a533a518ebf872b31e3e7d5dc41", + "determinism": "two runs, byte-identical output; rebuilt with -Werror, byte-identical", + "generated": "2026-10-26" + }, + "input_codes": "-128..127 in order; output[i] is for input code i-128", + "output_scale": 0.0078125, + "output_zero_point": 0, + "cases": [ + { + "input_scale": 0.0078125, + "input_scale_bits": "0x3c000000", + "input_zero_point": 0, + "prepare_ok": true, + "invoke_ok": true, + "input_multiplier": 1073741824, + "input_left_shift": 21, + "input_range_radius": 960, + "output": [ + -97, + -97, + -97, + -96, + -96, + -95, + -95, + -94, + -94, + -93, + -93, + -93, + -92, + -92, + -91, + -91, + -90, + -90, + -89, + -89, + -88, + -88, + -87, + -86, + -86, + -85, + -85, + -84, + -84, + -83, + -82, + -82, + -81, + -81, + -80, + -79, + -79, + -78, + -78, + -77, + -76, + -76, + -75, + -74, + -74, + -73, + -72, + -72, + -71, + -70, + -70, + -69, + -68, + -67, + -67, + -66, + -65, + -65, + -64, + -63, + -62, + -61, + -61, + -60, + -59, + -58, + -58, + -57, + -56, + -55, + -54, + -54, + -53, + -52, + -51, + -50, + -49, + -48, + -48, + -47, + -46, + -45, + -44, + -43, + -42, + -41, + -41, + -40, + -39, + -38, + -37, + -36, + -35, + -34, + -33, + -32, + -31, + -30, + -29, + -29, + -28, + -27, + -26, + -25, + -24, + -23, + -22, + -21, + -20, + -19, + -18, + -17, + -16, + -15, + -14, + -13, + -12, + -11, + -10, + -9, + -8, + -7, + -6, + -5, + -4, + -3, + -2, + -1, + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 29, + 30, + 31, + 32, + 33, + 34, + 35, + 36, + 37, + 38, + 39, + 40, + 41, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 48, + 49, + 50, + 51, + 52, + 53, + 54, + 54, + 55, + 56, + 57, + 58, + 58, + 59, + 60, + 61, + 61, + 62, + 63, + 64, + 65, + 65, + 66, + 67, + 67, + 68, + 69, + 70, + 70, + 71, + 72, + 72, + 73, + 74, + 74, + 75, + 76, + 76, + 77, + 78, + 78, + 79, + 79, + 80, + 81, + 81, + 82, + 82, + 83, + 84, + 84, + 85, + 85, + 86, + 86, + 87, + 88, + 88, + 89, + 89, + 90, + 90, + 91, + 91, + 92, + 92, + 93, + 93, + 93, + 94, + 94, + 95, + 95, + 96, + 96, + 97, + 97 + ] + }, + { + "input_scale": 0.0500000007, + "input_scale_bits": "0x3d4ccccd", + "input_zero_point": -3, + "prepare_ok": true, + "invoke_ok": true, + "input_multiplier": 1717986944, + "input_left_shift": 23, + "input_range_radius": 240, + "output": [ + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -126, + -126, + -126, + -126, + -126, + -125, + -125, + -125, + -125, + -124, + -124, + -123, + -123, + -122, + -122, + -121, + -120, + -120, + -119, + -118, + -117, + -116, + -115, + -113, + -112, + -110, + -109, + -107, + -105, + -102, + -100, + -97, + -95, + -92, + -88, + -85, + -81, + -77, + -73, + -69, + -64, + -59, + -54, + -49, + -43, + -37, + -31, + -25, + -19, + -13, + -6, + 0, + 6, + 13, + 19, + 25, + 31, + 37, + 43, + 49, + 54, + 59, + 64, + 69, + 73, + 77, + 81, + 85, + 88, + 92, + 95, + 97, + 100, + 102, + 105, + 107, + 109, + 110, + 112, + 113, + 115, + 116, + 117, + 118, + 119, + 120, + 120, + 121, + 122, + 122, + 123, + 123, + 124, + 124, + 125, + 125, + 125, + 125, + 126, + 126, + 126, + 126, + 126, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127 + ] + }, + { + "input_scale": 0.100000001, + "input_scale_bits": "0x3dcccccd", + "input_zero_point": 20, + "prepare_ok": true, + "invoke_ok": true, + "input_multiplier": 1717986944, + "input_left_shift": 24, + "input_range_radius": 120, + "output": [ + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -127, + -127, + -127, + -127, + -127, + -127, + -126, + -126, + -125, + -125, + -124, + -123, + -122, + -121, + -120, + -118, + -116, + -113, + -110, + -107, + -102, + -97, + -92, + -85, + -77, + -69, + -59, + -49, + -37, + -25, + -13, + 0, + 13, + 25, + 37, + 49, + 59, + 69, + 77, + 85, + 92, + 97, + 102, + 107, + 110, + 113, + 116, + 118, + 120, + 121, + 122, + 123, + 124, + 125, + 125, + 126, + 126, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127 + ] + }, + { + "input_scale": 0.0137, + "input_scale_bits": "0x3c6075f7", + "input_zero_point": -128, + "prepare_ok": true, + "invoke_ok": true, + "input_multiplier": 1882913664, + "input_left_shift": 21, + "input_range_radius": 960, + "output": [ + 0, + 2, + 4, + 5, + 7, + 9, + 10, + 12, + 14, + 16, + 17, + 19, + 21, + 23, + 24, + 26, + 28, + 29, + 31, + 33, + 34, + 36, + 37, + 39, + 41, + 42, + 44, + 45, + 47, + 48, + 50, + 51, + 53, + 54, + 56, + 57, + 58, + 60, + 61, + 63, + 64, + 65, + 66, + 68, + 69, + 70, + 71, + 73, + 74, + 75, + 76, + 77, + 78, + 79, + 81, + 82, + 83, + 84, + 85, + 86, + 87, + 87, + 88, + 89, + 90, + 91, + 92, + 93, + 94, + 94, + 95, + 96, + 97, + 97, + 98, + 99, + 100, + 100, + 101, + 102, + 102, + 103, + 104, + 104, + 105, + 105, + 106, + 106, + 107, + 107, + 108, + 108, + 109, + 109, + 110, + 110, + 111, + 111, + 112, + 112, + 112, + 113, + 113, + 114, + 114, + 114, + 115, + 115, + 115, + 116, + 116, + 116, + 117, + 117, + 117, + 117, + 118, + 118, + 118, + 119, + 119, + 119, + 119, + 119, + 120, + 120, + 120, + 120, + 121, + 121, + 121, + 121, + 121, + 121, + 122, + 122, + 122, + 122, + 122, + 122, + 123, + 123, + 123, + 123, + 123, + 123, + 123, + 124, + 124, + 124, + 124, + 124, + 124, + 124, + 124, + 124, + 124, + 125, + 125, + 125, + 125, + 125, + 125, + 125, + 125, + 125, + 125, + 125, + 125, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 126, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127, + 127 + ] + }, + { + "input_scale": 0.5, + "input_scale_bits": "0x3f000000", + "input_zero_point": 127, + "prepare_ok": true, + "invoke_ok": true, + "input_multiplier": 1073741824, + "input_left_shift": 27, + "input_range_radius": 15, + "output": [ + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -127, + -126, + -123, + -116, + -97, + -59, + 0 + ] + }, + { + "input_scale": 9.99999975e-05, + "input_scale_bits": "0x38d1b717", + "input_zero_point": 0, + "prepare_ok": true, + "invoke_ok": true, + "input_multiplier": 1759218560, + "input_left_shift": 14, + "input_range_radius": 122880, + "output": [ + -2, + -2, + -2, + -2, + -2, + -2, + -2, + -2, + -2, + -2, + -2, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2 + ] + }, + { + "input_scale": 0.0234375, + "input_scale_bits": "0x3cc00000", + "input_zero_point": 60, + "prepare_ok": true, + "invoke_ok": true, + "input_multiplier": 1610612736, + "input_left_shift": 22, + "input_range_radius": 480, + "output": [ + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -128, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -127, + -126, + -126, + -126, + -126, + -126, + -126, + -126, + -126, + -126, + -126, + -126, + -125, + -125, + -125, + -125, + -125, + -125, + -125, + -124, + -124, + -124, + -124, + -124, + -124, + -123, + -123, + -123, + -123, + -122, + -122, + -122, + -122, + -121, + -121, + -121, + -120, + -120, + -120, + -119, + -119, + -118, + -118, + -117, + -117, + -116, + -116, + -115, + -115, + -114, + -113, + -113, + -112, + -111, + -111, + -110, + -109, + -108, + -107, + -107, + -106, + -105, + -104, + -103, + -101, + -100, + -99, + -98, + -97, + -95, + -94, + -93, + -91, + -90, + -88, + -86, + -85, + -83, + -81, + -79, + -78, + -76, + -74, + -72, + -70, + -67, + -65, + -63, + -61, + -58, + -56, + -54, + -51, + -48, + -46, + -43, + -41, + -38, + -35, + -32, + -29, + -27, + -24, + -21, + -18, + -15, + -12, + -9, + -6, + -3, + 0, + 3, + 6, + 9, + 12, + 15, + 18, + 21, + 24, + 27, + 29, + 32, + 35, + 38, + 41, + 43, + 46, + 48, + 51, + 54, + 56, + 58, + 61, + 63, + 65, + 67, + 70, + 72, + 74, + 76, + 78, + 79, + 81, + 83, + 85, + 86, + 88, + 90, + 91, + 93, + 94, + 95, + 97, + 98, + 99, + 100, + 101, + 103, + 104, + 105, + 106, + 107, + 107, + 108, + 109, + 110, + 111, + 111, + 112, + 113, + 113, + 114, + 115, + 115, + 116, + 116, + 117, + 117 + ] + } + ], + "nonconforming_output_probe": { + "input_case": 1, + "declared_output_scale": 0.1, + "declared_output_zero_point": 5, + "prepare_ok": true, + "invoke_ok": true, + "bytes_identical_to_conforming": true + } +} \ No newline at end of file diff --git a/tools/tflm_oracle/README.md b/tools/tflm_oracle/README.md new file mode 100644 index 00000000..3927e476 --- /dev/null +++ b/tools/tflm_oracle/README.md @@ -0,0 +1,41 @@ +# TFLite Micro oracles + +Independent reference outputs for TFLite Micro integer kernels. They are inputs +to future parity tests of native ports; nothing in this crate builds or runs +them. No native code was consulted to produce them. + +## `tanh_int8_oracle.cc` → `tests/data/tflm/tanh_int8_8f6596d.json` + +Runs the real `tflite::Register_TANH()` (prepare in `tanh.cc`, eval in +`integer_ops/tanh.h`) through `tflite::micro::KernelRunner` over all 256 int8 +input codes, for seven input calibrations, with the TANH output contract +(scale 1/128, zero point 0). It also records one non-conforming output +declaration, because TFLM's int8 TANH prepare does not check the output +quantization. + +The fixture carries its own provenance (TFLM commit, sha256 of the kernel +sources and of this harness, compiler, flags, third-party commits). + +### Reproduce + +```sh +git clone https://github.com/AdaWorldAPI/tflite-micro && cd tflite-micro +git checkout 8f6596d # kernel tree identical to the fixture's checkout +# Dependencies: `make ... third_party_downloads` fetches GitHub archive zips. +# Where those are blocked, clone flatbuffers v25.9.23, kissfft v130 and +# eyalroz/printf f8ed5a9b into tensorflow/lite/micro/tools/make/downloads/, +# apply the patches named in download_dependencies.py, and write its stamp. +# The person_detection example needs Pillow and numpy on PATH's python3. +make -f tensorflow/lite/micro/tools/make/Makefile test_kernel_tanh_test +g++ -std=c++17 -Os -fno-rtti -fno-exceptions -fno-threadsafe-statics \ + -DTFLM_REMOVE_VIRTUAL_DELETE -DTF_LITE_DISABLE_X86_NEON \ + -DKERNELS_OPTIMIZED_FOR_SPEED -DTF_LITE_USE_CTIME -Werror -Wall -Wextra \ + -Wno-unused-parameter -I. -Itensorflow/lite/micro/tools/make/downloads \ + -Itensorflow/lite/micro/tools/make/downloads/flatbuffers/include \ + -Itensorflow/lite/micro/tools/make/downloads/kissfft \ + -Igen/linux_x86_64_default_gcc/genfiles/ \ + /tools/tflm_oracle/tanh_int8_oracle.cc \ + gen/linux_x86_64_default_gcc/lib/libtensorflow-microlite.a \ + -Wl,--gc-sections -lm -o tanh_oracle +./tanh_oracle # the "cases" and "nonconforming_output_probe" of the fixture +``` diff --git a/tools/tflm_oracle/tanh_int8_oracle.cc b/tools/tflm_oracle/tanh_int8_oracle.cc new file mode 100644 index 00000000..f5cc6b4d --- /dev/null +++ b/tools/tflm_oracle/tanh_int8_oracle.cc @@ -0,0 +1,130 @@ +// Independent oracle for TFLite Micro int8 TANH. +// +// Runs the real TFLM kernel (tflite::Register_TANH(): tanh.cc prepare + the +// reference integer_ops/tanh.h eval) through micro::KernelRunner, once per +// input calibration, over all 256 int8 input codes, and prints the output +// bytes as JSON. Nothing here re-implements the kernel; it only builds +// tensors and calls TFLM. It is compiled against a TFLite Micro checkout and +// linked with that checkout's libtensorflow-microlite.a (see README.md). +// +// The prepared parameters (input_multiplier, input_left_shift, +// input_range_radius) are private to tanh.cc. They are recomputed below with +// the same TFLM functions (frexp, TfLiteRound, CalculateInputRadius) purely +// for the record; they do not feed the output bytes. + +#include +#include +#include +#include + +#include "tensorflow/lite/micro/kernels/internal/common.h" +#include "tensorflow/lite/micro/kernels/internal/cppmath.h" +#include "tensorflow/lite/micro/kernels/kernel_runner.h" +#include "tensorflow/lite/micro/micro_common.h" +#include "tensorflow/lite/micro/test_helpers.h" + +namespace { + +struct Calib { + float scale; + int zero_point; +}; + +// Legal int8 input calibrations: the TANH op constrains the output +// (scale 1/128, zero point 0), not the input. Chosen to cover a small, a +// unit-like and a large scale, zero points at both ends of the code range, +// and a scale whose radius saturates most codes. +constexpr Calib kInputs[] = { + {1.0f / 128.0f, 0}, // input already in tanh's output units + {0.05f, -3}, + {0.1f, 20}, + {0.0137f, -128}, // zero point at the lowest code + {0.5f, 127}, // zero point at the highest code; most codes saturate + {0.0001f, 0}, // tiny scale: near-linear region only + {0.0234375f, 60}, +}; + +constexpr float kOutScale = 1.0f / 128.0f; +constexpr int kOutZeroPoint = 0; + +// Runs Register_TANH over all 256 codes. Returns the TfLiteStatus of +// InitAndPrepare (Invoke runs only if prepare succeeded). +TfLiteStatus RunTanh(const Calib& in, float out_scale, int out_zp, int8_t* out, + TfLiteStatus* invoke_status) { + int8_t input[256]; + for (int i = 0; i < 256; ++i) input[i] = static_cast(i - 128); + int dims_data[] = {2, 1, 256}; + TfLiteIntArray* dims = tflite::testing::IntArrayFromInts(dims_data); + TfLiteTensor tensors[2] = { + tflite::testing::CreateQuantizedTensor(input, dims, in.scale, + in.zero_point), + tflite::testing::CreateQuantizedTensor(out, dims, out_scale, out_zp)}; + int inputs_data[] = {1, 0}; + int outputs_data[] = {1, 1}; + const TFLMRegistration registration = tflite::Register_TANH(); + tflite::micro::KernelRunner runner( + registration, tensors, 2, tflite::testing::IntArrayFromInts(inputs_data), + tflite::testing::IntArrayFromInts(outputs_data), nullptr); + TfLiteStatus prep = runner.InitAndPrepare(); + *invoke_status = prep == kTfLiteOk ? runner.Invoke() : kTfLiteError; + return prep; +} + +void PrintBytes(const int8_t* out) { + std::printf("["); + for (int i = 0; i < 256; ++i) std::printf(i ? ",%d" : "%d", out[i]); + std::printf("]"); +} + +} // namespace + +int main() { + std::printf("{\n \"input_codes\": \"-128..127 in order; output[i] is for input code i-128\",\n"); + std::printf(" \"output_scale\": %.9g,\n \"output_zero_point\": %d,\n", + static_cast(kOutScale), kOutZeroPoint); + std::printf(" \"cases\": [\n"); + const int n = sizeof(kInputs) / sizeof(kInputs[0]); + for (int c = 0; c < n; ++c) { + const Calib& in = kInputs[c]; + int8_t out[256] = {0}; + TfLiteStatus inv; + TfLiteStatus prep = RunTanh(in, kOutScale, kOutZeroPoint, out, &inv); + // The record of what tanh.cc prepare computes (same TFLM functions). + constexpr int kInputIntegerBits = 4; + const double real = static_cast(in.scale) * + static_cast(1 << (31 - kInputIntegerBits)); + int shift = 0; + const double q = std::frexp(real, &shift); + const int32_t mult = static_cast(tflite::TfLiteRound(q * (1ll << 31))); + const int32_t radius = + tflite::CalculateInputRadius(kInputIntegerBits, shift, 31); + uint32_t scale_bits = 0; + std::memcpy(&scale_bits, &in.scale, sizeof scale_bits); + std::printf(" {\"input_scale\": %.9g, \"input_scale_bits\": \"0x%08x\", " + "\"input_zero_point\": %d, \"prepare_ok\": %s, \"invoke_ok\": %s,\n" + " \"input_multiplier\": %d, \"input_left_shift\": %d, " + "\"input_range_radius\": %d,\n \"output\": ", + static_cast(in.scale), + scale_bits, in.zero_point, + prep == kTfLiteOk ? "true" : "false", + inv == kTfLiteOk ? "true" : "false", mult, shift, radius); + PrintBytes(out); + std::printf("}%s\n", c + 1 < n ? "," : ""); + } + std::printf(" ],\n"); + // Admission probe: TFLM's int8 TANH prepare does not check the output + // quantization. Declare a non-conforming output (scale 0.1, zp 5) and + // record whether prepare accepts it and whether the bytes change. + int8_t ref[256] = {0}, odd[256] = {0}; + TfLiteStatus inv_ref, inv_odd; + RunTanh(kInputs[1], kOutScale, kOutZeroPoint, ref, &inv_ref); + TfLiteStatus prep_odd = RunTanh(kInputs[1], 0.1f, 5, odd, &inv_odd); + bool same = true; + for (int i = 0; i < 256; ++i) same = same && ref[i] == odd[i]; + std::printf(" \"nonconforming_output_probe\": {\"input_case\": 1, " + "\"declared_output_scale\": 0.1, \"declared_output_zero_point\": 5, " + "\"prepare_ok\": %s, \"invoke_ok\": %s, \"bytes_identical_to_conforming\": %s}\n}\n", + prep_odd == kTfLiteOk ? "true" : "false", + inv_odd == kTfLiteOk ? "true" : "false", same ? "true" : "false"); + return 0; +}