Skip to content

fix(auto): generate independent keys for unkeyed batch inputs - #3772

Merged
LauraGPT merged 1 commit into
modelscope:mainfrom
hulkbig:fix/unique-auto-batch-keys-20261013
Oct 13, 2026
Merged

LauraGPT merged 1 commit into
modelscope:mainfrom
hulkbig:fix/unique-auto-batch-keys-20261013

Conversation

@hulkbig

@hulkbig hulkbig commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Generate a fresh automatic key for each non-path item in a batch when no explicit key was supplied.

prepare_data_iterator currently assigns the first generated key to the loop's key variable. Later array/byte items reuse it because key is no longer None. A path followed by an array similarly gives the array the preceding path's basename.

Minimal reproduction at e7e61293d308f4342ab9307895e710e4b51b949c:

import numpy as np
from funasr.auto.auto_model import prepare_data_iterator

waveforms = [np.zeros(3200, np.float32), np.ones(3200, np.float32)]
keys, _ = prepare_data_iterator(waveforms)
print(len(keys), len(set(keys)))
# Before: 2 1
# After:  2 2

The same duplicate keys propagate through the real AutoModel.generate wrapper with both batch_size=1 and batch_size=2. The result list still has two entries, but indexing it with {result["key"]: result for result in results} retains only the last entry. The regression tests exercise this using the repository's existing recording model.

The fix captures whether a key was supplied before the loop, rather than checking a value that the loop mutates.

Type of change

  • Bug fix
  • Documentation
  • Example or demo
  • Runtime or deployment
  • Benchmark or evaluation
  • Model/training change

Validation

python -m pytest -q tests/test_pcm_input_format.py tests/test_auto_model.py \
  tests/test_auto_model_ncpu.py tests/test_submodel_device.py \
  -k 'not test_merge_thr_in_cb_model'
python -m compileall funasr examples tests
git diff --check
  • Base production code with the new regression tests: 9 failed, 109 passed, 1 deselected, 2 subtests passed. All nine failures are the new automatic-key regressions.

  • Fixed code with the same selection: 118 passed, 1 deselected, 2 subtests passed.

  • Independent review repeated the selection in separate base/fixed checkouts and added tests/test_load_audio_bytes.py with outbound networking blocked: 9 failed, 123 passed, 1 deselected, 4 subtests passed on base; 132 passed, 1 deselected, 4 subtests passed after the fix. Another 70 boundary checks passed on both versions, and both checkouts passed compileall.

  • The deselected test downloads real model weights; it was not run.

  • Existing NumPy copy-keyword warnings also occur on the base checkout.

  • Linux x86_64, Python 3.12.14, PyTorch/torchaudio 2.8.0+cpu, NumPy 2.5.4.

  • Separately compared 26 unaffected input/key cases on base and fixed source with identical results, including explicit mixed-input keys and manifests. The URL dispatch control used a fake downloader, with no network request.

  • python -m compileall funasr examples tests

  • Docs or links checked

  • Runtime/deployment command tested

User impact

Applications passing batches of NumPy waveforms or explicit PCM bytes receive independently generated sample identifiers. Converting those results to a key-indexed mapping no longer deterministically collapses the batch to one entry.

Notes for reviewers

  • Explicit keys remain unchanged, including key="session" producing repeated "session" keys and explicit empty strings.
  • Existing path-basename behavior is preserved, including duplicate basenames and the current behavior of explicit-key mixed path/array batches. This does not introduce global basename deduplication.
  • Tests cover lists/tuples, repeated input objects, arrays, explicit PCM, mixed paths/arrays, sample preservation, and the real AutoModel.generate routing for both batch sizes.
  • Validation is weights-free. The existing _RecordingModel returns synthetic "ok" text; no ASR weights, recognition-quality benchmark, or GPU workload were run.
  • This does not change audio decoding or model computation. DatadirWriter warns and appends duplicate-key rows; the failure described here is downstream dictionary indexing, not overwriting by that writer.
  • AI assistance was used for investigation, implementation, and test preparation. The reproduction and listed checks were actually executed locally on CPU.

Remember whether the caller supplied a key before iterating over batch
items. Generate an automatic key for each non-path item instead of reusing
the key generated for an earlier item or the preceding path basename.

Preserve explicit keys and existing path-basename behavior. Add weights-free
iterator and AutoModel.generate regressions for arrays, PCM bytes, mixed
batches, and result dictionaries indexed by key.
@hulkbig
hulkbig marked this pull request as ready for review October 12, 2026 16:28

@LauraGPT LauraGPT left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified exact head 2e4eab4c531b45e14aac22e366dafd04b487d8d3 against base e7e61293d308f4342ab9307895e710e4b51b949c.

Independent weights-free validation reproduced the bug with the submitted regression tests on base (9 failed, 73 passed), then passed on this head (82 passed). The adjacent AutoModel/device/byte-loading selection passed 50 tests with 1 model-download test deselected, and compileall passed. The extracted source files matched the PR's Git blob IDs.

The change is limited to automatic result-key routing; it does not alter audio samples, decoding, model computation, or GPU runtime. Explicit keys and the existing path-basename behavior remain unchanged. I found no blocking issue in the patch.

@LauraGPT
LauraGPT merged commit f793eae into modelscope:main Oct 13, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants