diff --git a/app/boards/intel_adsp_ace15_mtpm.conf b/app/boards/intel_adsp_ace15_mtpm.conf index 0b5472cd282a..a660f20b8387 100644 --- a/app/boards/intel_adsp_ace15_mtpm.conf +++ b/app/boards/intel_adsp_ace15_mtpm.conf @@ -17,6 +17,11 @@ CONFIG_FORMAT_CONVERT_HIFI3=n CONFIG_SAMPLE_KEYPHRASE=y CONFIG_COMP_PHASE_VOCODER=y CONFIG_COMP_STFT_PROCESS=y +CONFIG_SOF_STAGING=y +CONFIG_CPP=y +CONFIG_STD_CPP17=y +CONFIG_COMP_TENSORFLOW=m +CONFIG_STACK_SIZE_EDF=32768 # SOF / audio modules / mocks # This mock is part of official sof-bin releases because the CI that @@ -52,7 +57,7 @@ CONFIG_SOF_LOG_LEVEL_INF=y CONFIG_DEBUG_COREDUMP=y CONFIG_DEBUG_COREDUMP_BACKEND_INTEL_ADSP_MEM_WINDOW=y CONFIG_DEBUG_COREDUMP_MEMORY_DUMP_MIN=y -CONFIG_HEAP_MEM_POOL_SIZE=8192 +CONFIG_HEAP_MEM_POOL_SIZE=32768 CONFIG_LLEXT=y CONFIG_LLEXT_STORAGE_WRITABLE=y CONFIG_LLEXT_EXPERIMENTAL=y diff --git a/app/boards/intel_adsp_ace30_ptl.conf b/app/boards/intel_adsp_ace30_ptl.conf index b6ac41938398..b514c5e1e756 100644 --- a/app/boards/intel_adsp_ace30_ptl.conf +++ b/app/boards/intel_adsp_ace30_ptl.conf @@ -15,6 +15,12 @@ CONFIG_FORMAT_CONVERT_HIFI3=n CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING=m CONFIG_GOOGLE_RTC_AUDIO_PROCESSING_MOCK=y CONFIG_COMP_STFT_PROCESS=y +CONFIG_SOF_STAGING=y +CONFIG_COMP_KPB=y +CONFIG_COMP_TENSORFLOW=y +CONFIG_COMP_MFCC=y +CONFIG_COMP_VOLUME=y +CONFIG_COMP_GAIN=y # SOF / infrastructure CONFIG_KCPS_DYNAMIC_CLOCK_CONTROL=n @@ -29,7 +35,7 @@ CONFIG_COLD_STORE_EXECUTE_DRAM=y CONFIG_INTEL_MODULES=y CONFIG_LIBRARY_AUTH_SUPPORT=y CONFIG_LIBRARY_MANAGER=y -CONFIG_LIBRARY_BASE_ADDRESS=0xa0688000 +CONFIG_LIBRARY_BASE_ADDRESS=0xa0700000 CONFIG_LIBRARY_BUILD_LIB=y CONFIG_LIBRARY_DEFAULT_MODULAR=y diff --git a/app/boards/intel_adsp_cavs25.conf b/app/boards/intel_adsp_cavs25.conf index 7cd938ec7ff8..e1c6df6e44f6 100644 --- a/app/boards/intel_adsp_cavs25.conf +++ b/app/boards/intel_adsp_cavs25.conf @@ -12,6 +12,13 @@ CONFIG_COMP_MFCC=y CONFIG_COMP_MULTIBAND_DRC=y CONFIG_COMP_VOLUME_WINDOWS_FADE=y CONFIG_FORMAT_CONVERT_HIFI3=n +CONFIG_SOF_STAGING=y +CONFIG_CPP=y +CONFIG_STD_CPP17=y +# cavs2.5 has no LLEXT/module-manager support (see CONFIG_LIBRARY_MANAGER=n +# below), so build tensorflow in statically rather than as an LLEXT module. +CONFIG_COMP_TENSORFLOW=y +CONFIG_STACK_SIZE_EDF=32768 CONFIG_PCM_CONVERTER_FORMAT_S16LE=y CONFIG_PCM_CONVERTER_FORMAT_S24LE=y CONFIG_PCM_CONVERTER_FORMAT_S32LE=y @@ -33,7 +40,8 @@ CONFIG_SOF_LOG_LEVEL_INF=y CONFIG_DEBUG_COREDUMP=y CONFIG_DEBUG_COREDUMP_BACKEND_INTEL_ADSP_MEM_WINDOW=y CONFIG_DEBUG_COREDUMP_MEMORY_DUMP_MIN=y -CONFIG_HEAP_MEM_POOL_SIZE=8192 +CONFIG_HEAP_MEM_POOL_SIZE=32768 +CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=32768 # Zephyr / device drivers CONFIG_DAI_INIT_PRIORITY=70 diff --git a/app/llext_relocatable.conf b/app/llext_relocatable.conf index 76b5339e1bb0..ce8dafe3a6aa 100644 --- a/app/llext_relocatable.conf +++ b/app/llext_relocatable.conf @@ -1 +1,2 @@ CONFIG_LLEXT_TYPE_ELF_RELOCATABLE=y +CONFIG_LLEXT_EXPORT_BUILTINS_BY_SLID=y diff --git a/scripts/llext_link_helper.py b/scripts/llext_link_helper.py index f10777c9918f..c90ffd629c69 100755 --- a/scripts/llext_link_helper.py +++ b/scripts/llext_link_helper.py @@ -77,6 +77,23 @@ def main(): command = [args.command] + # If the linker command is clang, extract --target= and --ld-path= from params + # and add them to the command (before the linker flags). Without --target, + # clang uses the host target (x86_64) when invoking the linker, which fails + # for cross-compiled (e.g. xtensa) object files. + clang_flags_to_hoist = [] + remaining_params = [] + for p_arg in args.params: + if p_arg.startswith('--target=') or p_arg.startswith('--ld-path='): + clang_flags_to_hoist.append(p_arg) + else: + remaining_params.append(p_arg) + if clang_flags_to_hoist: + command.extend(clang_flags_to_hoist) + args.params = remaining_params + + is_relocatable = '-r' in args.params + executable = [] writable = [] readonly = [] @@ -111,7 +128,8 @@ def main(): text_found = True text_addr = max_alignment(text_addr, 0x1000, s_alignment) text_size = s_size - command.append(f'-Wl,-Ttext=0x{text_addr:x}') + if not is_relocatable: + command.append(f'-Wl,-Ttext=0x{text_addr:x}') else: executable.append(section) @@ -164,7 +182,8 @@ def main(): dram_addr = align_up(dram_addr, s_alignment) - command.append(f'-Wl,--section-start={s_name}=0x{dram_addr:x}') + if not is_relocatable: + command.append(f'-Wl,--section-start={s_name}=0x{dram_addr:x}') dram_addr += section.header['sh_size'] @@ -177,7 +196,8 @@ def main(): dram_addr = align_up(dram_addr, s_alignment) - command.append(f'-Wl,--section-start={s_name}=0x{dram_addr:x}') + if not is_relocatable: + command.append(f'-Wl,--section-start={s_name}=0x{dram_addr:x}') dram_addr += section.header['sh_size'] @@ -189,7 +209,8 @@ def main(): start_addr = align_up(start_addr, s_alignment) - command.append(f'-Wl,--section-start={s_name}=0x{start_addr:x}') + if not is_relocatable: + command.append(f'-Wl,--section-start={s_name}=0x{start_addr:x}') start_addr += section.header['sh_size'] @@ -201,12 +222,22 @@ def main(): start_addr = align_up(start_addr, s_alignment) - if s_name == '.data': - command.append(f'-Wl,-Tdata=0x{start_addr:x}') - else: - command.append(f'-Wl,--section-start={s_name}=0x{start_addr:x}') - - start_addr += section.header['sh_size'] + if not is_relocatable: + if s_name == '.data': + command.append(f'-Wl,-Tdata=0x{start_addr:x}') + else: + command.append(f'-Wl,--section-start={s_name}=0x{start_addr:x}') + + ld_script_path = pathlib.Path(args.file).parent / 'llext_merge.ld' + with open(ld_script_path, 'w') as f_ld: + f_ld.write('SECTIONS\n{\n' + ' .text : { *(.text .text.* .stub .gnu.linkonce.t.*) }\n' + ' .literal : { *(.literal .literal.*) }\n' + ' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }\n' + ' .data : { *(.data .data.* .gnu.linkonce.d.*) }\n' + ' .bss : { *(.bss .bss.* COMMON) }\n' + '}\n') + command.append(f'-Wl,-T,{ld_script_path}') command.extend(['-o', f'{args.file}.tmp']) command.extend(args.params) @@ -215,6 +246,18 @@ def main(): copy_command = [args.copy] + copy_command.extend([ + '--strip-debug', + '--remove-section=.comment', + '--remove-section=.llvm_addrsig', + '--remove-section=.eh_frame', + '--remove-section=.rela.eh_frame', + '--remove-section=.xt.prop', + '--remove-section=.rela.xt.prop', + '--remove-section=.xt.lit', + '--remove-section=.rela.xt.lit', + ]) + if first_dram_text: copy_command.extend(['--set-section-alignment', f'{first_dram_text}=4096']) if first_dram_rodata: diff --git a/scripts/llext_offset_calc.py b/scripts/llext_offset_calc.py index 0f302a8cbe12..2a07984b725f 100755 --- a/scripts/llext_offset_calc.py +++ b/scripts/llext_offset_calc.py @@ -47,6 +47,9 @@ def get_elf_size(elf_name): if section.header['sh_addr'] + section.header['sh_size'] > end: end = section.header['sh_addr'] + section.header['sh_size'] + if start == 0xffffffff: + return 0 + size = end - start return size diff --git a/src/audio/mfcc/tune/setup_mfcc.m b/src/audio/mfcc/tune/setup_mfcc.m index dbf69587a74f..e1f9ac4e57ed 100644 --- a/src/audio/mfcc/tune/setup_mfcc.m +++ b/src/audio/mfcc/tune/setup_mfcc.m @@ -31,6 +31,16 @@ function setup_mfcc() setup.tplg_fn = 'mel80_compress.conf'; export_mfcc_setup(gen_cfg, setup); + % Blob for 40-bin/20ms-hop mel spectrogram, matching TFLM micro_speech's + % front-end shape (TFLM_FEATURE_SIZE=40, TFLM_FEATURE_STRIDE_MS=20, + % TFLM_FEATURE_DURATION_MS=30) for interim wake-word sanity-checking. + setup = get_mel_spectrogram_config(); + setup.frame_length = 30.0; % 480 samples at 16 kHz + setup.frame_shift = 20.0; % 320 samples at 16 kHz + setup.num_mel_bins = 40; + setup.tplg_fn = 'mel40.conf'; + export_mfcc_setup(gen_cfg, setup); + % Blob for mel spectrogram with compress PCM output and DTX setup = get_mel_spectrogram_config(); setup.compress_output = true; diff --git a/src/audio/tensorflow/CMakeLists.txt b/src/audio/tensorflow/CMakeLists.txt index 5cb3086f46b1..895d19745d1d 100644 --- a/src/audio/tensorflow/CMakeLists.txt +++ b/src/audio/tensorflow/CMakeLists.txt @@ -1,6 +1,11 @@ # Copyright (c) 2025 Intel Corporation. # SPDX-License-Identifier: Apache-2.0 +# -mllvm --text-section-literals=false is a Clang-only flag; guard it +if(CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + add_compile_options(-mllvm --text-section-literals=false) +endif() + # are we building the llext module ? if(CONFIG_COMP_TENSORFLOW STREQUAL "m" AND DEFINED CONFIG_LLEXT) add_subdirectory(llext ${PROJECT_BINARY_DIR}/tflm_llext) @@ -8,7 +13,16 @@ if(CONFIG_COMP_TENSORFLOW STREQUAL "m" AND DEFINED CONFIG_LLEXT) return() endif() -# TODO: detect Hifi4/5 for NN lib kernels +# nnlib-hifi4's NN kernels are HiFi4-specific (HiFi4 TIE intrinsics/headers); +# they don't exist for HiFi3 targets (e.g. tgl/cavs2.5) and there is no +# nnlib-hifi3 equivalent checked out. Gate on the actual core ISA +# (CONFIG_XTENSA_HIFI4), not just "is the compiler Clang" -- Clang is also +# used to build for HiFi3 targets, where this library must not be built. +set(TENSORFLOW_HAVE_NNLIB_HIFI4 FALSE) +if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CONFIG_XTENSA_HIFI4) + set(TENSORFLOW_HAVE_NNLIB_HIFI4 TRUE) +endif() + set(NN_HIFI_PATH ${sof_top_dir}/../nnlib-hifi4/xa_nnlib) # paths for dependencies @@ -17,64 +31,42 @@ set(FLATBUFFERS_PATH ${sof_top_dir}/../flatbuffers) set(GEMMLOWP_PATH ${sof_top_dir}/../gemmlowp) set(RUY_PATH ${sof_top_dir}/../ruy) +# Toolchain include dirs are per-platform (SOC_TOOLCHAIN_NAME matches both +# the zephyr-sdk gnu/xtensa-_zephyr-elf toolchain dir and the +# modules/hal/xtensa/zephyr/soc/ HAL dir for every supported SoC). +set(TFLM_TOOLCHAIN_INCLUDE_ROOT + ${ZEPHYR_SDK_INSTALL_DIR}/gnu/xtensa-${SOC_TOOLCHAIN_NAME}_zephyr-elf/xtensa-${SOC_TOOLCHAIN_NAME}_zephyr-elf/include) +set(TFLM_HAL_SOC_PATH ${sof_top_dir}/../modules/hal/xtensa/zephyr/soc/${SOC_TOOLCHAIN_NAME}) + +if(TENSORFLOW_HAVE_NNLIB_HIFI4) + add_library(nn_hifi_lib STATIC ${NN_HIFI_PATH}/algo/common/src/xa_nnlib_common_api.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_pad_8.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_stride_slice_int16.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_batch_to_space_nd_8.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_transpose_8.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_depth_to_space_8.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_pad_16.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_stride_slice_int32.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_space_to_batch_nd_8.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_pad_32.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_stride_slice_int8.c - ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_space_to_depth_8.c ${NN_HIFI_PATH}/algo/kernels/activations/hifi4/xa_nn_activations_32_32.c ${NN_HIFI_PATH}/algo/kernels/activations/hifi4/xa_nn_activations_f32_f32.c - ${NN_HIFI_PATH}/algo/kernels/activations/hifi4/xa_nn_activations_sym16_sym16.c - ${NN_HIFI_PATH}/algo/kernels/activations/hifi4/xa_nn_softmax_asym8_asym8.c - ${NN_HIFI_PATH}/algo/kernels/activations/hifi4/xa_nn_activations_32_16.c - ${NN_HIFI_PATH}/algo/kernels/activations/hifi4/xa_nn_activations_16_16.c - ${NN_HIFI_PATH}/algo/kernels/activations/hifi4/xa_nn_activations_8_8.c - ${NN_HIFI_PATH}/algo/kernels/activations/hifi4/xa_nn_activations_asym16_asym16.c - ${NN_HIFI_PATH}/algo/kernels/activations/hifi4/xa_nn_activations_32_8.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_16x16_16_circ_nb.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_pointwise_16x16.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_16x16.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_transpose_conv_circ_buf.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_group_sym8sxasym8s.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_asym8xasym8_asym8_circ_nb.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_sym8sxasym8s.c + ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_add_f32.c + ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_sub_f32.c + ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_mul_f32.c + ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_abs_f32.c + ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/nanf_tbl.c + ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/inff_tbl.c + ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/pow2f_tbl.c + ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/expf_tbl.c + ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_8x8_8_circ.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_pointwise_sym8sxsym16s.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_pointwise_8x16.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_circ_buf.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_std_8x8.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_16x16_16_circ.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_transpose_conv_f32.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_f32.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_f32_circ.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_std_circ_buf.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_transpose_conv_sym8sxasym8s.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_8x16_16_circ.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_sym8sxsym16s_sym16s_circ.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv1d_std_8x8.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_std_8x16.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_std_sym8sxsym16s.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_pointwise_f32.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_f32_circ_nb.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_std_sym8sxasym8s.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_asym8xasym8_asym8_circ.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_pointwise_sym8sxasym8s.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_8x8.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_transpose_conv_sym8sxsym16s.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_std_f32.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv1d_std_8x16.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_pointwise_8x8.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_std_16x16.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_sym8sxasym8s_asym8s_circ.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_pointwise_asym8xasym8.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_std_asym8xasym8.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv1d_std_circ_buf.c @@ -82,117 +74,11 @@ add_library(nn_hifi_lib STATIC ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_8x16_16_circ_nb.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv1d_std_16x16.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_matXvec_8x8_8_circ_nb.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_std_sym4sxasym8s.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv1d_std_f32.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_sym8sxsym16s.c ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_asym8xasym8.c - ${NN_HIFI_PATH}/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_8x16.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matmul_f32.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_asym8xasym8_batch.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matmul_sym8sxasym8s.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matmul_8x8.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matmul_8x16.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_asym4sxasym8s.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matmul_sym8sxsym16s.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_f32_batch.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_f32.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_16x16_batch.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_sym8sxsym16s.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matmul_asym8sxasym8s.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_16x8.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_8x16.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_8x8.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_asym8sxasym8s.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matmul_asym8xasym8.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matmul_16x16.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_8x16_batch.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_16x16.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_asym8xasym8.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_sym8sxasym8s.c - ${NN_HIFI_PATH}/algo/kernels/matXvec/hifi4/xa_nn_matXvec_8x8_batch.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_maxpool_f32.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_maxpool_8.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_maxpool_asym8_nhwc.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_avgpool_f32_nhwc.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_maxpool_16_nhwc.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_avgpool_16.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_avgpool_16_nhwc.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_avgpool_asym8.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_avgpool.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_inv_256_tbl.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_maxpool.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_maxpool_8_nhwc.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_maxpool_16.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_avgpool_8.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_maxpool_f32_nhwc.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_avgpool_asym8_nhwc.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_avgpool_f32.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_maxpool_asym8.c - ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_avgpool_8_nhwc.c - ${NN_HIFI_PATH}/algo/kernels/norm/hifi4/xa_nn_l2_norm_asym8s.c - ${NN_HIFI_PATH}/algo/kernels/norm/hifi4/xa_nn_l2_norm_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_sqrt_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_mul_16x16.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_add_quant8.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_mul_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_lstm_utils.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_dot_prod_16x16.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_sub_quant16.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_round_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_mul_acc_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_cosine_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_reduce_asym8s_asym8s.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_memset_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_floor_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_squared_diff_quant8.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_add_quant16.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_reduce_asym16s_asym16s.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_minmax_8.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_logn_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_div_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_sub_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_compare_quant8.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_logical_bool.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_broadcast_8_8.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_quantize.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_neg_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_mul_quant16.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_add_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_vec_interpolation_q15.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_square_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_sine_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_abs_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_memmove.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_mul_quant8.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_squared_diff_quant16.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_memmove_16.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_sub_quant8.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_ceil_f32.c - ${NN_HIFI_PATH}/algo/kernels/basic/hifi4/xa_nn_elm_rsqrt_f32.c ${NN_HIFI_PATH}/algo/kernels/fc/hifi4/xa_nn_fully_connected.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_tanh32x32_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/scl_sigmoidf_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/inv2pif_tbl.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/nanf_tbl.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/pow2f_tbl.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_sigmoidf_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_reluf_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/expf_tbl.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_tanhf_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_sigmoid32x32_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/lognf_tbl.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/sinf_tbl.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_cosinef_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_sinef_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_softmaxf_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/scl_tanhf_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_softmax32x32_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_lognf_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/sqrt2f_tbl.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/inff_tbl.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_alognf_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/vec_relu32x32_hifi4.c - ${NN_HIFI_PATH}/algo/ndsp/hifi4/src/tanhf_tbl.c + ${NN_HIFI_PATH}/algo/kernels/pool/hifi4/xa_nn_inv_256_tbl.c + ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_transpose_8.c + ${NN_HIFI_PATH}/algo/kernels/reorg/hifi4/xa_nn_pad_8.c ) target_include_directories(nn_hifi_lib PRIVATE @@ -200,31 +86,63 @@ target_include_directories(nn_hifi_lib PRIVATE ${NN_HIFI_PATH}/algo/common/include/ ${NN_HIFI_PATH}/include/nnlib/ ${NN_HIFI_PATH}/algo/ndsp/hifi4/include + ${TFLM_TOOLCHAIN_INCLUDE_ROOT} + ${TFLM_HAL_SOC_PATH} +) + +target_compile_options(nn_hifi_lib PRIVATE + -mllvm --text-section-literals=false ) +endif() # TENSORFLOW_HAVE_NNLIB_HIFI4 + + +if(TENSORFLOW_HAVE_NNLIB_HIFI4) + # TODO: Need to detect and add mul16/32 options. -#ifeq "$(has_mul16)" "0" -#CFLAGS += -mno-mul16 -#endif -#ifeq "$(has_mul32)" "0" -#CFLAGS += -mno-mul32 -mno-div32 -#endif +target_compile_definitions(nn_hifi_lib PRIVATE + -DHIFI4=1 + -DHAVE_VFPU=1 + -DHAVE_VFPU_SINGLE_PRECISION=1 + -DXCHAL_HAVE_HIFI4=1 + -DXCHAL_HAVE_HIFI4_VFPU=1 + + __xtensa__=1 + __XTENSA__=1 + __XCC__ + __XCC_CLANG__ + "XT_MAX(a,b)=((a)>(b)?(a):(b))" + "XT_MIN(a,b)=((a)<(b)?(a):(b))" + "AE_MOVINT16_FROMINT32(v)=((ae_int16)(v))" + "AE_CVT64F32_H(v)=((ae_int64)(int64_t)(int32_t)AE_MOVAD32_H(v))" + "AE_CVT64F32_L(v)=((ae_int64)(int64_t)(int32_t)AE_MOVAD32_L(v))" +) + target_compile_options(nn_hifi_lib PRIVATE -fsigned-char -fno-exceptions -mlongcalls - -INLINE:requested - -mcoproc -fno-zero-initialized-in-bss - -mtext-section-literals -Wsign-compare - -m32 -DMODEL_INT16 -DNNLIB_V2 -Dhifi4 -DTFLITE_SINGLE_ROUNDING=1 ) +target_compile_definitions(nn_hifi_lib PRIVATE + __XCC__ + __XCC_CLANG__ +) +target_compile_options(nn_hifi_lib PRIVATE + -mcpu=${SOC_TOOLCHAIN_NAME} + "SHELL:-include xtensahifiintrin.h" + "SHELL:-include ${NN_HIFI_PATH}/algo/common/include/xa_nnlib_hifi_isa_compat.h" +) + +endif() # TENSORFLOW_HAVE_NNLIB_HIFI4 (nn_hifi_lib defs/opts) + + # TODO: complete sources have been added here from userspace build but # not all are needed so this is a list of "needed" sources to build # a memory and performance optimized TFLM for SOF. @@ -332,46 +250,12 @@ add_library(tflm_lib STATIC #${TFLM_PATH}/tensorflow/lite/micro/kernels/var_handle.cc #${TFLM_PATH}/tensorflow/lite/micro/kernels/while.cc #${TFLM_PATH}/tensorflow/lite/micro/kernels/zeros_like.cc - # xtensa kernels - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/sub.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/conv_hifi.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/pooling_vision.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/softmax_vision.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/unidirectional_sequence_lstm.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/transpose_conv.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/fully_connected_common_xtensa.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/lstm_eval_hifi.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/add_vision.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/depthwise_conv.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/reshape.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/softmax.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/depthwise_conv_hifi.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/conv_int16_reference.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/conv_int8_reference.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/fully_connected_int8.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/pad_vision.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/fully_connected_vision.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/conv_int8_int16.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/conv.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/softmax_int8_int16.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/logistic.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/reduce.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/lstm_eval.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/reshape_vision.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/pad.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/reduce_vision.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/dequantize.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/add.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/pooling_int8.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/depthwise_conv_vision.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/strided_slice.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/conv_common_xtensa.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/svdf.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/fully_connected.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/leaky_relu.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/quantize.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/pooling.cc - ${TFLM_PATH}/tensorflow/lite/micro/kernels/xtensa/conv_vision.cc + # reference kernels for speech model ops + ${TFLM_PATH}/tensorflow/lite/micro/kernels/conv.cc + ${TFLM_PATH}/tensorflow/lite/micro/kernels/depthwise_conv.cc + ${TFLM_PATH}/tensorflow/lite/micro/kernels/fully_connected.cc + ${TFLM_PATH}/tensorflow/lite/micro/kernels/reshape.cc + ${TFLM_PATH}/tensorflow/lite/micro/kernels/softmax.cc ${TFLM_PATH}/tensorflow/lite/micro/mock_micro_graph.cc ${TFLM_PATH}/tensorflow/lite/micro/flatbuffer_utils.cc ${TFLM_PATH}/tensorflow/lite/micro/micro_interpreter_graph.cc @@ -413,10 +297,16 @@ target_include_directories(tflm_lib PRIVATE ${FLATBUFFERS_PATH}/include ${GEMMLOWP_PATH} ${RUY_PATH} + ${sof_top_dir}/posix/include + ${sof_top_dir}/../modules/hal/xtensa/include + ${TFLM_HAL_SOC_PATH} +) +if(TENSORFLOW_HAVE_NNLIB_HIFI4) +target_include_directories(tflm_lib PRIVATE ${NN_HIFI_PATH} ${NN_HIFI_PATH}/include - ${sof_top_dir}/posix/include ) +endif() # TODO: Need to detect and add mul16/32 options. #ifeq "$(has_mul16)" "0" @@ -425,9 +315,38 @@ target_include_directories(tflm_lib PRIVATE #ifeq "$(has_mul32)" "0" #CFLAGS += -mno-mul32 -mno-div32 #endif + +# These select/enable the HiFi4 nnlib-optimized code paths in some TFLM +# kernels. Only valid when nn_hifi_lib is actually built and linked -- +# forcing XCHAL_HAVE_HIFI4 on a non-HiFi4 target risks enabling HiFi4-only +# branches in system/toolchain headers that don't apply to this core. +if(TENSORFLOW_HAVE_NNLIB_HIFI4) +target_compile_definitions(tflm_lib PRIVATE + -DHIFI4=1 + -DHAVE_VFPU=1 + -DHAVE_VFPU_SINGLE_PRECISION=1 + -DXCHAL_HAVE_HIFI4=1 + -DXCHAL_HAVE_HIFI4_VFPU=1 + + __xtensa__=1 + __XTENSA__=1 + __XCC__ + __XCC_CLANG__ + "XT_MAX(a,b)=((a)>(b)?(a):(b))" + "XT_MIN(a,b)=((a)<(b)?(a):(b))" + "AE_MOVINT16_FROMINT32(v)=((ae_int16)(v))" + "AE_CVT64F32_H(v)=((ae_int64)(int64_t)(int32_t)AE_MOVAD32_H(v))" + "AE_CVT64F32_L(v)=((ae_int64)(int64_t)(int32_t)AE_MOVAD32_L(v))" +) +target_compile_options(tflm_lib PRIVATE + -DHIFI4 + -DKERNELS_OPTIMIZED_FOR_SPEED + -DNNLIB_V2 +) +endif() # TENSORFLOW_HAVE_NNLIB_HIFI4 + target_compile_options(tflm_lib PRIVATE -std=c++17 - -stdlib=libc++ -fno-rtti -fno-exceptions -fno-threadsafe-statics @@ -447,19 +366,114 @@ target_compile_options(tflm_lib PRIVATE -Wstrict-aliasing -Wno-unused-parameter -DXTENSA - -DKERNELS_OPTIMIZED_FOR_SPEED -DTF_LITE_MCU_DEBUG_LOG -DTF_LITE_USE_CTIME - --xtensa-core=ace10_LX7HiFi4_2022_10 - -mcoproc - -DHIFI4 -mlongcalls - -DNNLIB_V2 -Wno-shadow ) +if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + target_compile_options(tflm_lib PRIVATE + -stdlib=libc++ + "SHELL:-include xtensahifiintrin.h" + -fno-vectorize + -fno-slp-vectorize + ) +else() + # gcc equivalents for vectorize disabling + target_compile_options(tflm_lib PRIVATE + -fno-tree-vectorize + -fno-tree-slp-vectorize + ) +endif() + +if(CMAKE_C_COMPILER_ID STREQUAL "Xtensa") + target_compile_options(tflm_lib PRIVATE + --xtensa-core=ace10_LX7HiFi4_2022_10 + -mcoproc + ) +elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang") + target_compile_options(tflm_lib PRIVATE + -mcpu=${SOC_TOOLCHAIN_NAME} + ) +endif() + +if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + target_include_directories(tflm_lib SYSTEM PRIVATE + ${TFLM_TOOLCHAIN_INCLUDE_ROOT}/c++/14.3.0 + ${TFLM_TOOLCHAIN_INCLUDE_ROOT}/c++/14.3.0/xtensa-${SOC_TOOLCHAIN_NAME}_zephyr-elf + ${TFLM_TOOLCHAIN_INCLUDE_ROOT} + ) +endif() + add_local_sources(sof tflm-classify.c llext-wrap.c) -# Need to link libc++ and libc after tflm and nnlib -zephyr_link_libraries(tflm_lib nn_hifi_lib c++ c) +# Link nnlib only when it was actually built (HiFi4 target + Clang) +if(TENSORFLOW_HAVE_NNLIB_HIFI4) + zephyr_link_libraries(tflm_lib nn_hifi_lib) +else() + zephyr_link_libraries(tflm_lib) +endif() + +# CONFIG_MINIMAL_LIBC (SOF's global default) has no libm and is missing a +# few libc functions (e.g. abs()) that TFLM needs in a statically-linked +# (non-LLEXT) image. Linking the toolchain's whole libc.a is too broad: it +# conflicts with Zephyr's own malloc/free (multiple definition) and pulls in +# an __assert_no_args that needs an unavailable stderr. Instead, extract just +# the specific archive members TFLM actually needs into a small private +# archive and link only that. +if(NOT CONFIG_COMP_TENSORFLOW STREQUAL "m") + set(TFLM_TOOLCHAIN_LIBC_ARCHIVE + ${ZEPHYR_SDK_INSTALL_DIR}/gnu/xtensa-${SOC_TOOLCHAIN_NAME}_zephyr-elf/xtensa-${SOC_TOOLCHAIN_NAME}_zephyr-elf/lib/libc.a) + set(TFLM_LIBC_SHIM_MEMBERS + libc_stdlib_abs.c.o + libm_math_s_floor.c.o + libm_math_sf_exp.c.o + libm_math_sf_log.c.o + libm_common_sf_fmax.c.o + libm_common_sf_fmin.c.o + libm_common_sf_round.c.o + # error-handling/predicate helpers the above call internally + libm_common_sf_isnan.c.o + libm_common_sf_issignaling.c.o + libm_common_math_errf_uflowf.c.o + libm_common_math_errf_oflowf.c.o + libm_common_math_errf_divzerof.c.o + libm_common_math_errf_invalidf.c.o + ) + set(TFLM_LIBC_SHIM_DIR ${CMAKE_CURRENT_BINARY_DIR}/tflm_libc_shim) + set(TFLM_LIBC_SHIM_ARCHIVE ${CMAKE_CURRENT_BINARY_DIR}/libtflm_libc_shim.a) + file(MAKE_DIRECTORY ${TFLM_LIBC_SHIM_DIR}) + add_custom_command( + OUTPUT ${TFLM_LIBC_SHIM_ARCHIVE} + COMMAND ${CMAKE_AR} x ${TFLM_TOOLCHAIN_LIBC_ARCHIVE} ${TFLM_LIBC_SHIM_MEMBERS} + COMMAND ${CMAKE_AR} rcs ${TFLM_LIBC_SHIM_ARCHIVE} ${TFLM_LIBC_SHIM_MEMBERS} + WORKING_DIRECTORY ${TFLM_LIBC_SHIM_DIR} + DEPENDS ${TFLM_TOOLCHAIN_LIBC_ARCHIVE} + COMMENT "Extracting abs()/libm members TFLM needs from the toolchain libc.a" + ) + add_custom_target(tflm_libc_shim_gen DEPENDS ${TFLM_LIBC_SHIM_ARCHIVE}) + add_library(tflm_libc_shim STATIC IMPORTED GLOBAL) + set_target_properties(tflm_libc_shim PROPERTIES IMPORTED_LOCATION ${TFLM_LIBC_SHIM_ARCHIVE}) + add_dependencies(tflm_libc_shim tflm_libc_shim_gen) + zephyr_link_libraries(tflm_libc_shim) + + # TFLM/flatbuffers use assert() internally; the toolchain's own + # __assert_no_args implementation needs an unavailable stderr, so disable + # assert() outright instead (standard practice for release TFLM builds). + target_compile_definitions(tflm_lib PRIVATE NDEBUG) +endif() zephyr_include_directories(${TFLM_PATH}) +zephyr_include_directories(${FLATBUFFERS_PATH}/include) +zephyr_include_directories(${GEMMLOWP_PATH}) +zephyr_include_directories(${RUY_PATH}) +if(TENSORFLOW_HAVE_NNLIB_HIFI4) +zephyr_include_directories(${NN_HIFI_PATH}/algo/kernels/include) +zephyr_include_directories(${NN_HIFI_PATH}/include) +endif() +target_include_directories(modules_sof SYSTEM PRIVATE + ${TFLM_TOOLCHAIN_INCLUDE_ROOT}/c++/14.3.0 + ${TFLM_TOOLCHAIN_INCLUDE_ROOT}/c++/14.3.0/xtensa-${SOC_TOOLCHAIN_NAME}_zephyr-elf + ${TFLM_TOOLCHAIN_INCLUDE_ROOT} +) + diff --git a/src/audio/tensorflow/README.md b/src/audio/tensorflow/README.md index a8e3f59ba3ce..887aae8064c6 100644 --- a/src/audio/tensorflow/README.md +++ b/src/audio/tensorflow/README.md @@ -1,22 +1,368 @@ -# TensorFlow Lite Micro (TFLM) Architecture +# TensorFlow Lite Micro (TFLM) & Wake-on-Voice (WoV) Architecture -This directory acts as the bridge for running ML models. +This directory provides the TensorFlow Lite for Microcontrollers (TFLM) classification module (`TFLMCLY`) for Sound Open Firmware (SOF), including integration with MFCC feature extraction, `mtrace` stream shutdown summary logging, IPC host notifications, Key Phrase Buffer (KPB) Wake-on-Voice (WoV) trigger infrastructure, and abstracted audio input sources across **HDA**, **DMIC**, **SSP (I2S)**, and **SoundWire (ALH)**. + +Two build modes are supported, selected automatically per target core: + +- **LLEXT module** (HiFi4/HiFi5 targets: MTL, PTL, ARL, ...) — `CONFIG_COMP_TENSORFLOW=m`, built with Clang, linked against the HiFi4-optimized `nnlib-hifi4` kernels. +- **Statically linked into `zephyr.elf`** (HiFi3 targets with no LLEXT/module-manager support, e.g. TGL/cavs2.5) — `CONFIG_COMP_TENSORFLOW=y`, built with the Zephyr SDK GCC toolchain, `nnlib-hifi4` NOT built or linked (see [Build Instructions](#build-instructions) below). + +--- ## Overview -Integrates TensorFlow Lite for Microcontrollers into the SOF audio pipeline. Evaluates pre-trained neural network topologies inline with the audio stream for tasks like wake-word, noise cancellation, or sound classification. +The TFLM module evaluates pre-trained micro speech neural network models inline within the SOF audio processing graph. It receives pre-processed audio feature tensors (mel-log spectrograms from the `mfcc` component), runs model inference in the **Data Processing (DP) domain**, logs keyword detections and stream shutdown event summaries to `mtrace`, issues IPC4 notifications to the host audio driver, and signals the KPB module to drain pre-roll audio history upon keyword detection. + +--- -## Architecture Diagram +## Architecture & Data Flow + +### Dual-Path Wake-on-Voice (WoV) Architecture + +To allow continuous keyword evaluation without streaming audio to the host until a keyword is detected, the pipeline separates real-time keyword detection from host PCM draining via KPB: ```mermaid -graph LR - Feat[Audio Features] --> TFLM[TFLM Inference Engine] - SubGraph[FlatBuffer Model] -.-> TFLM - TFLM --> Out[Inference Labels/Scores] +graph TD + subgraph Audio_Inputs ["Abstracted Hardware DAI Input Sources"] + HDA["HDA Analog Input (dai_type: HDA)"] + DMIC["PCH DMIC Digital Mic (dai_type: DMIC)"] + SSP["I2S / Bluetooth Codec (dai_type: SSP)"] + SDW["SoundWire SmartMic (dai_type: ALH)"] + end + + subgraph DAI_Abstraction ["Backend DAI Copier Widget"] + DAI["dai-copier.1 (dai_type: $CAPTURE_DAI_TYPE)"] + end + + subgraph KPB_Pipeline ["Capture & KPB Pipeline"] + Gain["gain.2.1 (Volume Control)"] + KPB["kpb.2.1 (Key Phrase Buffer)"] + end + + subgraph Detect_Pipeline ["Real-Time Detection Path (KPB Pin 1) - DP Domain"] + SRC["src.1.1 (Resampler: 48kHz -> 16kHz)"] + MFCC["mfcc.1.1 (Mel-40 Feature Extractor)"] + TFLM["tflmcly.1.1 (TFLM Keyword Classifier)"] + DetHost["host-copier.1.capture (PCM device 1: arm/observe only, see Usage)"] + end + + subgraph Host_Pipeline ["Host WoV Draining Path (KPB Pin 2)"] + Host["host-copier.0.capture (PCM device 0: real drain target)"] + end + + HDA --> DAI + DMIC --> DAI + SSP --> DAI + SDW --> DAI + + DAI --> Gain + Gain --> KPB + KPB -- Pin 1: Live Audio --> SRC + SRC --> MFCC + MFCC -- Mel-40 Q9.23 Tensors --> TFLM + TFLM --> DetHost + + TFLM -.->|1. KPB_EVENT_BEGIN_DRAINING Notifier| KPB + TFLM -.->|2. IPC4 Notification (scaffolded, not wired up)| Host_Driver[Host Driver] + TFLM -.->|3. Stream Shutdown Event Logging| MTrace[mtrace / Trace Log] + + KPB -- Pin 2: Pre-roll History Buffer --> Host +``` + +**Note on pipe 1's host-copier**: `host-copier.1.capture` (PCM device 1, "HDA Mic TFLM Detect") exists to *arm/instantiate* the detection pipeline and to give a host-visible device for debugging — it is **not** the WoV drain target. `arecord`-ing it will get an immediate `Input/output error` once its ring buffer underruns, because `tflmcly` (not the host) is the real consumer on this pipe; this is expected. The pipeline stays fully armed and running as long as the PCM is *opened*, whether or not a read ever succeeds — see [Usage](#usage) for how to hold it open for testing without triggering the read-error teardown. The actual pre-roll audio delivered to the host on keyword detection always arrives via `host-copier.0.capture` (PCM device 0). + +--- + +## Domain Execution Model (LL vs. DP) + +- **Low Latency (LL) Domain (Timer Task / 1ms tick loop)**: + - Components: `dai-copier`, `eqiir`, `tdfb`, `drc`, `host-copier` + - Purpose: Fixed 1 ms tick loop executed on Core 0 to meet hard real-time audio hardware deadlines. + - Metrics: Reported via `ll_schedule.stats_report` (e.g. `ll core 0 timer avg 22,049 cycles (~56 µs)`). + +- **Data Processing (DP) Domain (Asynchronous Task)**: + - Components: `src`, `micsel`, `mfcc`, `tflmcly` + - Purpose: Runs asynchronously in a background task whenever a new feature hop is produced by `mfcc` — measured empirically at roughly one inference every ~500ms (bring-up wall-clock gate, see [Known Limitations](#known-limitations--open-issues)). + - **Separation**: Because TFLM runs in the DP domain, model inference cycles take place outside the 1 ms LL tick loop, guaranteeing zero impact on real-time LL audio latency or buffer overruns. On targets without a pre-existing DP scheduler user, `platform_init()` now calls `scheduler_dp_init()` explicitly so `mfcc`/`tflmcly` have somewhere to run (`src/platform/intel/cavs/platform.c`). + +### MFCC frame format + +Per hop, MFCC emits a 24-byte `struct mfcc_data_header` (magic/frame_number/reserved/energy/noise_energy/vad_flag) followed by `TFLM_FEATURE_SIZE` (40) `int32_t` Q9.23 mel-log values — `24 + 40*4 = 184` bytes (`MFCC_FRAME_BYTES` in the pipeline template). `tflm_process()` strips this header and requantizes each Q9.23 value into `int8_t` against the model's *real* input tensor `scale`/`zero_point` (read from the interpreter at prepare time, not assumed) before feeding the model's 49-hop sliding feature window. + +--- + +## Known Limitations / Open Issues + +- **Feature-representation mismatch (stock model only)**: the shipped 4-class `silence/unknown/yes/no` model was trained against TFLM's original `micro_speech` frontend, which applies a *nonlinear* PCAN auto-gain-control normalization before quantization. SOF's MFCC produces *linear* mel-log values. Empirically, real captured audio normalizes to `norm ≈ 0.03–1.5` (Q9.23 mel-log value / 2^23), while this model's actual `input_scale=0.101715`/`zero_point=-128` need `norm ≈ 0–26` to use its int8 dynamic range — so every real input saturates into the bottom ~8% of the range and the model outputs a flat, content-independent prediction. No linear rescale of `mfcc_mel_q23_to_int8()` fixes this; it needs either a model retrained directly on real SOF MFCC mel-log features (recommended — see [Training a Custom Keyword Model](#training-a-custom-keyword-model-with-piper-tts)), or a from-scratch PCAN-AGC-equivalent normalization stage ahead of quantization. **Training a new model on real SOF features, as described below, avoids this problem entirely** since train-time and inference-time feature extraction then match by construction. +- **500ms inference cadence is a bring-up shortcut**, not the model's trained stride (20ms/hop, 49-hop/~1s sliding window). Fine for initial bring-up; revisit before judging a new model's real-world accuracy, since a cadence mismatch vs. training assumptions can look like a model-quality problem. +- **IPC4 host notification is scaffolded but not wired up** (`tflm_ipc_notification_init()` never allocates/registers `cd->msg`, so `tflm_send_keyword_notification()` silently no-ops). KPB draining (a separate mechanism) still works. Finish this if the host driver needs an explicit "keyword X detected" event rather than just observing PCM start flowing on device 0. +- **Per-instance state is global**, not per-`comp_dev` (`g_tflm_cd`, `g_tflm_initialized`, per-category counters, etc.). Fine for a single detector instance; would need reworking for concurrent multi-`tflmcly` use. +- **Category count/labels are still hardcoded in a few places** beyond `TFLM_CATEGORY_DATA` (shutdown-summary format string, the KPB-trigger rule `max_idx >= 2`) — generalize before changing the category set. + +--- + +## Abstracted Audio Input Sources in Topology v2 + +The TFLM Keyword Detection and KPB pre-roll pipeline is decoupled from the physical DAI input source using the generic `dai-copier` widget: + +```conf +Object.Widget.dai-copier.1 { + dai_type $CAPTURE_DAI_TYPE # "HDA", "DMIC", "SSP", or "ALH" (SoundWire) + copier_type $CAPTURE_COPIER_TYPE # "HDA", "DMIC", "SSP", or "ALH" + stream_name $CAPTURE_DAI_NAME # "Analog", "DMIC01", "SSP0", "SDW0-Capture" + node_type $CAPTURE_NODE_TYPE # $HDA_LINK_INPUT_CLASS, $DMIC_LINK_INPUT_CLASS, etc. +} ``` -## Configuration and Scripts +Platform wrappers select the input source cleanly via configuration defines: +```conf +Define { + CAPTURE_SOURCE "hda" # Options: "hda", "dmic", "ssp", "soundwire" +} + +IncludeByKey.CAPTURE_SOURCE { + "hda" "platform/intel/capture-hda.conf" + "dmic" "platform/intel/capture-dmic.conf" + "ssp" "platform/intel/capture-ssp.conf" + "soundwire" "platform/intel/capture-sdw.conf" +} +``` + +--- + +## Topology v2 Integration & Usage + +### 1. Component Widget Definition (`include/components/tflm.conf`) + +Defines `Class.Widget."tflmcly"`: +- **UUID**: `42:c6:1d:c5:e1:a2:df:48:a4:90:e2:74:8c:b6:36:3e` (`c51dc642-a2e1-48df-a490e2748cb6363e`) +- **Type**: `effect` + +### 2. Detection Pipeline Template (`include/pipelines/cavs/host-gateway-src-mfcc-tflm-capture.conf`) + +Instantiates the real-time detection graph — `mfcc.1` carries a real bytes-control config default (`HDA_MIC_MFCC_PARAMS`, defaulting to `include/components/mfcc/mel40_compress.conf`), and `tflmcly.1`'s output is routed to a real host-copier rather than a terminal virtual sink: + +```conf +Object.Widget { + host-copier."1" { + type "aif_out" + node_type $HDA_HOST_INPUT_CLASS + stream_name "HDA Mic TFLM Detect" + pcm_id $index + } + src."1" { ... } + mfcc."1" { + Object.Control.bytes."1" { + name "HDA Mic MFCC bytes" + IncludeByKey.HDA_MIC_MFCC_PARAMS { + "default" "include/components/mfcc/mel40_compress.conf" + } + } + } + tflmcly."1" { scheduler_domain "DP" } +} + +Object.Base.route [ + { source tflmcly.$index.1; sink "host-copier.$index.capture" } +] +``` + +### 3. Top-Level Topology Configuration (`sof-hda-tflm.conf`) + +Instantiates the complete HDA Mic WoV topology with dual-path KPB routing: +```conf +Object.Base.route [ + # DAI -> Gain -> KPB + { source "dai-copier.HDA.Analog.capture"; sink "gain.2.1" } + { source "gain.2.1"; sink "kpb.2.1" } + + # KPB Pin 1 -> Real-time Detection Path (DP Domain) + { source "kpb.2.1"; sink "src.1.1" } + + # KPB Pin 2 -> Host WoV Draining Path + { source "kpb.2.1"; sink "host-copier.0.capture" } +] +``` + +A "HDA Mic TFLM Detect" PCM entry (`$HDA_TFLM_DETECT_PIPELINE_ID`, mono S32_LE @16kHz) exposes pipe 1's host-copier as PCM device 1. + +--- + +## Build Instructions + +### HiFi4/HiFi5 targets (MTL, PTL, ARL, ...) — LLEXT module, Clang + +```bash +source .venv/bin/activate +export LLVM_TOOLCHAIN_PATH= +west build -b intel_adsp/ app -d build--tflm -- \ + -DCONFIG_COMP_TENSORFLOW=m +``` + +### HiFi3 targets with no LLEXT support (TGL/cavs2.5) — static link, GCC + +Requires `CONFIG_COMP_TENSORFLOW=y` (not `m`) plus C++17 and enough stack/heap for the interpreter's arena, TFLM's own allocations, and `avcodec`-style blocking calls off the DP task — all already added to `app/boards/intel_adsp_cavs25.conf` on this branch: + +```conf +CONFIG_SOF_STAGING=y +CONFIG_CPP=y +CONFIG_STD_CPP17=y +CONFIG_COMP_TENSORFLOW=y +CONFIG_STACK_SIZE_EDF=32768 +CONFIG_HEAP_MEM_POOL_SIZE=32768 +CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=32768 +``` + +Build with the Zephyr SDK GCC toolchain (no Clang/LLVM involved): + +```bash +source .venv/bin/activate +export ZEPHYR_SDK_INSTALL_DIR=/home/lrg/zephyr-sdk-1.0.1 +export ZEPHYR_TOOLCHAIN_VARIANT=zephyr +unset LLVM_TOOLCHAIN_PATH +west build -b intel_adsp/cavs25 app -d build-tgl-tflm-gcc +``` + +`src/audio/tensorflow/CMakeLists.txt` auto-detects that this target has no `nnlib-hifi4` support (`CONFIG_XTENSA_HIFI4` unset, even though the compiler may still be Clang for other targets) and skips building/linking `nn_hifi_lib`, falling back to plain-C TFLM reference kernels. It also extracts just the `abs()`/libm archive members TFLM needs from the toolchain's `libc.a` into a small private `tflm_libc_shim`, since `CONFIG_MINIMAL_LIBC` lacks them and linking the whole `libc.a` collides with Zephyr's own `malloc`/`free`. + +### Building topology targets + +From `sof/tools/build_tools`: + +```bash +# Build HDA TFLM KPB Topologies (MTL / PTL / TGL) +ninja topology2_prod_sof-mtl-hda-tflm-kpb +ninja topology2_prod_sof-ptl-hda-tflm-kpb +ninja topology2_prod_sof-tgl-hda-tflm-kpb + +# Build SoundWire TFLM Topology (ARL-S) +ninja topology2_dev_sof-arl-cs42l43-l0-cs35l56-l23-mfcc-mel-normal +``` + +--- + +## Usage + +### PCM device map (`sof-hda-tflm` topology) + +| Device | Widget | Role | +|---|---|---| +| `hw:0,0` | `host-copier.0.capture` | Real WoV output: KPB pin 2's pre-roll drain target. Reading this exercises the full `dai-copier -> gain -> kpb -> host` chain and is the right place to capture the actual detected/drained audio. | +| `hw:0,1` | `host-copier.1.capture` ("HDA Mic TFLM Detect") | Arms/observes the detection pipeline only — see the [pipeline diagram note](#dual-path-wake-on-voice-wov-architecture). Not a continuous PCM stream; see below. | + +### Sanity-checking the WoV drain path end-to-end + +```bash +ssh root@ 'arecord -D hw:0,0 -f S32_LE -r 48000 -c 2 -d 4 /tmp/sanity.wav' +``` + +A clean, error-free capture here confirms the physical DAI, gain, and KPB chain are all healthy independent of TFLM/MFCC. + +### Arming the detection pipeline and watching inferences + +`hw:0,1` is not meant to be read continuously: `tflmcly` (not the host) is the real consumer on this pipe, so `arecord`'s first failed `read()` tears the pipeline straight back down. To arm it and hold it running for observation, open the PCM directly via `libasound` without ever reading from it — e.g. a small ctypes/C snippet calling `snd_pcm_open()` + `snd_pcm_set_params()` + `snd_pcm_start()` on `hw:0,1` and then just sleeping. In parallel, tail `mtrace` on the DUT: + +```bash +ssh root@ '/usr/local/bin/mtrace-reader.py' > /tmp/mtrace.log & +# ... arm hw:0,1 and play/speak keywords into the mic ... +# look for periodic "[TFLM PREPARE]", "[DBG hop]", "[DBG raw_output]" lines +``` + +On a high-confidence detection, `KPB_EVENT_BEGIN_DRAINING` fires and pre-roll history starts flowing via `host-copier.0.capture` — i.e. it shows up on `hw:0,0`, not `hw:0,1`. + +### Stream shutdown summary + +Upon stream reset or module destruction (`tflm_reset()` / `tflm_free()`), TFLM emits a summary to `printk`/`mtrace`: + +```text +[TFLM STREAM SHUTDOWN SUMMARY] Total Inferences=142 | Keyword Events: Silence=120, Unknown=18, Yes=3, No=1 | Total KPB Triggers=4 +``` + +- `Total Inferences`: cumulative classification inferences completed during the stream session. +- `Keyword Events`: per-category classification breakdown. +- `Total KPB Triggers`: high-confidence detections that triggered `KPB_EVENT_BEGIN_DRAINING`. + +--- + +## Training a Custom Keyword Model with Piper-TTS + +The stock model only recognizes `yes`/`no` (plus `silence`/`unknown`). To recognize custom keywords, train a new model with the same `tiny_conv` DS-CNN shape `speech.cc`'s `MicroMutableOpResolver<4>` already supports (Reshape, FullyConnected, DepthwiseConv2D, Softmax) — **and train it directly against real SOF MFCC mel-log features**, not the stock TFLM frontend, to avoid the PCAN-AGC mismatch described above. + +### 1. Generate a synthetic dataset with Piper-TTS + +Getting hundreds of real speakers to say a made-up keyword isn't practical — generate it with TTS instead: + +```bash +pip install piper-sample-generator +``` + +Two voice sources: +- Individual Piper voices (`.onnx` + `.onnx.json`, one voice = one speaker) — download 10-20 `en_US`/`en_GB` voices for variety. +- The LibriTTS-R "generator" checkpoint, which mixes speaker embeddings from up to 904 underlying speakers via `--max-speakers`/`--slerp-weights` — more voice diversity from a single model file; avoid the highest-numbered speaker indices (the tool's own docs warn these have few training samples and produce artifacts). + +Generate per keyword, looping over voices and speaking rates so samples aren't all one speaker/prosody: + +```bash +for voice in voices/*.onnx; do + for scale in 0.9 1.0 1.1; do + python3 -m piper_sample_generator "" \ + --model "$voice" --max-samples 50 \ + --length-scales "$scale" \ + --output-dir "raw//" + done +done +``` + +Augment with the tool's own augmentation pass — randomizes volume, convolves with room impulse responses, resamples to 16kHz: + +```bash +python3 -m piper_sample_generator.augment --input-dir raw/ --output-dir data/ +``` + +Optionally mix in background noise (Speech Commands v2's `_background_noise_` clips work well) at a few SNR levels for extra robustness. + +Target a low thousand positive clips per keyword spread across as many voices/speeds/rooms as practical — diversity matters more than raw count. Start with a few hundred to validate the pipeline end-to-end, check the confusion matrix, then scale up if accuracy/false-accept rate isn't good enough. Supplement with a small set of real human recordings if available, even just for held-out validation. + +Reuse Speech Commands v2's `_background_noise_` clips for the `silence` class, and a sample of its other 30 words (or spare non-keyword Piper output) for `unknown`. All of it just needs to land as plain 16kHz mono 16-bit WAV under `data_dir/