├── .github ├── mergify.yml └── workflows │ ├── ci.yml │ ├── remove-labels.yml │ └── sync.yml ├── .gitignore ├── AUTHORS ├── CODEOWNERS ├── LICENSE ├── README.md ├── docs ├── TMLK_WS_wm8960_images │ ├── IMG_1355.JPG │ ├── IMG_1356.JPG │ ├── IMG_1357.JPG │ ├── IMG_1360.JPG │ ├── IMG_1361.JPG │ ├── IMG_1362.JPG │ └── IMG_1363.JPG ├── animation_on_arduino.gif ├── hello_world_animation.gif ├── hello_world_screenshot.png ├── hello_world_serial_plotter.png └── magic_wand_attachment.jpg ├── examples ├── hello_world │ ├── README.md │ ├── arduino_constants.cpp │ ├── arduino_main.cpp │ ├── arduino_output_handler.cpp │ ├── constants.h │ ├── hello_world.ino │ ├── main_functions.h │ ├── model.cpp │ ├── model.h │ └── output_handler.h ├── magic_wand │ ├── LICENSE │ ├── README.md │ ├── magic_wand.ino │ ├── magic_wand_model_data.cpp │ ├── magic_wand_model_data.h │ ├── rasterize_stroke.cpp │ ├── rasterize_stroke.h │ ├── train │ │ └── train_magic_wand_model.ipynb │ └── website │ │ └── index.html ├── micro_speech │ ├── README.md │ ├── arduino_audio_provider.cpp │ ├── arduino_command_responder.cpp │ ├── arduino_main.cpp │ ├── audio_provider.h │ ├── command_responder.h │ ├── data │ │ ├── no_1000ms.wav │ │ ├── serial_test_config.json │ │ └── yes_1000ms.wav │ ├── feature_provider.cpp │ ├── feature_provider.h │ ├── main_functions.h │ ├── micro_features_micro_features_generator.cpp │ ├── micro_features_micro_features_generator.h │ ├── micro_features_micro_model_settings.cpp │ ├── micro_features_micro_model_settings.h │ ├── micro_features_model.cpp │ ├── micro_features_model.h │ ├── micro_speech.ino │ ├── recognize_commands.cpp │ └── recognize_commands.h └── person_detection │ ├── README.md │ ├── arduino_detection_responder.cpp │ ├── arduino_image_provider.cpp │ ├── arduino_main.cpp │ ├── data │ ├── README.md │ ├── no_person.jpg │ ├── no_person_96x96.bmp │ ├── person.jpg │ ├── person_96x96.bmp │ └── serial_test_config.json │ ├── detection_responder.h │ ├── image_provider.h │ ├── main_functions.h │ ├── model_settings.cpp │ ├── model_settings.h │ ├── person_detect_model_data.cpp │ ├── person_detect_model_data.h │ └── person_detection.ino ├── library.properties ├── scripts ├── MANIFEST.ini ├── README_create_tflm_arduino.md ├── create_tflm_arduino.py ├── fix_arduino_subfolders.py ├── helper_functions.sh ├── install_arduino_cli.sh ├── sync_from_tflite_micro.sh ├── test_arduino_library.sh ├── test_over_serial.py ├── test_repo_tree.sh ├── test_tflm_arduino.sh └── transform_source.py └── src ├── TensorFlowLite.h ├── peripherals ├── README.md ├── audio_codec.h ├── audio_common.h ├── audio_device.h ├── audio_i2s.h ├── button.h ├── button_arduino.cpp ├── examples │ └── audio_play_record │ │ └── audio_play_record.ino ├── i2c.h ├── i2c_arduino.cpp ├── i2c_arduino.h ├── i2s_nrf52840.cpp ├── i2s_nrf52840.h ├── led.h ├── led_arduino.cpp ├── peripherals.h ├── tests │ ├── test_i2s_codec │ │ └── test_i2s_codec.ino │ └── test_wm8960 │ │ └── test_wm8960.ino ├── utility.h ├── utility_arduino.cpp ├── wm8960.cpp ├── wm8960.h ├── wm8960_regs.h ├── ws_wm8960_audio_hat_nrf52840.cpp └── ws_wm8960_audio_hat_nrf52840.h ├── tensorflow └── lite │ ├── builtin_op_data.h │ ├── builtin_ops.h │ ├── c │ ├── builtin_op_data.h │ ├── c_api_types.h │ └── common.h │ ├── context_util.h │ ├── core │ ├── api │ │ ├── error_reporter.cpp │ │ ├── error_reporter.h │ │ ├── flatbuffer_conversions.cpp │ │ ├── flatbuffer_conversions.h │ │ ├── op_resolver.cpp │ │ ├── op_resolver.h │ │ ├── tensor_utils.cpp │ │ └── tensor_utils.h │ └── c │ │ ├── builtin_op_data.h │ │ ├── c_api_types.h │ │ ├── common.cpp │ │ └── common.h │ ├── experimental │ └── microfrontend │ │ └── lib │ │ ├── bits.h │ │ ├── fft.cpp │ │ ├── fft.h │ │ ├── fft_util.cpp │ │ ├── fft_util.h │ │ ├── filterbank.c │ │ ├── filterbank.h │ │ ├── filterbank_util.c │ │ ├── filterbank_util.h │ │ ├── frontend.c │ │ ├── frontend.h │ │ ├── frontend_util.c │ │ ├── frontend_util.h │ │ ├── kiss_fft_common.h │ │ ├── kiss_fft_int16.cpp │ │ ├── kiss_fft_int16.h │ │ ├── log_lut.c │ │ ├── log_lut.h │ │ ├── log_scale.c │ │ ├── log_scale.h │ │ ├── log_scale_util.c │ │ ├── log_scale_util.h │ │ ├── noise_reduction.c │ │ ├── noise_reduction.h │ │ ├── noise_reduction_util.c │ │ ├── noise_reduction_util.h │ │ ├── pcan_gain_control.c │ │ ├── pcan_gain_control.h │ │ ├── pcan_gain_control_util.c │ │ ├── pcan_gain_control_util.h │ │ ├── window.c │ │ ├── window.h │ │ ├── window_util.c │ │ └── window_util.h │ ├── kernels │ ├── internal │ │ ├── common.h │ │ ├── compatibility.h │ │ ├── cppmath.h │ │ ├── max.h │ │ ├── min.h │ │ ├── optimized │ │ │ └── neon_check.h │ │ ├── portable_tensor.h │ │ ├── portable_tensor_utils.cpp │ │ ├── portable_tensor_utils.h │ │ ├── quantization_util.cpp │ │ ├── quantization_util.h │ │ ├── reference │ │ │ ├── add.h │ │ │ ├── add_n.h │ │ │ ├── arg_min_max.h │ │ │ ├── batch_matmul.h │ │ │ ├── batch_to_space_nd.h │ │ │ ├── binary_function.h │ │ │ ├── broadcast_args.h │ │ │ ├── broadcast_to.h │ │ │ ├── ceil.h │ │ │ ├── comparisons.h │ │ │ ├── concatenation.h │ │ │ ├── conv.h │ │ │ ├── cumsum.h │ │ │ ├── depth_to_space.h │ │ │ ├── depthwiseconv_float.h │ │ │ ├── depthwiseconv_uint8.h │ │ │ ├── dequantize.h │ │ │ ├── div.h │ │ │ ├── elu.h │ │ │ ├── exp.h │ │ │ ├── fill.h │ │ │ ├── floor.h │ │ │ ├── floor_div.h │ │ │ ├── floor_mod.h │ │ │ ├── fully_connected.h │ │ │ ├── hard_swish.h │ │ │ ├── integer_ops │ │ │ │ ├── add.h │ │ │ │ ├── conv.h │ │ │ │ ├── depthwise_conv.h │ │ │ │ ├── fully_connected.h │ │ │ │ ├── l2normalization.h │ │ │ │ ├── logistic.h │ │ │ │ ├── mean.h │ │ │ │ ├── mul.h │ │ │ │ ├── pooling.h │ │ │ │ ├── tanh.h │ │ │ │ └── transpose_conv.h │ │ │ ├── l2normalization.h │ │ │ ├── leaky_relu.h │ │ │ ├── log_softmax.h │ │ │ ├── logistic.h │ │ │ ├── lstm_cell.h │ │ │ ├── maximum_minimum.h │ │ │ ├── mul.h │ │ │ ├── neg.h │ │ │ ├── pad.h │ │ │ ├── pooling.h │ │ │ ├── portable_tensor_utils.cpp │ │ │ ├── portable_tensor_utils.h │ │ │ ├── portable_tensor_utils_impl.h │ │ │ ├── prelu.h │ │ │ ├── process_broadcast_shapes.h │ │ │ ├── quantize.h │ │ │ ├── reduce.h │ │ │ ├── requantize.h │ │ │ ├── resize_bilinear.h │ │ │ ├── resize_nearest_neighbor.h │ │ │ ├── round.h │ │ │ ├── select.h │ │ │ ├── slice.h │ │ │ ├── softmax.h │ │ │ ├── space_to_batch_nd.h │ │ │ ├── space_to_depth.h │ │ │ ├── strided_slice.h │ │ │ ├── sub.h │ │ │ ├── tanh.h │ │ │ ├── transpose.h │ │ │ └── transpose_conv.h │ │ ├── runtime_shape.h │ │ ├── strided_slice_logic.h │ │ ├── tensor_ctypes.h │ │ ├── tensor_utils.cpp │ │ └── types.h │ ├── kernel_util.cpp │ ├── kernel_util.h │ ├── op_macros.h │ └── padding.h │ ├── micro │ ├── all_ops_resolver.cpp │ ├── all_ops_resolver.h │ ├── arena_allocator │ │ ├── ibuffer_allocator.h │ │ ├── non_persistent_arena_buffer_allocator.cpp │ │ ├── non_persistent_arena_buffer_allocator.h │ │ ├── persistent_arena_buffer_allocator.cpp │ │ ├── persistent_arena_buffer_allocator.h │ │ ├── recording_single_arena_buffer_allocator.cpp │ │ ├── recording_single_arena_buffer_allocator.h │ │ ├── single_arena_buffer_allocator.cpp │ │ └── single_arena_buffer_allocator.h │ ├── compatibility.h │ ├── debug_log.cpp │ ├── debug_log.h │ ├── fake_micro_context.cpp │ ├── fake_micro_context.h │ ├── flatbuffer_utils.cpp │ ├── flatbuffer_utils.h │ ├── kernels │ │ ├── activation_utils.h │ │ ├── activations.cpp │ │ ├── activations.h │ │ ├── activations_common.cpp │ │ ├── add.h │ │ ├── add_common.cpp │ │ ├── add_n.cpp │ │ ├── arg_min_max.cpp │ │ ├── assign_variable.cpp │ │ ├── batch_to_space_nd.cpp │ │ ├── broadcast_args.cpp │ │ ├── broadcast_to.cpp │ │ ├── call_once.cpp │ │ ├── cast.cpp │ │ ├── ceil.cpp │ │ ├── circular_buffer.cpp │ │ ├── circular_buffer.h │ │ ├── circular_buffer_common.cpp │ │ ├── circular_buffer_flexbuffers_generated_data.h │ │ ├── cmsis_nn │ │ │ ├── add.cpp │ │ │ ├── conv.cpp │ │ │ ├── depthwise_conv.cpp │ │ │ ├── fully_connected.cpp │ │ │ ├── mul.cpp │ │ │ ├── pooling.cpp │ │ │ ├── softmax.cpp │ │ │ └── svdf.cpp │ │ ├── comparisons.cpp │ │ ├── concatenation.cpp │ │ ├── conv.h │ │ ├── conv_common.cpp │ │ ├── conv_test.h │ │ ├── cumsum.cpp │ │ ├── depth_to_space.cpp │ │ ├── depthwise_conv.h │ │ ├── depthwise_conv_common.cpp │ │ ├── dequantize.cpp │ │ ├── dequantize.h │ │ ├── dequantize_common.cpp │ │ ├── detection_postprocess.cpp │ │ ├── detection_postprocess_flexbuffers_generated_data.h │ │ ├── div.cpp │ │ ├── elementwise.cpp │ │ ├── elu.cpp │ │ ├── ethosu.cpp │ │ ├── ethosu.h │ │ ├── exp.cpp │ │ ├── expand_dims.cpp │ │ ├── fill.cpp │ │ ├── floor.cpp │ │ ├── floor_div.cpp │ │ ├── floor_mod.cpp │ │ ├── fully_connected.h │ │ ├── fully_connected_common.cpp │ │ ├── gather.cpp │ │ ├── gather_nd.cpp │ │ ├── hard_swish.cpp │ │ ├── hard_swish.h │ │ ├── hard_swish_common.cpp │ │ ├── if.cpp │ │ ├── kernel_runner.cpp │ │ ├── kernel_runner.h │ │ ├── kernel_util.cpp │ │ ├── kernel_util.h │ │ ├── l2_pool_2d.cpp │ │ ├── l2norm.cpp │ │ ├── leaky_relu.cpp │ │ ├── leaky_relu.h │ │ ├── leaky_relu_common.cpp │ │ ├── log_softmax.cpp │ │ ├── logical.cpp │ │ ├── logical.h │ │ ├── logical_common.cpp │ │ ├── logistic.cpp │ │ ├── logistic.h │ │ ├── logistic_common.cpp │ │ ├── lstm_eval.cpp │ │ ├── lstm_eval.h │ │ ├── lstm_eval_test.h │ │ ├── lstm_shared.h │ │ ├── maximum_minimum.cpp │ │ ├── micro_ops.h │ │ ├── micro_tensor_utils.cpp │ │ ├── micro_tensor_utils.h │ │ ├── mirror_pad.cpp │ │ ├── mul.h │ │ ├── mul_common.cpp │ │ ├── neg.cpp │ │ ├── pack.cpp │ │ ├── pad.cpp │ │ ├── pad.h │ │ ├── pooling.h │ │ ├── pooling_common.cpp │ │ ├── prelu.cpp │ │ ├── prelu.h │ │ ├── prelu_common.cpp │ │ ├── quantize.cpp │ │ ├── quantize.h │ │ ├── quantize_common.cpp │ │ ├── read_variable.cpp │ │ ├── reduce.cpp │ │ ├── reduce.h │ │ ├── reduce_common.cpp │ │ ├── reshape.cpp │ │ ├── resize_bilinear.cpp │ │ ├── resize_nearest_neighbor.cpp │ │ ├── round.cpp │ │ ├── select.cpp │ │ ├── shape.cpp │ │ ├── slice.cpp │ │ ├── softmax.h │ │ ├── softmax_common.cpp │ │ ├── space_to_batch_nd.cpp │ │ ├── space_to_depth.cpp │ │ ├── split.cpp │ │ ├── split_v.cpp │ │ ├── squared_difference.cpp │ │ ├── squeeze.cpp │ │ ├── strided_slice.cpp │ │ ├── sub.cpp │ │ ├── sub.h │ │ ├── sub_common.cpp │ │ ├── svdf.h │ │ ├── svdf_common.cpp │ │ ├── tanh.cpp │ │ ├── transpose.cpp │ │ ├── transpose_conv.cpp │ │ ├── unidirectional_sequence_lstm.cpp │ │ ├── unpack.cpp │ │ ├── var_handle.cpp │ │ ├── while.cpp │ │ └── zeros_like.cpp │ ├── memory_helpers.cpp │ ├── memory_helpers.h │ ├── memory_planner │ │ ├── greedy_memory_planner.cpp │ │ ├── greedy_memory_planner.h │ │ ├── linear_memory_planner.cpp │ │ ├── linear_memory_planner.h │ │ ├── memory_plan_struct.h │ │ ├── micro_memory_planner.h │ │ ├── non_persistent_buffer_planner_shim.cpp │ │ └── non_persistent_buffer_planner_shim.h │ ├── micro_allocation_info.cpp │ ├── micro_allocation_info.h │ ├── micro_allocator.cpp │ ├── micro_allocator.h │ ├── micro_arena_constants.h │ ├── micro_context.cpp │ ├── micro_context.h │ ├── micro_graph.cpp │ ├── micro_graph.h │ ├── micro_interpreter.cpp │ ├── micro_interpreter.h │ ├── micro_log.cpp │ ├── micro_log.h │ ├── micro_mutable_op_resolver.h │ ├── micro_op_resolver.h │ ├── micro_profiler.cpp │ ├── micro_profiler.h │ ├── micro_profiler_interface.h │ ├── micro_resource_variable.cpp │ ├── micro_resource_variable.h │ ├── micro_string.cpp │ ├── micro_string.h │ ├── micro_time.cpp │ ├── micro_time.h │ ├── micro_utils.cpp │ ├── micro_utils.h │ ├── mock_micro_graph.cpp │ ├── mock_micro_graph.h │ ├── recording_micro_allocator.cpp │ ├── recording_micro_allocator.h │ ├── recording_micro_interpreter.h │ ├── system_setup.cpp │ ├── system_setup.h │ ├── test_helper_custom_ops.cpp │ ├── test_helper_custom_ops.h │ ├── test_helpers.cpp │ ├── test_helpers.h │ └── tflite_bridge │ │ ├── flatbuffer_conversions_bridge.cpp │ │ ├── flatbuffer_conversions_bridge.h │ │ ├── micro_error_reporter.cpp │ │ ├── micro_error_reporter.h │ │ ├── op_resolver_bridge.cpp │ │ └── op_resolver_bridge.h │ ├── portable_type_to_tflitetype.h │ └── schema │ ├── schema_generated.h │ ├── schema_utils.cpp │ └── schema_utils.h ├── test_over_serial ├── README.md ├── base64.cpp ├── base64.h ├── test_over_serial.cpp └── test_over_serial.h └── third_party ├── cmsis ├── CMSIS │ └── Core │ │ └── Include │ │ ├── cachel1_armv7.h │ │ ├── cmsis_armcc.h │ │ ├── cmsis_armclang.h │ │ ├── cmsis_armclang_ltm.h │ │ ├── cmsis_compiler.h │ │ ├── cmsis_gcc.h │ │ ├── cmsis_iccarm.h │ │ ├── cmsis_version.h │ │ ├── core_armv81mml.h │ │ ├── core_armv8mbl.h │ │ ├── core_armv8mml.h │ │ ├── core_cm0.h │ │ ├── core_cm0plus.h │ │ ├── core_cm1.h │ │ ├── core_cm23.h │ │ ├── core_cm3.h │ │ ├── core_cm33.h │ │ ├── core_cm35p.h │ │ ├── core_cm4.h │ │ ├── core_cm55.h │ │ ├── core_cm7.h │ │ ├── core_cm85.h │ │ ├── core_sc000.h │ │ ├── core_sc300.h │ │ ├── core_starmc1.h │ │ ├── mpu_armv7.h │ │ ├── mpu_armv8.h │ │ ├── pac_armv81.h │ │ ├── pmu_armv8.h │ │ └── tz_context.h └── LICENSE.txt ├── cmsis_nn ├── Include │ ├── arm_nn_math_types.h │ ├── arm_nn_tables.h │ ├── arm_nn_types.h │ ├── arm_nnfunctions.h │ └── arm_nnsupportfunctions.h ├── LICENSE.txt └── Source │ ├── ActivationFunctions │ ├── arm_relu6_s8.c │ ├── arm_relu_q15.c │ └── arm_relu_q7.c │ ├── BasicMathFunctions │ ├── arm_elementwise_add_s16.c │ ├── arm_elementwise_add_s8.c │ ├── arm_elementwise_mul_s16.c │ └── arm_elementwise_mul_s8.c │ ├── ConcatenationFunctions │ ├── arm_concatenation_s8_w.c │ ├── arm_concatenation_s8_x.c │ ├── arm_concatenation_s8_y.c │ └── arm_concatenation_s8_z.c │ ├── ConvolutionFunctions │ ├── arm_convolve_1_x_n_s8.c │ ├── arm_convolve_1x1_s8_fast.c │ ├── arm_convolve_fast_s16.c │ ├── arm_convolve_s16.c │ ├── arm_convolve_s8.c │ ├── arm_convolve_wrapper_s16.c │ ├── arm_convolve_wrapper_s8.c │ ├── arm_depthwise_conv_3x3_s8.c │ ├── arm_depthwise_conv_fast_s16.c │ ├── arm_depthwise_conv_s16.c │ ├── arm_depthwise_conv_s8.c │ ├── arm_depthwise_conv_s8_opt.c │ ├── arm_depthwise_conv_wrapper_s16.c │ ├── arm_depthwise_conv_wrapper_s8.c │ ├── arm_nn_depthwise_conv_s8_core.c │ ├── arm_nn_mat_mult_kernel_s8_s16.c │ └── arm_nn_mat_mult_s8.c │ ├── FullyConnectedFunctions │ ├── arm_fully_connected_s16.c │ └── arm_fully_connected_s8.c │ ├── NNSupportFunctions │ ├── arm_nn_depthwise_conv_nt_t_padded_s8.c │ ├── arm_nn_depthwise_conv_nt_t_s16.c │ ├── arm_nn_depthwise_conv_nt_t_s8.c │ ├── arm_nn_mat_mul_core_1x_s8.c │ ├── arm_nn_mat_mul_core_4x_s8.c │ ├── arm_nn_mat_mul_kernel_s16.c │ ├── arm_nn_mat_mult_nt_t_s8.c │ ├── arm_nn_vec_mat_mult_t_s16.c │ ├── arm_nn_vec_mat_mult_t_s8.c │ ├── arm_nn_vec_mat_mult_t_svdf_s8.c │ ├── arm_nntables.c │ └── arm_q7_to_q15_with_offset.c │ ├── PoolingFunctions │ ├── arm_avgpool_s16.c │ ├── arm_avgpool_s8.c │ ├── arm_max_pool_s16.c │ └── arm_max_pool_s8.c │ ├── ReshapeFunctions │ └── arm_reshape_s8.c │ ├── SVDFunctions │ ├── arm_svdf_s8.c │ └── arm_svdf_state_s16_s8.c │ └── SoftmaxFunctions │ ├── arm_nn_softmax_common_s8.c │ ├── arm_softmax_s16.c │ ├── arm_softmax_s8.c │ ├── arm_softmax_s8_s16.c │ └── arm_softmax_u8.c ├── flatbuffers ├── LICENSE.txt └── include │ └── flatbuffers │ ├── allocator.h │ ├── array.h │ ├── base.h │ ├── buffer.h │ ├── buffer_ref.h │ ├── default_allocator.h │ ├── detached_buffer.h │ ├── flatbuffer_builder.h │ ├── flatbuffers.h │ ├── flexbuffers.h │ ├── stl_emulation.h │ ├── string.h │ ├── struct.h │ ├── table.h │ ├── util.h │ ├── vector.h │ ├── vector_downward.h │ └── verifier.h ├── gemmlowp ├── LICENSE ├── fixedpoint │ ├── fixedpoint.h │ ├── fixedpoint_neon.h │ └── fixedpoint_sse.h └── internal │ └── detect_platform.h ├── kissfft ├── COPYING ├── _kiss_fft_guts.h ├── kiss_fft.c ├── kiss_fft.h └── tools │ ├── kiss_fftr.c │ └── kiss_fftr.h └── ruy └── ruy └── profiler └── instrumentation.h /.github/mergify.yml: -------------------------------------------------------------------------------- 1 | queue_rules: 2 | - name: default 3 | conditions: 4 | - label=ci:mergify 5 | 6 | 7 | pull_request_rules: 8 | - name: push to default merge queue 9 | conditions: 10 | - base=main 11 | - label=ci:mergify 12 | - check-success=cla/google 13 | actions: 14 | queue: 15 | name: default 16 | require_branch_protection: true 17 | method: squash 18 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # YAML schema for GitHub Actions: 2 | # https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions 3 | # 4 | # Helpful YAML parser to clarify YAML syntax: 5 | # https://yaml-online-parser.appspot.com/ 6 | # 7 | # 8 | # This file contains the workflows that are run prior to merging a pull request. 9 | 10 | name: CI 11 | 12 | on: 13 | pull_request: 14 | types: [labeled] 15 | branches: 16 | - main 17 | 18 | # schedule: 19 | # 10am UTC is 3am or 4am PT depending on daylight savings. 20 | # - cron: '0 10 * * *' 21 | 22 | # Allow manually triggering of the workflow. 23 | workflow_dispatch: {} 24 | 25 | jobs: 26 | arduino: 27 | runs-on: ubuntu-latest 28 | 29 | if: | 30 | github.event_name == 'workflow_dispatch' || 31 | (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'ci:run')) || 32 | (github.event_name == 'schedule' && github.repository == 'tensorflow/tflite-micro-arduino-examples') 33 | 34 | name: Arduino CLI Build 35 | steps: 36 | - uses: actions/checkout@v2 37 | - name: Test 38 | run: | 39 | ./scripts/install_arduino_cli.sh 40 | ./scripts/test_arduino_library.sh "${PWD}" 41 | -------------------------------------------------------------------------------- /.github/workflows/remove-labels.yml: -------------------------------------------------------------------------------- 1 | name: Remove Labels 2 | 3 | on: 4 | pull_request_target: 5 | types: [labeled] 6 | 7 | jobs: 8 | label_cleanup: 9 | runs-on: ubuntu-latest 10 | 11 | name: remove CI runner labels 12 | steps: 13 | - name: remove tags 14 | uses: actions/github-script@a3e7071a34d7e1f219a8a4de9a5e0a34d1ee1293 15 | with: 16 | github-token: ${{secrets.GITHUB_TOKEN}} 17 | script: | 18 | github.issues.removeLabel({ 19 | issue_number: context.issue.number, 20 | owner: context.repo.owner, 21 | repo: context.repo.repo, 22 | name: 'ci:run' 23 | }) 24 | github.issues.removeLabel({ 25 | issue_number: context.issue.number, 26 | owner: context.repo.owner, 27 | repo: context.repo.repo, 28 | name: 'ci:test' 29 | }) 30 | # Prevent erroring out if label doesn't exist 31 | continue-on-error: true 32 | -------------------------------------------------------------------------------- /.github/workflows/sync.yml: -------------------------------------------------------------------------------- 1 | # YAML schema for GitHub Actions: 2 | # https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions 3 | # 4 | # Helpful YAML parser to clarify YAML syntax: 5 | # https://yaml-online-parser.appspot.com/ 6 | # 7 | 8 | name: (Arduino) Sync from tflite-micro 9 | 10 | on: 11 | # schedule: 12 | # 10am UTC is 3am or 4am PT depending on daylight savings. 13 | # - cron: '0 10 * * *' 14 | 15 | # Allow manually triggering of the workflow. 16 | workflow_dispatch: {} 17 | 18 | jobs: 19 | sync: 20 | runs-on: ubuntu-latest 21 | 22 | if: | 23 | github.event_name == 'workflow_dispatch' || 24 | (github.event_name == 'schedule' && github.repository == 'tensorflow/tflite-micro-arduino-examples') 25 | 26 | steps: 27 | - uses: actions/setup-python@v2 28 | - uses: actions/checkout@v2 29 | with: 30 | token: ${{ secrets.TFLM_BOT_REPO_TOKEN }} 31 | 32 | - name: Sync the code 33 | run: | 34 | pip3 install six Pillow Wave 35 | ./scripts/sync_from_tflite_micro.sh 36 | git config --local user.name "TFLM-bot" 37 | git config --local user.email "tflm-github-bot@google.com" 38 | git add * 39 | 40 | if [[ $(git status --porcelain | wc -l) == 0 ]]; then 41 | echo "no changes" 42 | else 43 | git commit -m "Sync from tflite-micro." 44 | fi 45 | 46 | - name: Create Pull Request 47 | id: create-pr 48 | uses: peter-evans/create-pull-request@052fc72b4198ba9fbc81b818c6e1859f747d49a8 49 | with: 50 | branch: sync-from-tflite-micro 51 | delete-branch: true 52 | token: ${{ secrets.TFLM_BOT_REPO_TOKEN }} 53 | title: (Arduino) Automated sync from github.com/tensorflow/tflite-micro 54 | commit-message: Automated sync from github.com/tensorflow/tflite-micro 55 | committer: TFLM-bot 56 | author: TFLM-bot 57 | body: "(Arduino) Automated sync from github.com/tensorflow/tflite-micro" 58 | labels: bot:sync-tf, ci:run 59 | reviewers: advaitjain 60 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | *.swp 3 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # This is the list of Tensorflow's significant contributors. 2 | # 3 | # This does not necessarily list everyone who has contributed code, 4 | # especially since many employees of one corporation may be contributing. 5 | # To see the full list of contributors, see the revision history in 6 | # source control. 7 | 8 | Google LLC 9 | Yuan Tang 10 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @tensorflow/micro @ddavis-2015 2 | 3 | /.github/ @advaitjain 4 | /ci/ @advaitjain 5 | -------------------------------------------------------------------------------- /docs/TMLK_WS_wm8960_images/IMG_1355.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/TMLK_WS_wm8960_images/IMG_1355.JPG -------------------------------------------------------------------------------- /docs/TMLK_WS_wm8960_images/IMG_1356.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/TMLK_WS_wm8960_images/IMG_1356.JPG -------------------------------------------------------------------------------- /docs/TMLK_WS_wm8960_images/IMG_1357.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/TMLK_WS_wm8960_images/IMG_1357.JPG -------------------------------------------------------------------------------- /docs/TMLK_WS_wm8960_images/IMG_1360.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/TMLK_WS_wm8960_images/IMG_1360.JPG -------------------------------------------------------------------------------- /docs/TMLK_WS_wm8960_images/IMG_1361.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/TMLK_WS_wm8960_images/IMG_1361.JPG -------------------------------------------------------------------------------- /docs/TMLK_WS_wm8960_images/IMG_1362.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/TMLK_WS_wm8960_images/IMG_1362.JPG -------------------------------------------------------------------------------- /docs/TMLK_WS_wm8960_images/IMG_1363.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/TMLK_WS_wm8960_images/IMG_1363.JPG -------------------------------------------------------------------------------- /docs/animation_on_arduino.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/animation_on_arduino.gif -------------------------------------------------------------------------------- /docs/hello_world_animation.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/hello_world_animation.gif -------------------------------------------------------------------------------- /docs/hello_world_screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/hello_world_screenshot.png -------------------------------------------------------------------------------- /docs/hello_world_serial_plotter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/hello_world_serial_plotter.png -------------------------------------------------------------------------------- /docs/magic_wand_attachment.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/docs/magic_wand_attachment.jpg -------------------------------------------------------------------------------- /examples/hello_world/arduino_constants.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "constants.h" 17 | 18 | // This is tuned so that a full cycle takes ~6.6 seconds on an 19 | // Arduino Nano 33 BLE. 20 | const int kInferencesPerCycle = 200; 21 | -------------------------------------------------------------------------------- /examples/hello_world/arduino_main.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "main_functions.h" 17 | 18 | // Arduino automatically calls the setup() and loop() functions in a sketch, so 19 | // where other systems need their own main routine in this file, it can be left 20 | // empty. 21 | -------------------------------------------------------------------------------- /examples/hello_world/arduino_output_handler.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include 17 | 18 | #include "Arduino.h" 19 | #include "constants.h" 20 | #include "output_handler.h" 21 | #include "tensorflow/lite/micro/micro_log.h" 22 | 23 | // The pin of the Arduino's built-in LED 24 | int led = LED_BUILTIN; 25 | 26 | // Track whether the function has run at least once 27 | bool initialized = false; 28 | 29 | // Animates a dot across the screen to represent the current x and y values 30 | void HandleOutput(float x_value, float y_value) { 31 | // Do this only once 32 | if (!initialized) { 33 | // Set the LED pin to output 34 | pinMode(led, OUTPUT); 35 | initialized = true; 36 | } 37 | 38 | // Calculate the brightness of the LED such that y=-1 is fully off 39 | // and y=1 is fully on. The LED's brightness can range from 0-255. 40 | int brightness = (int)(127.5f * (y_value + 1)); 41 | 42 | // The y value is not actually constrained to the range [-1, 1], so we need to 43 | // clamp the brightness value before sending it to the PWM/LED. 44 | int brightness_clamped = std::min(255, std::max(0, brightness)); 45 | 46 | // Set the brightness of the LED. If the specified pin does not support PWM, 47 | // this will result in the LED being on when brightness_clamped > 127, off 48 | // otherwise. 49 | analogWrite(led, brightness_clamped); 50 | 51 | // Log the current brightness value for display in the Arduino plotter 52 | MicroPrintf("%d\n", brightness); 53 | delay(33); 54 | } 55 | -------------------------------------------------------------------------------- /examples/hello_world/constants.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_ 18 | 19 | // This constant represents the range of x values our model was trained on, 20 | // which is from 0 to (2 * Pi). We approximate Pi to avoid requiring additional 21 | // libraries. 22 | const float kXrange = 2.f * 3.14159265359f; 23 | 24 | // This constant determines the number of inferences to perform across the range 25 | // of x values defined above. Since each inference takes time, the higher this 26 | // number, the more time it will take to run through the entire range. The value 27 | // of this constant can be tuned so that one full cycle takes a desired amount 28 | // of time. Since different devices take different amounts of time to perform 29 | // inference, this value should be defined per-device. 30 | extern const int kInferencesPerCycle; 31 | 32 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_ 33 | -------------------------------------------------------------------------------- /examples/hello_world/main_functions.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_MAIN_FUNCTIONS_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_MAIN_FUNCTIONS_H_ 18 | 19 | // Expose a C friendly interface for main functions. 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | // Initializes all data needed for the example. The name is important, and needs 25 | // to be setup() for Arduino compatibility. 26 | void setup(); 27 | 28 | // Runs one iteration of data gathering and inference. This should be called 29 | // repeatedly from the application code. The name needs to be loop() for Arduino 30 | // compatibility. 31 | void loop(); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_MAIN_FUNCTIONS_H_ 38 | -------------------------------------------------------------------------------- /examples/hello_world/model.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | // Automatically created from a TensorFlow Lite flatbuffer using the command: 17 | // xxd -i model.tflite > model.cc 18 | 19 | // This is a standard TensorFlow Lite model file that has been converted into a 20 | // C data array, so it can be easily compiled into a binary for devices that 21 | // don't have a file system. 22 | 23 | // See train/README.md for a full description of the creation process. 24 | 25 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_MODEL_H_ 26 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_MODEL_H_ 27 | 28 | extern const unsigned char g_model[]; 29 | extern const int g_model_len; 30 | 31 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_MODEL_H_ 32 | -------------------------------------------------------------------------------- /examples/hello_world/output_handler.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_OUTPUT_HANDLER_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_OUTPUT_HANDLER_H_ 18 | 19 | #include "tensorflow/lite/c/common.h" 20 | 21 | // Called by the main loop to produce some output based on the x and y values 22 | void HandleOutput(float x_value, float y_value); 23 | 24 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_OUTPUT_HANDLER_H_ 25 | -------------------------------------------------------------------------------- /examples/magic_wand/magic_wand_model_data.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 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 | 13 | // This is a standard TensorFlow Lite model file that has been converted into a 14 | // C data array, so it can be easily compiled into a binary for devices that 15 | // don't have a file system. It was created using the command: 16 | // xxd -i magic_wand_model.tflite > magic_wand_model_data.cc 17 | 18 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MAGIC_WAND_MAGIC_WAND_MODEL_DATA_H_ 19 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MAGIC_WAND_MAGIC_WAND_MODEL_DATA_H_ 20 | 21 | extern const unsigned char g_magic_wand_model_data[]; 22 | extern const int g_magic_wand_model_data_len; 23 | 24 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MAGIC_WAND_MAGIC_WAND_MODEL_DATA_H_ 25 | -------------------------------------------------------------------------------- /examples/magic_wand/rasterize_stroke.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 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 | 13 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MAGIC_WAND_RASTERIZE_STROKE_H 14 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MAGIC_WAND_RASTERIZE_STROKE_H 15 | 16 | #include 17 | 18 | void RasterizeStroke(int8_t* stroke_points, int stroke_points_count, 19 | float x_range, float y_range, int width, int height, 20 | int8_t* out_buffer); 21 | 22 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MAGIC_WAND_RASTERIZE_STROKE_H 23 | -------------------------------------------------------------------------------- /examples/micro_speech/arduino_main.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "main_functions.h" 17 | 18 | // Arduino automatically calls the setup() and loop() functions in a sketch, so 19 | // where other systems need their own main routine in this file, it can be left 20 | // empty. 21 | -------------------------------------------------------------------------------- /examples/micro_speech/command_responder.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | // Provides an interface to take an action based on an audio command. 17 | 18 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_COMMAND_RESPONDER_H_ 19 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_COMMAND_RESPONDER_H_ 20 | 21 | #include "tensorflow/lite/c/common.h" 22 | 23 | // Called every time the results of an audio recognition run are available. The 24 | // human-readable name of any recognized command is in the `found_command` 25 | // argument, `score` has the numerical confidence, and `is_new_command` is set 26 | // if the previous command was different to this one. 27 | void RespondToCommand(int32_t current_time, const char* found_command, 28 | uint8_t score, bool is_new_command); 29 | 30 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_COMMAND_RESPONDER_H_ 31 | -------------------------------------------------------------------------------- /examples/micro_speech/data/no_1000ms.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/examples/micro_speech/data/no_1000ms.wav -------------------------------------------------------------------------------- /examples/micro_speech/data/serial_test_config.json: -------------------------------------------------------------------------------- 1 | { 2 | "data type": "audio-pcm-16khz-mono-s16", 3 | "delay after": 4.0, 4 | "test data": [ 5 | { 6 | "file name": "examples/micro_speech/data/no_1000ms.wav", 7 | "label": "no", 8 | "regex": "Heard (.+) \\(\\d+\\) @\\d+ms", 9 | "expr": "groups[1] == label" 10 | }, 11 | { 12 | "file name": "examples/micro_speech/data/yes_1000ms.wav", 13 | "label": "yes", 14 | "regex": "Heard (.+) \\(\\d+\\) @\\d+ms", 15 | "expr": "groups[1] == label" 16 | } 17 | ] 18 | } -------------------------------------------------------------------------------- /examples/micro_speech/data/yes_1000ms.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/examples/micro_speech/data/yes_1000ms.wav -------------------------------------------------------------------------------- /examples/micro_speech/main_functions.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MAIN_FUNCTIONS_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MAIN_FUNCTIONS_H_ 18 | 19 | // Expose a C friendly interface for main functions. 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | // Initializes all data needed for the example. The name is important, and needs 25 | // to be setup() for Arduino compatibility. 26 | void setup(); 27 | 28 | // Runs one iteration of data gathering and inference. This should be called 29 | // repeatedly from the application code. The name needs to be loop() for Arduino 30 | // compatibility. 31 | void loop(); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MAIN_FUNCTIONS_H_ 38 | -------------------------------------------------------------------------------- /examples/micro_speech/micro_features_micro_features_generator.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MICRO_FEATURES_GENERATOR_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MICRO_FEATURES_GENERATOR_H_ 18 | 19 | #include "tensorflow/lite/c/common.h" 20 | 21 | // Sets up any resources needed for the feature generation pipeline. 22 | TfLiteStatus InitializeMicroFeatures(); 23 | 24 | // Converts audio sample data into a more compact form that's appropriate for 25 | // feeding into a neural network. 26 | TfLiteStatus GenerateMicroFeatures(const int16_t* input, int input_size, 27 | int output_size, int8_t* output, 28 | size_t* num_samples_read); 29 | 30 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MICRO_FEATURES_GENERATOR_H_ 31 | -------------------------------------------------------------------------------- /examples/micro_speech/micro_features_micro_model_settings.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "micro_features_micro_model_settings.h" 17 | 18 | const char* kCategoryLabels[kCategoryCount] = { 19 | "silence", 20 | "unknown", 21 | "yes", 22 | "no", 23 | }; 24 | -------------------------------------------------------------------------------- /examples/micro_speech/micro_features_model.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | // This is a standard TensorFlow Lite FlatBuffer model file that has been 17 | // converted into a C data array, so it can be easily compiled into a binary 18 | // for devices that don't have a file system. It was created using the command: 19 | // xxd -i model.tflite > model.cc 20 | 21 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MODEL_H_ 22 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MODEL_H_ 23 | 24 | extern const unsigned char g_model[]; 25 | extern const int g_model_len; 26 | 27 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MODEL_H_ 28 | -------------------------------------------------------------------------------- /examples/person_detection/arduino_main.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "main_functions.h" 17 | 18 | // Arduino automatically calls the setup() and loop() functions in a sketch, so 19 | // where other systems need their own main routine in this file, it can be left 20 | // empty. 21 | -------------------------------------------------------------------------------- /examples/person_detection/data/README.md: -------------------------------------------------------------------------------- 1 | # Attribution information for test images 2 | 3 | | | | 4 | | :--- | :--- | 5 | | File | person.jpg
person_96x96.bmp | 6 | | License | Public Domain | 7 | | Description | Admiral Grace Hopper | 8 | | Link | https://commons.wikimedia.org/wiki/File:Grace_Hopper.jpg | 9 | 10 | | | | 11 | | :--- | :--- | 12 | | File | no_person.jpg
no_person_96x96.bmp | 13 | | License | Public Domain | 14 | | Description | Giraffe at Nairobi National Park, Kenya | 15 | | Link | https://commons.wikimedia.org/wiki/File:Giraffe_at_Nairobi_National_Park,_Kenya.jpg | 16 | -------------------------------------------------------------------------------- /examples/person_detection/data/no_person.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/examples/person_detection/data/no_person.jpg -------------------------------------------------------------------------------- /examples/person_detection/data/no_person_96x96.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/examples/person_detection/data/no_person_96x96.bmp -------------------------------------------------------------------------------- /examples/person_detection/data/person.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/examples/person_detection/data/person.jpg -------------------------------------------------------------------------------- /examples/person_detection/data/person_96x96.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorflow/tflite-micro-arduino-examples/7948b85e020470a30a83bf00e10fc43b486bfcba/examples/person_detection/data/person_96x96.bmp -------------------------------------------------------------------------------- /examples/person_detection/data/serial_test_config.json: -------------------------------------------------------------------------------- 1 | { 2 | "data type": "image-grayscale", 3 | "delay after": 4.0, 4 | "test data": [ 5 | { 6 | "file name": "examples/person_detection/data/person.jpg", 7 | "label": "person", 8 | "regex": "Person score: (\\d+\\.\\d+)% No person score: (\\d+\\.\\d+)%", 9 | "expr": "groups[1] > groups[2]", 10 | "qqvga size": false 11 | }, 12 | { 13 | "file name": "examples/person_detection/data/no_person.jpg", 14 | "label": "no person", 15 | "regex": "Person score: (\\d+\\.\\d+)% No person score: (\\d+\\.\\d+)%", 16 | "expr": "groups[1] < groups[2]", 17 | "qqvga size": false 18 | }, 19 | { 20 | "file name": "examples/person_detection/data/person.jpg", 21 | "label": "person", 22 | "regex": "Person score: (\\d+\\.\\d+)% No person score: (\\d+\\.\\d+)%", 23 | "expr": "groups[1] > groups[2]", 24 | "qqvga size": true 25 | }, 26 | { 27 | "file name": "examples/person_detection/data/no_person.jpg", 28 | "label": "no person", 29 | "regex": "Person score: (\\d+\\.\\d+)% No person score: (\\d+\\.\\d+)%", 30 | "expr": "groups[1] < groups[2]", 31 | "qqvga size": true 32 | }, 33 | { 34 | "file name": "examples/person_detection/data/person_96x96.bmp", 35 | "label": "person", 36 | "regex": "Person score: (\\d+\\.\\d+)% No person score: (\\d+\\.\\d+)%", 37 | "expr": "groups[1] > groups[2]", 38 | "qqvga size": false 39 | }, 40 | { 41 | "file name": "examples/person_detection/data/no_person_96x96.bmp", 42 | "label": "no person", 43 | "regex": "Person score: (\\d+\\.\\d+)% No person score: (\\d+\\.\\d+)%", 44 | "expr": "groups[1] < groups[2]", 45 | "qqvga size": false 46 | } 47 | ] 48 | } -------------------------------------------------------------------------------- /examples/person_detection/detection_responder.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | // Provides an interface to take an action based on the output from the person 17 | // detection model. 18 | 19 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_DETECTION_RESPONDER_H_ 20 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_DETECTION_RESPONDER_H_ 21 | 22 | #include "tensorflow/lite/c/common.h" 23 | 24 | // Called every time the results of a person detection run are available. The 25 | // `person_score` has the numerical confidence that the captured image contains 26 | // a person, and `no_person_score` has the numerical confidence that the image 27 | // does not contain a person. Typically if person_score > no person score, the 28 | // image is considered to contain a person. This threshold may be adjusted for 29 | // particular applications. 30 | void RespondToDetection(float person_score, float no_person_score); 31 | 32 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_DETECTION_RESPONDER_H_ 33 | -------------------------------------------------------------------------------- /examples/person_detection/image_provider.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_IMAGE_PROVIDER_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_IMAGE_PROVIDER_H_ 18 | 19 | #include "tensorflow/lite/c/common.h" 20 | 21 | // This is an abstraction around an image source like a camera, and is 22 | // expected to place 8-bit quantized sample data into the tensor. 23 | // 24 | // The assumption is that this will be 25 | // called in a low duty-cycle fashion in a low-power application. In these 26 | // cases, the imaging sensor need not be run in a streaming mode, but rather can 27 | // be idled in a relatively low-power mode between calls to GetImage(). The 28 | // assumption is that the overhead and time of bringing the low-power sensor out 29 | // of this standby mode is commensurate with the expected duty cycle of the 30 | // application. The underlying sensor may actually be put into a streaming 31 | // configuration, but the tensor provided to GetImage should not be 32 | // overwritten by the driver code until the next call to GetImage(); 33 | // 34 | // For real applications, you should 35 | // ensure there's a specialized implementation that accesses hardware APIs. 36 | TfLiteStatus GetImage(const TfLiteTensor* tensor); 37 | 38 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_IMAGE_PROVIDER_H_ 39 | -------------------------------------------------------------------------------- /examples/person_detection/main_functions.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_MAIN_FUNCTIONS_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_MAIN_FUNCTIONS_H_ 18 | 19 | // Expose a C friendly interface for main functions. 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | // Initializes all data needed for the example. The name is important, and needs 25 | // to be setup() for Arduino compatibility. 26 | void setup(); 27 | 28 | // Runs one iteration of data gathering and inference. This should be called 29 | // repeatedly from the application code. The name needs to be loop() for Arduino 30 | // compatibility. 31 | void loop(); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_MAIN_FUNCTIONS_H_ 38 | -------------------------------------------------------------------------------- /examples/person_detection/model_settings.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "model_settings.h" 17 | 18 | const char* kCategoryLabels[kCategoryCount] = { 19 | "notperson", 20 | "person", 21 | }; 22 | -------------------------------------------------------------------------------- /examples/person_detection/model_settings.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_MODEL_SETTINGS_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_MODEL_SETTINGS_H_ 18 | 19 | // Keeping these as constant expressions allow us to allocate fixed-sized arrays 20 | // on the stack for our working memory. 21 | 22 | // All of these values are derived from the values used during model training, 23 | // if you change your model you'll need to update these constants. 24 | constexpr int kNumCols = 96; 25 | constexpr int kNumRows = 96; 26 | constexpr int kNumChannels = 1; 27 | 28 | constexpr int kMaxImageSize = kNumCols * kNumRows * kNumChannels; 29 | 30 | constexpr int kCategoryCount = 2; 31 | constexpr int kPersonIndex = 1; 32 | constexpr int kNotAPersonIndex = 0; 33 | extern const char* kCategoryLabels[kCategoryCount]; 34 | 35 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_MODEL_SETTINGS_H_ 36 | -------------------------------------------------------------------------------- /examples/person_detection/person_detect_model_data.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | // This is a standard TensorFlow Lite model file that has been converted into a 17 | // C data array, so it can be easily compiled into a binary for devices that 18 | // don't have a file system. It was created using the command: 19 | // xxd -i person_detect.tflite > person_detect_model_data.cc 20 | 21 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_PERSON_DETECT_MODEL_DATA_H_ 22 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_PERSON_DETECT_MODEL_DATA_H_ 23 | 24 | extern const unsigned char g_person_detect_model_data[]; 25 | extern const int g_person_detect_model_data_len; 26 | 27 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_PERSON_DETECTION_PERSON_DETECT_MODEL_DATA_H_ 28 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Arduino_TensorFlowLite 2 | version=2.4.0-ALPHA 3 | author=TensorFlow Authors 4 | maintainer=Pete Warden 5 | sentence=Allows you to run machine learning models locally on your device. 6 | paragraph=This library runs TensorFlow machine learning models on microcontrollers, allowing you to build AI/ML applications powered by deep learning and neural networks. With the included examples, you can recognize speech, detect people using a camera, and recognise "magic wand" gestures using an accelerometer. The examples work best with the Arduino Nano 33 BLE Sense board, which has a microphone and accelerometer. 7 | category=Data Processing 8 | url=https://www.tensorflow.org/lite/microcontrollers/overview 9 | ldflags= 10 | includes=TensorFlowLite.h 11 | precompiled=full 12 | dot_a_linkage=false 13 | depends=ArduinoBLE,Arduino_LSM9DS1 14 | -------------------------------------------------------------------------------- /scripts/helper_functions.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Copyright 2019 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | 17 | 18 | # Collection of helper functions that can be used in the different continuous 19 | # integration scripts. 20 | 21 | # A small utility to run the command and only print logs if the command fails. 22 | # On success, all logs are hidden. This helps to keep the log output clean and 23 | # makes debugging easier. 24 | function readable_run { 25 | "$@" 2>&1 26 | echo "Command completed successfully at $(date)" 27 | } 28 | 29 | # Check if the regex ${1} is to be found in the pathspec ${2}. 30 | # An optional error messsage can be passed with ${3} 31 | function check_contents() { 32 | GREP_OUTPUT=$(git grep -E -rn ${1} -- ${2}) 33 | 34 | if [ "${GREP_OUTPUT}" ]; then 35 | echo "==============================================" 36 | echo "Found matches for ${1} that are not permitted." 37 | echo "${3}" 38 | echo "==============================================" 39 | echo "${GREP_OUTPUT}" 40 | return 1 41 | fi 42 | } 43 | -------------------------------------------------------------------------------- /scripts/install_arduino_cli.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Copyright 2019 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # Installs the latest arduino-cli tool in /tmp/arduino-cli 18 | 19 | set -e 20 | 21 | cd /tmp 22 | 23 | rm -rf arduino-cli* 24 | curl -L -O "https://github.com/arduino/arduino-cli/releases/download/0.19.3/arduino-cli_0.19.3_Linux_64bit.tar.gz" 25 | tar xzf arduino-cli_0.19.3_Linux_64bit.tar.gz 26 | 27 | # To use with MacOS, replace the previous two lines with: 28 | # curl -L -O "https://github.com/arduino/arduino-cli/releases/download/0.19.3/arduino-cli_0.19.3_MacOS_64bit.tar.gz" 29 | # tar xzf arduino-cli_0.19.3_MacOS_64bit.tar.gz 30 | 31 | /tmp/arduino-cli core update-index 32 | /tmp/arduino-cli core install arduino:mbed 33 | -------------------------------------------------------------------------------- /scripts/test_repo_tree.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Copyright 2021 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | # 17 | # Tests the local git repo to make sure the Arduino examples can successfully 18 | # be built using the Arduino CLI. 19 | 20 | set -e 21 | 22 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 23 | ROOT_DIR="${SCRIPT_DIR}/.." 24 | cd "${ROOT_DIR}" 25 | 26 | source "${SCRIPT_DIR}"/helper_functions.sh 27 | 28 | readable_run "${SCRIPT_DIR}"/install_arduino_cli.sh 29 | 30 | # test_arduino_libarary.sh must be passed a normalized path, 31 | # thus the cd ${ROOT_DIR} above is required for ${PWD} here. 32 | readable_run "${SCRIPT_DIR}"/test_arduino_library.sh \ 33 | "${PWD}" 34 | -------------------------------------------------------------------------------- /src/TensorFlowLite.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_TOOLS_MAKE_TEMPLATES_TENSORFLOWLITE_H_ 16 | #define TENSORFLOW_LITE_MICRO_TOOLS_MAKE_TEMPLATES_TENSORFLOWLITE_H_ 17 | 18 | // This header is deliberately empty, and is only present because including it 19 | // in a .ino sketch forces the Arduino toolchain to build the rest of the 20 | // library. 21 | 22 | #include "peripherals/peripherals.h" 23 | 24 | #endif // TENSORFLOW_LITE_MICRO_TOOLS_MAKE_TEMPLATES_TENSORFLOWLITE_H_ 25 | -------------------------------------------------------------------------------- /src/peripherals/audio_codec.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_AUDIO_CODEC_H_ 17 | #define PERIPHERALS_AUDIO_CODEC_H_ 18 | 19 | #include "audio_common.h" 20 | 21 | namespace peripherals { 22 | 23 | class IAudioCodec { 24 | public: 25 | virtual AudioConfiguration GetCurrentConfiguration() const = 0; 26 | virtual bool SetCurrentConfiguration(const AudioConfiguration& config) = 0; 27 | 28 | virtual void Mute(const AudioFunction which, const bool enable) = 0; 29 | virtual void Start(const AudioFunction which) = 0; 30 | virtual void Stop(const AudioFunction which) = 0; 31 | virtual void SetVolume(const AudioFunction which, const float percent) = 0; 32 | 33 | virtual bool HasSampleRate(const AudioFunction which, 34 | const AudioSampleRate rate) const = 0; 35 | virtual bool HasChannelConfig(const AudioChannelConfig channel) const = 0; 36 | virtual bool HasFunction(const AudioFunction which) const = 0; 37 | virtual bool HasSampleWidth(const AudioSampleWidth width) const = 0; 38 | virtual bool HasSimultaneousPlayRecord() const = 0; 39 | virtual bool HasVolume(const AudioFunction which) const = 0; 40 | virtual bool HasMute(const AudioFunction which) const = 0; 41 | }; 42 | 43 | } // namespace peripherals 44 | 45 | #endif // PERIPHERALS_AUDIO_CODEC_H_ 46 | -------------------------------------------------------------------------------- /src/peripherals/audio_common.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_AUDIO_COMMON_H_ 17 | #define PERIPHERALS_AUDIO_COMMON_H_ 18 | 19 | #include 20 | 21 | namespace peripherals { 22 | 23 | enum AudioFunction { kPlay, kRecord }; 24 | 25 | enum AudioChannelConfig { 26 | kStereo, // stereo input to output onto left and right 27 | kMono, // mono input to output duplicated onto left and right 28 | kMonoLeft, // mono input to output onto left only 29 | kMonoRight, // mono input to output onto right only 30 | kMonoLeftRightMix, // stereo input mono-mixed to output onto left and right 31 | }; 32 | 33 | enum AudioSampleRate { 34 | kRate_8000 = 8000, 35 | kRate_11025 = 11025, 36 | kRate_12000 = 12000, 37 | kRate_16000 = 16000, 38 | kRate_22050 = 22050, 39 | kRate_24000 = 24000, 40 | kRate_32000 = 32000, 41 | kRate_44100 = 44100, 42 | kRate_48000 = 48000, 43 | }; 44 | 45 | enum AudioSampleWidth { 46 | kSize_8bit = 8, 47 | kSize_16bit = 16, 48 | kSize_20bit = 20, 49 | kSize_24bit = 24, 50 | kSize_32bit = 32, 51 | }; 52 | 53 | struct AudioConfiguration { 54 | AudioSampleRate play_rate; 55 | AudioSampleRate record_rate; 56 | AudioSampleWidth sample_width; 57 | AudioChannelConfig channel_config; 58 | }; 59 | 60 | using AudioCallback = std::function; 61 | 62 | } // namespace peripherals 63 | 64 | #endif // PERIPHERALS_AUDIO_COMMON_H_ 65 | -------------------------------------------------------------------------------- /src/peripherals/audio_i2s.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_AUDIO_I2S_H_ 17 | #define PERIPHERALS_AUDIO_I2S_H_ 18 | 19 | #include 20 | #include 21 | 22 | #include "audio_common.h" 23 | 24 | namespace peripherals { 25 | 26 | class IAudioI2S { 27 | public: 28 | virtual AudioConfiguration GetCurrentConfiguration() const = 0; 29 | virtual bool SetCurrentConfiguration(const AudioConfiguration& config) = 0; 30 | 31 | virtual void Start(const AudioFunction which) = 0; 32 | virtual void Stop(const AudioFunction which) = 0; 33 | virtual bool HadPlayUnderrun() = 0; 34 | virtual bool HadRecordOverrun() = 0; 35 | 36 | virtual size_t WritePlayBuffer(const void* from, const size_t samples) = 0; 37 | virtual size_t ReadRecordBuffer(void* to, const size_t samples) = 0; 38 | virtual uint64_t SampleCount(const AudioFunction which) = 0; 39 | virtual size_t BufferAvailable(const AudioFunction which) = 0; 40 | 41 | virtual void SetCallbackHandler(const AudioCallback handler) = 0; 42 | }; 43 | 44 | } // namespace peripherals 45 | 46 | #endif // PERIPHERALS_AUDIO_I2S_H_ 47 | -------------------------------------------------------------------------------- /src/peripherals/button.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_BUTTON_H_ 17 | #define PERIPHERALS_BUTTON_H_ 18 | 19 | namespace peripherals { 20 | 21 | enum ButtonState { kUp = 1, KDown = 0 }; 22 | enum ButtonPressState { kNone, kPressed, kLongPressDown, kLongPressUp }; 23 | 24 | class Button { 25 | public: 26 | static Button& Instance(); 27 | 28 | virtual ButtonPressState GetPressState() = 0; 29 | virtual ButtonState GetState() = 0; 30 | 31 | protected: 32 | Button() {} 33 | }; 34 | 35 | } // namespace peripherals 36 | 37 | #endif // PERIPHERALS_BUTTON_H_ -------------------------------------------------------------------------------- /src/peripherals/i2c.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_I2C_H_ 17 | #define PERIPHERALS_I2C_H_ 18 | 19 | #include 20 | 21 | namespace peripherals { 22 | 23 | class I2C { 24 | public: 25 | virtual bool Initialize() = 0; 26 | virtual void Write(uint8_t address, uint16_t value) = 0; 27 | virtual uint16_t Read(uint8_t address) = 0; 28 | }; 29 | 30 | } // namespace peripherals 31 | 32 | #endif // PERIPHERALS_I2C_H_ 33 | -------------------------------------------------------------------------------- /src/peripherals/i2c_arduino.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "i2c_arduino.h" 17 | 18 | #include "peripherals.h" 19 | 20 | namespace peripherals { 21 | 22 | I2C& I2C_Arduino::Instance0() { 23 | static I2C_Arduino i2c_arduino_static_0(Wire); 24 | 25 | return i2c_arduino_static_0; 26 | } 27 | 28 | I2C& I2C_Arduino::Instance1() { 29 | static I2C_Arduino i2c_arduino_static_1(Wire1); 30 | 31 | return i2c_arduino_static_1; 32 | } 33 | 34 | I2C_Arduino::I2C_Arduino(arduino::MbedI2C& channel) 35 | : I2C(), channel_(channel), channel_initialized_(false) {} 36 | 37 | bool I2C_Arduino::Initialize() { 38 | if (!channel_initialized_) { 39 | channel_.begin(); 40 | channel_.setClock(kI2C_CLOCK); 41 | channel_initialized_ = true; 42 | } 43 | return true; 44 | } 45 | 46 | void I2C_Arduino::Write(uint8_t address, uint16_t value) { 47 | if (!channel_initialized_) { 48 | return; 49 | } 50 | 51 | uint8_t result = 0; 52 | do { 53 | channel_.beginTransmission(address); 54 | channel_.write(static_cast(value >> 8)); 55 | channel_.write(static_cast(value & 0xFF)); 56 | result = channel_.endTransmission(); 57 | } while (result != 0); 58 | } 59 | 60 | uint16_t I2C_Arduino::Read(uint8_t address) { 61 | if (!channel_initialized_) { 62 | return 0; 63 | } 64 | 65 | uint16_t value = 0; 66 | channel_.requestFrom(address, sizeof(value)); 67 | value = (channel_.read() << 8) & 0xFF00; 68 | value |= channel_.read() & 0xFF; 69 | return value; 70 | } 71 | 72 | } // namespace peripherals 73 | -------------------------------------------------------------------------------- /src/peripherals/i2c_arduino.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_I2C_ARDUINO_H_ 17 | #define PERIPHERALS_I2C_ARDUINO_H_ 18 | 19 | #include 20 | #include 21 | 22 | #include "i2c.h" 23 | 24 | namespace peripherals { 25 | 26 | class I2C_Arduino : public I2C { 27 | public: 28 | static I2C& Instance0(); 29 | static I2C& Instance1(); 30 | 31 | private: 32 | arduino::MbedI2C& channel_; 33 | bool channel_initialized_; 34 | 35 | I2C_Arduino() = delete; 36 | explicit I2C_Arduino(arduino::MbedI2C& channel); 37 | 38 | bool Initialize() override; 39 | void Write(uint8_t address, uint16_t value) override; 40 | uint16_t Read(uint8_t address) override; 41 | }; 42 | 43 | } // namespace peripherals 44 | 45 | #endif // PERIPHERALS_I2C_ARDUINO_H_ -------------------------------------------------------------------------------- /src/peripherals/led.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_LED_H_ 17 | #define PERIPHERALS_LED_H_ 18 | 19 | #include 20 | 21 | namespace peripherals { 22 | 23 | class LED { 24 | public: 25 | static LED& Instance(); 26 | 27 | virtual void Show(const bool on) = 0; 28 | virtual void Blink() = 0; 29 | virtual void SetBlinkParams(const float duty_on, 30 | const uint16_t cycle_time_ms) { 31 | duty_on_ = duty_on; 32 | if (duty_on_ > 1.0f) { 33 | duty_on_ = 1.0f; 34 | } else if (duty_on < 0.0f) { 35 | duty_on_ = 0.0f; 36 | } 37 | cycle_time_ms_ = cycle_time_ms; 38 | } 39 | 40 | protected: 41 | float duty_on_; 42 | uint16_t cycle_time_ms_; 43 | 44 | LED() : duty_on_{0.5f}, cycle_time_ms_{1000} {} 45 | }; 46 | 47 | } // namespace peripherals 48 | 49 | #endif // PERIPHERALS_LED_H_ -------------------------------------------------------------------------------- /src/peripherals/peripherals.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_H_ 17 | #define PERIPHERALS_H_ 18 | 19 | #ifdef ARDUINO 20 | #include 21 | #include 22 | 23 | // Temporary fix, see buganizer #268498682, arduino-examples issue #169 24 | #undef abs 25 | 26 | #else // ARDUINO 27 | #error "unsupported framework" 28 | #endif // ARDUINO 29 | 30 | #include "utility.h" 31 | 32 | #ifdef ARDUINO 33 | 34 | #if defined(ARDUINO_ARDUINO_NANO33BLE) 35 | #include 36 | 37 | #include "button.h" 38 | #include "led.h" 39 | #include "ws_wm8960_audio_hat_nrf52840.h" 40 | 41 | #define AUDIO_DEVICE_WS_WM8960_AUDIO_HAT \ 42 | &peripherals::WS_WM8960_AudioHat_NRF52840::Instance() 43 | 44 | namespace peripherals { 45 | 46 | constexpr PinName kI2S_BIT_CLK = P0_27; // D9 47 | constexpr PinName kI2S_LR_CLK = P1_2; // D10 48 | constexpr PinName kI2S_DATA_IN = P1_12; // D3 49 | constexpr PinName kI2S_DATA_OUT = P1_11; // D2 50 | constexpr uint32_t kI2S_IRQ_PRIORITY = 7; 51 | 52 | constexpr uint32_t kI2C_CLOCK = 100000; 53 | 54 | constexpr pin_size_t kBUTTON_GPIO = D8; 55 | 56 | constexpr pin_size_t kLED_DEFAULT_GPIO = D13; 57 | 58 | } // namespace peripherals 59 | 60 | #else // ARDUINO_ARDUINO_NANO33BLE 61 | #error "unsupported board" 62 | 63 | #endif // ARDUINO_ARDUINO_NANO33BLE 64 | 65 | #endif // ARDUINO 66 | 67 | #endif // PERIPHERALS_H_ 68 | -------------------------------------------------------------------------------- /src/peripherals/utility.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_UTILITY_H_ 17 | #define PERIPHERALS_UTILITY_H_ 18 | 19 | #include 20 | #include 21 | 22 | namespace peripherals { 23 | 24 | extern void DelayMicroseconds(uint32_t delay); 25 | extern void DelayMilliseconds(uint32_t delay); 26 | 27 | extern uint32_t MicrosecondsCounter(); 28 | extern uint32_t MillisecondsCounter(); 29 | 30 | extern void DebugOutput(const char* s); 31 | 32 | class TimestampBuffer { 33 | public: 34 | static TimestampBuffer& Instance(); 35 | 36 | void Insert(const char c); 37 | void Show(); 38 | 39 | private: 40 | TimestampBuffer(); 41 | 42 | size_t insert_index_; 43 | size_t show_index_; 44 | 45 | static constexpr size_t kNumEntries = 100; 46 | struct { 47 | uint32_t timestamp_us_; 48 | char c_; 49 | } entries_[kNumEntries]; 50 | }; 51 | 52 | } // namespace peripherals 53 | 54 | #endif // PERIPHERALS_UTILITY_H_ -------------------------------------------------------------------------------- /src/peripherals/ws_wm8960_audio_hat_nrf52840.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "ws_wm8960_audio_hat_nrf52840.h" 17 | 18 | #include "i2c_arduino.h" 19 | #include "i2s_nrf52840.h" 20 | #include "wm8960.h" 21 | 22 | namespace peripherals { 23 | 24 | // Composite audio device implementation for: 25 | // WaveShare WM8960 Audio Hat used with nrf52840 SOC 26 | 27 | AudioDevice& WS_WM8960_AudioHat_NRF52840::Instance() { 28 | static WS_WM8960_AudioHat_NRF52840 instance_static; 29 | 30 | return instance_static; 31 | } 32 | 33 | WS_WM8960_AudioHat_NRF52840::WS_WM8960_AudioHat_NRF52840() 34 | : AudioDevice(I2S_nrf52840::Instance(), 35 | WM8960::Instance(I2C_Arduino::Instance0())) {} 36 | 37 | } // namespace peripherals 38 | -------------------------------------------------------------------------------- /src/peripherals/ws_wm8960_audio_hat_nrf52840.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef PERIPHERALS_WS_WM8960_AUDIO_HAT_NRF52840_H_ 17 | #define PERIPHERALS_WS_WM8960_AUDIO_HAT_NRF52840_H_ 18 | 19 | #include "audio_device.h" 20 | 21 | namespace peripherals { 22 | 23 | // Composite audio device implementation for: 24 | // WaveShare WM8960 Audio Hat used with nrf52840 SOC 25 | class WS_WM8960_AudioHat_NRF52840 final : public AudioDevice { 26 | public: 27 | static AudioDevice& Instance(); 28 | 29 | private: 30 | WS_WM8960_AudioHat_NRF52840(); 31 | }; 32 | 33 | } // namespace peripherals 34 | 35 | #endif // PERIPHERALS_WS_WM8960_AUDIO_HAT_NRF52840_H_ 36 | -------------------------------------------------------------------------------- /src/tensorflow/lite/builtin_op_data.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | // Compatibility shim for new location of interface definitions. 16 | 17 | #ifndef TENSORFLOW_LITE_BUILTIN_OP_DATA_H_ 18 | #define TENSORFLOW_LITE_BUILTIN_OP_DATA_H_ 19 | 20 | #include "tensorflow/lite/core/c/builtin_op_data.h" 21 | 22 | #endif // TENSORFLOW_LITE_BUILTIN_OP_DATA_H_ 23 | -------------------------------------------------------------------------------- /src/tensorflow/lite/c/builtin_op_data.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_ 16 | #define TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_ 17 | 18 | #include "tensorflow/lite/core/c/builtin_op_data.h" 19 | 20 | #endif // TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_ 21 | -------------------------------------------------------------------------------- /src/tensorflow/lite/c/c_api_types.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_C_C_API_TYPES_H_ 16 | #define TENSORFLOW_LITE_C_C_API_TYPES_H_ 17 | 18 | #include "tensorflow/lite/core/c/c_api_types.h" 19 | 20 | #endif // TENSORFLOW_LITE_C_C_API_TYPES_H_ 21 | -------------------------------------------------------------------------------- /src/tensorflow/lite/context_util.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | /// \file 16 | /// 17 | /// This provides a few C++ helpers that are useful for manipulating C 18 | /// structures in C++. 19 | #ifndef TENSORFLOW_LITE_CONTEXT_UTIL_H_ 20 | #define TENSORFLOW_LITE_CONTEXT_UTIL_H_ 21 | 22 | #include 23 | 24 | #include "tensorflow/lite/core/c/common.h" 25 | 26 | namespace tflite { 27 | 28 | /// Provides a range iterable wrapper for TfLiteIntArray* (C lists) that TfLite 29 | /// C api uses. 30 | // Can't use the google array_view, since we can't depend on even 31 | // absl for embedded device reasons. 32 | class TfLiteIntArrayView { 33 | public: 34 | /// Construct a view of a TfLiteIntArray*. Note, `int_array` should be 35 | /// non-null and this view does not take ownership of it. 36 | explicit TfLiteIntArrayView(const TfLiteIntArray* int_array) 37 | : int_array_(int_array) {} 38 | 39 | TfLiteIntArrayView(const TfLiteIntArrayView&) = default; 40 | TfLiteIntArrayView& operator=(const TfLiteIntArrayView& rhs) = default; 41 | 42 | typedef const int* const_iterator; 43 | const_iterator begin() const { return int_array_->data; } 44 | const_iterator end() const { return &int_array_->data[int_array_->size]; } 45 | size_t size() const { return end() - begin(); } 46 | int operator[](size_t pos) const { return int_array_->data[pos]; } 47 | 48 | private: 49 | const TfLiteIntArray* int_array_; 50 | }; 51 | 52 | } // namespace tflite 53 | 54 | #endif // TENSORFLOW_LITE_CONTEXT_UTIL_H_ 55 | -------------------------------------------------------------------------------- /src/tensorflow/lite/core/api/error_reporter.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #include "tensorflow/lite/core/api/error_reporter.h" 16 | #include 17 | 18 | namespace tflite { 19 | 20 | int ErrorReporter::Report(const char* format, ...) { 21 | va_list args; 22 | va_start(args, format); 23 | int code = Report(format, args); 24 | va_end(args); 25 | return code; 26 | } 27 | 28 | // TODO(aselle): Make the name of ReportError on context the same, so 29 | // we can use the ensure functions w/o a context and w/ a reporter. 30 | int ErrorReporter::ReportError(void*, const char* format, ...) { 31 | va_list args; 32 | va_start(args, format); 33 | int code = Report(format, args); 34 | va_end(args); 35 | return code; 36 | } 37 | 38 | } // namespace tflite 39 | -------------------------------------------------------------------------------- /src/tensorflow/lite/core/api/tensor_utils.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "tensorflow/lite/core/api/tensor_utils.h" 17 | 18 | #include 19 | 20 | #include "tensorflow/lite/core/c/common.h" 21 | 22 | namespace tflite { 23 | 24 | TfLiteStatus ResetVariableTensor(TfLiteTensor* tensor) { 25 | if (!tensor->is_variable) { 26 | return kTfLiteOk; 27 | } 28 | // TODO(b/115961645): Implement - If a variable tensor has a buffer, reset it 29 | // to the value of the buffer. 30 | int value = 0; 31 | if (tensor->type == kTfLiteInt8) { 32 | value = tensor->params.zero_point; 33 | } 34 | // TODO(b/139446230): Provide a platform header to better handle these 35 | // specific scenarios. 36 | #if __ANDROID__ || defined(__x86_64__) || defined(__i386__) || \ 37 | defined(__i386) || defined(__x86__) || defined(__X86__) || \ 38 | defined(_X86_) || defined(_M_IX86) || defined(_M_X64) 39 | memset(tensor->data.raw, value, tensor->bytes); 40 | #else 41 | char* raw_ptr = tensor->data.raw; 42 | for (size_t i = 0; i < tensor->bytes; ++i) { 43 | *raw_ptr = value; 44 | raw_ptr++; 45 | } 46 | #endif 47 | return kTfLiteOk; 48 | } 49 | 50 | } // namespace tflite 51 | -------------------------------------------------------------------------------- /src/tensorflow/lite/core/api/tensor_utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_CORE_API_TENSOR_UTILS_H_ 17 | #define TENSORFLOW_LITE_CORE_API_TENSOR_UTILS_H_ 18 | 19 | #include "tensorflow/lite/core/c/common.h" 20 | 21 | namespace tflite { 22 | 23 | // Resets a variable tensor to the default value. 24 | TfLiteStatus ResetVariableTensor(TfLiteTensor* tensor); 25 | 26 | } // namespace tflite 27 | 28 | #endif // TENSORFLOW_LITE_CORE_API_TENSOR_UTILS_H_ 29 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/fft.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #include "tensorflow/lite/experimental/microfrontend/lib/fft.h" 16 | 17 | #include 18 | 19 | #include "tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.h" 20 | 21 | void FftCompute(struct FftState* state, const int16_t* input, 22 | int input_scale_shift) { 23 | const size_t input_size = state->input_size; 24 | const size_t fft_size = state->fft_size; 25 | 26 | int16_t* fft_input = state->input; 27 | // First, scale the input by the given shift. 28 | size_t i; 29 | for (i = 0; i < input_size; ++i) { 30 | fft_input[i] = static_cast(static_cast(input[i]) 31 | << input_scale_shift); 32 | } 33 | // Zero out whatever else remains in the top part of the input. 34 | for (; i < fft_size; ++i) { 35 | fft_input[i] = 0; 36 | } 37 | 38 | // Apply the FFT. 39 | kissfft_fixed16::kiss_fftr( 40 | reinterpret_cast(state->scratch), 41 | state->input, 42 | reinterpret_cast(state->output)); 43 | } 44 | 45 | void FftInit(struct FftState* state) { 46 | // All the initialization is done in FftPopulateState() 47 | } 48 | 49 | void FftReset(struct FftState* state) { 50 | memset(state->input, 0, state->fft_size * sizeof(*state->input)); 51 | memset(state->output, 0, (state->fft_size / 2 + 1) * sizeof(*state->output)); 52 | } 53 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/fft.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_H_ 17 | 18 | #include 19 | #include 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | struct complex_int16_t { 26 | int16_t real; 27 | int16_t imag; 28 | }; 29 | 30 | struct FftState { 31 | int16_t* input; 32 | struct complex_int16_t* output; 33 | size_t fft_size; 34 | size_t input_size; 35 | void* scratch; 36 | size_t scratch_size; 37 | }; 38 | 39 | void FftCompute(struct FftState* state, const int16_t* input, 40 | int input_scale_shift); 41 | 42 | void FftInit(struct FftState* state); 43 | 44 | void FftReset(struct FftState* state); 45 | 46 | #ifdef __cplusplus 47 | } // extern "C" 48 | #endif 49 | 50 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_H_ 51 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/fft_util.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_UTIL_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_UTIL_H_ 17 | 18 | #include "tensorflow/lite/experimental/microfrontend/lib/fft.h" 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | // Prepares and FFT for the given input size. 25 | int FftPopulateState(struct FftState* state, size_t input_size); 26 | 27 | // Frees any allocated buffers. 28 | void FftFreeStateContents(struct FftState* state); 29 | 30 | #ifdef __cplusplus 31 | } // extern "C" 32 | #endif 33 | 34 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FFT_UTIL_H_ 35 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/filterbank_util.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_UTIL_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_UTIL_H_ 17 | 18 | #include "tensorflow/lite/experimental/microfrontend/lib/filterbank.h" 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | struct FilterbankConfig { 25 | // number of frequency channel buckets for filterbank 26 | int num_channels; 27 | // maximum frequency to include 28 | float upper_band_limit; 29 | // minimum frequency to include 30 | float lower_band_limit; 31 | // unused 32 | int output_scale_shift; 33 | }; 34 | 35 | // Fills the frontendConfig with "sane" defaults. 36 | void FilterbankFillConfigWithDefaults(struct FilterbankConfig* config); 37 | 38 | // Allocates any buffers. 39 | int FilterbankPopulateState(const struct FilterbankConfig* config, 40 | struct FilterbankState* state, int sample_rate, 41 | int spectrum_size); 42 | 43 | // Frees any allocated buffers. 44 | void FilterbankFreeStateContents(struct FilterbankState* state); 45 | 46 | #ifdef __cplusplus 47 | } // extern "C" 48 | #endif 49 | 50 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_UTIL_H_ 51 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_common.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_COMMON_H_ 17 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_COMMON_H_ 18 | 19 | // This header file should be included in all variants of kiss_fft_$type.{h,cc} 20 | // so that their sub-included source files do not mistakenly wrap libc header 21 | // files within their kissfft_$type namespaces. 22 | // E.g, This header avoids kissfft_int16.h containing: 23 | // namespace kiss_fft_int16 { 24 | // #include "third_party/kissfft/kiss_fft.h" 25 | // } 26 | // where kiss_fft_.h contains: 27 | // #include 28 | // 29 | // TRICK: By including the following header files here, their preprocessor 30 | // header guards prevent them being re-defined inside of the kiss_fft_$type 31 | // namespaces declared within the kiss_fft_$type.{h,cc} sources. 32 | // Note that the original kiss_fft*.h files are untouched since they 33 | // may be used in libraries that include them directly. 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | #ifdef FIXED_POINT 42 | #include 43 | #endif 44 | 45 | #ifdef USE_SIMD 46 | #include 47 | #endif 48 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_COMMON_H_ 49 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "tensorflow/lite/experimental/microfrontend/lib/kiss_fft_common.h" 4 | 5 | #define FIXED_POINT 16 6 | namespace kissfft_fixed16 { 7 | #include "third_party/kissfft/kiss_fft.c" 8 | #include "third_party/kissfft/tools/kiss_fftr.c" 9 | } // namespace kissfft_fixed16 10 | #undef FIXED_POINT 11 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/kiss_fft_int16.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_INT16_H_ 17 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_INT16_H_ 18 | 19 | #include "tensorflow/lite/experimental/microfrontend/lib/kiss_fft_common.h" 20 | 21 | // Wrap 16-bit kiss fft in its own namespace. Enables us to link an application 22 | // with different kiss fft resultions (16/32 bit interger, float, double) 23 | // without getting a linker error. 24 | #define FIXED_POINT 16 25 | namespace kissfft_fixed16 { 26 | #include "third_party/kissfft/kiss_fft.h" 27 | #include "third_party/kissfft/tools/kiss_fftr.h" 28 | } // namespace kissfft_fixed16 29 | #undef FIXED_POINT 30 | #undef kiss_fft_scalar 31 | #undef KISS_FFT_H 32 | 33 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_KISS_FFT_INT16_H_ 34 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/log_lut.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #include "tensorflow/lite/experimental/microfrontend/lib/log_lut.h" 16 | const uint16_t kLogLut[] 17 | #ifndef _MSC_VER 18 | __attribute__((aligned(4))) 19 | #endif // _MSV_VER 20 | = {0, 224, 442, 654, 861, 1063, 1259, 1450, 1636, 1817, 1992, 2163, 21 | 2329, 2490, 2646, 2797, 2944, 3087, 3224, 3358, 3487, 3611, 3732, 3848, 22 | 3960, 4068, 4172, 4272, 4368, 4460, 4549, 4633, 4714, 4791, 4864, 4934, 23 | 5001, 5063, 5123, 5178, 5231, 5280, 5326, 5368, 5408, 5444, 5477, 5507, 24 | 5533, 5557, 5578, 5595, 5610, 5622, 5631, 5637, 5640, 5641, 5638, 5633, 25 | 5626, 5615, 5602, 5586, 5568, 5547, 5524, 5498, 5470, 5439, 5406, 5370, 26 | 5332, 5291, 5249, 5203, 5156, 5106, 5054, 5000, 4944, 4885, 4825, 4762, 27 | 4697, 4630, 4561, 4490, 4416, 4341, 4264, 4184, 4103, 4020, 3935, 3848, 28 | 3759, 3668, 3575, 3481, 3384, 3286, 3186, 3084, 2981, 2875, 2768, 2659, 29 | 2549, 2437, 2323, 2207, 2090, 1971, 1851, 1729, 1605, 1480, 1353, 1224, 30 | 1094, 963, 830, 695, 559, 421, 282, 142, 0, 0}; 31 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/log_lut.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_LUT_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_LUT_H_ 17 | 18 | #include 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | // Number of segments in the log lookup table. The table will be kLogSegments+1 25 | // in length (with some padding). 26 | #define kLogSegments 128 27 | #define kLogSegmentsLog2 7 28 | 29 | // Scale used by lookup table. 30 | #define kLogScale 65536 31 | #define kLogScaleLog2 16 32 | #define kLogCoeff 45426 33 | 34 | extern const uint16_t kLogLut[]; 35 | 36 | #ifdef __cplusplus 37 | } // extern "C" 38 | #endif 39 | 40 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_LUT_H_ 41 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/log_scale.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_H_ 17 | 18 | #include 19 | #include 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | struct LogScaleState { 26 | int enable_log; 27 | int scale_shift; 28 | }; 29 | 30 | // Applies a fixed point logarithm to the signal and converts it to 16 bit. Note 31 | // that the signal array will be modified. 32 | uint16_t* LogScaleApply(struct LogScaleState* state, uint32_t* signal, 33 | int signal_size, int correction_bits); 34 | 35 | #ifdef __cplusplus 36 | } // extern "C" 37 | #endif 38 | 39 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_H_ 40 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/log_scale_util.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #include "tensorflow/lite/experimental/microfrontend/lib/log_scale_util.h" 16 | 17 | void LogScaleFillConfigWithDefaults(struct LogScaleConfig* config) { 18 | config->enable_log = 1; 19 | config->scale_shift = 6; 20 | } 21 | 22 | int LogScalePopulateState(const struct LogScaleConfig* config, 23 | struct LogScaleState* state) { 24 | state->enable_log = config->enable_log; 25 | state->scale_shift = config->scale_shift; 26 | return 1; 27 | } 28 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/log_scale_util.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_UTIL_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_UTIL_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include "tensorflow/lite/experimental/microfrontend/lib/log_scale.h" 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | struct LogScaleConfig { 28 | // set to false (0) to disable this module 29 | int enable_log; 30 | // scale results by 2^(scale_shift) 31 | int scale_shift; 32 | }; 33 | 34 | // Populates the LogScaleConfig with "sane" default values. 35 | void LogScaleFillConfigWithDefaults(struct LogScaleConfig* config); 36 | 37 | // Allocates any buffers. 38 | int LogScalePopulateState(const struct LogScaleConfig* config, 39 | struct LogScaleState* state); 40 | 41 | #ifdef __cplusplus 42 | } // extern "C" 43 | #endif 44 | 45 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_LOG_SCALE_UTIL_H_ 46 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_H_ 17 | 18 | #define kNoiseReductionBits 14 19 | 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | struct NoiseReductionState { 28 | int smoothing_bits; 29 | uint16_t even_smoothing; 30 | uint16_t odd_smoothing; 31 | uint16_t min_signal_remaining; 32 | int num_channels; 33 | uint32_t* estimate; 34 | }; 35 | 36 | // Removes stationary noise from each channel of the signal using a low pass 37 | // filter. 38 | void NoiseReductionApply(struct NoiseReductionState* state, uint32_t* signal); 39 | 40 | void NoiseReductionReset(struct NoiseReductionState* state); 41 | 42 | #ifdef __cplusplus 43 | } // extern "C" 44 | #endif 45 | 46 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_H_ 47 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h" 16 | 17 | #include 18 | 19 | void NoiseReductionFillConfigWithDefaults(struct NoiseReductionConfig* config) { 20 | config->smoothing_bits = 10; 21 | config->even_smoothing = 0.025; 22 | config->odd_smoothing = 0.06; 23 | config->min_signal_remaining = 0.05; 24 | } 25 | 26 | int NoiseReductionPopulateState(const struct NoiseReductionConfig* config, 27 | struct NoiseReductionState* state, 28 | int num_channels) { 29 | state->smoothing_bits = config->smoothing_bits; 30 | state->odd_smoothing = config->odd_smoothing * (1 << kNoiseReductionBits); 31 | state->even_smoothing = config->even_smoothing * (1 << kNoiseReductionBits); 32 | state->min_signal_remaining = 33 | config->min_signal_remaining * (1 << kNoiseReductionBits); 34 | state->num_channels = num_channels; 35 | state->estimate = calloc(state->num_channels, sizeof(*state->estimate)); 36 | if (state->estimate == NULL) { 37 | fprintf(stderr, "Failed to alloc estimate buffer\n"); 38 | return 0; 39 | } 40 | return 1; 41 | } 42 | 43 | void NoiseReductionFreeStateContents(struct NoiseReductionState* state) { 44 | free(state->estimate); 45 | } 46 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_UTIL_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_UTIL_H_ 17 | 18 | #include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h" 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | struct NoiseReductionConfig { 25 | // scale the signal up by 2^(smoothing_bits) before reduction 26 | int smoothing_bits; 27 | // smoothing coefficient for even-numbered channels 28 | float even_smoothing; 29 | // smoothing coefficient for odd-numbered channels 30 | float odd_smoothing; 31 | // fraction of signal to preserve (1.0 disables this module) 32 | float min_signal_remaining; 33 | }; 34 | 35 | // Populates the NoiseReductionConfig with "sane" default values. 36 | void NoiseReductionFillConfigWithDefaults(struct NoiseReductionConfig* config); 37 | 38 | // Allocates any buffers. 39 | int NoiseReductionPopulateState(const struct NoiseReductionConfig* config, 40 | struct NoiseReductionState* state, 41 | int num_channels); 42 | 43 | // Frees any allocated buffers. 44 | void NoiseReductionFreeStateContents(struct NoiseReductionState* state); 45 | 46 | #ifdef __cplusplus 47 | } // extern "C" 48 | #endif 49 | 50 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_NOISE_REDUCTION_UTIL_H_ 51 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h" 16 | 17 | #include "tensorflow/lite/experimental/microfrontend/lib/bits.h" 18 | 19 | int16_t WideDynamicFunction(const uint32_t x, const int16_t* lut) { 20 | if (x <= 2) { 21 | return lut[x]; 22 | } 23 | 24 | const int16_t interval = MostSignificantBit32(x); 25 | lut += 4 * interval - 6; 26 | 27 | const int16_t frac = 28 | ((interval < 11) ? (x << (11 - interval)) : (x >> (interval - 11))) & 29 | 0x3FF; 30 | 31 | int32_t result = ((int32_t)lut[2] * frac) >> 5; 32 | result += (int32_t)((uint32_t)lut[1] << 5); 33 | result *= frac; 34 | result = (result + (1 << 14)) >> 15; 35 | result += lut[0]; 36 | return (int16_t)result; 37 | } 38 | 39 | uint32_t PcanShrink(const uint32_t x) { 40 | if (x < (2 << kPcanSnrBits)) { 41 | return (x * x) >> (2 + 2 * kPcanSnrBits - kPcanOutputBits); 42 | } else { 43 | return (x >> (kPcanSnrBits - kPcanOutputBits)) - (1 << kPcanOutputBits); 44 | } 45 | } 46 | 47 | void PcanGainControlApply(struct PcanGainControlState* state, 48 | uint32_t* signal) { 49 | int i; 50 | for (i = 0; i < state->num_channels; ++i) { 51 | const uint32_t gain = 52 | WideDynamicFunction(state->noise_estimate[i], state->gain_lut); 53 | const uint32_t snr = ((uint64_t)signal[i] * gain) >> state->snr_shift; 54 | signal[i] = PcanShrink(snr); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_H_ 17 | 18 | #include 19 | #include 20 | 21 | #define kPcanSnrBits 12 22 | #define kPcanOutputBits 6 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | // Details at https://research.google/pubs/pub45911.pdf 29 | struct PcanGainControlState { 30 | int enable_pcan; 31 | uint32_t* noise_estimate; 32 | int num_channels; 33 | int16_t* gain_lut; 34 | int32_t snr_shift; 35 | }; 36 | 37 | int16_t WideDynamicFunction(const uint32_t x, const int16_t* lut); 38 | 39 | uint32_t PcanShrink(const uint32_t x); 40 | 41 | void PcanGainControlApply(struct PcanGainControlState* state, uint32_t* signal); 42 | 43 | #ifdef __cplusplus 44 | } // extern "C" 45 | #endif 46 | 47 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_H_ 48 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/window.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_H_ 17 | 18 | #include 19 | #include 20 | 21 | #define kFrontendWindowBits 12 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | struct WindowState { 28 | size_t size; 29 | int16_t* coefficients; 30 | size_t step; 31 | 32 | int16_t* input; 33 | size_t input_used; 34 | int16_t* output; 35 | int16_t max_abs_output_value; 36 | }; 37 | 38 | // Applies a window to the samples coming in, stepping forward at the given 39 | // rate. 40 | int WindowProcessSamples(struct WindowState* state, const int16_t* samples, 41 | size_t num_samples, size_t* num_samples_read); 42 | 43 | void WindowReset(struct WindowState* state); 44 | 45 | #ifdef __cplusplus 46 | } // extern "C" 47 | #endif 48 | 49 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_H_ 50 | -------------------------------------------------------------------------------- /src/tensorflow/lite/experimental/microfrontend/lib/window_util.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_UTIL_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_UTIL_H_ 17 | 18 | #include "tensorflow/lite/experimental/microfrontend/lib/window.h" 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | struct WindowConfig { 25 | // length of window frame in milliseconds 26 | size_t size_ms; 27 | // length of step for next frame in milliseconds 28 | size_t step_size_ms; 29 | }; 30 | 31 | // Populates the WindowConfig with "sane" default values. 32 | void WindowFillConfigWithDefaults(struct WindowConfig* config); 33 | 34 | // Allocates any buffers. 35 | int WindowPopulateState(const struct WindowConfig* config, 36 | struct WindowState* state, int sample_rate); 37 | 38 | // Frees any allocated buffers. 39 | void WindowFreeStateContents(struct WindowState* state); 40 | 41 | #ifdef __cplusplus 42 | } // extern "C" 43 | #endif 44 | 45 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_WINDOW_UTIL_H_ 46 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/cppmath.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_ 17 | 18 | #include 19 | 20 | namespace tflite { 21 | 22 | #if defined(ARDUINO) || \ 23 | (defined(__ANDROID__) && !defined(__NDK_MAJOR__)) || defined(__ZEPHYR__) 24 | #define TF_LITE_GLOBAL_STD_PREFIX 25 | #else 26 | #define TF_LITE_GLOBAL_STD_PREFIX std 27 | #endif 28 | 29 | #define DECLARE_STD_GLOBAL_SWITCH1(tf_name, std_name) \ 30 | template \ 31 | inline T tf_name(const T x) { \ 32 | return TF_LITE_GLOBAL_STD_PREFIX::std_name(x); \ 33 | } 34 | 35 | DECLARE_STD_GLOBAL_SWITCH1(TfLiteRound, round); 36 | DECLARE_STD_GLOBAL_SWITCH1(TfLiteExpm1, expm1); 37 | 38 | } // namespace tflite 39 | 40 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_ 41 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/max.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ 17 | 18 | #include 19 | 20 | namespace tflite { 21 | 22 | #if defined(TF_LITE_USE_GLOBAL_MAX) || defined(__ZEPHYR__) 23 | inline float TfLiteMax(const float& x, const float& y) { 24 | return std::max(x, y); 25 | } 26 | #else 27 | template 28 | inline T TfLiteMax(const T& x, const T& y) { 29 | return std::fmax(x, y); 30 | } 31 | #endif 32 | 33 | } // namespace tflite 34 | 35 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ 36 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/min.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ 17 | 18 | #include 19 | 20 | namespace tflite { 21 | 22 | #if defined(TF_LITE_USE_GLOBAL_MIN) || defined(__ZEPHYR__) 23 | inline float TfLiteMin(const float& x, const float& y) { 24 | return std::min(x, y); 25 | } 26 | #else 27 | template 28 | inline T TfLiteMin(const T& x, const T& y) { 29 | return std::fmin(x, y); 30 | } 31 | #endif 32 | 33 | } // namespace tflite 34 | 35 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ 36 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/optimized/neon_check.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ 17 | 18 | // TFLM does not need to utilize any Neon optimizations. 19 | 20 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ 21 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/reference/ceil.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CEIL_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CEIL_H_ 17 | 18 | #include 19 | 20 | #include "tensorflow/lite/kernels/internal/types.h" 21 | 22 | namespace tflite { 23 | 24 | namespace reference_ops { 25 | 26 | inline void Ceil(const RuntimeShape& input_shape, const float* input_data, 27 | const RuntimeShape& output_shape, float* output_data) { 28 | const int flat_size = MatchingFlatSize(input_shape, output_shape); 29 | 30 | for (int i = 0; i < flat_size; ++i) { 31 | output_data[i] = std::ceil(input_data[i]); 32 | } 33 | } 34 | 35 | } // namespace reference_ops 36 | } // namespace tflite 37 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CEIL_H_ 38 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/reference/elu.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ELU_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ELU_H_ 17 | 18 | #include "tensorflow/lite/kernels/internal/cppmath.h" 19 | #include "tensorflow/lite/kernels/internal/types.h" 20 | 21 | namespace tflite { 22 | 23 | namespace reference_ops { 24 | 25 | inline void Elu(const RuntimeShape& input_shape, const float* input_data, 26 | const RuntimeShape& output_shape, float* output_data) { 27 | const int flat_size = MatchingFlatSize(input_shape, output_shape); 28 | for (int i = 0; i < flat_size; ++i) { 29 | const float val = input_data[i]; 30 | output_data[i] = val < 0.0f ? TfLiteExpm1(val) : val; 31 | } 32 | } 33 | 34 | } // namespace reference_ops 35 | } // namespace tflite 36 | 37 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ELU_H_ 38 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/reference/exp.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_EXP_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_EXP_H_ 17 | 18 | #include 19 | 20 | #include "third_party/ruy/ruy/profiler/instrumentation.h" 21 | #include "tensorflow/lite/kernels/internal/types.h" 22 | 23 | namespace tflite { 24 | namespace reference_ops { 25 | 26 | template 27 | inline void Exp(const T* input_data, const size_t num_elements, 28 | T* output_data) { 29 | ruy::profiler::ScopeLabel label("Exp"); 30 | for (size_t idx = 0; idx < num_elements; ++idx) { 31 | output_data[idx] = std::exp(input_data[idx]); 32 | } 33 | } 34 | 35 | } // namespace reference_ops 36 | } // namespace tflite 37 | 38 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_EXP_H_ 39 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/reference/fill.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FILL_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FILL_H_ 17 | 18 | #include 19 | 20 | #include "tensorflow/lite/kernels/internal/types.h" 21 | 22 | namespace tflite { 23 | namespace reference_ops { 24 | 25 | template 26 | void Fill(const RuntimeShape& value_shape, const T* value_data, 27 | const RuntimeShape& output_shape, T* output_data) { 28 | TFLITE_DCHECK_EQ(value_shape.DimensionsCount(), 0); 29 | const int flat_size = output_shape.FlatSize(); 30 | for (int i = 0; i < flat_size; ++i) { 31 | output_data[i] = *value_data; 32 | } 33 | } 34 | 35 | } // namespace reference_ops 36 | } // namespace tflite 37 | 38 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FILL_H_ 39 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/reference/floor.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_H_ 17 | 18 | #include 19 | 20 | #include "tensorflow/lite/kernels/internal/types.h" 21 | 22 | namespace tflite { 23 | 24 | namespace reference_ops { 25 | 26 | inline void Floor(const RuntimeShape& input_shape, const float* input_data, 27 | const RuntimeShape& output_shape, float* output_data) { 28 | const int flat_size = MatchingFlatSize(input_shape, output_shape); 29 | 30 | for (int i = 0; i < flat_size; i++) { 31 | int offset = i; 32 | output_data[offset] = std::floor(input_data[offset]); 33 | } 34 | } 35 | 36 | } // namespace reference_ops 37 | } // namespace tflite 38 | 39 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_H_ 40 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/reference/floor_div.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_DIV_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_DIV_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include "tensorflow/lite/kernels/internal/types.h" 22 | 23 | namespace tflite { 24 | namespace reference_ops { 25 | 26 | template 27 | T FloorDiv(T input1, T input2) { 28 | return std::floor(std::divides()(static_cast(input1), 29 | static_cast(input2))); 30 | } 31 | 32 | } // namespace reference_ops 33 | } // namespace tflite 34 | 35 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_DIV_H_ 36 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/reference/floor_mod.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_MOD_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_MOD_H_ 17 | 18 | #include 19 | #include 20 | 21 | namespace tflite { 22 | 23 | namespace reference_ops { 24 | 25 | template 26 | T FloorMod(T input1, T input2) { 27 | struct FloatMod { 28 | float operator()(const float lhs, const float rhs) const { 29 | return std::fmod(lhs, rhs); 30 | } 31 | }; 32 | using ModFunc = typename std::conditional::value, 33 | std::modulus, FloatMod>::type; 34 | ModFunc mod_func; 35 | T trunc_mod = mod_func(input1, input2); 36 | return (trunc_mod != 0) && ((input2 < 0) != (trunc_mod < 0)) 37 | ? (trunc_mod + input2) 38 | : trunc_mod; 39 | } 40 | 41 | } // namespace reference_ops 42 | } // namespace tflite 43 | 44 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_MOD_H_ 45 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/reference/neg.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_NEG_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_NEG_H_ 17 | 18 | #include "tensorflow/lite/kernels/internal/types.h" 19 | 20 | namespace tflite { 21 | 22 | namespace reference_ops { 23 | 24 | template 25 | inline void Negate(const RuntimeShape& input_shape, const T* input_data, 26 | const RuntimeShape& output_shape, T* output_data) { 27 | const int flat_size = MatchingFlatSize(input_shape, output_shape); 28 | 29 | for (int i = 0; i < flat_size; ++i) { 30 | output_data[i] = -input_data[i]; 31 | } 32 | } 33 | 34 | } // namespace reference_ops 35 | } // namespace tflite 36 | 37 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_NEG_H_ 38 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/reference/round.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ROUND_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ROUND_H_ 17 | 18 | #include 19 | 20 | #include "tensorflow/lite/kernels/internal/types.h" 21 | 22 | namespace tflite { 23 | 24 | namespace reference_ops { 25 | 26 | inline float RoundToNearest(float value) { 27 | auto floor_val = std::floor(value); 28 | auto diff = value - floor_val; 29 | if ((diff < 0.5f) || 30 | ((diff == 0.5f) && (static_cast(floor_val) % 2 == 0))) { 31 | return floor_val; 32 | } else { 33 | return floor_val = floor_val + 1.0f; 34 | } 35 | } 36 | 37 | inline void Round(const RuntimeShape& input_shape, const float* input_data, 38 | const RuntimeShape& output_shape, float* output_data) { 39 | const int flat_size = MatchingFlatSize(input_shape, output_shape); 40 | for (int i = 0; i < flat_size; ++i) { 41 | // Note that this implementation matches that of tensorFlow tf.round 42 | // and corresponds to the bankers rounding method. 43 | // cfenv (for fesetround) is not yet supported universally on Android, so 44 | // using a work around. 45 | output_data[i] = RoundToNearest(input_data[i]); 46 | } 47 | } 48 | 49 | } // namespace reference_ops 50 | } // namespace tflite 51 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ROUND_H_ 52 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/tensor_ctypes.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_CTYPES_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_CTYPES_H_ 17 | 18 | #include "tensorflow/lite/core/c/common.h" 19 | #include "tensorflow/lite/kernels/internal/types.h" 20 | 21 | namespace tflite { 22 | 23 | template 24 | inline T* GetTensorData(TfLiteTensor* tensor) { 25 | return tensor != nullptr ? reinterpret_cast(tensor->data.raw) : nullptr; 26 | } 27 | 28 | template 29 | inline const T* GetTensorData(const TfLiteTensor* tensor) { 30 | return tensor != nullptr ? reinterpret_cast(tensor->data.raw) 31 | : nullptr; 32 | } 33 | 34 | inline RuntimeShape GetTensorShape(const TfLiteTensor* tensor) { 35 | if (tensor == nullptr) { 36 | return RuntimeShape(); 37 | } 38 | 39 | TfLiteIntArray* dims = tensor->dims; 40 | const int dims_size = dims->size; 41 | const int32_t* dims_data = reinterpret_cast(dims->data); 42 | return RuntimeShape(dims_size, dims_data); 43 | } 44 | 45 | } // namespace tflite 46 | 47 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_CTYPES_H_ 48 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/internal/tensor_utils.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ============================================================================== 15 | */ 16 | 17 | // internal/reference/portable_tensor_utils.h has the implementation of the 18 | // functions declared in internal/portable_tensor_utils.h. This somewhat 19 | // confusing setup is derived from how the code is organized in TfLite where it 20 | // is used to select between NEON, SSE and portable implementaitons. See 21 | // https://github.com/tensorflow/tensorflow/blob/d76c23975c4a3a0d7987cfe3f45c76566df06180/tensorflow/lite/kernels/internal/tensor_utils.cc 22 | // for how the code is written in TfLite. 23 | 24 | #include "tensorflow/lite/kernels/internal/portable_tensor_utils.h" 25 | #include "tensorflow/lite/kernels/internal/reference/portable_tensor_utils.h" 26 | -------------------------------------------------------------------------------- /src/tensorflow/lite/kernels/op_macros.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_KERNELS_OP_MACROS_H_ 16 | #define TENSORFLOW_LITE_KERNELS_OP_MACROS_H_ 17 | 18 | #include "tensorflow/lite/micro/debug_log.h" 19 | 20 | #if !defined(TF_LITE_MCU_DEBUG_LOG) 21 | #include 22 | #define TFLITE_ABORT abort() 23 | #else 24 | inline void AbortImpl() { 25 | DebugLog("HALTED\n"); 26 | while (1) { 27 | } 28 | } 29 | #define TFLITE_ABORT AbortImpl(); 30 | #endif 31 | 32 | #if defined(ARDUINO) 33 | #define TFLITE_ASSERT_FALSE (static_cast(0)) 34 | #else 35 | #define TFLITE_ASSERT_FALSE TFLITE_ABORT 36 | #endif 37 | 38 | #endif // TENSORFLOW_LITE_KERNELS_OP_MACROS_H_ 39 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/all_ops_resolver.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_ALL_OPS_RESOLVER_H_ 16 | #define TENSORFLOW_LITE_MICRO_ALL_OPS_RESOLVER_H_ 17 | 18 | #include "tensorflow/lite/micro/compatibility.h" 19 | #include "tensorflow/lite/micro/micro_mutable_op_resolver.h" 20 | 21 | namespace tflite { 22 | 23 | // The magic number in the template parameter is the maximum number of ops that 24 | // can be added to AllOpsResolver. It can be increased if needed. And most 25 | // applications that care about the memory footprint will want to directly use 26 | // MicroMutableOpResolver and have an application specific template parameter. 27 | // The examples directory has sample code for this. 28 | class AllOpsResolver : public MicroMutableOpResolver<128> { 29 | public: 30 | AllOpsResolver(); 31 | 32 | private: 33 | TF_LITE_REMOVE_VIRTUAL_DELETE 34 | }; 35 | 36 | } // namespace tflite 37 | 38 | #endif // TENSORFLOW_LITE_MICRO_ALL_OPS_RESOLVER_H_ 39 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #include "tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator.h" 16 | 17 | #include "tensorflow/lite/micro/memory_helpers.h" 18 | #include "tensorflow/lite/micro/micro_log.h" 19 | 20 | namespace tflite { 21 | 22 | PersistentArenaBufferAllocator::PersistentArenaBufferAllocator( 23 | uint8_t* buffer, size_t buffer_size) 24 | : buffer_head_(buffer), 25 | buffer_tail_(buffer + buffer_size), 26 | tail_temp_(buffer_tail_) {} 27 | 28 | PersistentArenaBufferAllocator::~PersistentArenaBufferAllocator() {} 29 | 30 | uint8_t* PersistentArenaBufferAllocator::AllocatePersistentBuffer( 31 | size_t size, size_t alignment) { 32 | uint8_t* const aligned_result = 33 | AlignPointerDown(tail_temp_ - size, alignment); 34 | if (aligned_result < buffer_head_) { 35 | #ifndef TF_LITE_STRIP_ERROR_STRINGS 36 | const size_t missing_memory = buffer_head_ - aligned_result; 37 | MicroPrintf( 38 | "Failed to allocate tail memory. Requested: %u, " 39 | "available %u, missing: %u", 40 | size, size - missing_memory, missing_memory); 41 | #endif 42 | return nullptr; 43 | } 44 | tail_temp_ = aligned_result; 45 | return aligned_result; 46 | } 47 | 48 | size_t PersistentArenaBufferAllocator::GetPersistentUsedBytes() const { 49 | return buffer_tail_ - tail_temp_; 50 | } 51 | 52 | } // namespace tflite 53 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/compatibility.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_COMPATIBILITY_H_ 16 | #define TENSORFLOW_LITE_MICRO_COMPATIBILITY_H_ 17 | 18 | // C++ will automatically create class-specific delete operators for virtual 19 | // objects, which by default call the global delete function. For embedded 20 | // applications we want to avoid this, and won't be calling new/delete on these 21 | // objects, so we need to override the default implementation with one that does 22 | // nothing to avoid linking in ::delete(). 23 | // This macro needs to be included in all subclasses of a virtual base class in 24 | // the private section. 25 | #ifdef ARDUINO 26 | #define TF_LITE_REMOVE_VIRTUAL_DELETE \ 27 | void operator delete(void* p) {} 28 | #else 29 | #define TF_LITE_REMOVE_VIRTUAL_DELETE 30 | #endif 31 | 32 | #endif // TENSORFLOW_LITE_MICRO_COMPATIBILITY_H_ 33 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/debug_log.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | // This file is empty to ensure that a specialized implementation of 16 | // debug_log.h is used (instead of the default implementation from 17 | // tensorflow/lite/micro/debug_log.cc). 18 | // 19 | // The actual target-specific implementation of debug_log.h is in 20 | // system_setup.cc since that allows us to consolidate all the target-specific 21 | // specializations into one source file. 22 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/debug_log.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_DEBUG_LOG_H_ 16 | #define TENSORFLOW_LITE_MICRO_DEBUG_LOG_H_ 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif // __cplusplus 21 | 22 | // This function should be implemented by each target platform, and provide a 23 | // way for strings to be output to some text stream. For more information, see 24 | // tensorflow/lite/micro/debug_log.cc. 25 | void DebugLog(const char* s); 26 | 27 | #ifdef __cplusplus 28 | } // extern "C" 29 | #endif // __cplusplus 30 | 31 | #endif // TENSORFLOW_LITE_MICRO_DEBUG_LOG_H_ 32 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/fake_micro_context.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_FAKE_MICRO_CONTEXT_H_ 17 | #define TENSORFLOW_LITE_MICRO_FAKE_MICRO_CONTEXT_H_ 18 | 19 | #include "tensorflow/lite/micro/micro_context.h" 20 | #include "tensorflow/lite/micro/micro_graph.h" 21 | 22 | namespace tflite { 23 | // A fake of MicroContext for kernel util tests. 24 | class FakeMicroContext : public MicroContext { 25 | public: 26 | FakeMicroContext(TfLiteTensor* tensors, SingleArenaBufferAllocator* allocator, 27 | MicroGraph* micro_graph); 28 | 29 | void* AllocatePersistentBuffer(size_t bytes) override; 30 | TfLiteStatus RequestScratchBufferInArena(size_t bytes, 31 | int* buffer_index) override; 32 | void* GetScratchBuffer(int buffer_index) override; 33 | 34 | TfLiteTensor* AllocateTempTfLiteTensor(int tensor_index) override; 35 | void DeallocateTempTfLiteTensor(TfLiteTensor* tensor) override; 36 | bool IsAllTempTfLiteTensorDeallocated(); 37 | 38 | TfLiteEvalTensor* GetEvalTensor(int tensor_index) override; 39 | 40 | private: 41 | static constexpr int kNumScratchBuffers_ = 12; 42 | 43 | int scratch_buffer_count_ = 0; 44 | uint8_t* scratch_buffers_[kNumScratchBuffers_]; 45 | 46 | TfLiteTensor* tensors_; 47 | int allocated_tensor_count_ = 0; 48 | 49 | SingleArenaBufferAllocator* allocator_; 50 | 51 | TF_LITE_REMOVE_VIRTUAL_DELETE 52 | }; 53 | 54 | } // namespace tflite 55 | 56 | #endif // TENSORFLOW_LITE_MICRO_FAKE_MICRO_CONTEXT_H_ 57 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/activation_utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_ 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_ 18 | 19 | #include 20 | #include 21 | 22 | #include "tensorflow/lite/c/builtin_op_data.h" 23 | #include "tensorflow/lite/kernels/internal/cppmath.h" 24 | #include "tensorflow/lite/kernels/internal/max.h" 25 | #include "tensorflow/lite/kernels/internal/min.h" 26 | 27 | namespace tflite { 28 | namespace ops { 29 | namespace micro { 30 | 31 | // Returns the floating point value for a fused activation: 32 | inline float ActivationValFloat(TfLiteFusedActivation act, float a) { 33 | switch (act) { 34 | case kTfLiteActNone: 35 | return a; 36 | case kTfLiteActRelu: 37 | return TfLiteMax(0.0f, a); 38 | case kTfLiteActReluN1To1: 39 | return TfLiteMax(-1.0f, TfLiteMin(a, 1.0f)); 40 | case kTfLiteActRelu6: 41 | return TfLiteMax(0.0f, TfLiteMin(a, 6.0f)); 42 | case kTfLiteActTanh: 43 | return std::tanh(a); 44 | case kTfLiteActSignBit: 45 | return std::signbit(a); 46 | case kTfLiteActSigmoid: 47 | return 1.0f / (1.0f + std::exp(-a)); 48 | } 49 | return 0.0f; // To indicate an unsupported activation (i.e. when a new fused 50 | // activation is added to the enum and not handled here). 51 | } 52 | 53 | } // namespace micro 54 | } // namespace ops 55 | } // namespace tflite 56 | 57 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_ 58 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/circular_buffer.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_CIRCULAR_BUFFER_H_ 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_CIRCULAR_BUFFER_H_ 18 | 19 | #include "tensorflow/lite/c/builtin_op_data.h" 20 | #include "tensorflow/lite/c/common.h" 21 | 22 | namespace tflite { 23 | 24 | // The CircularBuffer op has one input and one output tensor. 25 | extern const int kCircularBufferInputTensor; 26 | extern const int kCircularBufferOutputTensor; 27 | 28 | // Indices into the init flexbuffer's vector. 29 | // The parameter's name is in the comment that follows. 30 | // Elements in the vectors are ordered alphabetically by parameter name. 31 | extern const int kCircularBufferCyclesMaxIndex; // 'cycles_max' 32 | 33 | // TODO(b/149795762): Add this to TfLiteStatus enum. 34 | extern const TfLiteStatus kTfLiteAbort; 35 | 36 | // These fields control the stride period of a strided streaming model. This op 37 | // returns kTfLiteAbort until cycles_until_run-- is zero. At this time, 38 | // cycles_until_run is reset to cycles_max. 39 | struct OpDataCircularBuffer { 40 | int cycles_until_run; 41 | int cycles_max; 42 | }; 43 | 44 | TfLiteStatus CircularBufferPrepare(TfLiteContext* context, TfLiteNode* node); 45 | 46 | } // namespace tflite 47 | 48 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_CIRCULAR_BUFFER_H_ 49 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/circular_buffer_flexbuffers_generated_data.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_FLEXBUFFERS_GENERATED_DATA_H 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_FLEXBUFFERS_GENERATED_DATA_H 18 | 19 | extern const int g_gen_data_size_circular_buffer_config; 20 | extern const unsigned char g_gen_data_circular_buffer_config[]; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/dequantize.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_DEQUANTIZE_H_ 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_DEQUANTIZE_H_ 18 | 19 | #include "tensorflow/lite/c/builtin_op_data.h" 20 | #include "tensorflow/lite/c/common.h" 21 | #include "tensorflow/lite/kernels/internal/types.h" 22 | 23 | namespace tflite { 24 | 25 | struct DequantizeOpData { 26 | tflite::DequantizationParams quantization_params; 27 | // The scaling factor from input to output (aka the 'real multiplier') can 28 | // be represented as a fixed point multiplier plus a left shift. 29 | int32_t output_multiplier; 30 | int output_shift; 31 | int32_t output_zero_point; 32 | }; 33 | 34 | TfLiteStatus DequantizePrepare(TfLiteContext* context, TfLiteNode* node); 35 | 36 | } // namespace tflite 37 | 38 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_DEQUANTIZE_H_ 39 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/detection_postprocess_flexbuffers_generated_data.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_FLEXBUFFERS_GENERATED_DATA_H 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_FLEXBUFFERS_GENERATED_DATA_H 18 | 19 | extern const int g_gen_data_size_none_regular_nms; 20 | extern const unsigned char g_gen_data_none_regular_nms[]; 21 | 22 | extern const int g_gen_data_size_regular_nms; 23 | extern const unsigned char g_gen_data_regular_nms[]; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/ethosu.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | // 17 | // This is a stub file for non-Ethos platforms 18 | // 19 | #include "tensorflow/lite/c/common.h" 20 | 21 | namespace tflite { 22 | 23 | TfLiteRegistration* Register_ETHOSU() { return nullptr; } 24 | 25 | const char* GetString_ETHOSU() { return ""; } 26 | 27 | } // namespace tflite 28 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/ethosu.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_ETHOSU_H_ 16 | #define TENSORFLOW_LITE_MICRO_KERNELS_ETHOSU_H_ 17 | 18 | #include "tensorflow/lite/c/common.h" 19 | 20 | namespace tflite { 21 | 22 | TfLiteRegistration* Register_ETHOSU(); 23 | 24 | const char* GetString_ETHOSU(); 25 | 26 | } // namespace tflite 27 | 28 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_ETHOSU_H_ 29 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/floor.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "tensorflow/lite/kernels/internal/reference/floor.h" 17 | 18 | #include "tensorflow/lite/c/common.h" 19 | #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" 20 | #include "tensorflow/lite/micro/kernels/kernel_util.h" 21 | 22 | namespace tflite { 23 | 24 | namespace { 25 | 26 | constexpr int kInputTensor = 0; 27 | constexpr int kOutputTensor = 0; 28 | 29 | TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { 30 | const TfLiteEvalTensor* input = 31 | tflite::micro::GetEvalInput(context, node, kInputTensor); 32 | TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); 33 | TfLiteEvalTensor* output = 34 | tflite::micro::GetEvalOutput(context, node, kOutputTensor); 35 | reference_ops::Floor(tflite::micro::GetTensorShape(input), 36 | tflite::micro::GetTensorData(input), 37 | tflite::micro::GetTensorShape(output), 38 | tflite::micro::GetTensorData(output)); 39 | return kTfLiteOk; 40 | } 41 | 42 | } // namespace 43 | 44 | TfLiteRegistration Register_FLOOR() { 45 | return tflite::micro::RegisterOp(nullptr, nullptr, Eval); 46 | } 47 | 48 | } // namespace tflite 49 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/hard_swish.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_HARD_SWISH_H_ 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_HARD_SWISH_H_ 18 | 19 | #include "tensorflow/lite/c/builtin_op_data.h" 20 | #include "tensorflow/lite/c/common.h" 21 | 22 | namespace tflite { 23 | 24 | extern const int kHardSwishInputTensor; 25 | extern const int kHardSwishOutputTensor; 26 | 27 | TfLiteStatus HardSwishPrepare(TfLiteContext* context, TfLiteNode* node); 28 | } // namespace tflite 29 | 30 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_HARD_SWISH_H_ 31 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/leaky_relu.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_LEAKY_RELU_H_ 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_LEAKY_RELU_H_ 18 | 19 | #include "tensorflow/lite/c/common.h" 20 | 21 | namespace tflite { 22 | 23 | // Input/output tensor index. 24 | extern const int kInputTensor; 25 | extern const int kOutputTensor; 26 | 27 | struct LeakyReluOpData { 28 | // quantization parameters 29 | int32_t output_multiplier_alpha; 30 | int32_t output_shift_alpha; 31 | int32_t output_multiplier_identity; 32 | int32_t output_shift_identity; 33 | int32_t input_zero_point; 34 | int32_t output_zero_point; 35 | }; 36 | 37 | TfLiteStatus CalculateOpDataLeakyRelu(TfLiteContext* context, TfLiteNode* node); 38 | 39 | TfLiteStatus LeakyReluPrepare(TfLiteContext* context, TfLiteNode* node); 40 | 41 | } // namespace tflite 42 | 43 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_LEAKY_RELU_H_ 44 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/logical.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #include "tensorflow/lite/micro/kernels/logical.h" 16 | 17 | #include "tensorflow/lite/c/common.h" 18 | #include "tensorflow/lite/kernels/internal/reference/binary_function.h" 19 | #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" 20 | #include "tensorflow/lite/kernels/op_macros.h" 21 | #include "tensorflow/lite/micro/kernels/kernel_util.h" 22 | 23 | namespace tflite { 24 | namespace { 25 | 26 | TfLiteStatus LogicalOrEval(TfLiteContext* context, TfLiteNode* node) { 27 | return LogicalImpl(context, node, LogicalOr); 28 | } 29 | 30 | TfLiteStatus LogicalAndEval(TfLiteContext* context, TfLiteNode* node) { 31 | return LogicalImpl(context, node, LogicalAnd); 32 | } 33 | 34 | } // namespace 35 | 36 | TfLiteRegistration Register_LOGICAL_OR() { 37 | return tflite::micro::RegisterOp(nullptr, nullptr, LogicalOrEval); 38 | } 39 | 40 | TfLiteRegistration Register_LOGICAL_AND() { 41 | return tflite::micro::RegisterOp(nullptr, nullptr, LogicalAndEval); 42 | } 43 | 44 | } // namespace tflite 45 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/logical.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_LOGICAL_H_ 16 | #define TENSORFLOW_LITE_MICRO_KERNELS_LOGICAL_H_ 17 | 18 | #include "tensorflow/lite/c/builtin_op_data.h" 19 | #include "tensorflow/lite/c/common.h" 20 | 21 | namespace tflite { 22 | // Input/output tensor index. 23 | extern const int kLogicalInputTensor1; 24 | extern const int kLogicalInputTensor2; 25 | extern const int kLogicalOutputTensor; 26 | 27 | TfLiteStatus LogicalImpl(TfLiteContext* context, TfLiteNode* node, 28 | bool (*func)(bool, bool)); 29 | 30 | bool LogicalOr(bool x, bool y); 31 | bool LogicalAnd(bool x, bool y); 32 | 33 | } // namespace tflite 34 | 35 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_LOGICAL_H_ 36 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/logistic.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_LOGISTIC_H_ 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_LOGISTIC_H_ 18 | 19 | #include 20 | 21 | #include "tensorflow/lite/c/builtin_op_data.h" 22 | #include "tensorflow/lite/c/common.h" 23 | 24 | namespace tflite { 25 | extern const int kLogisticInputTensor; 26 | extern const int kLogisticOutputTensor; 27 | 28 | struct OpDataLogistic { 29 | int32_t input_zero_point; 30 | int32_t input_range_radius; 31 | int32_t input_multiplier; 32 | int input_left_shift; 33 | }; 34 | 35 | TfLiteStatus CalculateArithmeticOpDataLogistic(TfLiteContext* context, 36 | TfLiteNode* node, 37 | OpDataLogistic* data); 38 | 39 | TfLiteStatus LogisticPrepare(TfLiteContext* context, TfLiteNode* node); 40 | 41 | } // namespace tflite 42 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_LOGISTIC_H_ 43 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/pad.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_PAD_H_ 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_PAD_H_ 18 | 19 | #include "tensorflow/lite/c/common.h" 20 | 21 | namespace tflite { 22 | 23 | TfLiteStatus PadPrepare(TfLiteContext* context, TfLiteNode* node); 24 | 25 | } // namespace tflite 26 | 27 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_PAD_H_ 28 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/prelu.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_PRELU_H_ 17 | #define TENSORFLOW_LITE_MICRO_KERNELS_PRELU_H_ 18 | 19 | #include "tensorflow/lite/c/common.h" 20 | #include "tensorflow/lite/kernels/internal/types.h" 21 | 22 | namespace tflite { 23 | 24 | TfLiteStatus CalculatePreluParams(const TfLiteTensor* input, 25 | const TfLiteTensor* alpha, 26 | TfLiteTensor* output, PreluParams* params); 27 | 28 | void BroadcastPrelu4DSlowFloat(const RuntimeShape& unextended_input1_shape, 29 | const float* input1_data, 30 | const RuntimeShape& unextended_input2_shape, 31 | const float* input2_data, 32 | const RuntimeShape& unextended_output_shape, 33 | float* output_data); 34 | 35 | TfLiteStatus PreluPrepare(TfLiteContext* context, TfLiteNode* node); 36 | 37 | } // namespace tflite 38 | 39 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_PRELU_H_ 40 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/quantize.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "tensorflow/lite/micro/kernels/quantize.h" 17 | 18 | #include "tensorflow/lite/c/common.h" 19 | #include "tensorflow/lite/kernels/internal/quantization_util.h" 20 | #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" 21 | #include "tensorflow/lite/kernels/kernel_util.h" 22 | #include "tensorflow/lite/micro/kernels/kernel_util.h" 23 | #include "tensorflow/lite/micro/micro_utils.h" 24 | 25 | namespace tflite { 26 | namespace { 27 | 28 | void* Init(TfLiteContext* context, const char* buffer, size_t length) { 29 | TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); 30 | return context->AllocatePersistentBuffer(context, 31 | sizeof(OpDataQuantizeReference)); 32 | } 33 | 34 | } // namespace 35 | 36 | TfLiteRegistration Register_QUANTIZE() { 37 | return tflite::micro::RegisterOp(Init, PrepareQuantizeReference, 38 | EvalQuantizeReference); 39 | } 40 | 41 | } // namespace tflite 42 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/kernels/quantize.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_QUANTIZE_H_ 16 | #define TENSORFLOW_LITE_MICRO_KERNELS_QUANTIZE_H_ 17 | 18 | #include "tensorflow/lite/c/common.h" 19 | #include "tensorflow/lite/kernels/internal/types.h" 20 | 21 | namespace tflite { 22 | 23 | struct OpDataQuantizeReference { 24 | tflite::QuantizationParams quantization_params; 25 | // The scaling factor from input to output (aka the 'real multiplier') can 26 | // be represented as a fixed point multiplier plus a left shift. 27 | int32_t requantize_output_multiplier; 28 | int requantize_output_shift; 29 | 30 | int32_t input_zero_point; 31 | }; 32 | 33 | TfLiteStatus EvalQuantizeReference(TfLiteContext* context, TfLiteNode* node); 34 | TfLiteStatus PrepareQuantizeReference(TfLiteContext* context, TfLiteNode* node); 35 | } // namespace tflite 36 | 37 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_QUANTIZE_H_ 38 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/memory_planner/linear_memory_planner.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_LINEAR_MEMORY_PLANNER_H_ 17 | #define TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_LINEAR_MEMORY_PLANNER_H_ 18 | 19 | #include "tensorflow/lite/micro/compatibility.h" 20 | #include "tensorflow/lite/micro/memory_planner/micro_memory_planner.h" 21 | 22 | namespace tflite { 23 | 24 | // The simplest possible memory planner that just lays out all buffers at 25 | // increasing offsets without trying to reuse memory. 26 | class LinearMemoryPlanner : public MicroMemoryPlanner { 27 | public: 28 | LinearMemoryPlanner(); 29 | ~LinearMemoryPlanner() override; 30 | 31 | TfLiteStatus AddBuffer(int size, int first_time_used, 32 | int last_time_used) override; 33 | 34 | size_t GetMaximumMemorySize() override; 35 | int GetBufferCount() override; 36 | TfLiteStatus GetOffsetForBuffer(int buffer_index, int* offset) override; 37 | 38 | private: 39 | static constexpr int kMaxBufferCount = 1024; 40 | size_t buffer_offsets_[kMaxBufferCount]; 41 | int current_buffer_count_; 42 | size_t next_free_offset_; 43 | 44 | TF_LITE_REMOVE_VIRTUAL_DELETE 45 | }; 46 | 47 | } // namespace tflite 48 | 49 | #endif // TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_LINEAR_MEMORY_PLANNER_H_ 50 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/micro_arena_constants.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_MICRO_ARENA_CONSTANTS_H_ 17 | #define TENSORFLOW_LITE_MICRO_MICRO_ARENA_CONSTANTS_H_ 18 | 19 | namespace tflite { 20 | 21 | // The default buffer alignment requirement. 22 | // We align tensor buffers to 16-byte boundaries, since this is a common 23 | // requirement for SIMD extensions. 24 | constexpr int MicroArenaBufferAlignment() { return 16; } 25 | 26 | } // namespace tflite 27 | 28 | #endif // TENSORFLOW_LITE_MICRO_MICRO_ARENA_CONSTANTS_H_ 29 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/micro_log.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "tensorflow/lite/micro/micro_log.h" 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #if !defined(TF_LITE_STRIP_ERROR_STRINGS) 23 | #include "tensorflow/lite/micro/debug_log.h" 24 | #include "tensorflow/lite/micro/micro_string.h" 25 | #endif 26 | 27 | void Log(const char* format, va_list args) { 28 | #if !defined(TF_LITE_STRIP_ERROR_STRINGS) 29 | // Only pulling in the implementation of this function for builds where we 30 | // expect to make use of it to be extra cautious about not increasing the code 31 | // size. 32 | static constexpr int kMaxLogLen = 256; 33 | char log_buffer[kMaxLogLen]; 34 | MicroVsnprintf(log_buffer, kMaxLogLen, format, args); 35 | DebugLog(log_buffer); 36 | DebugLog("\r\n"); 37 | #endif 38 | } 39 | 40 | #if !defined(TF_LITE_STRIP_ERROR_STRINGS) 41 | void MicroPrintf(const char* format, ...) { 42 | va_list args; 43 | va_start(args, format); 44 | Log(format, args); 45 | va_end(args); 46 | } 47 | #endif 48 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/micro_log.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_MICRO_LOG_H_ 16 | #define TENSORFLOW_LITE_MICRO_MICRO_LOG_H_ 17 | 18 | #include 19 | 20 | // This is a free function used to perform the actual logging. 21 | // This function will be used by MicroPrintf and MicroErrorReporter::Report() 22 | void Log(const char* format, va_list args); 23 | 24 | #if !defined(TF_LITE_STRIP_ERROR_STRINGS) 25 | // This function can be used independent of the MicroErrorReporter to get 26 | // printf-like functionalitys and are common to all target platforms. 27 | void MicroPrintf(const char* format, ...); 28 | #else 29 | // We use a #define to ensure that the strings are completely stripped, to 30 | // prevent an unnecessary increase in the binary size. 31 | #define MicroPrintf(...) tflite::Unused(__VA_ARGS__) 32 | #endif 33 | 34 | namespace tflite { 35 | 36 | // From 37 | // https://stackoverflow.com/questions/23235910/variadic-unused-function-macro 38 | template 39 | void Unused(Args&&... args) { 40 | (void)(sizeof...(args)); 41 | } 42 | } // namespace tflite 43 | 44 | #endif // TENSORFLOW_LITE_MICRO_MICRO_LOG_H_ 45 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/micro_profiler_interface.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_MICRO_PROFILER_INTERFACE_H_ 17 | #define TENSORFLOW_LITE_MICRO_MICRO_PROFILER_INTERFACE_H_ 18 | 19 | #include 20 | 21 | namespace tflite { 22 | 23 | // Interface class that the TFLM framework relies on for profiling. 24 | class MicroProfilerInterface { 25 | public: 26 | virtual ~MicroProfilerInterface() {} 27 | 28 | // Marks the start of a new event and returns an event handle that can be used 29 | // to mark the end of the event via EndEvent. 30 | virtual uint32_t BeginEvent(const char* tag) = 0; 31 | 32 | // Marks the end of an event associated with event_handle. 33 | virtual void EndEvent(uint32_t event_handle) = 0; 34 | }; 35 | 36 | } // namespace tflite 37 | 38 | #endif // TENSORFLOW_LITE_MICRO_MICRO_PROFILER_INTERFACE_H_ 39 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/micro_string.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_MICRO_STRING_H_ 16 | #define TENSORFLOW_LITE_MICRO_MICRO_STRING_H_ 17 | 18 | #include 19 | 20 | // Implements simple string formatting for numeric types. Returns the number of 21 | // bytes written to output. 22 | extern "C" { 23 | // Functionally equivalent to vsnprintf, trimmed down for TFLite Micro. 24 | // MicroSnprintf() is implemented using MicroVsnprintf(). 25 | int MicroVsnprintf(char* output, int len, const char* format, va_list args); 26 | // Functionally equavalent to snprintf, trimmed down for TFLite Micro. 27 | // For example, MicroSnprintf(buffer, 10, "int %d", 10) will put the string 28 | // "int 10" in the buffer. 29 | // Floating point values are logged in exponent notation (1.XXX*2^N). 30 | int MicroSnprintf(char* output, int len, const char* format, ...); 31 | } 32 | 33 | #endif // TENSORFLOW_LITE_MICRO_MICRO_STRING_H_ 34 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/micro_time.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_MICRO_TIME_H_ 16 | #define TENSORFLOW_LITE_MICRO_MICRO_TIME_H_ 17 | 18 | #include 19 | 20 | namespace tflite { 21 | 22 | // These functions should be implemented by each target platform, and provide an 23 | // accurate tick count along with how many ticks there are per second. 24 | uint32_t ticks_per_second(); 25 | 26 | // Return time in ticks. The meaning of a tick varies per platform. 27 | uint32_t GetCurrentTimeTicks(); 28 | 29 | inline uint32_t TicksToMs(int32_t ticks) { 30 | return static_cast(1000.0f * static_cast(ticks) / 31 | static_cast(ticks_per_second())); 32 | } 33 | 34 | } // namespace tflite 35 | 36 | #endif // TENSORFLOW_LITE_MICRO_MICRO_TIME_H_ 37 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/test_helper_custom_ops.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #ifndef TENSORFLOW_LITE_MICRO_TEST_HELPER_CUSTOM_OPS_H_ 17 | #define TENSORFLOW_LITE_MICRO_TEST_HELPER_CUSTOM_OPS_H_ 18 | 19 | #include 20 | #include 21 | 22 | #include "third_party/flatbuffers/include/flatbuffers/flatbuffers.h" 23 | #include "tensorflow/lite/c/common.h" 24 | #include "tensorflow/lite/kernels/internal/compatibility.h" 25 | #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" 26 | #include "tensorflow/lite/micro/all_ops_resolver.h" 27 | #include "tensorflow/lite/micro/micro_utils.h" 28 | #include "tensorflow/lite/portable_type_to_tflitetype.h" 29 | #include "tensorflow/lite/schema/schema_generated.h" 30 | 31 | namespace tflite { 32 | namespace testing { 33 | 34 | class PackerOp { 35 | public: 36 | static const TfLiteRegistration* getRegistration(); 37 | static TfLiteRegistration* GetMutableRegistration(); 38 | static void* Init(TfLiteContext* context, const char* buffer, size_t length); 39 | static void Free(TfLiteContext* context, void* buffer); 40 | static TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node); 41 | static TfLiteStatus Invoke(TfLiteContext* context, TfLiteNode* node); 42 | 43 | private: 44 | static bool freed_; 45 | }; 46 | 47 | } // namespace testing 48 | } // namespace tflite 49 | 50 | #endif // TENSORFLOW_LITE_MICRO_TEST_HELPER_CUSTOM_OPS_H_ 51 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/tflite_bridge/flatbuffer_conversions_bridge.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #include "tensorflow/lite/micro/tflite_bridge/flatbuffer_conversions_bridge.h" 16 | 17 | #include "tensorflow/lite/c/c_api_types.h" 18 | #include "tensorflow/lite/core/api/error_reporter.h" 19 | #include "tensorflow/lite/core/api/flatbuffer_conversions.h" 20 | #include "tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h" 21 | #include "tensorflow/lite/schema/schema_generated.h" 22 | 23 | namespace tflite { 24 | TfLiteStatus ConvertTensorType(TensorType tensor_type, TfLiteType* type) { 25 | return ConvertTensorType(tensor_type, type, tflite::GetMicroErrorReporter()); 26 | } 27 | 28 | TfLiteStatus CallBuiltinParseFunction(TfLiteBridgeBuiltinParseFunction parser, 29 | const Operator* op, 30 | BuiltinDataAllocator* allocator, 31 | void** builtin_data) { 32 | return parser(op, tflite::GetMicroErrorReporter(), allocator, builtin_data); 33 | } 34 | } // namespace tflite 35 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h" 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "tensorflow/lite/micro/micro_log.h" 23 | 24 | namespace { 25 | uint8_t micro_error_reporter_buffer[sizeof(tflite::MicroErrorReporter)]; 26 | tflite::MicroErrorReporter* error_reporter_ = nullptr; 27 | 28 | } // namespace 29 | 30 | namespace tflite { 31 | ErrorReporter* GetMicroErrorReporter() { 32 | if (error_reporter_ == nullptr) { 33 | error_reporter_ = new (micro_error_reporter_buffer) MicroErrorReporter(); 34 | } 35 | return error_reporter_; 36 | } 37 | 38 | int MicroErrorReporter::Report(const char* format, va_list args) { 39 | Log(format, args); 40 | return 0; 41 | } 42 | 43 | } // namespace tflite 44 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_TFLITE_BRIDGE_MICRO_ERROR_REPORTER_H_ 16 | #define TENSORFLOW_LITE_MICRO_TFLITE_BRIDGE_MICRO_ERROR_REPORTER_H_ 17 | 18 | #include 19 | 20 | #include "tensorflow/lite/core/api/error_reporter.h" 21 | #include "tensorflow/lite/micro/compatibility.h" 22 | 23 | namespace tflite { 24 | // Get a pointer to a singleton global error reporter. 25 | ErrorReporter* GetMicroErrorReporter(); 26 | class MicroErrorReporter : public ErrorReporter { 27 | public: 28 | ~MicroErrorReporter() override {} 29 | int Report(const char* format, va_list args) override; 30 | 31 | private: 32 | TF_LITE_REMOVE_VIRTUAL_DELETE 33 | }; 34 | 35 | } // namespace tflite 36 | 37 | #endif // TENSORFLOW_LITE_MICRO_TFLITE_BRIDGE_MICRO_ERROR_REPORTER_H_ 38 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/tflite_bridge/op_resolver_bridge.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | #include "tensorflow/lite/micro/tflite_bridge/op_resolver_bridge.h" 17 | 18 | #include "tensorflow/lite/c/common.h" 19 | #include "tensorflow/lite/core/api/error_reporter.h" 20 | #include "tensorflow/lite/core/api/op_resolver.h" 21 | #include "tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h" 22 | #include "tensorflow/lite/schema/schema_utils.h" 23 | 24 | namespace tflite { 25 | 26 | TfLiteStatus GetRegistrationFromOpCode( 27 | const OperatorCode* opcode, const OpResolver& op_resolver, 28 | const TfLiteRegistration** registration) { 29 | return GetRegistrationFromOpCode( 30 | opcode, op_resolver, tflite::GetMicroErrorReporter(), registration); 31 | } 32 | } // namespace tflite 33 | -------------------------------------------------------------------------------- /src/tensorflow/lite/micro/tflite_bridge/op_resolver_bridge.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_TFLITE_BRIDGE_OP_RESOLVER_BRIDGE_H_ 16 | #define TENSORFLOW_LITE_MICRO_TFLITE_BRIDGE_OP_RESOLVER_BRIDGE_H_ 17 | 18 | #include "tensorflow/lite/c/c_api_types.h" 19 | #include "tensorflow/lite/c/common.h" 20 | #include "tensorflow/lite/core/api/op_resolver.h" 21 | 22 | namespace tflite { 23 | 24 | // Forward declaration of the classes and structs used here. 25 | struct OperatorCode; 26 | 27 | using TfLiteBridgeOpResolver = OpResolver; 28 | 29 | // Handles the logic for converting between an OperatorCode structure extracted 30 | // from a flatbuffer and information about a registered operator 31 | // implementation. 32 | TfLiteStatus GetRegistrationFromOpCode(const OperatorCode* opcode, 33 | const OpResolver& op_resolver, 34 | const TfLiteRegistration** registration); 35 | 36 | } // namespace tflite 37 | 38 | #endif // TENSORFLOW_LITE_MICRO_TFLITE_BRIDGE_OP_RESOLVER_BRIDGE_H_ 39 | -------------------------------------------------------------------------------- /src/tensorflow/lite/schema/schema_utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_SCHEMA_SCHEMA_UTILS_H_ 16 | #define TENSORFLOW_LITE_SCHEMA_SCHEMA_UTILS_H_ 17 | 18 | #include "third_party/flatbuffers/include/flatbuffers/flatbuffers.h" 19 | #include "tensorflow/lite/schema/schema_generated.h" 20 | 21 | namespace tflite { 22 | 23 | // The following methods are introduced to resolve op builtin code shortage 24 | // problem. The new builtin operator will be assigned to the extended builtin 25 | // code field in the flatbuffer schema. Those methods helps to hide builtin code 26 | // details. 27 | BuiltinOperator GetBuiltinCode(const OperatorCode *op_code); 28 | 29 | BuiltinOperator GetBuiltinCode(const OperatorCodeT *op_code); 30 | 31 | } // namespace tflite 32 | 33 | #endif // TENSORFLOW_LITE_SCHEMA_SCHEMA_UTILS_H_ 34 | -------------------------------------------------------------------------------- /src/test_over_serial/base64.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | #ifndef TENSORFLOW_LITE_MICRO_BASE64_H_ 16 | #define TENSORFLOW_LITE_MICRO_BASE64_H_ 17 | 18 | #include 19 | #include 20 | 21 | namespace test_over_serial { 22 | 23 | // DecodeBase64 24 | // Decode a base64 encoding specified by and . 25 | // The decoded bytes are stored in , which must be large 26 | // enough to hold all the decoded bytes. 27 | // Returns the number of bytes decoded. 28 | // Returns -1 on decode error or if is too small to contain 29 | // all decoded data. 30 | int DecodeBase64(const char* input, size_t input_length, size_t output_length, 31 | uint8_t* output); 32 | 33 | template 34 | inline int DecodeBase64(const char* input, size_t length, 35 | uint8_t (&output)[N]) { 36 | return DecodeBase64(input, length, N, output); 37 | } 38 | 39 | } // namespace test_over_serial 40 | 41 | #endif // TENSORFLOW_LITE_MICRO_BASE64_H_ 42 | -------------------------------------------------------------------------------- /src/third_party/cmsis/CMSIS/Core/Include/cmsis_version.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file cmsis_version.h 3 | * @brief CMSIS Core(M) Version definitions 4 | * @version V5.0.5 5 | * @date 02. February 2022 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2009-2022 ARM Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef __CMSIS_VERSION_H 32 | #define __CMSIS_VERSION_H 33 | 34 | /* CMSIS Version definitions */ 35 | #define __CM_CMSIS_VERSION_MAIN ( 5U) /*!< [31:16] CMSIS Core(M) main version */ 36 | #define __CM_CMSIS_VERSION_SUB ( 6U) /*!< [15:0] CMSIS Core(M) sub version */ 37 | #define __CM_CMSIS_VERSION ((__CM_CMSIS_VERSION_MAIN << 16U) | \ 38 | __CM_CMSIS_VERSION_SUB ) /*!< CMSIS Core(M) version number */ 39 | #endif 40 | -------------------------------------------------------------------------------- /src/third_party/cmsis_nn/Include/arm_nn_tables.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-FileCopyrightText: Copyright 2010-2022 Arm Limited and/or its affiliates 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the License); you may 7 | * not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /* ---------------------------------------------------------------------- 20 | * Project: CMSIS NN Library 21 | * Title: arm_nn_tables.h 22 | * Description: Extern declaration for NN tables 23 | * 24 | * $Date: 30. September 2022 25 | * $Revision: V.2.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * -------------------------------------------------------------------- */ 29 | 30 | #ifndef _ARM_NN_TABLES_H 31 | #define _ARM_NN_TABLES_H 32 | 33 | #include "third_party/cmsis_nn/Include/arm_nn_math_types.h" 34 | 35 | /** 36 | * @brief tables for various activation functions 37 | * 38 | */ 39 | 40 | 41 | #endif /* ARM_NN_TABLES_H */ -------------------------------------------------------------------------------- /src/third_party/cmsis_nn/Source/ActivationFunctions/arm_relu6_s8.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2019 Arm Limited or its affiliates. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the License); you may 7 | * not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /* ---------------------------------------------------------------------- 20 | * Project: CMSIS NN Library 21 | * Title: arm_relu6_s8.c 22 | * Description: Basic s8 version of ReLU6 23 | * 24 | * $Date: 09. October 2020 25 | * $Revision: V.1.0.1 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "third_party/cmsis_nn/Include/arm_nnfunctions.h" 32 | #include "third_party/cmsis_nn/Include/arm_nnsupportfunctions.h" 33 | 34 | /** 35 | * @ingroup Public 36 | */ 37 | 38 | /** 39 | * @addtogroup Acti 40 | * @{ 41 | */ 42 | 43 | /* 44 | * Basic ReLU6 function 45 | * 46 | * Refer to header file for details. 47 | * 48 | */ 49 | 50 | void arm_relu6_s8(q7_t *data, uint16_t size) 51 | { 52 | int32_t i; 53 | 54 | for (i = 0; i < size; i++) 55 | { 56 | int32_t ip = data[i]; 57 | 58 | ip = MAX(ip, 0); 59 | data[i] = MIN(ip, 6); 60 | } 61 | } 62 | 63 | /** 64 | * @} end of Acti group 65 | */ 66 | -------------------------------------------------------------------------------- /src/third_party/cmsis_nn/Source/NNSupportFunctions/arm_nntables.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-FileCopyrightText: Copyright 2010-2022 Arm Limited and/or its affiliates 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the License); you may 7 | * not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /* ---------------------------------------------------------------------- 20 | * Project: CMSIS NN Library 21 | * Title: arm_nntables.c 22 | * Description: Converts the elements of the Q7 vector to Q15 vector without left-shift 23 | * 24 | * $Date: 30 September 2022 25 | * $Revision: V.2.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "third_party/cmsis_nn/Include/arm_nnsupportfunctions.h" 32 | 33 | /** 34 | * @brief tables for various activation functions 35 | * 36 | * This file include the declaration of common tables. 37 | * Most of them are used for activation functions 38 | * 39 | */ 40 | 41 | -------------------------------------------------------------------------------- /src/third_party/cmsis_nn/Source/ReshapeFunctions/arm_reshape_s8.c: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-FileCopyrightText: Copyright 2010-2022 Arm Limited and/or its affiliates 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the License); you may 7 | * not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /* ---------------------------------------------------------------------- 20 | * Project: CMSIS NN Library 21 | * Title: arm_reshape_s8.c 22 | * Description: Reshape a s8 vector 23 | * 24 | * $Date: 4 Aug 2022 25 | * $Revision: V.1.0.1 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "third_party/cmsis_nn/Include/arm_nnfunctions.h" 32 | #include "third_party/cmsis_nn/Include/arm_nnsupportfunctions.h" 33 | 34 | /** 35 | * @ingroup Public 36 | */ 37 | 38 | /** 39 | * @addtogroup Reshape 40 | * @{ 41 | */ 42 | 43 | /* 44 | * Basic s8 reshape function. 45 | * 46 | * Refer header file for details. 47 | * 48 | */ 49 | 50 | void arm_reshape_s8(const int8_t *input, int8_t *output, const uint32_t total_size) 51 | { 52 | arm_memcpy_q7(output, input, total_size); 53 | } 54 | 55 | /** 56 | * @} end of Reshape group 57 | */ 58 | -------------------------------------------------------------------------------- /src/third_party/cmsis_nn/Source/SoftmaxFunctions/arm_softmax_s8_s16.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 Arm Limited or its affiliates. 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the License); you may 7 | * not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /* ---------------------------------------------------------------------- 20 | * Project: CMSIS NN Library 21 | * Title: arm_softmax_s8_s16.c 22 | * Description: S8 to s16 softmax function 23 | * 24 | * $Date: 7 January 2022 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "third_party/cmsis_nn/Include/arm_nnfunctions.h" 32 | #include "third_party/cmsis_nn/Include/arm_nnsupportfunctions.h" 33 | 34 | /** 35 | * @ingroup Public 36 | */ 37 | 38 | /** 39 | * @addtogroup Softmax 40 | * @{ 41 | */ 42 | 43 | void arm_softmax_s8_s16(const int8_t *input, 44 | const int32_t num_rows, 45 | const int32_t row_size, 46 | const int32_t mult, 47 | const int32_t shift, 48 | const int32_t diff_min, 49 | int16_t *output) 50 | { 51 | arm_nn_softmax_common_s8(input, num_rows, row_size, mult, shift, diff_min, true, (void *)output); 52 | } 53 | /** 54 | * @} end of Softmax group 55 | */ 56 | -------------------------------------------------------------------------------- /src/third_party/flatbuffers/include/flatbuffers/buffer_ref.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef FLATBUFFERS_BUFFER_REF_H_ 18 | #define FLATBUFFERS_BUFFER_REF_H_ 19 | 20 | #include "third_party/flatbuffers/include/flatbuffers/base.h" 21 | #include "third_party/flatbuffers/include/flatbuffers/verifier.h" 22 | 23 | namespace flatbuffers { 24 | 25 | // Convenient way to bundle a buffer and its length, to pass it around 26 | // typed by its root. 27 | // A BufferRef does not own its buffer. 28 | struct BufferRefBase {}; // for std::is_base_of 29 | 30 | template struct BufferRef : BufferRefBase { 31 | BufferRef() : buf(nullptr), len(0), must_free(false) {} 32 | BufferRef(uint8_t *_buf, uoffset_t _len) 33 | : buf(_buf), len(_len), must_free(false) {} 34 | 35 | ~BufferRef() { 36 | if (must_free) free(buf); 37 | } 38 | 39 | const T *GetRoot() const { return flatbuffers::GetRoot(buf); } 40 | 41 | bool Verify() { 42 | Verifier verifier(buf, len); 43 | return verifier.VerifyBuffer(nullptr); 44 | } 45 | 46 | uint8_t *buf; 47 | uoffset_t len; 48 | bool must_free; 49 | }; 50 | 51 | } // namespace flatbuffers 52 | 53 | #endif // FLATBUFFERS_BUFFER_REF_H_ -------------------------------------------------------------------------------- /src/third_party/flatbuffers/include/flatbuffers/struct.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef FLATBUFFERS_STRUCT_H_ 18 | #define FLATBUFFERS_STRUCT_H_ 19 | 20 | #include "third_party/flatbuffers/include/flatbuffers/base.h" 21 | 22 | namespace flatbuffers { 23 | 24 | // "structs" are flat structures that do not have an offset table, thus 25 | // always have all members present and do not support forwards/backwards 26 | // compatible extensions. 27 | 28 | class Struct FLATBUFFERS_FINAL_CLASS { 29 | public: 30 | template T GetField(uoffset_t o) const { 31 | return ReadScalar(&data_[o]); 32 | } 33 | 34 | template T GetStruct(uoffset_t o) const { 35 | return reinterpret_cast(&data_[o]); 36 | } 37 | 38 | const uint8_t *GetAddressOf(uoffset_t o) const { return &data_[o]; } 39 | uint8_t *GetAddressOf(uoffset_t o) { return &data_[o]; } 40 | 41 | private: 42 | // private constructor & copy constructor: you obtain instances of this 43 | // class by pointing to existing data only 44 | Struct(); 45 | Struct(const Struct &); 46 | Struct &operator=(const Struct &); 47 | 48 | uint8_t data_[1]; 49 | }; 50 | 51 | } // namespace flatbuffers 52 | 53 | #endif // FLATBUFFERS_STRUCT_H_ -------------------------------------------------------------------------------- /src/third_party/kissfft/COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2003-2010 Mark Borgerding 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | -------------------------------------------------------------------------------- /src/third_party/kissfft/tools/kiss_fftr.h: -------------------------------------------------------------------------------- 1 | #ifndef KISS_FTR_H 2 | #define KISS_FTR_H 3 | 4 | #include "third_party/kissfft/kiss_fft.h" 5 | #ifdef __cplusplus 6 | extern "C++" { 7 | #endif 8 | 9 | 10 | /* 11 | 12 | Real optimized version can save about 45% cpu time vs. complex fft of a real seq. 13 | 14 | 15 | 16 | */ 17 | 18 | typedef struct kiss_fftr_state *kiss_fftr_cfg; 19 | 20 | 21 | kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); 22 | /* 23 | nfft must be even 24 | 25 | If you don't care to allocate space, use mem = lenmem = NULL 26 | */ 27 | 28 | 29 | void kiss_fftr(kiss_fftr_cfg cfg,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata); 30 | /* 31 | input timedata has nfft scalar points 32 | output freqdata has nfft/2+1 complex points 33 | */ 34 | 35 | void kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata); 36 | /* 37 | input freqdata has nfft/2+1 complex points 38 | output timedata has nfft scalar points 39 | */ 40 | 41 | #define kiss_fftr_free free 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | #endif 47 | --------------------------------------------------------------------------------