30 / docs /seld233_implementation.md
schmittzhuattencent's picture
Add files using upload-large-folder tool
65ae8ca verified
|
Raw
History Blame Contribute Delete
13.7 kB
# SELD233 Spatial Implementation Guide
## Purpose
This document is the implementation guide for the independent SELD233 spatial modality.
It answers four questions:
- which classes and functions were implemented for the core path
- which classes are already scaffolded and should be reused as-is
- what each interface accepts and returns
- what concrete code is still deferred outside the current scope
This guide should be read together with:
- `docs/spatial_encoder_plan.md`
- `docs/seld233_spatial_scaffold.md`
## Fixed Constraints
- Do not modify the DCASE baseline repository.
- Do not modify the original QwenOmni main classes for this feature; use the new spatial subclasses.
- A batch must be either all mono or all FOA.
- Mono batch:
- must contain exactly one `<|AUDIO|>`
- must not contain `<|spatial|>`
- only the original audio path is active
- FOA batch:
- must contain exactly one `<|AUDIO|>`
- must contain exactly one `<|spatial|>`
- both audio and spatial paths are active
- Audio is clipped to `0-20s`.
- Sample rate is `16kHz`.
- Spatial token rate target is `2.5 Hz`.
- Full `20s` limits:
- max samples: `320000`
- max feature frames: `1000`
- max SELD frames: `200`
- max spatial tokens: `50`
## Status Summary
### Already scaffolded and reusable
- `qwen2_5_omni_spur/spatial_seld233_utils.py`
- `qwen2_5_omni_spur/modules/seldnet233_spatial_adapter.py`
- `qwen2_5_omni_spur/modules/spatial_token_projector.py`
- `qwen2_5_omni_spur/processing_qwen2_5_omni_spatial.py`
- `qwen2_5_omni_spur/modeling_qwen2_5_omni_spatial.py`
- config additions in `qwen2_5_omni_spur/configuration_qwen2_5_omni.py`
### Must still be implemented
- training script migration to the new spatial subclasses
- inference script migration to the new spatial subclasses
### Explicitly deferred for now
- `image + spatial` joint RoPE
- `use_audio_in_video=True`
- `return_audio=True` talker path with spatial modality
## Implementation Inventory
| Priority | File | Symbol | Current State | Required Action |
| --- | --- | --- | --- | --- |
| P0 | `qwen2_5_omni_spur/modules/seldnet233_feature_bridge.py` | `SeldNet233FeatureBridge._extract_online_features` | implemented | no further action for the current scope |
| P0 | `qwen2_5_omni_spur/modules/seldnet233_backbone.py` | `SeldNet233Backbone._run_seldnet_backbone` | implemented | no further action for the current scope |
| P1 | `training-qwen-omni/train_spur_spatial_only_hf.py` | `main()` and config wiring | legacy | migrate only if this script is needed for training runs |
| P1 | `qwen-omni-inference.py` | script body | legacy | migrate only if this script is needed for ad hoc inference |
| P2 | `qwen2_5_omni_spur/modeling_qwen2_5_omni_spatial.py` | `get_rope_index` | partial | only needed later if image + spatial is required |
| P2 | `qwen2_5_omni_spur/modeling_qwen2_5_omni_spatial.py` | `generate(return_audio=True)` path | partial | only needed if talker output is required later |
## Detailed Interface Guide
### 1. `SeldNet233FeatureBridge`
File:
- `qwen2_5_omni_spur/modules/seldnet233_feature_bridge.py`
Class:
- `SeldNet233FeatureBridge`
Already implemented:
- constructor
- input validation
- waveform mask -> valid length conversion
- waveform length -> feature length conversion
- output mask bookkeeping
- online baseline-aligned STFT / log-mel / intensity-vector extraction
- baseline `foa_wts` normalization
Input contract:
- `spatial_audio`: `torch.Tensor`, shape `[B, T_audio_max, 4]`
- `spatial_audio_lengths`: `torch.LongTensor`, shape `[B]`
- `feature_lengths`: `torch.LongTensor`, shape `[B]`
- `feature_attention_mask`: `torch.BoolTensor`, shape `[B, T_feat_max]`
Output contract:
- `SeldNet233FeatureBridgeOutput.features`: `[B, 7, T_feat_max, 64]`
- `SeldNet233FeatureBridgeOutput.feature_attention_mask`: `[B, T_feat_max]`
- `SeldNet233FeatureBridgeOutput.feature_lengths`: `[B]`
Required implementation content:
1. Load or derive the exact task-233 feature configuration.
2. Reproduce the baseline feature math for FOA:
- `4ch` log-mel
- `3ch` FOA intensity vector
3. Use the task-233 normalization weights.
4. Keep output frame count aligned with `feature_lengths`.
5. Zero-fill padded tail frames only after valid frames are computed.
Recommended private helpers to add inside this file:
- `_load_task233_feature_config()`
- `_load_feature_stats()`
- `_compute_stft(...)`
- `_compute_logmel_channels(...)`
- `_compute_intensity_vector_channels(...)`
- `_normalize_feature_tensor(...)`
Important shape transitions:
- raw FOA waveform: `[B, T_audio_max, 4]`
- internal channels-first view: `[B, 4, T_audio_max]`
- mel-like features before stacking: `[B, 4, T_feat_max, 64]`
- intensity-vector features: `[B, 3, T_feat_max, 64]`
- final baseline tensor: `[B, 7, T_feat_max, 64]`
Validation checklist:
- same sample length gives same `T_feat` as `samples_to_feature_frames`
- values match offline baseline extraction on the same clip
- normalization path is identical to baseline task `233`
### 2. `SeldNet233Backbone`
File:
- `qwen2_5_omni_spur/modules/seldnet233_backbone.py`
Class:
- `SeldNet233Backbone`
Already implemented:
- constructor
- input validation
- feature length -> SELD length conversion
- output mask bookkeeping
- dynamic baseline loading
- checkpoint restore with shape-compatible filtering
- last MHSA LayerNorm hidden capture
Input contract:
- `seld233_features`: `torch.Tensor`, shape `[B, 7, T_feat_max, 64]`
- `seld233_feature_lengths`: `torch.LongTensor`, shape `[B]`
- `hidden_lengths`: `torch.LongTensor`, shape `[B]`
- `hidden_attention_mask`: `torch.BoolTensor`, shape `[B, T_seld_max]`
Output contract:
- `SeldNet233BackboneOutput.hidden_states`: `[B, T_seld_max, 128]`
- `SeldNet233BackboneOutput.hidden_attention_mask`: `[B, T_seld_max]`
- `SeldNet233BackboneOutput.hidden_lengths`: `[B]`
Required implementation content:
1. Dynamically load `parameters.py` and `seldnet_model.py` from the baseline repo path.
2. Build the task-233 baseline model.
3. Restore the pretrained checkpoint from `seld233_checkpoint_path`.
4. Freeze the backbone if `seld233_freeze_backbone=True`.
5. Register a hook at the final MHSA shared representation.
6. Feed the baseline-compatible features into the model.
7. Return the captured hidden tensor, not accdoa heads and not sed logits.
Recommended private helpers to add inside this file:
- `_load_baseline_modules()`
- `_build_seld233_model()`
- `_load_checkpoint_weights()`
- `_register_hidden_hook()`
- `_prepare_baseline_input_layout(...)`
Important shape transitions:
- model input for this wrapper: `[B, 7, T_feat_max, 64]`
- likely baseline internal flattened layout: `[B, T_feat_max, 448]`
- captured MHSA hidden: `[B, T_seld_max, 128]`
Validation checklist:
- checkpoint path exists
- hook fires exactly once per forward
- hidden dim is exactly `128`
- hidden length matches `feature_frames_to_seld_frames`
### 3. `SeldNet233SpatialAdapter`
File:
- `qwen2_5_omni_spur/modules/seldnet233_spatial_adapter.py`
Class:
- `SeldNet233SpatialAdapter`
Status:
- already usable once feature bridge and backbone are implemented
Input modes already supported:
- raw audio path:
- `spatial_audio [B, T_audio_max, 4]`
- offline feature path:
- `seld233_features [B, 7, T_feat_max, 64]`
- direct hidden path:
- `seld233_hidden_states [B, T_seld_max, 128]`
Output:
- `spatial_tokens [B, T_spat_max, 256]`
- `spatial_token_attention_mask [B, T_spat_max]`
- `spatial_token_lengths [B]`
No new implementation is required here unless you want to change:
- downsampling rule
- token MLP architecture
- token dimension
Current downsampling rule:
- `T_spat = ceil(T_seld / 4)`
- effective rate: `10 Hz -> 2.5 Hz`
### 4. `SpatialTokenProjector`
File:
- `qwen2_5_omni_spur/modules/spatial_token_projector.py`
Class:
- `SpatialTokenProjector`
Status:
- already implemented
Input:
- `spatial_tokens [B, T_spat, D_in]`
Output:
- projected tokens `[B, T_spat, D_llm]`
No further code is required here unless model capacity needs tuning.
### 5. `Qwen2_5OmniSpatialProcessor`
File:
- `qwen2_5_omni_spur/processing_qwen2_5_omni_spatial.py`
Class:
- `Qwen2_5OmniSpatialProcessor`
Status:
- implemented scaffold
- should be reused, not rewritten
Important public interfaces:
- `__call__(...)`
- `sync_spatial_tokenizer_with_model(model)`
Input behavior:
- mono batch:
- prompt must include one `<|AUDIO|>`
- prompt must not include `<|spatial|>`
- FOA batch:
- prompt must include one `<|AUDIO|>`
- prompt must include one `<|spatial|>`
Output keys already supported:
- `input_ids`
- `input_features`
- `feature_attention_mask`
- `spatial_audio`
- `spatial_audio_attention_mask`
- `spatial_audio_lengths`
- `spatial_tokens`
- `seld233_features`
- `seld233_feature_attention_mask`
- `seld233_feature_lengths`
- `spatial_token_lengths`
No core algorithm is missing here.
What still needs to happen outside this file:
- training script must actually instantiate this processor
- inference script must actually instantiate this processor
- both scripts must call `processor.sync_spatial_tokenizer_with_model(model)`
### 6. `Qwen2_5OmniSpatialThinkerForConditionalGeneration`
File:
- `qwen2_5_omni_spur/modeling_qwen2_5_omni_spatial.py`
Class:
- `Qwen2_5OmniSpatialThinkerForConditionalGeneration`
Status:
- spatial injection path is implemented
- tokenizer sync helper is implemented
- prefill spatial routing is implemented
- video + audio + spatial RoPE is implemented for `use_audio_in_video=False`
Important public interfaces:
- `forward(...)`
- `prepare_inputs_for_generation(...)`
- `sync_spatial_tokenizer(...)`
Input contract:
- normal Qwen text/audio inputs
- optional spatial inputs:
- `spatial_audio [B, T_audio_max, 4]`
- `seld233_features [B, 7, T_feat_max, 64]`
- `spatial_tokens [B, T_spat_max, D_spat]`
- matching length or mask tensors
Output behavior:
- if no spatial input is provided, it falls back to the base thinker
- if spatial input is provided, it injects projected spatial embeddings at
`<|spatial|>` positions
Still missing only if scope expands later:
- true multimodal RoPE for `image + spatial`
- talker/audio-output path
### 7. `Qwen2_5OmniSpatialForConditionalGeneration`
File:
- `qwen2_5_omni_spur/modeling_qwen2_5_omni_spatial.py`
Class:
- `Qwen2_5OmniSpatialForConditionalGeneration`
Status:
- implemented scaffold
Important public interfaces:
- `sync_spatial_tokenizer(...)`
- `generate(...)`
No core code is missing for text-only generation with audio/spatial input.
Still deferred:
- `return_audio=True` path
## Script-Level Work
### 8. Training Script
File:
- `training-qwen-omni/train_spur_spatial_only_hf.py`
Current issue:
- it still uses the old SPUR audio-fusion line
Minimum required changes:
1. Replace imports:
- old:
- `Qwen2_5OmniProcessor`
- `Qwen2_5OmniForConditionalGeneration`
- new:
- `Qwen2_5OmniSpatialProcessor`
- `Qwen2_5OmniSpatialForConditionalGeneration`
2. Replace old SPUR config overrides with new `seld233_*` thinker config fields.
3. After loading processor and model, call:
- `processor.sync_spatial_tokenizer_with_model(model)`
4. Update prompt construction:
- mono batch prompt: includes `<|AUDIO|>` only
- FOA batch prompt: includes `<|AUDIO|><|spatial|>`
5. Ensure collator preserves mono or FOA batch homogeneity.
6. Ensure audio clips are clipped to `20s`.
7. Ensure training batch uses the new processor output keys.
Functions that most likely need modification:
- `apply_spur_config_overrides(...)`
- `main()`
### 9. Inference Script
File:
- `qwen-omni-inference.py`
Current issue:
- it still uses the old processor/model path
Minimum required changes:
1. Replace imports with the new spatial subclasses.
2. Load FOA audio without collapsing channels.
3. Build prompt with canonical tokens:
- FOA: `<|AUDIO|><|spatial|> ...`
- mono: `<|AUDIO|> ...`
4. Call:
- `processor.sync_spatial_tokenizer_with_model(model)`
5. Print or assert:
- `spatial_token_lengths`
- number of `<|spatial|>` token positions after tokenization
6. Run one prefill/generation sanity check.
There is no reusable function boundary in the current script; the top-level
script body itself needs to be migrated.
## Suggested Implementation Order
1. Implement `SeldNet233FeatureBridge._extract_online_features`.
2. Implement `SeldNet233Backbone._run_seldnet_backbone`.
3. Run a direct module test:
- `spatial_audio -> feature bridge -> backbone -> adapter`
4. Migrate `training-qwen-omni/train_spur_spatial_only_hf.py`.
5. Migrate `qwen-omni-inference.py`.
6. Only after that, consider optional RoPE/talker extensions.
## Required Checks After Each Stage
### After feature bridge
- same clip gives matching online vs offline task-233 features
- feature tensor is `[B, 7, T_feat_max, 64]`
- feature mask matches `feature_lengths`
### After backbone
- checkpoint loads successfully
- captured hidden is `[B, T_seld_max, 128]`
- hidden length matches derived `T_seld`
### After end-to-end spatial adapter
- `spatial_tokens` is `[B, T_spat_max, 256]`
- `spatial_token_lengths` equals `ceil(T_seld / 4)`
- no placeholder count mismatch
### After training/inference migration
- `<|spatial|>` is present in tokenizer vocab
- `config.thinker_config.spatial_token_index` is set
- thinker embedding size matches tokenizer size
- mono batch runs without spatial injection
- FOA batch runs with spatial injection
## Out of Scope For The Next Coding Stage
- support mixed mono/FOA batches
- support image/video + spatial at the same time
- support talker audio generation with spatial tokens
- change the task-233 feature definition or baseline architecture