diff --git a/src/audio/microwakeword/README.md b/src/audio/microwakeword/README.md index 040a595dac06..1ae7418922c0 100644 --- a/src/audio/microwakeword/README.md +++ b/src/audio/microwakeword/README.md @@ -182,6 +182,18 @@ Alternatively, a single built-in model can be forced via Kconfig (`CONFIG_COMP_M Each instance can also be dynamically customized via SOF IPC4 `LARGE_CONFIG_SET` / bytes control or LLEXT model loading. +A topology can select a built-in model by keyword name instead of relying on +the slot. The bytes control default data is then only the 64-byte +`struct sof_mww_model_hdr` with `model_size` 0 and the keyword name, e.g. +`hi_intel`, behind `struct sof_abi_hdr`. Non-zero `threshold_q8` and `avg_len` +in it override the trigger settings of the built-in model. A name that is not +built in fails the prepare. The selection blobs are created with +[tune/sof_mww_select_conf.py](tune/sof_mww_select_conf.py), e.g. +[select_hi_intel.conf](../../../tools/topology/topology2/include/components/mww/select_hi_intel.conf). +The single-slot SoundWire DMIC WoV selects it with +`SDW_DMIC_WOV_MWW_MODEL=hi_intel`, used by the +`sof-ptl-rt713-l3-rt1320-l12-wov` development topology. + ### 4. Model Blob Format The built-in models and the bytes control use the same model blob: `struct sof_mww_model_hdr` from [user/mww.h](../../include/user/mww.h) followed by the TFLite flatbuffer. In the bytes control the blob follows the standard `struct sof_abi_hdr`. The 64-byte header carries: - magic `MWWM`, version and header size, so that fields can be added later, diff --git a/src/audio/microwakeword/mww.c b/src/audio/microwakeword/mww.c index f83687c17a93..f8f2e6d821e2 100644 --- a/src/audio/microwakeword/mww.c +++ b/src/audio/microwakeword/mww.c @@ -99,6 +99,7 @@ void __assert_func(const char *file, int line, const char *func, /* The tune scripts write a 64-byte header, which keeps the model 16-byte aligned */ STATIC_ASSERT(sizeof(struct sof_mww_model_hdr) == 64, mww_model_hdr_size); +STATIC_ASSERT(SOF_MWW_MODEL_NAME_LEN == MWW_MODEL_NAME_LEN, mww_model_name_len); /* Number of startup inferences to warm up the temporal ring buffers before enabling triggers (~1 sec). */ #define MWW_WARMUP_INFERENCES 33 @@ -598,6 +599,47 @@ static int mww_parse_model(struct processing_module *mod, const uint8_t *blob, s return 0; } +/** + * \brief Resolve a model selection blob to a built-in model. + * + * A selection blob is struct sof_mww_model_hdr with model_size 0. Its name + * selects the built-in model, so a topology can choose the keyword without + * the model data. + * + * \param[in] mod Module. + * \param[in,out] blob Control blob, replaced by the built-in model blob. + * \param[in,out] size Blob size, replaced by the built-in model blob size. + * \param[out] sel Selection header, or NULL if the blob is not a selection. + * \return 0 on success, -EINVAL if the selected model is not built in. + */ +static int mww_select_builtin(struct processing_module *mod, const uint8_t **blob, + size_t *size, const struct sof_mww_model_hdr **sel) +{ + const struct sof_mww_model_hdr *hdr = (const struct sof_mww_model_hdr *)*blob; + char name[SOF_MWW_MODEL_NAME_LEN + 1]; + const uint8_t *model; + size_t model_size; + + *sel = NULL; + if (*size < sizeof(*hdr) || hdr->magic != SOF_MWW_MODEL_MAGIC || + hdr->version != SOF_MWW_MODEL_VERSION || hdr->model_size) + return 0; + + memcpy_s(name, sizeof(name), hdr->name, SOF_MWW_MODEL_NAME_LEN); + name[SOF_MWW_MODEL_NAME_LEN] = '\0'; + model = MWW_GetBuiltinModel(name, &model_size); + if (!model) { + comp_err(mod->dev, "model '%s' is not built in", name); + return -EINVAL; + } + + comp_info(mod->dev, "MWW: selected built-in model '%s'", name); + *sel = hdr; + *blob = model; + *size = model_size; + return 0; +} + static int mww_prepare(struct processing_module *mod, struct sof_source **sources, int num_of_sources, struct sof_sink **sinks, int num_of_sinks) @@ -612,6 +654,7 @@ static int mww_prepare(struct processing_module *mod, dev->period = MWW_MFCC_HOP_MS * 1000; if (!cd->initialized) { + const struct sof_mww_model_hdr *sel = NULL; const uint8_t *model_ptr = NULL; const uint8_t *tflite; size_t blob_size = 0; @@ -636,7 +679,12 @@ static int mww_prepare(struct processing_module *mod, blob_size = payload_len; } } - comp_info(dev, "MWW: loaded model from control blob, size=%zu", blob_size); + ret = mww_select_builtin(mod, &model_ptr, &blob_size, &sel); + if (ret < 0) + return ret; + if (!sel) + comp_info(dev, "MWW: loaded model from control blob, size=%zu", + blob_size); } else { model_ptr = MWW_GetDefaultModel(cd->mwc.slot_id, &blob_size); if (!model_ptr) { @@ -649,6 +697,16 @@ static int mww_prepare(struct processing_module *mod, if (ret < 0) return ret; + /* Non-zero trigger settings of a selection override the built-in ones */ + if (sel) { + if (sel->threshold_q8 >= 1 && sel->threshold_q8 <= 255) + cd->detect_threshold_q8 = sel->threshold_q8; + if (sel->avg_len >= 1 && sel->avg_len <= MWW_DETECT_AVG_MAX) + cd->detect_avg_len = sel->avg_len; + comp_info(dev, "MWW: trigger threshold %d/256, average of %d", + cd->detect_threshold_q8, cd->detect_avg_len); + } + ret = MWW_SetModel(&cd->mwc, tflite, tflite_size); if (ret < 0) { comp_err(dev, "MWW_SetModel failed: %d (%s)", ret, cd->mwc.error); diff --git a/src/audio/microwakeword/mww_model.cc b/src/audio/microwakeword/mww_model.cc index ea515430cfca..4dd1dd02cc64 100644 --- a/src/audio/microwakeword/mww_model.cc +++ b/src/audio/microwakeword/mww_model.cc @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -212,6 +213,45 @@ const unsigned char *MWW_GetDefaultModel(uint8_t slot_id, size_t *size) #endif } +/** \brief A built-in model and its keyword name for MWW_GetBuiltinModel() */ +struct mww_builtin_model { + const char *name; /**< Keyword name, as in the model header */ + const unsigned char *data; /**< Model blob with struct sof_mww_model_hdr */ + size_t size; /**< Model blob size in bytes */ +}; + +/** \brief The models built into the firmware, as selected with COMP_MWW_MODEL */ +static const struct mww_builtin_model mww_builtin_models[] = { +#if CONFIG_COMP_MWW_MODEL_HI_INTEL + { "hi_intel", mww_model_data_hi_intel, sizeof(mww_model_data_hi_intel) }, +#elif CONFIG_COMP_MWW_MODEL_HEY_JARVIS + { "hey_jarvis", mww_model_data_hey_jarvis, sizeof(mww_model_data_hey_jarvis) }, +#elif CONFIG_COMP_MWW_MODEL_STRAWBERRY + { "strawberry", mww_model_data_strawberry, sizeof(mww_model_data_strawberry) }, +#elif CONFIG_COMP_MWW_MODEL_ORANGE + { "orange", mww_model_data_orange, sizeof(mww_model_data_orange) }, +#elif CONFIG_COMP_MWW_MODEL_BANANA + { "banana", mww_model_data_banana, sizeof(mww_model_data_banana) }, +#else + { "strawberry", mww_model_data_strawberry, sizeof(mww_model_data_strawberry) }, + { "hi_intel", mww_model_data_hi_intel, sizeof(mww_model_data_hi_intel) }, + { "orange", mww_model_data_orange, sizeof(mww_model_data_orange) }, +#endif +}; + +const unsigned char *MWW_GetBuiltinModel(const char *name, size_t *size) +{ + for (const struct mww_builtin_model &m : mww_builtin_models) { + if (!strncmp(name, m.name, MWW_MODEL_NAME_LEN)) { + if (size) + *size = m.size; + return m.data; + } + } + + return nullptr; +} + int MWW_SetModel(struct mww_classify *mwc, const unsigned char *model_tflite, size_t model_size) { if (!mwc) diff --git a/src/audio/microwakeword/mww_model.h b/src/audio/microwakeword/mww_model.h index 92ea16e5709a..aa074175f99b 100644 --- a/src/audio/microwakeword/mww_model.h +++ b/src/audio/microwakeword/mww_model.h @@ -22,6 +22,9 @@ #define MWW_FEATURE_STRIDE_MS 10 #define MWW_FEATURE_DURATION_MS 30 +/** \brief Keyword name length, equal to SOF_MWW_MODEL_NAME_LEN in user/mww.h */ +#define MWW_MODEL_NAME_LEN 16 + struct mww_classify { uint8_t slot_id; /**< Slot index: 0=strawberry, 1=banana, 2=orange */ void *instance; /**< Opaque pointer to per-instance C++ TFLM context */ @@ -63,6 +66,17 @@ extern "C" const unsigned char *MWW_GetDefaultModel(uint8_t slot_id, size_t *size); + /** + * \brief Get a built-in model by its keyword name. + * \param[in] name Keyword name as in the name field of struct + * sof_mww_model_hdr, zero padded or terminated, at most + * 16 characters. + * \param[out] size Size of the model blob in bytes. + * \return Model blob with struct sof_mww_model_hdr, or NULL if no + * model with the name is built in. + */ + const unsigned char *MWW_GetBuiltinModel(const char *name, size_t *size); + /** * \brief Get per-operator profile of the steady-state inferences. * \param[in] mwc Classifier context. diff --git a/src/audio/microwakeword/tune/README.md b/src/audio/microwakeword/tune/README.md index 8a524cb8ef2e..f2e0a1873d2c 100644 --- a/src/audio/microwakeword/tune/README.md +++ b/src/audio/microwakeword/tune/README.md @@ -28,6 +28,7 @@ This directory contains offline scripts to train, quantize, verify, and export s For a `Hi Intel` model, useful phrases include `hey siri`, `hey google`, `ok google`, `alexa`, `hey copilot`, and `hey jarvis`. + - [sof_mww_select_conf.py](sof_mww_select_conf.py): Emits a Topology2 bytes control blob that selects a built-in model by keyword name without the model data, e.g. `./sof_mww_select_conf.py hi_intel ../../../../tools/topology/topology2/include/components/mww/select_hi_intel.conf`. `--threshold` and `--avg-len` optionally override the trigger settings of the built-in model. 4. **Off-Device Verification & Automation:** - [sof_mww_verify.py](sof_mww_verify.py): Feeds 3-hop streaming slices through the quantized TFLite model to verify detection rates and false alarm rates. diff --git a/src/audio/microwakeword/tune/sof_mww_select_conf.py b/src/audio/microwakeword/tune/sof_mww_select_conf.py new file mode 100755 index 000000000000..32112b663b7f --- /dev/null +++ b/src/audio/microwakeword/tune/sof_mww_select_conf.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +"""Emit a Topology2 MWW bytes control blob that selects a built-in model. + +The blob is struct sof_mww_model_hdr (src/include/user/mww.h) with +model_size 0 behind struct sof_abi_hdr. The firmware loads the built-in +model whose header has the same keyword name, so a topology can choose the +keyword without carrying the model data. + +Example: + ./sof_mww_select_conf.py hi_intel \\ + ../../../../tools/topology/topology2/include/components/mww/select_hi_intel.conf +""" + +import argparse +import datetime +import struct +from pathlib import Path + +SOF_IPC4_ABI_MAGIC = 0x34464F53 # 'SOF4' +SOF_ABI_VERSION = (3 << 24) | (29 << 12) | 1 # 3.29.1 +SOF_MWW_MODEL_MAGIC = 0x4D57574D # 'MWWM' +SOF_MWW_MODEL_VERSION = 1 +SOF_MWW_MODEL_HDR = struct.Struct(" bytes: + """Pack a model selection header behind struct sof_abi_hdr. + + Zero threshold and avg_len keep the trigger settings of the built-in model. + """ + if not 1 <= len(name) <= 16: + raise ValueError(f"keyword name '{name}' must be 1..16 characters") + threshold_q8 = min(max(int(round(threshold * 256)), 1), 255) if threshold else 0 + if not 0 <= avg_len <= 16: + raise ValueError(f"avg_len {avg_len} not in 0..16") + hdr = SOF_MWW_MODEL_HDR.pack( + SOF_MWW_MODEL_MAGIC, SOF_MWW_MODEL_VERSION, SOF_MWW_MODEL_HDR.size, + 0, 0, 0, name.encode(), 0, 0, 0, 0, threshold_q8, avg_len, 0, 0, 0, 0, 0, 0) + abi = struct.pack("<8I", SOF_IPC4_ABI_MAGIC, 0, len(hdr), SOF_ABI_VERSION, 0, 0, 0, 0) + return abi + hdr + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("name", help="keyword name of the built-in model, e.g. hi_intel") + parser.add_argument("conf", type=Path, help="output Topology2 .conf file") + parser.add_argument("--threshold", type=float, default=0.0, + help="trigger threshold 0..1, 0 keeps the model setting") + parser.add_argument("--avg-len", type=int, default=0, + help="number of averaged probabilities, 0 keeps the model setting") + args = parser.parse_args() + + blob = build_select_blob(args.name, args.threshold, args.avg_len) + # Topology2 data objects take only the bytes attribute + lines = [",".join(f"0x{b:02x}" for b in blob[i:i + 8]) for i in range(0, len(blob), 8)] + today = datetime.date.today().strftime("%d-%b-%Y") + args.conf.parent.mkdir(parents=True, exist_ok=True) + with open(args.conf, "w") as f: + f.write(f"# Exported microWakeWord built-in model selection {today}\n") + f.write(f"# Model: {args.name}\n") + f.write("# Auto-generated by sof_mww_select_conf.py, do not edit.\n") + f.write(f'Object.Base.data."mww_select_{args.name}" {{\n') + f.write('\tbytes "\n') + f.write(",\n".join(f"\t\t{line}" for line in lines)) + f.write('"\n}\n') + + +if __name__ == "__main__": + main() diff --git a/src/include/user/mww.h b/src/include/user/mww.h index 80713815142b..99a9eba9f8cb 100644 --- a/src/include/user/mww.h +++ b/src/include/user/mww.h @@ -14,6 +14,9 @@ /** \brief Current version of struct sof_mww_model_hdr */ #define SOF_MWW_MODEL_VERSION 1 +/** \brief Size of the keyword name in struct sof_mww_model_hdr */ +#define SOF_MWW_MODEL_NAME_LEN 16 + /** \brief Type of the int8 audio features the model was trained with */ enum sof_mww_feature_type { SOF_MWW_FEATURE_UNKNOWN = 0, /**< Not specified */ @@ -28,6 +31,12 @@ enum sof_mww_feature_type { * where it follows struct sof_abi_hdr. The TFLite flatbuffer follows this * header at offset hdr_size. Its size is a multiple of 16 bytes to keep the * flatbuffer aligned. + * + * A bytes control blob with only this header and model_size 0 selects a + * built-in model by name instead of carrying one, so a topology can choose + * the keyword without the model data. In such a selection header the + * feature, CRC and arena fields are ignored, and threshold_q8 and avg_len + * override the trigger settings of the built-in model when non-zero. */ struct sof_mww_model_hdr { uint32_t magic; /**< SOF_MWW_MODEL_MAGIC */ @@ -36,7 +45,7 @@ struct sof_mww_model_hdr { uint32_t model_size; /**< Size of the TFLite flatbuffer in bytes */ uint32_t model_crc32; /**< CRC32 (IEEE 802.3, zlib) of the flatbuffer */ uint32_t arena_size; /**< Needed tensor arena in bytes, 0 if unknown */ - char name[16]; /**< Keyword name, zero padded, for information */ + char name[SOF_MWW_MODEL_NAME_LEN]; /**< Keyword name, zero padded */ uint8_t feature_type; /**< enum sof_mww_feature_type */ uint8_t num_mels; /**< Mel bands per feature hop, e.g. 40 */ uint8_t hop_ms; /**< Feature hop length in ms, e.g. 10 */ diff --git a/tools/topology/topology2/cavs-sdw.conf b/tools/topology/topology2/cavs-sdw.conf index 03ca8dec2db1..7bbaa2eb6bae 100644 --- a/tools/topology/topology2/cavs-sdw.conf +++ b/tools/topology/topology2/cavs-sdw.conf @@ -145,6 +145,10 @@ Define { SDW_ECHO_REF_DAI "false" # speaker echo reference to the multi-slot WoV ECNS instead of a host PCM SDW_SPK_ECHO_REF_ECNS "false" + # Built-in MWW model of the single-slot SoundWire DMIC WoV selected by + # keyword name, e.g. "hi_intel". With "default" the firmware picks the + # model by detector slot. + SDW_DMIC_WOV_MWW_MODEL "default" } # override defaults with platform-specific config diff --git a/tools/topology/topology2/development/tplg-targets.cmake b/tools/topology/topology2/development/tplg-targets.cmake index a3ae2e0ba69d..595822545c93 100644 --- a/tools/topology/topology2/development/tplg-targets.cmake +++ b/tools/topology/topology2/development/tplg-targets.cmake @@ -569,6 +569,6 @@ COMPRESSED_1=false,COMPRESSED_2=true" "cavs-sdw\;sof-ptl-rt713-l3-rt1320-l12-wov\;PLATFORM=ptl,SDW_DMIC=1,NUM_SDW_AMP_LINKS=2,\ SDW_AMP_FEEDBACK=false,SDW_SPK_STREAM=Playback-SmartAmp,SDW_DMIC_STREAM=Capture-SmartMic,\ SDW_JACK_OUT_STREAM=Playback-SimpleJack,SDW_JACK_IN_STREAM=Capture-SimpleJack,\ -SDW_DMIC_WOV_SINGLE=true" +SDW_DMIC_WOV_SINGLE=true,SDW_DMIC_WOV_MWW_MODEL=hi_intel" ) diff --git a/tools/topology/topology2/include/components/mww/select_hi_intel.conf b/tools/topology/topology2/include/components/mww/select_hi_intel.conf new file mode 100644 index 000000000000..7491a05c3fab --- /dev/null +++ b/tools/topology/topology2/include/components/mww/select_hi_intel.conf @@ -0,0 +1,18 @@ +# Exported microWakeWord built-in model selection 26-Nov-2026 +# Model: hi_intel +# Auto-generated by sof_mww_select_conf.py, do not edit. +Object.Base.data."mww_select_hi_intel" { + bytes " + 0x53,0x4f,0x46,0x34,0x00,0x00,0x00,0x00, + 0x40,0x00,0x00,0x00,0x01,0xd0,0x01,0x03, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x4d,0x57,0x57,0x4d,0x01,0x00,0x40,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x68,0x69,0x5f,0x69, + 0x6e,0x74,0x65,0x6c,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00" +} diff --git a/tools/topology/topology2/platform/intel/sdw-dmic-wov-single.conf b/tools/topology/topology2/platform/intel/sdw-dmic-wov-single.conf index 89a2b8a901ab..0dd89e67ee13 100644 --- a/tools/topology/topology2/platform/intel/sdw-dmic-wov-single.conf +++ b/tools/topology/topology2/platform/intel/sdw-dmic-wov-single.conf @@ -308,6 +308,9 @@ Object.Pipeline."custom-capture" [ bytes."1" { name $DMIC_WOV_MWW_BYTES_NAME max 65536 + IncludeByKey.SDW_DMIC_WOV_MWW_MODEL { + "hi_intel" "include/components/mww/select_hi_intel.conf" + } } enum."1" { name $DMIC_WOV_MWW_SCORE_NAME