├── .gitignore ├── LICENSE ├── README.md ├── README_en.md ├── SConscript ├── docs ├── ModelConvert.md ├── api.md ├── introduction.md ├── principle.md ├── samples.md ├── user-guide.md └── version.md ├── examples └── audio_main.cc ├── tensorflow ├── SConscript ├── core │ └── public │ │ └── version.h └── lite │ ├── SConscript │ ├── c │ ├── SConscript │ ├── builtin_op_data.h │ ├── common.c │ └── common.h │ ├── core │ ├── SConscript │ └── api │ │ ├── SConscript │ │ ├── error_reporter.cc │ │ ├── error_reporter.h │ │ ├── flatbuffer_conversions.cc │ │ ├── flatbuffer_conversions.h │ │ ├── op_resolver.cc │ │ ├── op_resolver.h │ │ ├── profiler.h │ │ ├── tensor_utils.cc │ │ └── tensor_utils.h │ ├── experimental │ ├── SConscript │ └── microfrontend │ │ ├── SConscript │ │ └── lib │ │ ├── SConscript │ │ ├── bits.h │ │ ├── fft.cc │ │ ├── fft.h │ │ ├── fft_util.cc │ │ ├── fft_util.h │ │ ├── filterbank.c │ │ ├── filterbank.h │ │ ├── filterbank_util.c │ │ ├── filterbank_util.h │ │ ├── frontend.c │ │ ├── frontend.h │ │ ├── frontend_util.c │ │ ├── frontend_util.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 │ ├── SConscript │ ├── internal │ │ ├── common.h │ │ ├── compatibility.h │ │ ├── cppmath.h │ │ ├── max.h │ │ ├── min.h │ │ ├── optimized │ │ │ └── neon_check.h │ │ ├── quantization_util.cc │ │ ├── quantization_util.h │ │ ├── reference │ │ │ ├── add.h │ │ │ ├── arg_min_max.h │ │ │ ├── binary_function.h │ │ │ ├── ceil.h │ │ │ ├── comparisons.h │ │ │ ├── concatenation.h │ │ │ ├── conv.h │ │ │ ├── depthwiseconv_float.h │ │ │ ├── depthwiseconv_uint8.h │ │ │ ├── dequantize.h │ │ │ ├── floor.h │ │ │ ├── fully_connected.h │ │ │ ├── hard_swish.h │ │ │ ├── integer_ops │ │ │ │ ├── add.h │ │ │ │ ├── conv.h │ │ │ │ ├── depthwise_conv.h │ │ │ │ ├── fully_connected.h │ │ │ │ ├── l2normalization.h │ │ │ │ ├── logistic.h │ │ │ │ ├── mul.h │ │ │ │ ├── pooling.h │ │ │ │ └── tanh.h │ │ │ ├── l2normalization.h │ │ │ ├── logistic.h │ │ │ ├── maximum_minimum.h │ │ │ ├── mul.h │ │ │ ├── neg.h │ │ │ ├── pad.h │ │ │ ├── pooling.h │ │ │ ├── prelu.h │ │ │ ├── process_broadcast_shapes.h │ │ │ ├── quantize.h │ │ │ ├── reduce.h │ │ │ ├── requantize.h │ │ │ ├── resize_nearest_neighbor.h │ │ │ ├── round.h │ │ │ ├── softmax.h │ │ │ ├── strided_slice.h │ │ │ ├── sub.h │ │ │ └── tanh.h │ │ ├── strided_slice_logic.h │ │ ├── tensor.h │ │ ├── tensor_ctypes.h │ │ └── types.h │ ├── kernel_util.cc │ ├── kernel_util.h │ ├── op_macros.h │ └── padding.h │ ├── micro │ ├── SConscript │ ├── all_ops_resolver.cc │ ├── all_ops_resolver.h │ ├── compatibility.h │ ├── debug_log.cc │ ├── debug_log.h │ ├── examples │ │ └── micro_speech │ │ │ ├── audio_provider.cc │ │ │ ├── audio_provider.h │ │ │ ├── command_responder.cc │ │ │ ├── command_responder.h │ │ │ ├── feature_provider.cc │ │ │ ├── feature_provider.h │ │ │ ├── main_functions.cc │ │ │ ├── main_functions.h │ │ │ ├── micro_features │ │ │ ├── micro_features_generator.cc │ │ │ ├── micro_features_generator.h │ │ │ ├── micro_model_settings.cc │ │ │ ├── micro_model_settings.h │ │ │ ├── model.cc │ │ │ ├── model.h │ │ │ ├── no_micro_features_data.cc │ │ │ ├── no_micro_features_data.h │ │ │ ├── yes_micro_features_data.cc │ │ │ └── yes_micro_features_data.h │ │ │ ├── recognize_commands.cc │ │ │ └── recognize_commands.h │ ├── kernels │ │ ├── activation_utils.h │ │ ├── activations.cc │ │ ├── add.cc │ │ ├── arg_min_max.cc │ │ ├── ceil.cc │ │ ├── circular_buffer.cc │ │ ├── cmsis-nn │ │ │ ├── add.cc │ │ │ ├── conv.cc │ │ │ ├── depthwise_conv.cc │ │ │ ├── fully_connected.cc │ │ │ ├── mul.cc │ │ │ ├── pooling.cc │ │ │ └── softmax.cc │ │ ├── comparisons.cc │ │ ├── concatenation.cc │ │ ├── conv.cc │ │ ├── depthwise_conv.cc │ │ ├── dequantize.cc │ │ ├── elementwise.cc │ │ ├── ethosu.cc │ │ ├── floor.cc │ │ ├── fully_connected.cc │ │ ├── hard_swish.cc │ │ ├── kernel_runner.cc │ │ ├── kernel_runner.h │ │ ├── kernel_util.cc │ │ ├── kernel_util.h │ │ ├── l2norm.cc │ │ ├── logical.cc │ │ ├── logistic.cc │ │ ├── maximum_minimum.cc │ │ ├── micro_ops.h │ │ ├── micro_utils.h │ │ ├── mul.cc │ │ ├── neg.cc │ │ ├── pack.cc │ │ ├── pad.cc │ │ ├── pooling.cc │ │ ├── prelu.cc │ │ ├── quantize.cc │ │ ├── reduce.cc │ │ ├── reshape.cc │ │ ├── resize_nearest_neighbor.cc │ │ ├── round.cc │ │ ├── softmax.cc │ │ ├── split.cc │ │ ├── strided_slice.cc │ │ ├── sub.cc │ │ ├── svdf.cc │ │ ├── tanh.cc │ │ └── unpack.cc │ ├── memory_helpers.cc │ ├── memory_helpers.h │ ├── memory_planner │ │ ├── greedy_memory_planner.cc │ │ ├── greedy_memory_planner.h │ │ ├── linear_memory_planner.cc │ │ ├── linear_memory_planner.h │ │ └── memory_planner.h │ ├── micro_allocator.cc │ ├── micro_allocator.h │ ├── micro_error_reporter.cc │ ├── micro_error_reporter.h │ ├── micro_interpreter.cc │ ├── micro_interpreter.h │ ├── micro_mutable_op_resolver.h │ ├── micro_op_resolver.h │ ├── micro_optional_debug_tools.cc │ ├── micro_optional_debug_tools.h │ ├── micro_profiler.cc │ ├── micro_profiler.h │ ├── micro_string.cc │ ├── micro_string.h │ ├── micro_time.cc │ ├── micro_time.h │ ├── micro_utils.cc │ ├── micro_utils.h │ ├── recording_micro_allocator.cc │ ├── recording_micro_allocator.h │ ├── recording_micro_interpreter.h │ ├── recording_simple_memory_allocator.cc │ ├── recording_simple_memory_allocator.h │ ├── simple_memory_allocator.cc │ ├── simple_memory_allocator.h │ ├── test_helpers.cc │ ├── test_helpers.h │ ├── testing │ │ ├── micro_test.h │ │ ├── test_conv_model.cc │ │ ├── test_conv_model.h │ │ ├── test_utils.cc │ │ └── test_utils.h │ └── tools │ │ └── cmsis │ │ └── CMSIS │ │ ├── DSP │ │ └── Include │ │ │ ├── arm_common_tables.h │ │ │ ├── arm_math.h │ │ │ ├── cmsis_armcc.h │ │ │ ├── cmsis_armclang.h │ │ │ ├── cmsis_armclang_ltm.h │ │ │ ├── cmsis_compiler.h │ │ │ └── cmsis_gcc.h │ │ └── NN │ │ ├── Include │ │ ├── arm_nn_tables.h │ │ ├── arm_nn_types.h │ │ ├── arm_nnfunctions.h │ │ └── arm_nnsupportfunctions.h │ │ └── Source │ │ ├── ActivationFunctions │ │ ├── arm_nn_activations_q15.c │ │ ├── arm_nn_activations_q7.c │ │ ├── arm_relu6_s8.c │ │ ├── arm_relu_q15.c │ │ └── arm_relu_q7.c │ │ ├── BasicMathFunctions │ │ ├── arm_elementwise_add_s8.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_HWC_q7_fast_nonsquare.c │ │ ├── arm_convolve_1x1_s8_fast.c │ │ ├── arm_convolve_HWC_q15_basic.c │ │ ├── arm_convolve_HWC_q15_fast.c │ │ ├── arm_convolve_HWC_q15_fast_nonsquare.c │ │ ├── arm_convolve_HWC_q7_RGB.c │ │ ├── arm_convolve_HWC_q7_basic.c │ │ ├── arm_convolve_HWC_q7_basic_nonsquare.c │ │ ├── arm_convolve_HWC_q7_fast.c │ │ ├── arm_convolve_HWC_q7_fast_nonsquare.c │ │ ├── arm_convolve_s8.c │ │ ├── arm_convolve_wrapper_s8.c │ │ ├── arm_depthwise_conv_3x3_s8.c │ │ ├── arm_depthwise_conv_s8.c │ │ ├── arm_depthwise_conv_s8_opt.c │ │ ├── arm_depthwise_conv_u8_basic_ver1.c │ │ ├── arm_depthwise_conv_wrapper_s8.c │ │ ├── arm_depthwise_separable_conv_HWC_q7.c │ │ ├── arm_depthwise_separable_conv_HWC_q7_nonsquare.c │ │ ├── arm_nn_depthwise_conv_s8_core.c │ │ ├── arm_nn_mat_mult_kernel_q7_q15.c │ │ ├── arm_nn_mat_mult_kernel_q7_q15_reordered.c │ │ ├── arm_nn_mat_mult_kernel_s8_s16.c │ │ ├── arm_nn_mat_mult_kernel_s8_s16_reordered.c │ │ └── arm_nn_mat_mult_s8.c │ │ ├── FullyConnectedFunctions │ │ ├── arm_fully_connected_mat_q7_vec_q15.c │ │ ├── arm_fully_connected_mat_q7_vec_q15_opt.c │ │ ├── arm_fully_connected_q15.c │ │ ├── arm_fully_connected_q15_opt.c │ │ ├── arm_fully_connected_q7.c │ │ ├── arm_fully_connected_q7_opt.c │ │ └── arm_fully_connected_s8.c │ │ ├── NNSupportFunctions │ │ ├── arm_nn_accumulate_q7_to_q15.c │ │ ├── arm_nn_add_q7.c │ │ ├── arm_nn_depthwise_conv_nt_t_padded_s8.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_mult_nt_t_s8.c │ │ ├── arm_nn_mult_q15.c │ │ ├── arm_nn_mult_q7.c │ │ ├── arm_nn_vec_mat_mult_t_s8.c │ │ ├── arm_nntables.c │ │ ├── arm_q7_to_q15_no_shift.c │ │ ├── arm_q7_to_q15_reordered_no_shift.c │ │ ├── arm_q7_to_q15_reordered_with_offset.c │ │ └── arm_q7_to_q15_with_offset.c │ │ ├── PoolingFunctions │ │ ├── arm_avgpool_s8.c │ │ ├── arm_max_pool_s8.c │ │ └── arm_pool_q7_HWC.c │ │ ├── ReshapeFunctions │ │ └── arm_reshape_s8.c │ │ └── SoftmaxFunctions │ │ ├── arm_softmax_q15.c │ │ ├── arm_softmax_q7.c │ │ ├── arm_softmax_s8.c │ │ ├── arm_softmax_u8.c │ │ └── arm_softmax_with_batch_q7.c │ ├── schema │ └── schema_generated.h │ ├── string_type.h │ ├── string_util.h │ ├── type_to_tflitetype.h │ └── version.h └── third_party ├── SConscript ├── flatbuffers └── include │ └── flatbuffers │ ├── base.h │ ├── flatbuffers.h │ └── stl_emulation.h ├── gemmlowp ├── fixedpoint │ ├── fixedpoint.h │ └── fixedpoint_neon.h └── internal │ └── detect_platform.h ├── kissfft ├── COPYING ├── SConscript ├── _kiss_fft_guts.h ├── kiss_fft.c ├── kiss_fft.h └── tools │ ├── kiss_fftr.c │ └── kiss_fftr.h └── ruy └── ruy └── profiler └── instrumentation.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | .vscode 45 | 46 | # Kernel Module Compile Results 47 | *.mod* 48 | *.cmd 49 | .tmp_versions/ 50 | modules.order 51 | Module.symvers 52 | Mkfile.old 53 | dkms.conf 54 | -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | # RT-Thread building script for bridge 2 | 3 | import os 4 | from building import * 5 | 6 | cwd = GetCurrentDir() 7 | objs = [] 8 | list = os.listdir(cwd) 9 | 10 | if GetDepend('PKG_USING_TENSORFLOWLITEMICRO'): 11 | for d in list: 12 | path = os.path.join(cwd, d) 13 | if os.path.isfile(os.path.join(path, 'SConscript')): 14 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 15 | 16 | Return('objs') 17 | -------------------------------------------------------------------------------- /docs/api.md: -------------------------------------------------------------------------------- 1 | # Tensorflow Lite Micro API 2 | 3 | ## 目前软件包支持的算子(共52个算子) 4 | 5 | 每个算子定义的详细内容可以参见[`all_ops_resolver.cc`](../tensorflow/lite/micro/all_ops_resolver.cc) 6 | 7 | | 算子名称 | 描述 | 8 | |:-------------------------------------|--------| 9 | |`ABS()` | 元素级取绝对值 | 10 | | `ADD()` | 元素级求和 | 11 | | `ARG_MAX()` | 获得最大值对应的下标 | 12 | | `ARG_MIN()` | 获得最小值对应的下标 | 13 | | `AVERAGE_POOL_2D()` | 二维均值池化算子 | 14 | | `CEIL()` | 对tensor向上取整 | 15 | | `CONCATENATION()` | tensor按第一维进行聚合 | 16 | | `CONV_2D()` | 二维卷积 | 17 | | `COS()` | cos(x)函数 | 18 | | `DEPTHWISE_CONV_2D()` | 深度可分离二维卷积 | 19 | | `DEQUANTIZE()` | 反量化算子 | 20 | | `EQUAL()` | 量化算子 | 21 | | `FLOOR()` | 对tensor向下取整 | 22 | | `FULLY_CONNECTED()` | 全连接层 | 23 | | `GREATER()` | | 24 | | `GREATER_EQUAL()` | | 25 | | `L2_NORMALIZATION()` | 应用欧式距离进行归一化 | 26 | | `LESS()` | | 27 | | `LESS_EQUAL()` | | 28 | | `LOG()` | 对tensor进行log运算 | 29 | | `AND()` | 元素级与 | 30 | | `NOT()` | 元素级取反 | 31 | | `OR()` | 元素级或 | 32 | | `LOGISTIC()` | 对tensor引用逻辑回归函数 | 33 | | `MAX_POOL_2D()` | 最大值池化 | 34 | | `MAXIMUM()` | 对两个输入tensor求较大的tensor | 35 | | `MEAN()` | | 36 | | `MINIMUM()` | | 37 | | `MUL()` | | 38 | | `NEG()` | | 39 | | `NOT_EQUAL()` | | 40 | | `PACK()` | | 41 | | `PAD()` | | 42 | | `PADV2()` | | 43 | | `PRELU()` | | 44 | | `QUANTIZE()` | | 45 | | `RELU()` | RELU激活函数 | 46 | | `RELU6()` | | 47 | | `RESHAPE()` | 对tensor维度进行重新设置 | 48 | | `RESIZE_NEAREST_NEIGHBOR()` | | 49 | | `ROUND()` | | 50 | | `RSQRT()` | | 51 | | `SIN()` | sin(x)函数 | 52 | | `SOFTMAX()` | | 53 | | `SPLIT()` | | 54 | | `SQRT()` | | 55 | | `SQUARE()` | | 56 | | `STRIDED_SLICE()` | | 57 | | `SVDF()` | | 58 | | `TANH()` | tan(x)函数 | 59 | | `UNPACK()` | | 60 | -------------------------------------------------------------------------------- /docs/introduction.md: -------------------------------------------------------------------------------- 1 | # TensorFlow Lite Micro软件包 2 | 3 | TensorFlow Lite Micro 是 TensorFlow Lite 的实验性端口,专门用于在嵌入式设备和其他只有几千字节内存的设备上运行机器学习模型。 4 | 5 | 它不需要操作系统支持、任何标准 C/C++ 库或动态内存分配。核心运行时在 Arm Cortex M3 上占用 16 KB 的内存,并且具有足够多的运算符来运行语音关键字检测模型,运行时总共占用 22 KB 的内存。 6 | 7 | 一些示例应用演示了如何使用微控制器执行唤醒字词检测,根据加速度计数据进行手势分类(待移植),以及使用相机数据进行图像分类(待移植)等任务。 8 | 9 | ## 使用入门 10 | 11 | 如需试用示例应用并了解如何使用该 API,请参阅[用户指导](user-guide.md)。 12 | 13 | ## 为什么要在嵌入式设备中使用机器学习模型 14 | 15 | 嵌入式设备通常是小型低功耗计算设备,往往嵌入到需要执行基本计算的硬件(包括家用电器和物联网设备)中。微控制芯片的年生产量高达数十亿。 16 | 17 | 微控制器芯片通常经过优化以实现低能耗和小尺寸,但处理能力、内存和存储空间会受到影响。同时一些微控制器是为了优化机器学习任务的性能而被设计出来的。 18 | 19 | 通过在微控制器上运行机器学习推断,开发者可以在不依靠网络连接的情况下向各种硬件设备添加 AI 功能,而依赖网络的AI功能通常会受到带宽和功率限制,并且会导致长时间延迟。由于数据无需离开设备,因此在设备端运行推断也有助于保护隐私。 20 | 21 | ## 开发者工作流程 22 | 23 | 为了将 TensorFlow 模型部署到微控制器,您需要遵循以下流程: 24 | 25 | 1. **创建或获取 TensorFlow 模型** 26 | 27 | 该模型必须足够小,在转换后适合目标设备,并且只能使用[支持的操作](https://tensorflow.google.cn/lite/microcontrollers/build_convert#operation_support)。如果您想使用目前不受支持的操作,可以提供自己的实现。 28 | 29 | 2. **将模型转换为 TensorFlow Lite FlatBuffer** 30 | 31 | 您可以使用 [TensorFlow Lite 转换器](https://tensorflow.google.cn/lite/microcontrollers/build_convert#model_conversion)将模型转换为标准 TensorFlow Lite 格式。您可能希望输出量化模型,因为它们更小而且执行效率更高。 32 | 33 | 3. **将 FlatBuffer 转换为 C 字节数组** 34 | 35 | 模型保存在只读程序内存中,并以简单 C 文件的形式提供。您可以使用标准工具[将 FlatBuffer 转换为 C 数组](https://tensorflow.google.cn/lite/microcontrollers/build_convert#convert_to_a_c_array)。 36 | 37 | 4. **集成适用于微控制器的 TensorFlow Lite C++ 库** 38 | 39 | 编写微控制器代码以收集数据、使用 [C++ 库](https://tensorflow.google.cn/lite/microcontrollers/library)执行推断,并利用结果。 40 | 41 | 5. **部署到您的设备** 42 | 43 | 构建程序并将其部署到您的设备。 44 | 45 | ## 限制 46 | 47 | 适用于微控制器的 TensorFlow Lite 专为满足微控制器开发的特定限制条件而设计。如果您使用的是更强大的设备(例如 Raspberry Pi 等嵌入式 Linux 设备),那么标准 TensorFlow Lite 框架可能更易于集成。 48 | 49 | 应考虑以下限制条件: 50 | 51 | - 支持的 TensorFlow 操作[有限](https://tensorflow.google.cn/lite/microcontrollers/build_convert#operation_support) 52 | - 支持的设备有限 53 | - 需要手动内存管理的低阶 C++ API 54 | - 不支持训练 -------------------------------------------------------------------------------- /docs/principle.md: -------------------------------------------------------------------------------- 1 | # Tensorflow Lite Micro 工作原理 2 | 3 | -------------------------------------------------------------------------------- /docs/version.md: -------------------------------------------------------------------------------- 1 | # 版本和修订 # 2 | 3 | | Date | Version | Author | Note | 4 | | -------- | :-----: | :---- | :---- | 5 | | 2020-09-25 | v0.1 | QingChuanWS | 初始版本 | 6 | | 2020-10-10 | v1.0.0 | QingChuanWS | 第一版稳定版本 | -------------------------------------------------------------------------------- /examples/audio_main.cc: -------------------------------------------------------------------------------- 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 17 | #include 18 | #include 19 | #include "tflite/micro/examples/micro_speech/main_functions.h" 20 | 21 | // This is the default main used on systems that have the standard C entry 22 | // point. Other devices (for example FreeRTOS or ESP32) that have different 23 | // requirements for entry code (like an app_main function) should specialize 24 | // this main.cc file in a target-specific subfolder. 25 | int main(int argc, char* argv[]) { 26 | setup(); 27 | rt_kprintf("model load successfully!!\n"); 28 | while (true) { 29 | loop(); 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /tensorflow/SConscript: -------------------------------------------------------------------------------- 1 | # RT-Thread building script for bridge 2 | 3 | import os 4 | from building import * 5 | 6 | cwd = GetCurrentDir() 7 | objs = [] 8 | list = os.listdir(cwd) 9 | 10 | if GetDepend('PKG_USING_TENSORFLOWLITEMICRO'): 11 | for d in list: 12 | path = os.path.join(cwd, d) 13 | if os.path.isfile(os.path.join(path, 'SConscript')): 14 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 15 | 16 | Return('objs') 17 | -------------------------------------------------------------------------------- /tensorflow/lite/SConscript: -------------------------------------------------------------------------------- 1 | # RT-Thread building script for bridge 2 | 3 | import os 4 | from building import * 5 | 6 | cwd = GetCurrentDir() 7 | objs = [] 8 | list = os.listdir(cwd) 9 | 10 | if GetDepend('PKG_USING_TENSORFLOWLITEMICRO'): 11 | for d in list: 12 | path = os.path.join(cwd, d) 13 | if os.path.isfile(os.path.join(path, 'SConscript')): 14 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 15 | 16 | Return('objs') 17 | -------------------------------------------------------------------------------- /tensorflow/lite/c/SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | import os 3 | 4 | cwd = GetCurrentDir() 5 | src = Glob('*.c') + Glob('*.cc') 6 | 7 | #. 8 | root = str(Dir('#')) 9 | packages = os.path.join(root, 'packages') 10 | file_list = os.listdir(packages) 11 | for f in file_list: 12 | if(f.split('-')[0] == 'TensorflowLiteMicro'): 13 | tflm_pkg = os.path.join(packages, f) 14 | break 15 | #./third_party/flatbuffer/include 16 | flatbuffer = os.path.join(tflm_pkg, "third_party/flatbuffers/include") 17 | #./third_party/gemmlowp 18 | gemmlowp = os.path.join(tflm_pkg, "third_party/gemmlowp") 19 | #./third_party/kissfft 20 | kissfft = os.path.join(tflm_pkg, "third_party/kissfft") 21 | #./third_party/ruy 22 | ruy = os.path.join(tflm_pkg, "third_party/ruy") 23 | 24 | 25 | CPPPATH = [tflm_pkg, flatbuffer, gemmlowp, kissfft, ruy] 26 | 27 | group = DefineGroup('lite/c', src, depend = ['PKG_USING_TENSORFLOWLITEMICRO'], CPPPATH = CPPPATH) 28 | 29 | Return('group') 30 | -------------------------------------------------------------------------------- /tensorflow/lite/core/SConscript: -------------------------------------------------------------------------------- 1 | # RT-Thread building script for bridge 2 | 3 | import os 4 | from building import * 5 | 6 | cwd = GetCurrentDir() 7 | objs = [] 8 | list = os.listdir(cwd) 9 | 10 | if GetDepend('PKG_USING_TENSORFLOWLITEMICRO'): 11 | for d in list: 12 | path = os.path.join(cwd, d) 13 | if os.path.isfile(os.path.join(path, 'SConscript')): 14 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 15 | 16 | Return('objs') 17 | -------------------------------------------------------------------------------- /tensorflow/lite/core/api/SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | import os 3 | 4 | cwd = GetCurrentDir() 5 | src = Glob('*.c') + Glob('*.cc') 6 | 7 | #. 8 | root = str(Dir('#')) 9 | packages = os.path.join(root, 'packages') 10 | file_list = os.listdir(packages) 11 | for f in file_list: 12 | if(f.split('-')[0] == 'TensorflowLiteMicro'): 13 | tflm_pkg = os.path.join(packages, f) 14 | break 15 | #./third_party/flatbuffer/include 16 | flatbuffer = os.path.join(tflm_pkg, "third_party/flatbuffers/include") 17 | #./third_party/gemmlowp 18 | gemmlowp = os.path.join(tflm_pkg, "third_party/gemmlowp") 19 | #./third_party/kissfft 20 | kissfft = os.path.join(tflm_pkg, "third_party/kissfft") 21 | #./third_party/ruy 22 | ruy = os.path.join(tflm_pkg, "third_party/ruy") 23 | 24 | 25 | CPPPATH = [tflm_pkg, flatbuffer, gemmlowp, kissfft, ruy] 26 | 27 | group = DefineGroup('lite/core', src, depend = ['PKG_USING_TENSORFLOWLITEMICRO'], CPPPATH = CPPPATH) 28 | 29 | Return('group') 30 | -------------------------------------------------------------------------------- /tensorflow/lite/core/api/error_reporter.cc: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /tensorflow/lite/core/api/error_reporter.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_CORE_API_ERROR_REPORTER_H_ 16 | #define TENSORFLOW_LITE_CORE_API_ERROR_REPORTER_H_ 17 | 18 | #include 19 | 20 | namespace tflite { 21 | 22 | /// A functor that reports error to supporting system. Invoked similar to 23 | /// printf. 24 | /// 25 | /// Usage: 26 | /// ErrorReporter foo; 27 | /// foo.Report("test %d", 5); 28 | /// or 29 | /// va_list args; 30 | /// foo.Report("test %d", args); // where args is va_list 31 | /// 32 | /// Subclass ErrorReporter to provide another reporting destination. 33 | /// For example, if you have a GUI program, you might redirect to a buffer 34 | /// that drives a GUI error log box. 35 | class ErrorReporter { 36 | public: 37 | virtual ~ErrorReporter() {} 38 | virtual int Report(const char* format, va_list args) = 0; 39 | int Report(const char* format, ...); 40 | int ReportError(void*, const char* format, ...); 41 | }; 42 | 43 | } // namespace tflite 44 | 45 | // You should not make bare calls to the error reporter, instead use the 46 | // TF_LITE_REPORT_ERROR macro, since this allows message strings to be 47 | // stripped when the binary size has to be optimized. If you are looking to 48 | // reduce binary size, define TF_LITE_STRIP_ERROR_STRINGS when compiling and 49 | // every call will be stubbed out, taking no memory. 50 | #ifndef TF_LITE_STRIP_ERROR_STRINGS 51 | #define TF_LITE_REPORT_ERROR(reporter, ...) \ 52 | do { \ 53 | static_cast(reporter)->Report(__VA_ARGS__); \ 54 | } while (false) 55 | #else // TF_LITE_STRIP_ERROR_STRINGS 56 | #define TF_LITE_REPORT_ERROR(reporter, ...) 57 | #endif // TF_LITE_STRIP_ERROR_STRINGS 58 | 59 | #endif // TENSORFLOW_LITE_CORE_API_ERROR_REPORTER_H_ 60 | -------------------------------------------------------------------------------- /tensorflow/lite/core/api/op_resolver.cc: -------------------------------------------------------------------------------- 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/core/api/op_resolver.h" 17 | 18 | #include "flatbuffers/flatbuffers.h" // from @flatbuffers 19 | #include "tensorflow/lite/c/common.h" 20 | #include "tensorflow/lite/core/api/error_reporter.h" 21 | 22 | namespace tflite { 23 | 24 | TfLiteStatus GetRegistrationFromOpCode( 25 | const OperatorCode* opcode, const OpResolver& op_resolver, 26 | ErrorReporter* error_reporter, const TfLiteRegistration** registration) { 27 | TfLiteStatus status = kTfLiteOk; 28 | *registration = nullptr; 29 | auto builtin_code = opcode->builtin_code(); 30 | int version = opcode->version(); 31 | 32 | if (builtin_code > BuiltinOperator_MAX || 33 | builtin_code < BuiltinOperator_MIN) { 34 | TF_LITE_REPORT_ERROR( 35 | error_reporter, 36 | "Op builtin_code out of range: %d. Are you using old TFLite binary " 37 | "with newer model?", 38 | builtin_code); 39 | status = kTfLiteError; 40 | } else if (builtin_code != BuiltinOperator_CUSTOM) { 41 | *registration = op_resolver.FindOp(builtin_code, version); 42 | if (*registration == nullptr) { 43 | TF_LITE_REPORT_ERROR( 44 | error_reporter, 45 | "Didn't find op for builtin opcode '%s' version '%d'\n", 46 | EnumNameBuiltinOperator(builtin_code), version); 47 | status = kTfLiteError; 48 | } 49 | } else if (!opcode->custom_code()) { 50 | TF_LITE_REPORT_ERROR( 51 | error_reporter, 52 | "Operator with CUSTOM builtin_code has no custom_code.\n"); 53 | status = kTfLiteError; 54 | } else { 55 | const char* name = opcode->custom_code()->c_str(); 56 | *registration = op_resolver.FindOp(name, version); 57 | if (*registration == nullptr) { 58 | // Do not report error for unresolved custom op, we do the final check 59 | // while preparing ops. 60 | status = kTfLiteError; 61 | } 62 | } 63 | return status; 64 | } 65 | 66 | } // namespace tflite 67 | -------------------------------------------------------------------------------- /tensorflow/lite/core/api/op_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_CORE_API_OP_RESOLVER_H_ 16 | #define TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_ 17 | 18 | #include "tensorflow/lite/c/common.h" 19 | #include "tensorflow/lite/core/api/error_reporter.h" 20 | #include "tensorflow/lite/schema/schema_generated.h" 21 | 22 | namespace tflite { 23 | 24 | /// Abstract interface that returns TfLiteRegistrations given op codes or custom 25 | /// op names. This is the mechanism that ops being referenced in the flatbuffer 26 | /// model are mapped to executable function pointers (TfLiteRegistrations). 27 | class OpResolver { 28 | public: 29 | /// Finds the op registration for a builtin operator by enum code. 30 | virtual const TfLiteRegistration* FindOp(tflite::BuiltinOperator op, 31 | int version) const = 0; 32 | /// Finds the op registration of a custom operator by op name. 33 | virtual const TfLiteRegistration* FindOp(const char* op, 34 | int version) const = 0; 35 | virtual ~OpResolver() {} 36 | }; 37 | 38 | // Handles the logic for converting between an OperatorCode structure extracted 39 | // from a flatbuffer and information about a registered operator 40 | // implementation. 41 | TfLiteStatus GetRegistrationFromOpCode(const OperatorCode* opcode, 42 | const OpResolver& op_resolver, 43 | ErrorReporter* error_reporter, 44 | const TfLiteRegistration** registration); 45 | 46 | } // namespace tflite 47 | 48 | #endif // TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_ 49 | -------------------------------------------------------------------------------- /tensorflow/lite/core/api/tensor_utils.cc: -------------------------------------------------------------------------------- 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/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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/SConscript: -------------------------------------------------------------------------------- 1 | # RT-Thread building script for bridge 2 | 3 | import os 4 | from building import * 5 | 6 | cwd = GetCurrentDir() 7 | objs = [] 8 | list = os.listdir(cwd) 9 | 10 | if GetDepend('PKG_USING_TENSORFLOWLITEMICRO_AUDIO_EXAMPLE'): 11 | for d in list: 12 | path = os.path.join(cwd, d) 13 | if os.path.isfile(os.path.join(path, 'SConscript')): 14 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 15 | 16 | Return('objs') 17 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/SConscript: -------------------------------------------------------------------------------- 1 | # RT-Thread building script for bridge 2 | 3 | import os 4 | from building import * 5 | 6 | cwd = GetCurrentDir() 7 | objs = [] 8 | list = os.listdir(cwd) 9 | 10 | if GetDepend('PKG_USING_TENSORFLOWLITEMICRO'): 11 | for d in list: 12 | path = os.path.join(cwd, d) 13 | if os.path.isfile(os.path.join(path, 'SConscript')): 14 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 15 | 16 | Return('objs') 17 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | import os 3 | 4 | cwd = GetCurrentDir() 5 | src = Glob('*.c') + Glob('*.cc') 6 | 7 | #. 8 | root = str(Dir('#')) 9 | packages = os.path.join(root, 'packages') 10 | file_list = os.listdir(packages) 11 | for f in file_list: 12 | if(f.split('-')[0] == 'TensorflowLiteMicro'): 13 | tflm_pkg = os.path.join(packages, f) 14 | break 15 | #./third_party/flatbuffer/include 16 | flatbuffer = os.path.join(tflm_pkg, "third_party/flatbuffers/include") 17 | #./third_party/gemmlowp 18 | gemmlowp = os.path.join(tflm_pkg, "third_party/gemmlowp") 19 | #./third_party/kissfft 20 | kissfft = os.path.join(tflm_pkg, "third_party/kissfft") 21 | #./third_party/ruy 22 | ruy = os.path.join(tflm_pkg, "third_party/ruy") 23 | 24 | CPPPATH = [tflm_pkg, flatbuffer, gemmlowp, kissfft, ruy] 25 | 26 | group = DefineGroup('lite/experimental', src, depend = ['PKG_USING_TENSORFLOWLITEMICRO'], CPPPATH = CPPPATH) 27 | 28 | Return('group') 29 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/fft.cc: -------------------------------------------------------------------------------- 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 | #define FIXED_POINT 16 20 | #include "kiss_fft.h" 21 | #include "tools/kiss_fftr.h" 22 | 23 | void FftCompute(struct FftState* state, const int16_t* input, 24 | int input_scale_shift) { 25 | const size_t input_size = state->input_size; 26 | const size_t fft_size = state->fft_size; 27 | 28 | int16_t* fft_input = state->input; 29 | // First, scale the input by the given shift. 30 | size_t i; 31 | for (i = 0; i < input_size; ++i) { 32 | fft_input[i] = static_cast(static_cast(input[i]) 33 | << input_scale_shift); 34 | } 35 | // Zero out whatever else remains in the top part of the input. 36 | for (; i < fft_size; ++i) { 37 | fft_input[i] = 0; 38 | } 39 | 40 | // Apply the FFT. 41 | kiss_fftr( 42 | reinterpret_cast(state->scratch), 43 | state->input, 44 | reinterpret_cast(state->output)); 45 | } 46 | 47 | void FftInit(struct FftState* state) { 48 | // All the initialization is done in FftPopulateState() 49 | } 50 | 51 | void FftReset(struct FftState* state) { 52 | memset(state->input, 0, state->fft_size * sizeof(*state->input)); 53 | memset(state->output, 0, (state->fft_size / 2 + 1) * sizeof(*state->output)); 54 | } 55 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/fft_util.cc: -------------------------------------------------------------------------------- 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_util.h" 16 | 17 | #include 18 | 19 | #define FIXED_POINT 16 20 | #include "kiss_fft.h" 21 | #include "tools/kiss_fftr.h" 22 | 23 | int FftPopulateState(struct FftState* state, size_t input_size) { 24 | state->input_size = input_size; 25 | state->fft_size = 1; 26 | while (state->fft_size < state->input_size) { 27 | state->fft_size <<= 1; 28 | } 29 | 30 | state->input = reinterpret_cast( 31 | malloc(state->fft_size * sizeof(*state->input))); 32 | if (state->input == nullptr) { 33 | fprintf(stderr, "Failed to alloc fft input buffer\n"); 34 | return 0; 35 | } 36 | 37 | state->output = reinterpret_cast( 38 | malloc((state->fft_size / 2 + 1) * sizeof(*state->output) * 2)); 39 | if (state->output == nullptr) { 40 | fprintf(stderr, "Failed to alloc fft output buffer\n"); 41 | return 0; 42 | } 43 | 44 | // Ask kissfft how much memory it wants. 45 | size_t scratch_size = 0; 46 | kiss_fftr_cfg kfft_cfg = kiss_fftr_alloc( 47 | state->fft_size, 0, nullptr, &scratch_size); 48 | if (kfft_cfg != nullptr) { 49 | fprintf(stderr, "Kiss memory sizing failed.\n"); 50 | return 0; 51 | } 52 | state->scratch = malloc(scratch_size); 53 | if (state->scratch == nullptr) { 54 | fprintf(stderr, "Failed to alloc fft scratch buffer\n"); 55 | return 0; 56 | } 57 | state->scratch_size = scratch_size; 58 | // Let kissfft configure the scratch space we just allocated 59 | kfft_cfg = kiss_fftr_alloc(state->fft_size, 0, 60 | state->scratch, &scratch_size); 61 | if (kfft_cfg != state->scratch) { 62 | fprintf(stderr, "Kiss memory preallocation strategy failed.\n"); 63 | return 0; 64 | } 65 | return 1; 66 | } 67 | 68 | void FftFreeStateContents(struct FftState* state) { 69 | free(state->input); 70 | free(state->output); 71 | free(state->scratch); 72 | } 73 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/filterbank.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_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include "tensorflow/lite/experimental/microfrontend/lib/fft.h" 22 | 23 | #define kFilterbankBits 12 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | struct FilterbankState { 30 | int num_channels; 31 | int start_index; 32 | int end_index; 33 | int16_t* channel_frequency_starts; 34 | int16_t* channel_weight_starts; 35 | int16_t* channel_widths; 36 | int16_t* weights; 37 | int16_t* unweights; 38 | uint64_t* work; 39 | }; 40 | 41 | // Converts the relevant complex values of an FFT output into energy (the 42 | // square magnitude). 43 | void FilterbankConvertFftComplexToEnergy(struct FilterbankState* state, 44 | struct complex_int16_t* fft_output, 45 | int32_t* energy); 46 | 47 | // Computes the mel-scale filterbank on the given energy array. Output is cached 48 | // internally - to fetch it, you need to call FilterbankSqrt. 49 | void FilterbankAccumulateChannels(struct FilterbankState* state, 50 | const int32_t* energy); 51 | 52 | // Applies an integer square root to the 64 bit intermediate values of the 53 | // filterbank, and returns a pointer to them. Memory will be invalidated the 54 | // next time FilterbankAccumulateChannels is called. 55 | uint32_t* FilterbankSqrt(struct FilterbankState* state, int scale_down_shift); 56 | 57 | void FilterbankReset(struct FilterbankState* state); 58 | 59 | #ifdef __cplusplus 60 | } // extern "C" 61 | #endif 62 | 63 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FILTERBANK_H_ 64 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/frontend.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/frontend.h" 16 | 17 | #include "tensorflow/lite/experimental/microfrontend/lib/bits.h" 18 | 19 | struct FrontendOutput FrontendProcessSamples(struct FrontendState* state, 20 | const int16_t* samples, 21 | size_t num_samples, 22 | size_t* num_samples_read) { 23 | struct FrontendOutput output; 24 | output.values = NULL; 25 | output.size = 0; 26 | 27 | // Try to apply the window - if it fails, return and wait for more data. 28 | if (!WindowProcessSamples(&state->window, samples, num_samples, 29 | num_samples_read)) { 30 | return output; 31 | } 32 | 33 | // Apply the FFT to the window's output (and scale it so that the fixed point 34 | // FFT can have as much resolution as possible). 35 | int input_shift = 36 | 15 - MostSignificantBit32(state->window.max_abs_output_value); 37 | FftCompute(&state->fft, state->window.output, input_shift); 38 | 39 | // We can re-ruse the fft's output buffer to hold the energy. 40 | int32_t* energy = (int32_t*)state->fft.output; 41 | 42 | FilterbankConvertFftComplexToEnergy(&state->filterbank, state->fft.output, 43 | energy); 44 | 45 | FilterbankAccumulateChannels(&state->filterbank, energy); 46 | uint32_t* scaled_filterbank = FilterbankSqrt(&state->filterbank, input_shift); 47 | 48 | // Apply noise reduction. 49 | NoiseReductionApply(&state->noise_reduction, scaled_filterbank); 50 | 51 | if (state->pcan_gain_control.enable_pcan) { 52 | PcanGainControlApply(&state->pcan_gain_control, scaled_filterbank); 53 | } 54 | 55 | // Apply the log and scale. 56 | int correction_bits = 57 | MostSignificantBit32(state->fft.fft_size) - 1 - (kFilterbankBits / 2); 58 | uint16_t* logged_filterbank = 59 | LogScaleApply(&state->log_scale, scaled_filterbank, 60 | state->filterbank.num_channels, correction_bits); 61 | 62 | output.size = state->filterbank.num_channels; 63 | output.values = logged_filterbank; 64 | return output; 65 | } 66 | 67 | void FrontendReset(struct FrontendState* state) { 68 | WindowReset(&state->window); 69 | FftReset(&state->fft); 70 | FilterbankReset(&state->filterbank); 71 | NoiseReductionReset(&state->noise_reduction); 72 | } 73 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/frontend.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_FRONTEND_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include "tensorflow/lite/experimental/microfrontend/lib/fft.h" 22 | #include "tensorflow/lite/experimental/microfrontend/lib/filterbank.h" 23 | #include "tensorflow/lite/experimental/microfrontend/lib/log_scale.h" 24 | #include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h" 25 | #include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h" 26 | #include "tensorflow/lite/experimental/microfrontend/lib/window.h" 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | struct FrontendState { 33 | struct WindowState window; 34 | struct FftState fft; 35 | struct FilterbankState filterbank; 36 | struct NoiseReductionState noise_reduction; 37 | struct PcanGainControlState pcan_gain_control; 38 | struct LogScaleState log_scale; 39 | }; 40 | 41 | struct FrontendOutput { 42 | const uint16_t* values; 43 | size_t size; 44 | }; 45 | 46 | // Main entry point to processing frontend samples. Updates num_samples_read to 47 | // contain the number of samples that have been consumed from the input array. 48 | // Returns a struct containing the generated output. If not enough samples were 49 | // added to generate a feature vector, the returned size will be 0 and the 50 | // values pointer will be NULL. Note that the output pointer will be invalidated 51 | // as soon as FrontendProcessSamples is called again, so copy the contents 52 | // elsewhere if you need to use them later. 53 | struct FrontendOutput FrontendProcessSamples(struct FrontendState* state, 54 | const int16_t* samples, 55 | size_t num_samples, 56 | size_t* num_samples_read); 57 | 58 | void FrontendReset(struct FrontendState* state); 59 | 60 | #ifdef __cplusplus 61 | } // extern "C" 62 | #endif 63 | 64 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_H_ 65 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/frontend_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_FRONTEND_UTIL_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_UTIL_H_ 17 | 18 | #include "tensorflow/lite/experimental/microfrontend/lib/fft_util.h" 19 | #include "tensorflow/lite/experimental/microfrontend/lib/filterbank_util.h" 20 | #include "tensorflow/lite/experimental/microfrontend/lib/frontend.h" 21 | #include "tensorflow/lite/experimental/microfrontend/lib/log_scale_util.h" 22 | #include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h" 23 | #include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.h" 24 | #include "tensorflow/lite/experimental/microfrontend/lib/window_util.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | struct FrontendConfig { 31 | struct WindowConfig window; 32 | struct FilterbankConfig filterbank; 33 | struct NoiseReductionConfig noise_reduction; 34 | struct PcanGainControlConfig pcan_gain_control; 35 | struct LogScaleConfig log_scale; 36 | }; 37 | 38 | // Fills the frontendConfig with "sane" defaults. 39 | void FrontendFillConfigWithDefaults(struct FrontendConfig* config); 40 | 41 | // Allocates any buffers. 42 | int FrontendPopulateState(const struct FrontendConfig* config, 43 | struct FrontendState* state, int sample_rate); 44 | 45 | // Frees any allocated buffers. 46 | void FrontendFreeStateContents(struct FrontendState* state); 47 | 48 | #ifdef __cplusplus 49 | } // extern "C" 50 | #endif 51 | 52 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_FRONTEND_UTIL_H_ 53 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/noise_reduction.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.h" 16 | 17 | #include 18 | 19 | void NoiseReductionApply(struct NoiseReductionState* state, uint32_t* signal) { 20 | int i; 21 | for (i = 0; i < state->num_channels; ++i) { 22 | const uint32_t smoothing = 23 | ((i & 1) == 0) ? state->even_smoothing : state->odd_smoothing; 24 | const uint32_t one_minus_smoothing = (1 << kNoiseReductionBits) - smoothing; 25 | 26 | // Update the estimate of the noise. 27 | const uint32_t signal_scaled_up = signal[i] << state->smoothing_bits; 28 | uint32_t estimate = 29 | (((uint64_t)signal_scaled_up * smoothing) + 30 | ((uint64_t)state->estimate[i] * one_minus_smoothing)) >> 31 | kNoiseReductionBits; 32 | state->estimate[i] = estimate; 33 | 34 | // Make sure that we can't get a negative value for the signal - estimate. 35 | if (estimate > signal_scaled_up) { 36 | estimate = signal_scaled_up; 37 | } 38 | 39 | const uint32_t floor = 40 | ((uint64_t)signal[i] * state->min_signal_remaining) >> 41 | kNoiseReductionBits; 42 | const uint32_t subtracted = 43 | (signal_scaled_up - estimate) >> state->smoothing_bits; 44 | const uint32_t output = subtracted > floor ? subtracted : floor; 45 | signal[i] = output; 46 | } 47 | } 48 | 49 | void NoiseReductionReset(struct NoiseReductionState* state) { 50 | memset(state->estimate, 0, sizeof(*state->estimate) * state->num_channels); 51 | } 52 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_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_PCAN_GAIN_CONTROL_UTIL_H_ 16 | #define TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_UTIL_H_ 17 | 18 | #include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h" 19 | 20 | #define kWideDynamicFunctionBits 32 21 | #define kWideDynamicFunctionLUTSize (4 * kWideDynamicFunctionBits - 3) 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | struct PcanGainControlConfig { 28 | // set to false (0) to disable this module 29 | int enable_pcan; 30 | // gain normalization exponent (0.0 disables, 1.0 full strength) 31 | float strength; 32 | // positive value added in the normalization denominator 33 | float offset; 34 | // number of fractional bits in the gain 35 | int gain_bits; 36 | }; 37 | 38 | void PcanGainControlFillConfigWithDefaults( 39 | struct PcanGainControlConfig* config); 40 | 41 | int16_t PcanGainLookupFunction(const struct PcanGainControlConfig* config, 42 | int32_t input_bits, uint32_t x); 43 | 44 | int PcanGainControlPopulateState(const struct PcanGainControlConfig* config, 45 | struct PcanGainControlState* state, 46 | uint32_t* noise_estimate, 47 | const int num_channels, 48 | const uint16_t smoothing_bits, 49 | const int32_t input_correction_bits); 50 | 51 | void PcanGainControlFreeStateContents(struct PcanGainControlState* state); 52 | 53 | #ifdef __cplusplus 54 | } // extern "C" 55 | #endif 56 | 57 | #endif // TENSORFLOW_LITE_EXPERIMENTAL_MICROFRONTEND_LIB_PCAN_GAIN_CONTROL_UTIL_H_ 58 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/window.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/window.h" 16 | 17 | #include 18 | 19 | int WindowProcessSamples(struct WindowState* state, const int16_t* samples, 20 | size_t num_samples, size_t* num_samples_read) { 21 | const int size = state->size; 22 | 23 | // Copy samples from the samples buffer over to our local input. 24 | size_t max_samples_to_copy = state->size - state->input_used; 25 | if (max_samples_to_copy > num_samples) { 26 | max_samples_to_copy = num_samples; 27 | } 28 | memcpy(state->input + state->input_used, samples, 29 | max_samples_to_copy * sizeof(*samples)); 30 | *num_samples_read = max_samples_to_copy; 31 | state->input_used += max_samples_to_copy; 32 | 33 | if (state->input_used < state->size) { 34 | // We don't have enough samples to compute a window. 35 | return 0; 36 | } 37 | 38 | // Apply the window to the input. 39 | const int16_t* coefficients = state->coefficients; 40 | const int16_t* input = state->input; 41 | int16_t* output = state->output; 42 | int i; 43 | int16_t max_abs_output_value = 0; 44 | for (i = 0; i < size; ++i) { 45 | int16_t new_value = 46 | (((int32_t)*input++) * *coefficients++) >> kFrontendWindowBits; 47 | *output++ = new_value; 48 | if (new_value < 0) { 49 | new_value = -new_value; 50 | } 51 | if (new_value > max_abs_output_value) { 52 | max_abs_output_value = new_value; 53 | } 54 | } 55 | // Shuffle the input down by the step size, and update how much we have used. 56 | memmove(state->input, state->input + state->step, 57 | sizeof(*state->input) * (state->size - state->step)); 58 | state->input_used -= state->step; 59 | state->max_abs_output_value = max_abs_output_value; 60 | 61 | // Indicate that the output buffer is valid for the next stage. 62 | return 1; 63 | } 64 | 65 | void WindowReset(struct WindowState* state) { 66 | memset(state->input, 0, state->size * sizeof(*state->input)); 67 | memset(state->output, 0, state->size * sizeof(*state->output)); 68 | state->input_used = 0; 69 | state->max_abs_output_value = 0; 70 | } 71 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/experimental/microfrontend/lib/window_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/window_util.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | // Some platforms don't have M_PI 23 | #ifndef M_PI 24 | #define M_PI 3.14159265358979323846 25 | #endif 26 | 27 | void WindowFillConfigWithDefaults(struct WindowConfig* config) { 28 | config->size_ms = 25; 29 | config->step_size_ms = 10; 30 | } 31 | 32 | int WindowPopulateState(const struct WindowConfig* config, 33 | struct WindowState* state, int sample_rate) { 34 | state->size = config->size_ms * sample_rate / 1000; 35 | state->step = config->step_size_ms * sample_rate / 1000; 36 | 37 | state->coefficients = malloc(state->size * sizeof(*state->coefficients)); 38 | if (state->coefficients == NULL) { 39 | fprintf(stderr, "Failed to allocate window coefficients\n"); 40 | return 0; 41 | } 42 | 43 | // Populate the window values. 44 | const float arg = M_PI * 2.0 / ((float)state->size); 45 | int i; 46 | for (i = 0; i < state->size; ++i) { 47 | float float_value = 0.5 - (0.5 * cos(arg * (i + 0.5))); 48 | // Scale it to fixed point and round it. 49 | state->coefficients[i] = 50 | floor(float_value * (1 << kFrontendWindowBits) + 0.5); 51 | } 52 | 53 | state->input_used = 0; 54 | state->input = malloc(state->size * sizeof(*state->input)); 55 | if (state->input == NULL) { 56 | fprintf(stderr, "Failed to allocate window input\n"); 57 | return 0; 58 | } 59 | 60 | state->output = malloc(state->size * sizeof(*state->output)); 61 | if (state->output == NULL) { 62 | fprintf(stderr, "Failed to allocate window output\n"); 63 | return 0; 64 | } 65 | 66 | return 1; 67 | } 68 | 69 | void WindowFreeStateContents(struct WindowState* state) { 70 | free(state->coefficients); 71 | free(state->input); 72 | free(state->output); 73 | } 74 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/kernels/SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | import os 3 | 4 | cwd = GetCurrentDir() 5 | src = Glob('*.c') + Glob('*.cc') + Glob('internal/*.cc') 6 | 7 | #. 8 | root = str(Dir('#')) 9 | packages = os.path.join(root, 'packages') 10 | file_list = os.listdir(packages) 11 | for f in file_list: 12 | if(f.split('-')[0] == 'TensorflowLiteMicro'): 13 | tflm_pkg = os.path.join(packages, f) 14 | break 15 | #./third_party/flatbuffer/include 16 | flatbuffer = os.path.join(tflm_pkg, "third_party/flatbuffers/include") 17 | #./third_party/gemmlowp 18 | gemmlowp = os.path.join(tflm_pkg, "third_party/gemmlowp") 19 | #./third_party/kissfft 20 | kissfft = os.path.join(tflm_pkg, "third_party/kissfft") 21 | #./third_party/ruy 22 | ruy = os.path.join(tflm_pkg, "third_party/ruy") 23 | 24 | 25 | CPPPATH = [tflm_pkg, flatbuffer, gemmlowp, kissfft, ruy] 26 | 27 | group = DefineGroup('lite/kernels', src, depend = ['PKG_USING_TENSORFLOWLITEMICRO'], CPPPATH = CPPPATH) 28 | 29 | Return('group') 30 | -------------------------------------------------------------------------------- /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(TF_LITE_USE_GLOBAL_CMATH_FUNCTIONS) || \ 23 | (defined(__ANDROID__) && !defined(__NDK_MAJOR__)) || defined(ARDUINO) || \ 24 | defined(__ZEPHYR__) 25 | #define TF_LITE_GLOBAL_STD_PREFIX 26 | #else 27 | #define TF_LITE_GLOBAL_STD_PREFIX //std 28 | #endif 29 | 30 | #define DECLARE_STD_GLOBAL_SWITCH1(tf_name, std_name) \ 31 | template \ 32 | inline T tf_name(const T x) { \ 33 | return TF_LITE_GLOBAL_STD_PREFIX::std_name(x); \ 34 | } 35 | 36 | DECLARE_STD_GLOBAL_SWITCH1(TfLiteRound, round); 37 | 38 | } // namespace tflite 39 | 40 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_CPPMATH_H_ 41 | -------------------------------------------------------------------------------- /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 ::max(x, y); 25 | } 26 | #else 27 | template 28 | inline T TfLiteMax(const T& x, const T& y) { 29 | return ::fmax(x, y); 30 | } 31 | #endif 32 | 33 | } // namespace tflite 34 | 35 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ 36 | -------------------------------------------------------------------------------- /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 ::min(x, y); 25 | } 26 | #else 27 | template 28 | inline T TfLiteMin(const T& x, const T& y) { 29 | return ::fmin(x, y); 30 | } 31 | #endif 32 | 33 | } // namespace tflite 34 | 35 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ 36 | -------------------------------------------------------------------------------- /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 | #if defined(__ARM_NEON__) || defined(__ARM_NEON) 19 | #define USE_NEON 20 | #include 21 | #endif 22 | 23 | #if defined __GNUC__ && defined __SSE4_1__ && !defined TF_LITE_DISABLE_X86_NEON 24 | #define USE_NEON 25 | #include "NEON_2_SSE.h" 26 | #endif 27 | 28 | // NEON_OR_PORTABLE(SomeFunc, args) calls NeonSomeFunc(args) if USE_NEON is 29 | // defined, PortableSomeFunc(args) otherwise. 30 | #ifdef USE_NEON 31 | // Always use Neon code 32 | #define NEON_OR_PORTABLE(funcname, ...) Neon##funcname(__VA_ARGS__) 33 | 34 | #else 35 | // No NEON available: Use Portable code 36 | #define NEON_OR_PORTABLE(funcname, ...) Portable##funcname(__VA_ARGS__) 37 | 38 | #endif // defined(USE_NEON) 39 | 40 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_NEON_CHECK_H_ 41 | -------------------------------------------------------------------------------- /tensorflow/lite/kernels/internal/reference/arg_min_max.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_ARG_MIN_MAX_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ARG_MIN_MAX_H_ 17 | 18 | #include "tensorflow/lite/kernels/internal/types.h" 19 | 20 | namespace tflite { 21 | 22 | namespace reference_ops { 23 | 24 | template 25 | void ArgMinMax(const RuntimeShape& input1_shape, const T1* input1_data, 26 | const T3* input2_data, const RuntimeShape& output_shape, 27 | T2* output_data, const Cmp& cmp) { 28 | TFLITE_DCHECK_GT(input1_shape.DimensionsCount(), 0); 29 | TFLITE_DCHECK_EQ(input1_shape.DimensionsCount() - 1, 30 | output_shape.DimensionsCount()); 31 | int axis = input2_data[0]; 32 | if (axis < 0) { 33 | axis += input1_shape.DimensionsCount(); 34 | } 35 | const int axis_size = input1_shape.Dims(axis); 36 | 37 | int outer_size = 1; 38 | for (int i = 0; i < axis; ++i) { 39 | TFLITE_DCHECK_EQ(input1_shape.Dims(i), output_shape.Dims(i)); 40 | outer_size *= input1_shape.Dims(i); 41 | } 42 | 43 | int inner_size = 1; 44 | const int dims_count = input1_shape.DimensionsCount(); 45 | for (int i = axis + 1; i < dims_count; ++i) { 46 | TFLITE_DCHECK_EQ(input1_shape.Dims(i), output_shape.Dims(i - 1)); 47 | inner_size *= input1_shape.Dims(i); 48 | } 49 | for (int outer = 0; outer < outer_size; ++outer) { 50 | for (int inner = 0; inner < inner_size; ++inner) { 51 | auto min_max_value = input1_data[outer * axis_size * inner_size + inner]; 52 | T2 min_max_index = 0; 53 | for (int i = 1; i < axis_size; ++i) { 54 | const auto& curr_value = 55 | input1_data[(outer * axis_size + i) * inner_size + inner]; 56 | if (cmp(curr_value, min_max_value)) { 57 | min_max_value = curr_value; 58 | min_max_index = static_cast(i); 59 | } 60 | } 61 | output_data[outer * inner_size + inner] = min_max_index; 62 | } 63 | } 64 | } 65 | } // namespace reference_ops 66 | } // namespace tflite 67 | 68 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ARG_MIN_MAX_H_ 69 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/kernels/internal/reference/maximum_minimum.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_REFERENCE_MAXIMUM_MINIMUM_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MAXIMUM_MINIMUM_H_ 17 | 18 | #include "tensorflow/lite/kernels/internal/common.h" 19 | #include "tensorflow/lite/kernels/internal/types.h" 20 | 21 | namespace tflite { 22 | namespace reference_ops { 23 | 24 | template 25 | void MaximumMinimumBroadcastSlow(const RuntimeShape& unextended_input1_shape, 26 | const T* input1_data, 27 | const RuntimeShape& unextended_input2_shape, 28 | const T* input2_data, 29 | const RuntimeShape& unextended_output_shape, 30 | T* output_data, Op op) { 31 | // Uses element-wise calculation if broadcast is not required. 32 | if (unextended_input1_shape == unextended_input2_shape) { 33 | const int flat_size = 34 | MatchingElementsSize(unextended_input1_shape, unextended_input2_shape, 35 | unextended_output_shape); 36 | for (int i = 0; i < flat_size; ++i) { 37 | output_data[i] = op(input1_data[i], input2_data[i]); 38 | } 39 | } else { 40 | TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), N); 41 | TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), N); 42 | TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), N); 43 | 44 | NdArrayDesc desc1; 45 | NdArrayDesc desc2; 46 | NdArrayDesc output_desc; 47 | NdArrayDescsForElementwiseBroadcast( 48 | unextended_input1_shape, unextended_input2_shape, &desc1, &desc2); 49 | CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape), 50 | &output_desc); 51 | 52 | auto maxmin_func = [&](int indexes[N]) { 53 | output_data[SubscriptToIndex(output_desc, indexes)] = 54 | op(input1_data[SubscriptToIndex(desc1, indexes)], 55 | input2_data[SubscriptToIndex(desc2, indexes)]); 56 | }; 57 | NDOpsHelper(output_desc, maxmin_func); 58 | } 59 | } 60 | 61 | } // namespace reference_ops 62 | } // namespace tflite 63 | 64 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MAXIMUM_MINIMUM_H_ 65 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/kernels/internal/reference/quantize.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_QUANTIZE_H_ 16 | #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include "tensorflow/lite/kernels/internal/common.h" 22 | #include "tensorflow/lite/kernels/internal/compatibility.h" 23 | #include "tensorflow/lite/kernels/internal/cppmath.h" 24 | #include "tensorflow/lite/kernels/internal/types.h" 25 | 26 | namespace tflite { 27 | 28 | namespace reference_ops { 29 | 30 | template 31 | inline void AffineQuantize(const tflite::QuantizationParams& op_params, 32 | const RuntimeShape& input_shape, 33 | const InputT* input_data, 34 | const RuntimeShape& output_shape, 35 | OutputT* output_data) { 36 | const int32_t zero_point = op_params.zero_point; 37 | const double scale = op_params.scale; 38 | const int flat_size = MatchingFlatSize(input_shape, output_shape); 39 | static constexpr int32_t min_val = std::numeric_limits::min(); 40 | static constexpr int32_t max_val = std::numeric_limits::max(); 41 | 42 | for (int i = 0; i < flat_size; i++) { 43 | const InputT val = input_data[i]; 44 | int32_t unclamped = 45 | static_cast(TfLiteRound(val / static_cast(scale))) + 46 | zero_point; 47 | int32_t clamped = std::min(std::max(unclamped, min_val), max_val); 48 | output_data[i] = clamped; 49 | } 50 | } 51 | 52 | } // namespace reference_ops 53 | 54 | } // namespace tflite 55 | #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_ 56 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /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 | // If we're on a platform without standard IO functions, fall back to a 19 | // non-portable function. 20 | #ifdef TF_LITE_MCU_DEBUG_LOG 21 | 22 | #include "tensorflow/lite/micro/debug_log.h" 23 | 24 | #define DEBUG_LOG(x) \ 25 | do { \ 26 | DebugLog(x); \ 27 | } while (0) 28 | 29 | inline void InfiniteLoop() { 30 | DEBUG_LOG("HALTED\n"); 31 | while (1) { 32 | } 33 | } 34 | 35 | #define TFLITE_ABORT InfiniteLoop(); 36 | 37 | #else // TF_LITE_MCU_DEBUG_LOG 38 | 39 | #include 40 | #include 41 | 42 | #define DEBUG_LOG(x) \ 43 | do { \ 44 | fprintf(stderr, "%s", (x)); \ 45 | } while (0) 46 | 47 | // Report Error for unsupported type by op 'op_name' and returns kTfLiteError. 48 | #define TF_LITE_UNSUPPORTED_TYPE(context, type, op_name) \ 49 | do { \ 50 | TF_LITE_KERNEL_LOG((context), "%s:%d Type %s is unsupported by op %s.", \ 51 | __FILE__, __LINE__, TfLiteTypeGetName(type), \ 52 | (op_name)); \ 53 | return kTfLiteError; \ 54 | } while (0) 55 | 56 | #define TFLITE_ABORT abort() 57 | 58 | #endif // TF_LITE_MCU_DEBUG_LOG 59 | 60 | #ifdef NDEBUG 61 | #define TFLITE_ASSERT_FALSE (static_cast(0)) 62 | #else 63 | #define TFLITE_ASSERT_FALSE TFLITE_ABORT 64 | #endif 65 | 66 | #define TF_LITE_FATAL(msg) \ 67 | do { \ 68 | DEBUG_LOG(msg); \ 69 | DEBUG_LOG("\nFATAL\n"); \ 70 | TFLITE_ABORT; \ 71 | } while (0) 72 | 73 | #define TF_LITE_ASSERT(x) \ 74 | do { \ 75 | if (!(x)) TF_LITE_FATAL(#x); \ 76 | } while (0) 77 | 78 | #define TF_LITE_ASSERT_EQ(x, y) \ 79 | do { \ 80 | if ((x) != (y)) TF_LITE_FATAL(#x " didn't equal " #y); \ 81 | } while (0) 82 | 83 | #endif // TENSORFLOW_LITE_KERNELS_OP_MACROS_H_ 84 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/all_ops_resolver.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | Licensed under the Apache License, Version 2.0 (the "License"); 3 | you may not use this file except in compliance with the License. 4 | You may obtain a copy of the License at 5 | http://www.apache.org/licenses/LICENSE-2.0 6 | Unless required by applicable law or agreed to in writing, software 7 | distributed under the License is distributed on an "AS IS" BASIS, 8 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 9 | See the License for the specific language governing permissions and 10 | limitations under the License. 11 | ==============================================================================*/ 12 | 13 | #include "tensorflow/lite/micro/all_ops_resolver.h" 14 | 15 | #include "tensorflow/lite/micro/kernels/micro_ops.h" 16 | 17 | namespace tflite { 18 | namespace ops { 19 | namespace micro { 20 | namespace custom { 21 | TfLiteRegistration* Register_ETHOSU(); 22 | const char* GetString_ETHOSU(); 23 | } // namespace custom 24 | } // namespace micro 25 | } // namespace ops 26 | 27 | AllOpsResolver::AllOpsResolver() { 28 | // Please keep this list of Builtin Operators in alphabetical order. 29 | AddAbs(); 30 | AddAdd(); 31 | AddArgMax(); 32 | AddArgMin(); 33 | AddAveragePool2D(); 34 | AddCeil(); 35 | AddConcatenation(); 36 | AddConv2D(); 37 | AddCos(); 38 | AddDepthwiseConv2D(); 39 | AddDequantize(); 40 | AddEqual(); 41 | AddFloor(); 42 | AddFullyConnected(); 43 | AddGreater(); 44 | AddGreaterEqual(); 45 | AddHardSwish(); 46 | AddL2Normalization(); 47 | AddLess(); 48 | AddLessEqual(); 49 | AddLog(); 50 | AddLogicalAnd(); 51 | AddLogicalNot(); 52 | AddLogicalOr(); 53 | AddLogistic(); 54 | AddMaximum(); 55 | AddMaxPool2D(); 56 | AddMean(); 57 | AddMinimum(); 58 | AddMul(); 59 | AddNeg(); 60 | AddNotEqual(); 61 | AddPack(); 62 | AddPad(); 63 | AddPadV2(); 64 | AddPrelu(); 65 | AddQuantize(); 66 | AddRelu(); 67 | AddRelu6(); 68 | AddReshape(); 69 | AddResizeNearestNeighbor(); 70 | AddRound(); 71 | AddRsqrt(); 72 | AddSin(); 73 | AddSoftmax(); 74 | AddSplit(); 75 | AddSqrt(); 76 | AddSquare(); 77 | AddStridedSlice(); 78 | AddSub(); 79 | AddSvdf(); 80 | AddTanh(); 81 | AddUnpack(); 82 | 83 | // TODO(b/159644355): Figure out if custom Ops belong in AllOpsResolver. 84 | TfLiteRegistration* registration = 85 | tflite::ops::micro::custom::Register_ETHOSU(); 86 | if (registration) { 87 | AddCustom(tflite::ops::micro::custom::GetString_ETHOSU(), registration); 88 | } 89 | } 90 | 91 | } // namespace tflite 92 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/all_ops_resolver.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | Licensed under the Apache License, Version 2.0 (the "License"); 3 | you may not use this file except in compliance with the License. 4 | You may obtain a copy of the License at 5 | http://www.apache.org/licenses/LICENSE-2.0 6 | Unless required by applicable law or agreed to in writing, software 7 | distributed under the License is distributed on an "AS IS" BASIS, 8 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 9 | See the License for the specific language governing permissions and 10 | limitations under the License. 11 | ==============================================================================*/ 12 | #ifndef TENSORFLOW_LITE_MICRO_ALL_OPS_RESOLVER_H_ 13 | #define TENSORFLOW_LITE_MICRO_ALL_OPS_RESOLVER_H_ 14 | 15 | #include "tensorflow/lite/micro/compatibility.h" 16 | #include "tensorflow/lite/micro/micro_mutable_op_resolver.h" 17 | 18 | namespace tflite { 19 | 20 | // The magic number in the template parameter is the maximum number of ops that 21 | // can be added to AllOpsResolver. It can be increased if needed. And most 22 | // applications that care about the memory footprint will want to directly use 23 | // MicroMutableOpResolver and have an application specific template parameter. 24 | // The examples directory has sample code for this. 25 | class AllOpsResolver : public MicroMutableOpResolver<128> { 26 | public: 27 | AllOpsResolver(); 28 | 29 | private: 30 | TF_LITE_REMOVE_VIRTUAL_DELETE 31 | }; 32 | 33 | } // namespace tflite 34 | 35 | #endif // TENSORFLOW_LITE_MICRO_ALL_OPS_RESOLVER_H_ 36 | -------------------------------------------------------------------------------- /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 TF_LITE_STATIC_MEMORY 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 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/debug_log.cc: -------------------------------------------------------------------------------- 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 | // Reference implementation of the DebugLog() function that's required for a 17 | // platform to support the TensorFlow Lite for Microcontrollers library. This is 18 | // the only function that's absolutely required to be available on a target 19 | // device, since it's used for communicating test results back to the host so 20 | // that we can verify the implementation is working correctly. 21 | // It's designed to be as easy as possible to supply an implementation though. 22 | // On platforms that have a POSIX stack or C library, it can be written as a 23 | // single call to `fprintf(stderr, "%s", s)` to output a string to the error 24 | // stream of the console, but if there's no OS or C library available, there's 25 | // almost always an equivalent way to write out a string to some serial 26 | // interface that can be used instead. For example on Arm M-series MCUs, calling 27 | // the `bkpt #0xAB` assembler instruction will output the string in r1 to 28 | // whatever debug serial connection is available. If you're running mbed, you 29 | // can do the same by creating `Serial pc(USBTX, USBRX)` and then calling 30 | // `pc.printf("%s", s)`. 31 | // To add an equivalent function for your own platform, create your own 32 | // implementation file, and place it in a subfolder with named after the OS 33 | // you're targeting. For example, see the Cortex M bare metal version in 34 | // tensorflow/lite/micro/bluepill/debug_log.cc or the mbed one on 35 | // tensorflow/lite/micro/mbed/debug_log.cc. 36 | 37 | #include "tensorflow/lite/micro/debug_log.h" 38 | 39 | #include 40 | #include 41 | #include 42 | 43 | extern "C" void DebugLog(const char* s) { rt_kprintf("%s", s); } 44 | -------------------------------------------------------------------------------- /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 | // This function should be implemented by each target platform, and provide a 19 | // way for strings to be output to some text stream. For more information, see 20 | // tensorflow/lite/micro/debug_log.cc. 21 | extern "C" void DebugLog(const char* s); 22 | 23 | #endif // TENSORFLOW_LITE_MICRO_DEBUG_LOG_H_ 24 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/audio_provider.cc: -------------------------------------------------------------------------------- 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/examples/micro_speech/audio_provider.h" 17 | 18 | #include "tensorflow/lite/micro/examples/micro_speech/micro_features/micro_model_settings.h" 19 | 20 | namespace { 21 | int16_t g_dummy_audio_data[kMaxAudioSampleSize]; 22 | int32_t g_latest_audio_timestamp = 0; 23 | } // namespace 24 | 25 | TfLiteStatus GetAudioSamples(tflite::ErrorReporter* error_reporter, 26 | int start_ms, int duration_ms, 27 | int* audio_samples_size, int16_t** audio_samples) { 28 | for (int i = 0; i < kMaxAudioSampleSize; ++i) { 29 | g_dummy_audio_data[i] = 0; 30 | } 31 | *audio_samples_size = kMaxAudioSampleSize; 32 | *audio_samples = g_dummy_audio_data; 33 | return kTfLiteOk; 34 | } 35 | 36 | int32_t LatestAudioTimestamp() { 37 | g_latest_audio_timestamp += 100; 38 | return g_latest_audio_timestamp; 39 | } 40 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/audio_provider.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 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_AUDIO_PROVIDER_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_AUDIO_PROVIDER_H_ 18 | 19 | #include "tensorflow/lite/c/common.h" 20 | #include "tensorflow/lite/micro/micro_error_reporter.h" 21 | 22 | // This is an abstraction around an audio source like a microphone, and is 23 | // expected to return 16-bit PCM sample data for a given point in time. The 24 | // sample data itself should be used as quickly as possible by the caller, since 25 | // to allow memory optimizations there are no guarantees that the samples won't 26 | // be overwritten by new data in the future. In practice, implementations should 27 | // ensure that there's a reasonable time allowed for clients to access the data 28 | // before any reuse. 29 | // The reference implementation can have no platform-specific dependencies, so 30 | // it just returns an array filled with zeros. For real applications, you should 31 | // ensure there's a specialized implementation that accesses hardware APIs. 32 | TfLiteStatus GetAudioSamples(tflite::ErrorReporter* error_reporter, 33 | int start_ms, int duration_ms, 34 | int* audio_samples_size, int16_t** audio_samples); 35 | 36 | // Returns the time that audio data was last captured in milliseconds. There's 37 | // no contract about what time zero represents, the accuracy, or the granularity 38 | // of the result. Subsequent calls will generally not return a lower value, but 39 | // even that's not guaranteed if there's an overflow wraparound. 40 | // The reference implementation of this function just returns a constantly 41 | // incrementing value for each call, since it would need a non-portable platform 42 | // call to access time information. For real applications, you'll need to write 43 | // your own platform-specific implementation. 44 | int32_t LatestAudioTimestamp(); 45 | 46 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_AUDIO_PROVIDER_H_ 47 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/command_responder.cc: -------------------------------------------------------------------------------- 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/examples/micro_speech/command_responder.h" 17 | 18 | // The default implementation writes out the name of the recognized command 19 | // to the error console. Real applications will want to take some custom 20 | // action instead, and should implement their own versions of this function. 21 | void RespondToCommand(tflite::ErrorReporter* error_reporter, 22 | int32_t current_time, const char* found_command, 23 | uint8_t score, bool is_new_command) { 24 | if (is_new_command) { 25 | TF_LITE_REPORT_ERROR(error_reporter, "Heard %s (%d) @%dms", found_command, 26 | score, current_time); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/command_responder.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 | // 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 | #include "tensorflow/lite/micro/micro_error_reporter.h" 23 | 24 | // Called every time the results of an audio recognition run are available. The 25 | // human-readable name of any recognized command is in the `found_command` 26 | // argument, `score` has the numerical confidence, and `is_new_command` is set 27 | // if the previous command was different to this one. 28 | void RespondToCommand(tflite::ErrorReporter* error_reporter, 29 | int32_t current_time, const char* found_command, 30 | uint8_t score, bool is_new_command); 31 | 32 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_COMMAND_RESPONDER_H_ 33 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/feature_provider.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 | 16 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_FEATURE_PROVIDER_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_FEATURE_PROVIDER_H_ 18 | 19 | #include "tensorflow/lite/c/common.h" 20 | #include "tensorflow/lite/micro/micro_error_reporter.h" 21 | 22 | // Binds itself to an area of memory intended to hold the input features for an 23 | // audio-recognition neural network model, and fills that data area with the 24 | // features representing the current audio input, for example from a microphone. 25 | // The audio features themselves are a two-dimensional array, made up of 26 | // horizontal slices representing the frequencies at one point in time, stacked 27 | // on top of each other to form a spectrogram showing how those frequencies 28 | // changed over time. 29 | class FeatureProvider { 30 | public: 31 | // Create the provider, and bind it to an area of memory. This memory should 32 | // remain accessible for the lifetime of the provider object, since subsequent 33 | // calls will fill it with feature data. The provider does no memory 34 | // management of this data. 35 | FeatureProvider(int feature_size, int8_t* feature_data); 36 | ~FeatureProvider(); 37 | 38 | // Fills the feature data with information from audio inputs, and returns how 39 | // many feature slices were updated. 40 | TfLiteStatus PopulateFeatureData(tflite::ErrorReporter* error_reporter, 41 | int32_t last_time_in_ms, int32_t time_in_ms, 42 | int* how_many_new_slices); 43 | 44 | private: 45 | int feature_size_; 46 | int8_t* feature_data_; 47 | // Make sure we don't try to use cached information if this is the first call 48 | // into the provider. 49 | bool is_first_run_; 50 | }; 51 | 52 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_FEATURE_PROVIDER_H_ 53 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/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 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/micro_features/micro_features_generator.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_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 | #include "tensorflow/lite/micro/micro_error_reporter.h" 21 | 22 | // Sets up any resources needed for the feature generation pipeline. 23 | TfLiteStatus InitializeMicroFeatures(tflite::ErrorReporter* error_reporter); 24 | 25 | // Converts audio sample data into a more compact form that's appropriate for 26 | // feeding into a neural network. 27 | TfLiteStatus GenerateMicroFeatures(tflite::ErrorReporter* error_reporter, 28 | const int16_t* input, int input_size, 29 | int output_size, int8_t* output, 30 | size_t* num_samples_read); 31 | 32 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MICRO_FEATURES_GENERATOR_H_ 33 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/micro_features/micro_model_settings.cc: -------------------------------------------------------------------------------- 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/examples/micro_speech/micro_features/micro_model_settings.h" 17 | 18 | const char* kCategoryLabels[kCategoryCount] = { 19 | "silence", 20 | "unknown", 21 | "yes", 22 | "no", 23 | }; 24 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/micro_features/micro_model_settings.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_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MICRO_MODEL_SETTINGS_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MICRO_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 | // The size of the input time series data we pass to the FFT to produce the 23 | // frequency information. This has to be a power of two, and since we're dealing 24 | // with 30ms of 16KHz inputs, which means 480 samples, this is the next value. 25 | constexpr int kMaxAudioSampleSize = 512; 26 | constexpr int kAudioSampleFrequency = 16000; 27 | 28 | // The following values are derived from values used during model training. 29 | // If you change the way you preprocess the input, update all these constants. 30 | constexpr int kFeatureSliceSize = 40; 31 | constexpr int kFeatureSliceCount = 49; 32 | constexpr int kFeatureElementCount = (kFeatureSliceSize * kFeatureSliceCount); 33 | constexpr int kFeatureSliceStrideMs = 20; 34 | constexpr int kFeatureSliceDurationMs = 30; 35 | 36 | // Variables for the model's output categories. 37 | constexpr int kSilenceIndex = 0; 38 | constexpr int kUnknownIndex = 1; 39 | // If you modify the output categories, you need to update the following values. 40 | constexpr int kCategoryCount = 4; 41 | extern const char* kCategoryLabels[kCategoryCount]; 42 | 43 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_MICRO_MODEL_SETTINGS_H_ 44 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/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 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/micro_features/no_micro_features_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 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_NO_MICRO_FEATURES_DATA_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_NO_MICRO_FEATURES_DATA_H_ 18 | 19 | extern const int g_no_micro_f9643d42_nohash_4_width; 20 | extern const int g_no_micro_f9643d42_nohash_4_height; 21 | extern const signed char g_no_micro_f9643d42_nohash_4_data[]; 22 | 23 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_NO_MICRO_FEATURES_DATA_H_ 24 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/examples/micro_speech/micro_features/yes_micro_features_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 | #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_YES_MICRO_FEATURES_DATA_H_ 17 | #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_YES_MICRO_FEATURES_DATA_H_ 18 | 19 | extern const int g_yes_micro_f2e59fea_nohash_1_width; 20 | extern const int g_yes_micro_f2e59fea_nohash_1_height; 21 | extern const signed char g_yes_micro_f2e59fea_nohash_1_data[]; 22 | 23 | #endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_MICRO_FEATURES_YES_MICRO_FEATURES_DATA_H_ 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/kernels/ceil.cc: -------------------------------------------------------------------------------- 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/kernels/internal/reference/ceil.h" 17 | 18 | #include "tensorflow/lite/c/common.h" 19 | #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" 20 | #include "tensorflow/lite/kernels/kernel_util.h" 21 | #include "tensorflow/lite/micro/kernels/kernel_util.h" 22 | 23 | namespace tflite { 24 | namespace ops { 25 | namespace micro { 26 | namespace ceil { 27 | 28 | constexpr int kInputTensor = 0; 29 | constexpr int kOutputTensor = 0; 30 | 31 | TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { 32 | const TfLiteTensor* input = GetInput(context, node, kInputTensor); 33 | TfLiteTensor* output = GetOutput(context, node, kOutputTensor); 34 | TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); 35 | TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); 36 | TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); 37 | TF_LITE_ENSURE_TYPES_EQ(context, output->type, input->type); 38 | TF_LITE_ENSURE_EQ(context, output->bytes, input->bytes); 39 | TF_LITE_ENSURE_EQ(context, output->dims->size, input->dims->size); 40 | for (int i = 0; i < output->dims->size; ++i) { 41 | TF_LITE_ENSURE_EQ(context, output->dims->data[i], input->dims->data[i]); 42 | } 43 | return kTfLiteOk; 44 | } 45 | 46 | TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { 47 | const TfLiteEvalTensor* input = 48 | tflite::micro::GetEvalInput(context, node, kInputTensor); 49 | TfLiteEvalTensor* output = 50 | tflite::micro::GetEvalOutput(context, node, kOutputTensor); 51 | 52 | reference_ops::Ceil(tflite::micro::GetTensorShape(input), 53 | tflite::micro::GetTensorData(input), 54 | tflite::micro::GetTensorShape(output), 55 | tflite::micro::GetTensorData(output)); 56 | 57 | return kTfLiteOk; 58 | } 59 | } // namespace ceil 60 | 61 | TfLiteRegistration Register_CEIL() { 62 | return {/*init=*/nullptr, 63 | /*free=*/nullptr, 64 | /*prepare=*/ceil::Prepare, 65 | /*invoke=*/ceil::Eval, 66 | /*profiling_string=*/nullptr, 67 | /*builtin_code=*/0, 68 | /*custom_name=*/nullptr, 69 | /*version=*/0}; 70 | } 71 | 72 | } // namespace micro 73 | } // namespace ops 74 | } // namespace tflite 75 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/kernels/ethosu.cc: -------------------------------------------------------------------------------- 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 | namespace ops { 23 | namespace micro { 24 | namespace custom { 25 | TfLiteRegistration* Register_ETHOSU() { return nullptr; } 26 | 27 | const char* GetString_ETHOSU() { return ""; } 28 | 29 | } // namespace custom 30 | } // namespace micro 31 | } // namespace ops 32 | } // namespace tflite 33 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/kernels/floor.cc: -------------------------------------------------------------------------------- 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/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 | namespace ops { 24 | namespace micro { 25 | namespace floor { 26 | 27 | constexpr int kInputTensor = 0; 28 | constexpr int kOutputTensor = 0; 29 | 30 | TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { 31 | const TfLiteEvalTensor* input = 32 | tflite::micro::GetEvalInput(context, node, kInputTensor); 33 | TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); 34 | TfLiteEvalTensor* output = 35 | tflite::micro::GetEvalOutput(context, node, kOutputTensor); 36 | reference_ops::Floor(tflite::micro::GetTensorShape(input), 37 | tflite::micro::GetTensorData(input), 38 | tflite::micro::GetTensorShape(output), 39 | tflite::micro::GetTensorData(output)); 40 | return kTfLiteOk; 41 | } 42 | } // namespace floor 43 | 44 | TfLiteRegistration Register_FLOOR() { 45 | return {/*init=*/nullptr, 46 | /*free=*/nullptr, 47 | /*prepare=*/nullptr, 48 | /*invoke=*/floor::Eval, 49 | /*profiling_string=*/nullptr, 50 | /*builtin_code=*/0, 51 | /*custom_name=*/nullptr, 52 | /*version=*/0}; 53 | } 54 | 55 | } // namespace micro 56 | } // namespace ops 57 | } // namespace tflite 58 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/kernels/kernel_util.cc: -------------------------------------------------------------------------------- 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 | #include "tensorflow/lite/micro/kernels/kernel_util.h" 17 | 18 | #include "tensorflow/lite/c/common.h" 19 | 20 | namespace tflite { 21 | namespace micro { 22 | 23 | bool HaveSameShapes(const TfLiteEvalTensor* input1, 24 | const TfLiteEvalTensor* input2) { 25 | TFLITE_DCHECK(input1 != nullptr); 26 | TFLITE_DCHECK(input2 != nullptr); 27 | return TfLiteIntArrayEqual(input1->dims, input2->dims); 28 | } 29 | 30 | } // namespace micro 31 | } // namespace tflite 32 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/kernels/micro_utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | Licensed under the Apache License, Version 2.0 (the "License"); 3 | you may not use this file except in compliance with the License. 4 | You may obtain a copy of the License at 5 | http://www.apache.org/licenses/LICENSE-2.0 6 | Unless required by applicable law or agreed to in writing, software 7 | distributed under the License is distributed on an "AS IS" BASIS, 8 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 9 | See the License for the specific language governing permissions and 10 | limitations under the License. 11 | ==============================================================================*/ 12 | #ifndef TENSORFLOW_LITE_MICRO_KERNELS_MICRO_UTILS_H_ 13 | #define TENSORFLOW_LITE_MICRO_KERNELS_MICRO_UTILS_H_ 14 | namespace tflite { 15 | namespace ops { 16 | namespace micro { 17 | 18 | // Same as gtl::Greater but defined here to reduce dependencies and 19 | // binary size for micro environment. 20 | struct Greater { 21 | template 22 | bool operator()(const T& x, const T& y) const { 23 | return x > y; 24 | } 25 | }; 26 | 27 | struct Less { 28 | template 29 | bool operator()(const T& x, const T& y) const { 30 | return x < y; 31 | } 32 | }; 33 | 34 | } // namespace micro 35 | } // namespace ops 36 | } // namespace tflite 37 | #endif // TENSORFLOW_LITE_MICRO_KERNELS_MICRO_UTILS_H_ 38 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/kernels/neg.cc: -------------------------------------------------------------------------------- 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/kernels/internal/reference/neg.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 | namespace ops { 24 | namespace micro { 25 | namespace neg { 26 | 27 | constexpr int kInputTensor = 0; 28 | constexpr int kOutputTensor = 0; 29 | 30 | TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { 31 | const TfLiteEvalTensor* input = 32 | tflite::micro::GetEvalInput(context, node, kInputTensor); 33 | TfLiteEvalTensor* output = 34 | tflite::micro::GetEvalOutput(context, node, kOutputTensor); 35 | switch (input->type) { 36 | // TODO(wangtz): handle for kTfLiteInt8 37 | case kTfLiteFloat32: 38 | reference_ops::Negate(tflite::micro::GetTensorShape(input), 39 | tflite::micro::GetTensorData(input), 40 | tflite::micro::GetTensorShape(output), 41 | tflite::micro::GetTensorData(output)); 42 | break; 43 | default: 44 | TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.", 45 | TfLiteTypeGetName(input->type), input->type); 46 | return kTfLiteError; 47 | } 48 | return kTfLiteOk; 49 | } 50 | 51 | } // namespace neg 52 | 53 | TfLiteRegistration Register_NEG() { 54 | return {/*init=*/nullptr, 55 | /*free=*/nullptr, 56 | /*prepare=*/nullptr, 57 | /*invoke=*/neg::Eval, 58 | /*profiling_string=*/nullptr, 59 | /*builtin_code=*/0, 60 | /*custom_name=*/nullptr, 61 | /*version=*/0}; 62 | } 63 | 64 | } // namespace micro 65 | } // namespace ops 66 | } // namespace tflite 67 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/kernels/round.cc: -------------------------------------------------------------------------------- 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/kernels/internal/reference/round.h" 17 | 18 | #include "tensorflow/lite/c/common.h" 19 | #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" 20 | #include "tensorflow/lite/kernels/kernel_util.h" 21 | #include "tensorflow/lite/micro/kernels/kernel_util.h" 22 | 23 | namespace tflite { 24 | namespace ops { 25 | namespace micro { 26 | namespace round { 27 | 28 | constexpr int kInputTensor = 0; 29 | constexpr int kOutputTensor = 0; 30 | 31 | TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { 32 | const TfLiteTensor* input = GetInput(context, node, kInputTensor); 33 | TfLiteTensor* output = GetOutput(context, node, kOutputTensor); 34 | TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); 35 | TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); 36 | TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32); 37 | TF_LITE_ENSURE_TYPES_EQ(context, output->type, input->type); 38 | TF_LITE_ENSURE_EQ(context, output->bytes, input->bytes); 39 | TF_LITE_ENSURE_EQ(context, output->dims->size, input->dims->size); 40 | for (int i = 0; i < output->dims->size; ++i) { 41 | TF_LITE_ENSURE_EQ(context, output->dims->data[i], input->dims->data[i]); 42 | } 43 | return kTfLiteOk; 44 | } 45 | 46 | TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { 47 | const TfLiteEvalTensor* input = 48 | tflite::micro::GetEvalInput(context, node, kInputTensor); 49 | TfLiteEvalTensor* output = 50 | tflite::micro::GetEvalOutput(context, node, kOutputTensor); 51 | 52 | reference_ops::Round(tflite::micro::GetTensorShape(input), 53 | tflite::micro::GetTensorData(input), 54 | tflite::micro::GetTensorShape(output), 55 | tflite::micro::GetTensorData(output)); 56 | 57 | return kTfLiteOk; 58 | } 59 | } // namespace round 60 | 61 | TfLiteRegistration Register_ROUND() { 62 | return {/*init=*/nullptr, 63 | /*free=*/nullptr, 64 | /*prepare=*/round::Prepare, 65 | /*invoke=*/round::Eval, 66 | /*profiling_string=*/nullptr, 67 | /*builtin_code=*/0, 68 | /*custom_name=*/nullptr, 69 | /*version=*/0}; 70 | } 71 | 72 | } // namespace micro 73 | } // namespace ops 74 | } // namespace tflite 75 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/memory_helpers.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_MEMORY_HELPERS_H_ 16 | #define TENSORFLOW_LITE_MICRO_MEMORY_HELPERS_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include "tensorflow/lite/c/common.h" 22 | #include "tensorflow/lite/core/api/error_reporter.h" 23 | #include "tensorflow/lite/schema/schema_generated.h" 24 | 25 | namespace tflite { 26 | 27 | // Returns the next pointer address aligned to the given alignment. 28 | uint8_t* AlignPointerUp(uint8_t* data, size_t alignment); 29 | 30 | // Returns the previous pointer address aligned to the given alignment. 31 | uint8_t* AlignPointerDown(uint8_t* data, size_t alignment); 32 | 33 | // Returns an increased size that's a multiple of alignment. 34 | size_t AlignSizeUp(size_t size, size_t alignment); 35 | 36 | // Returns size in bytes for a given TfLiteType. 37 | TfLiteStatus TfLiteTypeSizeOf(TfLiteType type, size_t* size); 38 | 39 | // How many bytes are needed to hold a tensor's contents. 40 | TfLiteStatus BytesRequiredForTensor(const tflite::Tensor& flatbuffer_tensor, 41 | size_t* bytes, size_t* type_size, 42 | ErrorReporter* error_reporter); 43 | 44 | // How many bytes are used in a TfLiteEvalTensor instance. The byte length is 45 | // returned in out_bytes. 46 | TfLiteStatus TfLiteEvalTensorByteLength(const TfLiteEvalTensor* eval_tensor, 47 | size_t* out_bytes); 48 | 49 | // Deduce output dimensions from input and allocate given size. 50 | // Useful for operators with two inputs where the largest input should equal the 51 | // output dimension. 52 | TfLiteStatus AllocateOutputDimensionsFromInput(TfLiteContext* context, 53 | const TfLiteTensor* input1, 54 | const TfLiteTensor* input2, 55 | TfLiteTensor* output); 56 | 57 | } // namespace tflite 58 | 59 | #endif // TENSORFLOW_LITE_MICRO_MEMORY_HELPERS_H_ 60 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/memory_planner/linear_memory_planner.cc: -------------------------------------------------------------------------------- 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/memory_planner/linear_memory_planner.h" 17 | 18 | namespace tflite { 19 | 20 | LinearMemoryPlanner::LinearMemoryPlanner() 21 | : current_buffer_count_(0), next_free_offset_(0) {} 22 | LinearMemoryPlanner::~LinearMemoryPlanner() {} 23 | 24 | TfLiteStatus LinearMemoryPlanner::AddBuffer( 25 | tflite::ErrorReporter* error_reporter, int size, int first_time_used, 26 | int last_time_used) { 27 | if (current_buffer_count_ >= kMaxBufferCount) { 28 | TF_LITE_REPORT_ERROR(error_reporter, "Too many buffers (max is %d)", 29 | kMaxBufferCount); 30 | return kTfLiteError; 31 | } 32 | buffer_offsets_[current_buffer_count_] = next_free_offset_; 33 | next_free_offset_ += size; 34 | ++current_buffer_count_; 35 | return kTfLiteOk; 36 | } 37 | 38 | size_t LinearMemoryPlanner::GetMaximumMemorySize() { return next_free_offset_; } 39 | 40 | int LinearMemoryPlanner::GetBufferCount() { return current_buffer_count_; } 41 | 42 | TfLiteStatus LinearMemoryPlanner::GetOffsetForBuffer( 43 | tflite::ErrorReporter* error_reporter, int buffer_index, int* offset) { 44 | if ((buffer_index < 0) || (buffer_index >= current_buffer_count_)) { 45 | TF_LITE_REPORT_ERROR(error_reporter, 46 | "buffer index %d is outside range 0 to %d", 47 | buffer_index, current_buffer_count_); 48 | return kTfLiteError; 49 | } 50 | *offset = buffer_offsets_[buffer_index]; 51 | return kTfLiteOk; 52 | } 53 | 54 | } // namespace tflite 55 | -------------------------------------------------------------------------------- /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/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 MemoryPlanner { 27 | public: 28 | LinearMemoryPlanner(); 29 | ~LinearMemoryPlanner() override; 30 | 31 | TfLiteStatus AddBuffer(tflite::ErrorReporter* error_reporter, int size, 32 | int first_time_used, int last_time_used) override; 33 | 34 | size_t GetMaximumMemorySize() override; 35 | int GetBufferCount() override; 36 | TfLiteStatus GetOffsetForBuffer(tflite::ErrorReporter* error_reporter, 37 | int buffer_index, int* offset) override; 38 | 39 | private: 40 | static constexpr int kMaxBufferCount = 1024; 41 | size_t buffer_offsets_[kMaxBufferCount]; 42 | int current_buffer_count_; 43 | size_t next_free_offset_; 44 | 45 | TF_LITE_REMOVE_VIRTUAL_DELETE 46 | }; 47 | 48 | } // namespace tflite 49 | 50 | #endif // TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_LINEAR_MEMORY_PLANNER_H_ 51 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/micro_error_reporter.cc: -------------------------------------------------------------------------------- 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_error_reporter.h" 17 | 18 | #include 19 | 20 | #ifndef TF_LITE_STRIP_ERROR_STRINGS 21 | #include "tensorflow/lite/micro/debug_log.h" 22 | #include "tensorflow/lite/micro/micro_string.h" 23 | #endif 24 | 25 | namespace tflite { 26 | 27 | int MicroErrorReporter::Report(const char* format, va_list args) { 28 | #ifndef 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 | return 0; 39 | } 40 | 41 | } // namespace tflite 42 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/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_MICRO_ERROR_REPORTER_H_ 16 | #define TENSORFLOW_LITE_MICRO_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 | 25 | class MicroErrorReporter : public ErrorReporter { 26 | public: 27 | ~MicroErrorReporter() override {} 28 | int Report(const char* format, va_list args) override; 29 | 30 | private: 31 | TF_LITE_REMOVE_VIRTUAL_DELETE 32 | }; 33 | 34 | } // namespace tflite 35 | 36 | #endif // TENSORFLOW_LITE_MICRO_MICRO_ERROR_REPORTER_H_ 37 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/micro_optional_debug_tools.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 | // Optional debugging functionality. For small sized binaries, these are not 16 | // needed. 17 | #ifndef TENSORFLOW_LITE_MICRO_MICRO_OPTIONAL_DEBUG_TOOLS_H_ 18 | #define TENSORFLOW_LITE_MICRO_MICRO_OPTIONAL_DEBUG_TOOLS_H_ 19 | 20 | #include "tensorflow/lite/micro/micro_interpreter.h" 21 | 22 | namespace tflite { 23 | // Helper function to print model flatbuffer data. This function is not called 24 | // by default. Hence it's not linked in to the final binary code. 25 | void PrintModelData(const Model* model, ErrorReporter* error_reporter); 26 | // Prints a dump of what tensors and what nodes are in the interpreter. 27 | void PrintInterpreterState(MicroInterpreter* interpreter); 28 | } // namespace tflite 29 | 30 | #endif // TENSORFLOW_LITE_MICRO_MICRO_OPTIONAL_DEBUG_TOOLS_H_ 31 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/micro_profiler.cc: -------------------------------------------------------------------------------- 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 | #include "tensorflow/lite/micro/micro_profiler.h" 17 | 18 | #include "tensorflow/lite/kernels/internal/compatibility.h" 19 | #include "tensorflow/lite/micro/micro_time.h" 20 | 21 | namespace tflite { 22 | 23 | MicroProfiler::MicroProfiler(tflite::ErrorReporter* reporter) 24 | : reporter_(reporter) {} 25 | 26 | uint32_t MicroProfiler::BeginEvent(const char* tag, EventType event_type, 27 | int64_t event_metadata1, 28 | int64_t event_metadata2) { 29 | start_time_ = GetCurrentTimeTicks(); 30 | TFLITE_DCHECK(tag != nullptr); 31 | event_tag_ = tag; 32 | return 0; 33 | } 34 | 35 | void MicroProfiler::EndEvent(uint32_t event_handle) { 36 | #ifndef TF_LITE_STRIP_ERROR_STRINGS 37 | int32_t end_time = GetCurrentTimeTicks(); 38 | TF_LITE_REPORT_ERROR(reporter_, "%s took %d cycles\n", event_tag_, 39 | end_time - start_time_); 40 | #endif 41 | } 42 | } // namespace tflite 43 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/micro_profiler.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_MICRO_PROFILER_H_ 17 | #define TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_ 18 | 19 | #include "tensorflow/lite/core/api/error_reporter.h" 20 | #include "tensorflow/lite/core/api/profiler.h" 21 | #include "tensorflow/lite/micro/compatibility.h" 22 | 23 | namespace tflite { 24 | 25 | // MicroProfiler creates a common way to gain fine-grained insight into runtime 26 | // performance. Bottleck operators can be identified along with slow code 27 | // sections. This can be used in conjunction with running the relevant micro 28 | // benchmark to evaluate end-to-end performance. 29 | // 30 | // Usage example: 31 | // MicroProfiler profiler(error_reporter); 32 | // { 33 | // ScopedProfile scoped_profile(profiler, tag); 34 | // work_to_profile(); 35 | // } 36 | // 37 | // This will call the following methods in order: 38 | // int event_handle = profiler->BeginEvent(op_name, EventType::DEFAULT, 0) 39 | // work_to_profile(); 40 | // profiler->EndEvent(event_handle) 41 | class MicroProfiler : public tflite::Profiler { 42 | public: 43 | explicit MicroProfiler(tflite::ErrorReporter* reporter); 44 | ~MicroProfiler() override = default; 45 | 46 | // AddEvent is unused for Tf Micro. 47 | void AddEvent(const char* tag, EventType event_type, uint64_t start, 48 | uint64_t end, int64_t event_metadata1, 49 | int64_t event_metadata2) override{}; 50 | 51 | // BeginEvent followed by code followed by EndEvent will profile the code 52 | // enclosed. Multiple concurrent events are unsupported, so the return value 53 | // is always 0. Event_metadata1 and event_metadata2 are unused. The tag 54 | // pointer must be valid until EndEvent is called. 55 | uint32_t BeginEvent(const char* tag, EventType event_type, 56 | int64_t event_metadata1, 57 | int64_t event_metadata2) override; 58 | 59 | // Event_handle is ignored since TF Micro does not support concurrent events. 60 | void EndEvent(uint32_t event_handle) override; 61 | 62 | private: 63 | tflite::ErrorReporter* reporter_; 64 | int32_t start_time_; 65 | const char* event_tag_; 66 | TF_LITE_REMOVE_VIRTUAL_DELETE 67 | }; 68 | 69 | } // namespace tflite 70 | 71 | #endif // TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_ 72 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/micro_time.cc: -------------------------------------------------------------------------------- 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 | // Reference implementation of timer functions. Platforms are not required to 17 | // implement these timer methods, but they are required to enable profiling. 18 | 19 | // On platforms that have a POSIX stack or C library, it can be written using 20 | // methods from or clock() from . 21 | 22 | // To add an equivalent function for your own platform, create your own 23 | // implementation file, and place it in a subfolder with named after the OS 24 | // you're targeting. For example, see the Cortex M bare metal version in 25 | // tensorflow/lite/micro/bluepill/micro_time.cc or the mbed one on 26 | // tensorflow/lite/micro/mbed/micro_time.cc. 27 | 28 | #include "tensorflow/lite/micro/micro_time.h" 29 | 30 | namespace tflite { 31 | 32 | //80MHz 33 | constexpr int kClocksPerSecond = 80e6; 34 | 35 | // Reference implementation of the ticks_per_second() function that's required 36 | // for a platform to support Tensorflow Lite for Microcontrollers profiling. 37 | // This returns 0 by default because timing is an optional feature that builds 38 | // without errors on platforms that do not need it. 39 | int32_t ticks_per_second() { return kClocksPerSecond; } 40 | 41 | // Reference implementation of the GetCurrentTimeTicks() function that's 42 | // required for a platform to support Tensorflow Lite for Microcontrollers 43 | // profiling. This returns 0 by default because timing is an optional feature 44 | // that builds without errors on platforms that do not need it. 45 | int32_t GetCurrentTimeTicks() { return 0; } 46 | 47 | } // namespace tflite 48 | -------------------------------------------------------------------------------- /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 | int32_t ticks_per_second(); 25 | 26 | // Return time in ticks. The meaning of a tick varies per platform. 27 | int32_t GetCurrentTimeTicks(); 28 | 29 | } // namespace tflite 30 | 31 | #endif // TENSORFLOW_LITE_MICRO_MICRO_TIME_H_ 32 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/recording_micro_interpreter.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_RECORDING_MICRO_INTERPRETER_H_ 17 | #define TENSORFLOW_LITE_MICRO_RECORDING_MICRO_INTERPRETER_H_ 18 | 19 | #include "tensorflow/lite/micro/micro_interpreter.h" 20 | #include "tensorflow/lite/micro/recording_micro_allocator.h" 21 | 22 | namespace tflite { 23 | 24 | // Utility subclass that enables internal recordings of the MicroInterpreter. 25 | // This class should be used to audit and analyze memory arena usage for a given 26 | // model and interpreter. 27 | // 28 | // After construction and the first Invoke() or AllocateTensors() call - the 29 | // memory usage is recorded and available through the GetMicroAllocator() 30 | // function. See RecordingMicroAlloctor for more details on what is currently 31 | // recorded from arena allocations. 32 | // 33 | // It is recommended for users to increase the tensor arena size by at least 1kb 34 | // to ensure enough additional memory is available for internal recordings. 35 | class RecordingMicroInterpreter : public MicroInterpreter { 36 | public: 37 | RecordingMicroInterpreter(const Model* model, 38 | const MicroOpResolver& op_resolver, 39 | uint8_t* tensor_arena, size_t tensor_arena_size, 40 | ErrorReporter* error_reporter) 41 | : MicroInterpreter(model, op_resolver, 42 | RecordingMicroAllocator::Create( 43 | tensor_arena, tensor_arena_size, error_reporter), 44 | error_reporter), 45 | recording_micro_allocator_( 46 | static_cast(allocator())) {} 47 | 48 | RecordingMicroInterpreter(const Model* model, 49 | const MicroOpResolver& op_resolver, 50 | RecordingMicroAllocator* allocator, 51 | ErrorReporter* error_reporter) 52 | : MicroInterpreter(model, op_resolver, allocator, error_reporter), 53 | recording_micro_allocator_(*allocator) {} 54 | 55 | const RecordingMicroAllocator& GetMicroAllocator() const { 56 | return recording_micro_allocator_; 57 | } 58 | 59 | private: 60 | const RecordingMicroAllocator& recording_micro_allocator_; 61 | }; 62 | 63 | } // namespace tflite 64 | 65 | #endif // TENSORFLOW_LITE_MICRO_RECORDING_MICRO_INTERPRETER_H_ 66 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/recording_simple_memory_allocator.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_RECORDING_SIMPLE_MEMORY_ALLOCATOR_H_ 17 | #define TENSORFLOW_LITE_MICRO_RECORDING_SIMPLE_MEMORY_ALLOCATOR_H_ 18 | 19 | #include "tensorflow/lite/micro/compatibility.h" 20 | #include "tensorflow/lite/micro/simple_memory_allocator.h" 21 | 22 | namespace tflite { 23 | 24 | // Utility class used to log allocations of a SimpleMemoryAllocator. Should only 25 | // be used in debug/evaluation settings or unit tests to evaluate allocation 26 | // usage. 27 | class RecordingSimpleMemoryAllocator : public SimpleMemoryAllocator { 28 | public: 29 | RecordingSimpleMemoryAllocator(ErrorReporter* error_reporter, 30 | uint8_t* buffer_head, size_t buffer_size); 31 | // TODO(b/157615197): Cleanup constructors/destructor and use factory 32 | // functions. 33 | ~RecordingSimpleMemoryAllocator() override; 34 | 35 | static RecordingSimpleMemoryAllocator* Create(ErrorReporter* error_reporter, 36 | uint8_t* buffer_head, 37 | size_t buffer_size); 38 | 39 | // Returns the number of bytes requested from the head or tail. 40 | size_t GetRequestedBytes() const; 41 | 42 | // Returns the number of bytes actually allocated from the head or tail. This 43 | // value will be >= to the number of requested bytes due to padding and 44 | // alignment. 45 | size_t GetUsedBytes() const; 46 | 47 | // Returns the number of alloc calls from the head or tail. 48 | size_t GetAllocatedCount() const; 49 | 50 | TfLiteStatus EnsureHeadSize(size_t size, size_t alignment) override; 51 | uint8_t* AllocateFromTail(size_t size, size_t alignment) override; 52 | 53 | private: 54 | size_t requested_head_bytes_; 55 | size_t requested_tail_bytes_; 56 | size_t used_bytes_; 57 | size_t alloc_count_; 58 | 59 | TF_LITE_REMOVE_VIRTUAL_DELETE 60 | }; 61 | 62 | } // namespace tflite 63 | 64 | #endif // TENSORFLOW_LITE_MICRO_RECORDING_SIMPLE_MEMORY_ALLOCATOR_H_ 65 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/testing/test_conv_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 | #ifndef TENSORFLOW_LITE_MICRO_TESTING_TEST_CONV_MODEL_H_ 17 | #define TENSORFLOW_LITE_MICRO_TESTING_TEST_CONV_MODEL_H_ 18 | 19 | // See generate_test_models.py for updating the contents of this model: 20 | extern const unsigned char kTestConvModelData[]; 21 | extern const unsigned int kTestConvModelDataSize; 22 | 23 | #endif // TENSORFLOW_LITE_MICRO_TESTING_TEST_CONV_MODEL_H_ 24 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Include/arm_nn_tables.h: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------- 2 | * Project: CMSIS NN Library 3 | * Title: arm_nn_tables.h 4 | * Description: Extern declaration for NN tables 5 | * 6 | * $Date: 17. January 2018 7 | * $Revision: V.1.0.0 8 | * 9 | * Target Processor: Cortex-M cores 10 | * -------------------------------------------------------------------- */ 11 | /* 12 | * Copyright (C) 2010-2018 Arm Limited or its affiliates. All rights reserved. 13 | * 14 | * SPDX-License-Identifier: Apache-2.0 15 | * 16 | * Licensed under the Apache License, Version 2.0 (the License); you may 17 | * not use this file except in compliance with the License. 18 | * You may obtain a copy of the License at 19 | * 20 | * www.apache.org/licenses/LICENSE-2.0 21 | * 22 | * Unless required by applicable law or agreed to in writing, software 23 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 25 | * See the License for the specific language governing permissions and 26 | * limitations under the License. 27 | */ 28 | 29 | #ifndef _ARM_NN_TABLES_H 30 | #define _ARM_NN_TABLES_H 31 | 32 | #include "cmsis/CMSIS/DSP/Include/arm_math.h" 33 | 34 | /** 35 | * @brief tables for various activation functions 36 | * 37 | */ 38 | 39 | extern const q15_t sigmoidTable_q15[256]; 40 | extern const q7_t sigmoidTable_q7[256]; 41 | 42 | extern const q7_t tanhTable_q7[256]; 43 | extern const q15_t tanhTable_q15[256]; 44 | 45 | /** 46 | * @brief 2-way tables for various activation functions 47 | * 48 | * 2-way table, H table for value larger than 1/4 49 | * L table for value smaller than 1/4, H table for remaining 50 | * We have this only for the q15_t version. It does not make 51 | * sense to have it for q7_t type 52 | */ 53 | extern const q15_t sigmoidHTable_q15[192]; 54 | extern const q15_t sigmoidLTable_q15[128]; 55 | 56 | #endif /* ARM_NN_TABLES_H */ 57 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/ActivationFunctions/arm_nn_activations_q7.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2018 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_nn_activations_q7.c 22 | * Description: Q7 neural network activation function using direct table look-up 23 | * 24 | * $Date: 17. January 2018 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/DSP/Include/arm_math.h" 32 | #include "cmsis/CMSIS/DSP/Include/arm_common_tables.h" 33 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 34 | 35 | /** 36 | * @ingroup groupNN 37 | */ 38 | 39 | /** 40 | * @addtogroup Acti 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @brief Q7 neural network activation function using direct table look-up 46 | * @param[in,out] data pointer to input 47 | * @param[in] size number of elements 48 | * @param[in] int_width bit-width of the integer part, assume to be smaller than 3 49 | * @param[in] type type of activation functions 50 | * 51 | * @details 52 | * 53 | * This is the direct table look-up approach. 54 | * 55 | * Assume here the integer part of the fixed-point is <= 3. 56 | * More than 3 just not making much sense, makes no difference with 57 | * saturation followed by any of these activation functions. 58 | */ 59 | 60 | void arm_nn_activations_direct_q7(q7_t * data, uint16_t size, uint16_t int_width, arm_nn_activation_type type) 61 | { 62 | uint16_t i = size; 63 | q7_t *pIn = data; 64 | q7_t *pOut = data; 65 | q7_t in; 66 | q7_t out; 67 | uint16_t shift_size = 3 - int_width; 68 | const q7_t *lookup_table; 69 | switch (type) 70 | { 71 | case ARM_SIGMOID: 72 | lookup_table = sigmoidTable_q7; 73 | break; 74 | case ARM_TANH: 75 | default: 76 | lookup_table = tanhTable_q7; 77 | break; 78 | } 79 | while (i) 80 | { 81 | in = *pIn++; 82 | out = lookup_table[(uint8_t) (in >> shift_size)]; 83 | *pOut++ = out; 84 | i--; 85 | } 86 | } 87 | 88 | /** 89 | * @} end of Acti group 90 | */ 91 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/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: Spetember 2019 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/DSP/Include/arm_math.h" 32 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 33 | 34 | /** 35 | * @ingroup groupNN 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 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/ActivationFunctions/arm_relu_q15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2020 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_relu_q15.c 22 | * Description: Q15 version of ReLU 23 | * 24 | * $Date: February 27, 2020 25 | * $Revision: V.1.0.1 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/DSP/Include/arm_math.h" 32 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 33 | 34 | /** 35 | * @ingroup groupNN 36 | */ 37 | 38 | /** 39 | * @addtogroup Acti 40 | * @{ 41 | */ 42 | 43 | /** 44 | * @brief Q15 RELU function 45 | * @param[in,out] data pointer to input 46 | * @param[in] size number of elements 47 | * 48 | * @details 49 | * 50 | * Optimized relu with QSUB instructions. 51 | * 52 | */ 53 | 54 | void arm_relu_q15(q15_t *data, uint16_t size) 55 | { 56 | 57 | #if defined(ARM_MATH_DSP) 58 | /* Run the following code for M cores with DSP extension */ 59 | 60 | uint16_t i = size >> 1; 61 | q15_t *input = data; 62 | q15_t *output = data; 63 | q31_t in; 64 | q31_t buf; 65 | q31_t mask; 66 | 67 | while (i) 68 | { 69 | in = read_q15x2_ia(&input); 70 | 71 | /* extract the first bit */ 72 | buf = __ROR(in & 0x80008000, 15); 73 | 74 | /* if MSB=1, mask will be 0xFF, 0x0 otherwise */ 75 | mask = __QSUB16(0x00000000, buf); 76 | 77 | write_q15x2_ia(&output, in & (~mask)); 78 | i--; 79 | } 80 | 81 | if (size & 0x1) 82 | { 83 | if (*input < 0) 84 | { 85 | *input = 0; 86 | } 87 | input++; 88 | } 89 | #else 90 | /* Run the following code as reference implementation for M cores without DSP extension */ 91 | uint16_t i; 92 | 93 | for (i = 0; i < size; i++) 94 | { 95 | if (data[i] < 0) 96 | data[i] = 0; 97 | } 98 | 99 | #endif /* ARM_MATH_DSP */ 100 | } 101 | 102 | /** 103 | * @} end of Acti group 104 | */ 105 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/ActivationFunctions/arm_relu_q7.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2020 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_relu_q7.c 22 | * Description: Q7 version of ReLU 23 | * 24 | * $Date: May 29, 2020 25 | * $Revision: V.1.0.2 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/DSP/Include/arm_math.h" 32 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 33 | 34 | /** 35 | * @ingroup groupNN 36 | */ 37 | 38 | /** 39 | * @addtogroup Acti 40 | * @{ 41 | */ 42 | 43 | /** 44 | * @brief Q7 RELU function 45 | * @param[in,out] data pointer to input 46 | * @param[in] size number of elements 47 | * 48 | * @details 49 | * 50 | * Optimized relu with QSUB instructions. 51 | * 52 | */ 53 | 54 | void arm_relu_q7(q7_t *data, uint16_t size) 55 | { 56 | 57 | #if defined(ARM_MATH_DSP) 58 | /* Run the following code for M cores with DSP extension */ 59 | 60 | uint16_t i = size >> 2; 61 | q7_t *input = data; 62 | q7_t *output = data; 63 | q31_t in; 64 | q31_t buf; 65 | q31_t mask; 66 | 67 | while (i) 68 | { 69 | in = read_q7x4_ia(&input); 70 | 71 | /* extract the first bit */ 72 | buf = (int32_t)__ROR((uint32_t)in & 0x80808080, 7); 73 | 74 | /* if MSB=1, mask will be 0xFF, 0x0 otherwise */ 75 | mask = __QSUB8(0x00000000, buf); 76 | 77 | write_q7x4_ia(&output, in & (~mask)); 78 | 79 | i--; 80 | } 81 | 82 | i = size & 0x3; 83 | while (i) 84 | { 85 | if (*input < 0) 86 | { 87 | *input = 0; 88 | } 89 | input++; 90 | i--; 91 | } 92 | 93 | #else 94 | /* Run the following code as reference implementation for cores without DSP extension */ 95 | 96 | uint16_t i; 97 | 98 | for (i = 0; i < size; i++) 99 | { 100 | if (data[i] < 0) 101 | data[i] = 0; 102 | } 103 | 104 | #endif 105 | } 106 | 107 | /** 108 | * @} end of Acti group 109 | */ 110 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/ConcatenationFunctions/arm_concatenation_s8_w.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_concatenation_s8_w.c 22 | * Description: s8 version of concatenation along the W axis 23 | * 24 | * $Date: October 2019 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 32 | 33 | /** 34 | * @ingroup groupNN 35 | */ 36 | 37 | /** 38 | * @addtogroup Concatenation 39 | * @{ 40 | */ 41 | 42 | /* 43 | * s8 version of concatenation along the W axis 44 | * 45 | * Refer to header file for details. 46 | * 47 | */ 48 | void arm_concatenation_s8_w(const int8_t *input, 49 | const uint16_t input_x, 50 | const uint16_t input_y, 51 | const uint16_t input_z, 52 | const uint16_t input_w, 53 | int8_t *output, 54 | const uint32_t offset_w) 55 | { 56 | const uint32_t input_copy_size = input_x * input_y * input_z * input_w; 57 | 58 | output += offset_w * (input_x * input_y * input_z); 59 | 60 | memcpy(output, input, input_copy_size); 61 | } 62 | 63 | /** 64 | * @} end of Concatenation group 65 | */ 66 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/ConcatenationFunctions/arm_concatenation_s8_x.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_concatenation_s8_x.c 22 | * Description: s8 version of concatenation along the X axis 23 | * 24 | * $Date: October 2019 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 32 | 33 | /** 34 | * @ingroup groupNN 35 | */ 36 | 37 | /** 38 | * @addtogroup Concatenation 39 | * @{ 40 | */ 41 | 42 | /* 43 | * s8 version of concatenation along the X axis 44 | * 45 | * Refer to header file for details. 46 | * 47 | */ 48 | void arm_concatenation_s8_x(const int8_t *input, 49 | const uint16_t input_x, 50 | const uint16_t input_y, 51 | const uint16_t input_z, 52 | const uint16_t input_w, 53 | int8_t *output, 54 | const uint16_t output_x, 55 | const uint32_t offset_x) 56 | { 57 | const uint32_t num_iterations = input_y * input_z * input_w; 58 | 59 | output += offset_x; 60 | 61 | uint32_t i; 62 | 63 | // Copy per row 64 | for (i = 0; i < num_iterations; ++i) 65 | { 66 | memcpy(output, input, input_x); 67 | input += input_x; 68 | output += output_x; 69 | } 70 | } 71 | 72 | /** 73 | * @} end of Concatenation group 74 | */ 75 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/ConcatenationFunctions/arm_concatenation_s8_y.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_concatenation_s8_y.c 22 | * Description: s8 version of concatenation along the Y axis 23 | * 24 | * $Date: October 2019 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 32 | 33 | /** 34 | * @ingroup groupNN 35 | */ 36 | 37 | /** 38 | * @addtogroup Concatenation 39 | * @{ 40 | */ 41 | 42 | /* 43 | * s8 version of concatenation along the Y axis 44 | * 45 | * Refer to header file for details. 46 | * 47 | */ 48 | void arm_concatenation_s8_y(const int8_t *input, 49 | const uint16_t input_x, 50 | const uint16_t input_y, 51 | const uint16_t input_z, 52 | const uint16_t input_w, 53 | int8_t *output, 54 | const uint16_t output_y, 55 | const uint32_t offset_y) 56 | { 57 | const uint32_t num_iterations = input_z * input_w; 58 | const uint32_t input_copy_size = input_x * input_y; 59 | const uint32_t output_stride = input_x * output_y; 60 | 61 | output += offset_y * input_x; 62 | uint32_t i; 63 | 64 | // Copy per tile 65 | for (i = 0; i < num_iterations; ++i) 66 | { 67 | memcpy(output, input, input_copy_size); 68 | input += input_copy_size; 69 | output += output_stride; 70 | } 71 | } 72 | 73 | /** 74 | * @} end of Concatenation group 75 | */ 76 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/ConcatenationFunctions/arm_concatenation_s8_z.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_concatenation_s8_z.c 22 | * Description: s8 version of concatenation along the Z axis 23 | * 24 | * $Date: October 2019 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 32 | 33 | /** 34 | * @ingroup groupNN 35 | */ 36 | 37 | /** 38 | * @addtogroup Concatenation 39 | * @{ 40 | */ 41 | 42 | /* 43 | * s8 version of concatenation along the Z axis 44 | * 45 | * Refer to header file for details. 46 | * 47 | */ 48 | void arm_concatenation_s8_z(const int8_t *input, 49 | const uint16_t input_x, 50 | const uint16_t input_y, 51 | const uint16_t input_z, 52 | const uint16_t input_w, 53 | int8_t *output, 54 | const uint16_t output_z, 55 | const uint32_t offset_z) 56 | { 57 | const uint32_t input_copy_size = input_x * input_y * input_z; 58 | const uint32_t output_stride = input_x * input_y * output_z; 59 | 60 | output += offset_z * (input_x * input_y); 61 | 62 | uint32_t i; 63 | 64 | for (i = 0; i < input_w; ++i) 65 | { 66 | memcpy(output, input, input_copy_size); 67 | input += input_copy_size; 68 | output += output_stride; 69 | } 70 | } 71 | 72 | /** 73 | * @} end of Concatenation group 74 | */ 75 | -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/NNSupportFunctions/arm_nn_accumulate_q7_to_q15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2020 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_nn_accumulate_q7_to_q15.c 22 | * Description: Accumulate q7 vector into q15 one. 23 | * 24 | * $Date: May 29, 2020 25 | * $Revision: V.1.0.1 26 | * 27 | * pSrc Processor: Cortex-M CPUs 28 | * 29 | * -------------------------------------------------------------------- */ 30 | #include "cmsis/CMSIS/DSP/Include/arm_math.h" 31 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 32 | 33 | /** 34 | * @ingroup groupSupport 35 | */ 36 | 37 | /** 38 | * @addtogroup NNBasicMath 39 | * @{ 40 | */ 41 | 42 | void arm_nn_accumulate_q7_to_q15(q15_t *pDst, const q7_t *pSrc, uint32_t length) 43 | { 44 | q15_t *pCnt = pDst; 45 | const q7_t *pV = pSrc; 46 | q31_t v1, v2, vo1, vo2; 47 | int32_t cnt = length >> 2; 48 | q31_t in; 49 | 50 | while (cnt > 0l) 51 | { 52 | q31_t value = arm_nn_read_q7x4_ia(&pV); 53 | v1 = __SXTB16(__ROR((uint32_t)value, 8)); 54 | v2 = __SXTB16(value); 55 | #ifndef ARM_MATH_BIG_ENDIAN 56 | vo2 = (q31_t)__PKHTB(v1, v2, 16); 57 | vo1 = (q31_t)__PKHBT(v2, v1, 16); 58 | #else 59 | vo1 = (q31_t)__PKHTB(v1, v2, 16); 60 | vo2 = (q31_t)__PKHBT(v2, v1, 16); 61 | #endif 62 | 63 | in = arm_nn_read_q15x2(pCnt); 64 | write_q15x2_ia(&pCnt, __QADD16(vo1, in)); 65 | 66 | in = arm_nn_read_q15x2(pCnt); 67 | write_q15x2_ia(&pCnt, __QADD16(vo2, in)); 68 | 69 | cnt--; 70 | } 71 | cnt = length & 0x3; 72 | while (cnt > 0l) 73 | { 74 | *pCnt++ += *pV++; 75 | cnt--; 76 | } 77 | } 78 | 79 | /** 80 | * @} end of NNBasicMath group 81 | */ -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/NNSupportFunctions/arm_nn_add_q7.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2018 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_nn_add_q7.c 22 | * Description: Non saturating addition of elements of a q7 vector. 23 | * 24 | * $Date: July 2019 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | #include "cmsis/CMSIS/DSP/Include/arm_math.h" 31 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 32 | 33 | /** 34 | * @ingroup groupSupport 35 | */ 36 | 37 | /** 38 | * @addtogroup NNBasicMath 39 | * @{ 40 | */ 41 | 42 | void arm_nn_add_q7(const q7_t *input, q31_t *output, uint32_t block_size) 43 | { 44 | uint32_t block_count; 45 | q31_t result = 0; 46 | #if defined(ARM_MATH_DSP) 47 | /* Loop unrolling: Compute 4 outputs at a time */ 48 | block_count = block_size >> 2U; 49 | 50 | while (block_count > 0U) 51 | { 52 | const int32_t mult_q15x2 = (1UL << 16) | 1UL; 53 | q31_t in_q7x4 = arm_nn_read_q7x4_ia(&input); 54 | q31_t temp_q15x2 = __SXTAB16(__SXTB16(in_q7x4), 55 | __ROR((uint32_t)in_q7x4, 8)); 56 | 57 | result = __SMLAD(temp_q15x2, mult_q15x2, result); 58 | 59 | /* Decrement loop counter */ 60 | block_count--; 61 | } 62 | 63 | /* Loop unrolling: Compute remaining outputs */ 64 | block_count = block_size & 0x3; 65 | #else 66 | block_count = block_size; 67 | #endif 68 | while (block_count > 0U) 69 | { 70 | /* Add and store result in destination buffer. */ 71 | result += *input++; 72 | 73 | /* Decrement loop counter */ 74 | block_count--; 75 | } 76 | 77 | *output = result; 78 | } 79 | 80 | /** 81 | * @} end of NNBasicMath group 82 | */ -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/ReshapeFunctions/arm_reshape_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_reshape_s8.c 22 | * Description: Reshape a s8 vector 23 | * 24 | * $Date: September 2019 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 32 | 33 | /** 34 | * @ingroup groupNN 35 | */ 36 | 37 | /** 38 | * @addtogroup Reshape 39 | * @{ 40 | */ 41 | 42 | /** 43 | * Basic s8 reshape function. 44 | * 45 | * Refer header file for details. 46 | * 47 | */ 48 | 49 | void arm_reshape_s8(const int8_t *input, 50 | int8_t *output, 51 | const uint32_t total_size) 52 | { 53 | memcpy(output, input, total_size); 54 | } 55 | 56 | /** 57 | * @} end of Reshape group 58 | */ -------------------------------------------------------------------------------- /tensorflow/lite/micro/tools/cmsis/CMSIS/NN/Source/SoftmaxFunctions/arm_softmax_with_batch_q7.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_softmax_with_batch_q7.c 22 | * Description: Q7 softmax function 23 | * 24 | * $Date: 05. August 2019 25 | * $Revision: V.1.0.0 26 | * 27 | * Target Processor: Cortex-M and Cortex-A cores 28 | * 29 | * -------------------------------------------------------------------- */ 30 | 31 | #include "cmsis/CMSIS/DSP/Include/arm_math.h" 32 | #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h" 33 | 34 | /** 35 | * @ingroup groupNN 36 | */ 37 | 38 | /** 39 | * @addtogroup Softmax 40 | * @{ 41 | */ 42 | 43 | /** 44 | * @brief Q7 softmax function with batch parameter 45 | * @param[in] vec_in pointer to input vector 46 | * @param[in] nb_batches number of batches 47 | * @param[in] dim_vec input vector dimention 48 | * @param[out] p_out pointer to output vector 49 | * 50 | * @details 51 | * 52 | * Here, instead of typical natural logarithm e based softmax, we use 53 | * 2-based softmax here, i.e.,: 54 | * 55 | * y_i = 2^(x_i) / sum(2^x_j) 56 | * 57 | * The relative output will be different here. 58 | * But mathematically, the gradient will be the same 59 | * with a log(2) scaling factor. 60 | * 61 | */ 62 | 63 | void arm_softmax_with_batch_q7(const q7_t * vec_in, const uint16_t nb_batches,const uint16_t dim_vec, q7_t * p_out ) 64 | { 65 | for(int i=0; i 20 | 21 | namespace tflite { 22 | 23 | using std::string; 24 | 25 | } // namespace tflite 26 | 27 | #endif // TENSORFLOW_LITE_STRING_TYPE_H_ 28 | -------------------------------------------------------------------------------- /tensorflow/lite/type_to_tflitetype.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_TYPE_TO_TFLITETYPE_H_ 16 | #define TENSORFLOW_LITE_TYPE_TO_TFLITETYPE_H_ 17 | 18 | // Arduino build defines abs as a macro here. That is invalid C++, and breaks 19 | // libc++'s header, undefine it. 20 | #ifdef abs 21 | #undef abs 22 | #endif 23 | 24 | #include 25 | #include 26 | 27 | #include "tensorflow/lite/c/common.h" 28 | 29 | namespace tflite { 30 | 31 | // Map statically from a C++ type to a TfLiteType. Used in interpreter for 32 | // safe casts. 33 | // Example: 34 | // typeToTfLiteType() -> kTfLiteBool 35 | template 36 | constexpr TfLiteType typeToTfLiteType() { 37 | return kTfLiteNoType; 38 | } 39 | // Map from TfLiteType to the corresponding C++ type. 40 | // Example: 41 | // TfLiteTypeToType::Type -> bool 42 | template 43 | struct TfLiteTypeToType {}; // Specializations below 44 | 45 | // Template specialization for both typeToTfLiteType and TfLiteTypeToType. 46 | #define MATCH_TYPE_AND_TFLITE_TYPE(CPP_TYPE, TFLITE_TYPE_ENUM) \ 47 | template <> \ 48 | constexpr TfLiteType typeToTfLiteType() { \ 49 | return TFLITE_TYPE_ENUM; \ 50 | } \ 51 | template <> \ 52 | struct TfLiteTypeToType { \ 53 | using Type = CPP_TYPE; \ 54 | } 55 | 56 | MATCH_TYPE_AND_TFLITE_TYPE(int, kTfLiteInt32); 57 | MATCH_TYPE_AND_TFLITE_TYPE(int16_t, kTfLiteInt16); 58 | MATCH_TYPE_AND_TFLITE_TYPE(int64_t, kTfLiteInt64); 59 | MATCH_TYPE_AND_TFLITE_TYPE(float, kTfLiteFloat32); 60 | MATCH_TYPE_AND_TFLITE_TYPE(unsigned char, kTfLiteUInt8); 61 | MATCH_TYPE_AND_TFLITE_TYPE(int8_t, kTfLiteInt8); 62 | MATCH_TYPE_AND_TFLITE_TYPE(bool, kTfLiteBool); 63 | MATCH_TYPE_AND_TFLITE_TYPE(std::complex, kTfLiteComplex64); 64 | MATCH_TYPE_AND_TFLITE_TYPE(std::complex, kTfLiteComplex128); 65 | MATCH_TYPE_AND_TFLITE_TYPE(std::string, kTfLiteString); 66 | MATCH_TYPE_AND_TFLITE_TYPE(TfLiteFloat16, kTfLiteFloat16); 67 | MATCH_TYPE_AND_TFLITE_TYPE(double, kTfLiteFloat64); 68 | 69 | } // namespace tflite 70 | #endif // TENSORFLOW_LITE_TYPE_TO_TFLITETYPE_H_ 71 | -------------------------------------------------------------------------------- /tensorflow/lite/version.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_VERSION_H_ 16 | #define TENSORFLOW_LITE_VERSION_H_ 17 | 18 | #include "tensorflow/core/public/version.h" 19 | 20 | // The version number of the Schema. Ideally all changes will be backward 21 | // compatible. If that ever changes, we must ensure that version is the first 22 | // entry in the new tflite root so that we can see that version is not 1. 23 | #define TFLITE_SCHEMA_VERSION (3) 24 | 25 | // TensorFlow Lite Runtime version. 26 | // This value is currently shared with that of TensorFlow. 27 | #define TFLITE_VERSION_STRING TF_VERSION_STRING 28 | 29 | #endif // TENSORFLOW_LITE_VERSION_H_ 30 | -------------------------------------------------------------------------------- /third_party/SConscript: -------------------------------------------------------------------------------- 1 | # RT-Thread building script for bridge 2 | 3 | import os 4 | from building import * 5 | 6 | cwd = GetCurrentDir() 7 | objs = [] 8 | list = os.listdir(cwd) 9 | 10 | if GetDepend('PKG_USING_TENSORFLOWLITEMICRO'): 11 | for d in list: 12 | path = os.path.join(cwd, d) 13 | if os.path.isfile(os.path.join(path, 'SConscript')): 14 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 15 | 16 | Return('objs') 17 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /third_party/kissfft/SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | import os 3 | 4 | cwd = GetCurrentDir() 5 | src = Glob('tools/*.c') + Glob('*.c') 6 | 7 | #. 8 | root = str(Dir('#')) 9 | packages = os.path.join(root, 'packages') 10 | file_list = os.listdir(packages) 11 | for f in file_list: 12 | if(f.split('-')[0] == 'TensorflowLiteMicro'): 13 | tflm_pkg = os.path.join(packages, f) 14 | break 15 | #./third_party/flatbuffer/include 16 | flatbuffer = os.path.join(tflm_pkg, "third_party/flatbuffers/include") 17 | #./third_party/gemmlowp 18 | gemmlowp = os.path.join(tflm_pkg, "third_party/gemmlowp") 19 | #./third_party/kissfft 20 | kissfft = os.path.join(tflm_pkg, "third_party/kissfft") 21 | #./third_party/ruy 22 | ruy = os.path.join(tflm_pkg, "third_party/ruy") 23 | 24 | 25 | CPPPATH = [tflm_pkg, flatbuffer, gemmlowp, kissfft, ruy] 26 | 27 | group = DefineGroup('third_party', src, depend = ['PKG_USING_TENSORFLOWLITEMICRO'], CPPPATH = CPPPATH) 28 | 29 | Return('group') 30 | -------------------------------------------------------------------------------- /third_party/kissfft/tools/kiss_fftr.h: -------------------------------------------------------------------------------- 1 | #ifndef KISS_FTR_H 2 | #define KISS_FTR_H 3 | 4 | #include "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 | --------------------------------------------------------------------------------