# SELDNet-233 Spatial Modality Integration Design ## Document Status - Owner: Student Implementation - Reviewer: Project Maintainer - Target Repository: `/apdcephfs_cq10/share_1603164/user/schmittzhu/code/spur-qwen-2.5-omni` - Related Baseline Repository: `/apdcephfs_cq10/share_1603164/user/schmittzhu/code/DCASE2024_seld_baseline` ## 1. Background This project aims to extend the current `Qwen2.5-Omni`-based system with a new **independent spatial modality** derived from a pretrained `SELDNet` encoder. The current system already supports: - text - image - video The current SPUR-related spatial path in the Omni codebase is not a truly independent modality. Instead, it computes spatial features and fuses them back into the audio encoder branch. This is **not** the desired design for this project. - keep the **original audio encoder unchanged** - use the same FOA input audio in **two parallel paths** - inject a new set of **spatial tokens** into the LLM as a separate modality ## 2. Objective Given a 4-channel FOA audio input: 1. The **W channel** should continue to go through the original Omni mono audio pipeline. 2. The full **4-channel FOA** should go through a pretrained `SELDNet-233` encoder. 3. The `SELDNet-233` encoder should provide a sequence of **spatial tokens** derived from its **MHSA output**, not from its final localization/classification heads. 4. These spatial tokens should be projected into the LLM hidden space and injected into the thinker decoder input as a new modality `<|spatial|>`. ## 3. Non-Goals - modify the existing SELD baseline source code - modify existing SELD training or inference behavior - use SELDNet final `accdoa` or `sed_logits` as the LLM input representation ## 4. Fixed Dependencies - Path: `/apdcephfs_cq10/share_1603164/user/schmittzhu/code/DCASE2024_seld_baseline` ### 4.2 SELD checkpoint to use ### 4.3 SELD task configuration - Task ID: `233` ### 4.4 SELD model definition ## 5. Key Design Decision ### 5.1 Spatial representation source The spatial representation must come from the **shared temporal representation after the final MHSA block** in `SELDNet`, not from: - final `accdoa` - final `sed_logits` - explicit detection heads ### 5.2 Why this representation This tensor is: - temporally ordered - dense - scene-level - richer than detection output heads - better suited for LLM conditioning ## 6. Target Architecture ### 6.1 Input ### 6.2 Dual-path processing - take FOA `W channel` - convert to mono - feed into original Omni audio encoder - keep existing tokenization rate unchanged - expected token rate: about `25 Hz` Path B: New spatial path - keep all 4 FOA channels - convert to SELD baseline-compatible input features - feed into pretrained `SELDNet-233` - produce spatial token sequence - project into LLM hidden size - inject as `<|spatial|>` modality ### 6.3 Final LLM view - normal text tokens - normal audio tokens from original audio encoder - optional image/video tokens - new spatial tokens from SELDNet-233 branch ## 7. Temporal Resolution Requirements ### 7.1 Audio token frequency Keep current Omni audio token frequency unchanged: - approximately `25 Hz` ### 7.2 Spatial token frequency Spatial tokens should be lower frequency: - target `2.5 Hz` This is intentional because: - lower token rate reduces prompt length - lower token rate is sufficient for spatial reasoning Recommended first version: - `h_seld`: `[B, T_seld, 128]` - `spatial_tokens`: `[B, T_spat, 256]` - `projected_spatial`: `[B, T_spat, D_llm]` Where: - `T_spat = 5 * seconds` ## 8. Data Flow 1. Input FOA waveform enters processor. 2. Processor keeps original audio path intact: - use `W channel` for original audio encoder. 3. Processor also passes full FOA waveform as `spatial_audio`. - `spatial_audio` for new spatial path 5. New SELD spatial adapter computes: - SELD MHSA output - `2.5 Hz` spatial tokens 6. Spatial projector maps tokens to LLM hidden dimension. 8. Thinker injects projected spatial embeddings into `inputs_embeds`. 9. LLM decoder consumes them as a true multimodal prefix. ## 9. Required Code Work ### A. `qwen2_5_omni_spur/modules/seldnet233_backbone.py` Purpose: - build baseline `SeldModel` with task `233` - load pretrained checkpoint - freeze model by default - register a forward hook on the final MHSA output Expected behavior: - input: baseline-compatible SELD features - output: `h_seld [B, T_seld, D_seld]` - do not modify baseline code - use import-time wrapping or dynamic module loading - use forward hook on the last MHSA normalization output ### B. `qwen2_5_omni_spur/modules/seldnet233_feature_bridge.py` Purpose: - convert FOA waveform into the feature representation expected by SELD task `233` Expected behavior: - output: baseline feature tensor `[B, C, T_feat, F_feat]` Implementation notes: - must match SELD task `233` feature config exactly - sample rate must be `16k` - should reuse baseline feature logic where possible Purpose: - build a full SELD-based spatial token extractor Internal structure: - feature bridge - SELD backbone - spatial token head Expected behavior: - output: - `spatial_tokens [B, T_spat, D_spat]` Implementation notes: - token rate target must be `2.5 Hz` - default token dim should be `256` - token head may use: - pooling - MLP - recommended first implementation: ### D. Optional: `qwen2_5_omni_spur/modules/spatial_token_projector.py` Purpose: - project SELD spatial tokens into LLM hidden dimension ## 9.2 Existing files to modify ### A. `qwen2_5_omni_spur/configuration_qwen2_5_omni.py` Add config fields: - `spatial_token_index` - `use_seld233_spatial_modality` - `seld233_checkpoint_path` - `seld233_token_dim` - `seld233_token_rate_hz` - `seld233_projector_hidden_dim` - optional `seld233_freeze_backbone` - `attribute_map` to include `spatial_token_id` ### B. `qwen2_5_omni_spur/modules/__init__.py` Export new modules: - `SeldNet233Backbone` - optional new projector class ### C. `qwen2_5_omni_spur/processing_qwen2_5_omni.py` Add a new special token: - `<|spatial|>` - store `self.spatial_token` - optionally accept `spatial_tokens` and `spatial_token_lengths` - expand `<|spatial|>` into repeated placeholders based on spatial token length - expose spatial fields through `model_input_names` Important constraint: - original W-channel mono audio path must remain unchanged ### D. `qwen2_5_omni_spur/modeling_qwen2_5_omni.py` Required changes: - instantiate SELD spatial adapter in thinker - accept `spatial_audio` in thinker forward - compute spatial embeddings - inject them via `masked_scatter` - update multimodal rope logic - route spatial fields through generation ### E. `training-qwen-omni/train_spur_spatial_only_hf.py` Update collator: - additionally pass full FOA as `spatial_audio` ### F. `qwen-omni-inference.py` Update inference script: - register `<|spatial|>` token - construct prompt with spatial placeholder - validate both paths run together ## 10. Processor Design ### 10.1 Prompt convention - `<|spatial|>` Example: - `<|AUDIO|><|spatial|> Please answer the question using both sound content and spatial cues.` ### 10.2 Placeholder expansion behavior - `T_spat` repeated spatial token positions This mirrors current audio/image/video placeholder logic. ### 10.3 Recommended input mode - processor receives `spatial_audio` Fallback mode: - processor receives precomputed `spatial_tokens` ## 11. SELD Path Design ### 11.1 Backbone loading The SELD branch must: - use task `233` - load the fixed checkpoint above - stay frozen in the first training stage ### 11.2 Feature extraction The spatial branch must operate on: - full 4-channel FOA - `16kHz` - baseline-compatible feature format ### 11.3 Hidden representation extraction The representation to extract is: - final MHSA output - not final heads - not sed logits ### 11.4 Tokenization The spatial token head must: - preserve temporal ordering - reduce token rate to `2.5 Hz` - output a fixed hidden dim, recommended `256` ### 12.1 New thinker members Add: - optional `self.seld233_spatial_norm` ### 12.2 Forward inputs Thinker forward should support: - `spatial_audio` - `spatial_tokens` - `spatial_token_lengths` Priority: - if `spatial_tokens` provided, use them directly ### 12.3 Injection point Injection should happen in the same stage where current code merges: - text - audio - video The new logic should: 1. compute `projected_spatial` 2. build `spatial_mask = (input_ids == spatial_token_id)` 3. inject with `masked_scatter` Before scatter, verify: If not, raise an error. ### 13.1 Why needed Adding a new modality requires updating multimodal position ID construction. ### 13.2 Required additions - add `spatial_token_id` - add `spatial_seqlens` - count spatial modality occurrences - add `remain_spatials` - add spatial branch when selecting next multimodal segment ### 13.3 Spatial position assignment Spatial token positions should be assigned: - length equal to `spatial_token_lengths` - consistent with `2.5 Hz` token rate ## 14. Generation Path Changes Top-level `generate()` must forward: - `spatial_audio` - `spatial_tokens` - `spatial_token_lengths` to thinker generation. ### 14.2 Talker interaction Recommended first version: - spatial positions should be zeroed before passing thinker hidden states into talker Reason: - spatial modality is meant for reasoning - not necessarily for speech token generation conditioning ## 15. Training Strategy ### 15.1 Stage 1 Freeze: - SELD backbone Train only: - spatial token head - spatial projector - LoRA / lightweight LLM adapters if used - last spatial token head layers - projector - maybe a small part of SELD adapter ### 15.3 Stage 3 Optional advanced finetuning: - limited SELD backbone finetuning - only after full pipeline is stable ## 16. Validation Plan ### 16.1 Unit-level validation - checkpoint loads successfully - MHSA output is captured successfully - spatial token head outputs expected shape - token rate is `2.5 Hz` ### 16.2 Processor validation Validate: - spatial fields appear in batch output - original audio path is unchanged Validate: - spatial projector output matches LLM hidden size - no length mismatch - no dtype/device mismatch ### 16.4 Rope validation Validate: - generation does not crash when `<|spatial|>` is present Validate: - original audio tokens still exist - spatial tokens are injected independently - text generation completes ## 17. Risks ### 17.1 Feature mismatch risk If SELD features differ from training-time `233` config, checkpoint utility may collapse. Mitigation: - strictly reuse baseline feature logic If hook location is incorrect, extracted representation may not correspond to post-MHSA scene representation. Mitigation: - verify against baseline layer order - inspect shape and stability ### 17.3 Rope mismatch If `<|spatial|>` positions are not included in multimodal rope indexing, model behavior may degrade or fail. Mitigation: - treat spatial exactly as a fourth modality in rope construction ### 17.4 Prompt length growth Adding spatial tokens increases context length. Mitigation: ## 18. Final Requirements Checklist - [ ] Use checkpoint `233_merged29_foa_16k_sedwarmup_v1_dev_split0_multiaccdoa_foa_best_full_model.h5` - [ ] Keep original audio encoder unchanged - [ ] Audio path uses `W channel` only - [ ] Spatial path uses all 4 FOA channels - [ ] Spatial representation comes from final SELD MHSA output - [ ] Spatial token rate is `2.5 Hz` - [ ] Spatial token dimension defaults to `256` - [ ] Spatial tokens are injected as a true independent modality - [ ] `<|spatial|>` placeholder is supported in processor - [ ] RoPE logic includes spatial modality - [ ] End-to-end generate path runs successfully ## 19. Recommended Implementation Order 1. Implement `seldnet233_backbone.py` 2. Implement `seldnet233_feature_bridge.py` 3. Implement `seldnet233_spatial_adapter.py` 4. Add config fields 5. Add processor `<|spatial|>` support 6. Add thinker spatial encoder + projector 7. Add spatial `masked_scatter` 8. Update `get_rope_index()` 9. Update `generate()` 10. Update collator and inference script 11. Run shape-only validation 12. Run end-to-end generation validation ## 20. Reviewer Notes This design intentionally separates: - content understanding from the original audio encoder - spatial reasoning from the SELDNet-based encoder The design is successful only if the final model sees: - mono audio content tokens at normal resolution - spatial tokens at low resolution - both as independent modalities inside the thinker input stream