Skip to content

Studio: preserve S3 dataset files with colliding names - #16830

Merged
oobabooga merged 2 commits into
unslothai:mainfrom
DimitriChrysafis:studio-s3-distinct-objects
Nov 7, 2026
Merged

oobabooga merged 2 commits into
unslothai:mainfrom
DimitriChrysafis:studio-s3-distinct-objects

Conversation

@DimitriChrysafis

Copy link
Copy Markdown
Contributor

What was broken

S3 audio datasets can silently pair transcripts with the wrong recording. Distinct object keys can map to one local filename, so the later download replaces the earlier recording while both manifest rows remain. Manifest shards can also overwrite each other, and structured downloads overwrite existing files in an explicitly supplied destination.

Repro

  1. On main at 0ab3f0773, prepare an S3 audio dataset containing distinct datasets/audio/clip.wav and datasets/audio//clip.wav objects. Reference each by its full S3 URI in a JSONL manifest.
  2. Load it through prepare_s3_dataset_download.
  3. Both rewritten audio paths point to the second recording. Case-only and NFC/NFD filename differences cause the same loss on case-insensitive or normalization-insensitive filesystems.

Fix

Reserve distinct local paths before downloading, using the existing suffix allocator with case-insensitive, Unicode-normalized reservations. Keep the original S3 key-to-path mapping for JSON, JSONL and CSV reference rewriting. Existing local files are preserved.

Parquet references cannot be rewritten here. If an audio filename must change, fail before downloading with guidance instead of returning a dataset bound to the wrong file. Non-colliding Parquet audio paths stay unchanged, and colliding Parquet manifest shards can still be renamed safely.

Test

Thirteen initial regression cases fail on main. Expanded coverage checks recordings and transcripts across JSON, JSONL and CSV, manifest shards, generated suffix collisions, existing files, and Parquet refusal and compatibility paths. Tests use the existing fake S3 client with real local files; no AWS credentials or GPU are needed.

From studio/backend:

$ python -m pytest -q --tb=short --timeout=330 tests/test_s3_audio_dataset.py tests/test_s3_dataset.py
97 passed in 1.95s
$ python -m pytest -q --tb=short -n 4 --dist loadgroup --timeout=330 tests/test_s3_audio_dataset.py tests/test_s3_dataset.py tests/test_s3_runtime.py tests/test_s3_optional.py
120 passed in 11.16s

All seven repository checks, the pinned formatter and strict i18n parity pass locally. No schema or dependency changes.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T01:10:15.891264Z 02ea5b0 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 02ea5b037b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +213 to +215
local_by_key[key] = _unique_local_path(
os.path.dirname(path), os.path.basename(path), used_paths
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Reject audio-path renames for Parquet manifests

For path-backed Parquet audio datasets, this allocator can now rename a referenced recording, but _rewrite_audio_references skips .parquet, leaving the stored path unchanged. On case-sensitive Linux, for example, the previously distinct A.wav/a.wav files become A.wav/a_1.wav, breaking a reference to a.wav. If the destination already contains clip.wav, a Parquet row referencing that path silently reads the old recording while the new recording is downloaded as clip_1.wav. Reject Parquet downloads that require audio-path renames before downloading, or update the Parquet references instead of returning success.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Parquet audio paths are never resolved against the download directory, so renaming a downloaded file cannot change which file a row reads. load_dataset("parquet", ...) returns audio/a.wav exactly as stored, and both Audio.decode_example and _read_mono open a local path relative to the process working directory. Every production caller (trainer.py, worker.py, decision_trainer.py) downloads into a fresh mkdtemp directory that a Parquet file cannot name in advance, so audio/a.wav resolves the same way before and after this change. The existing clip.wav case needs an explicit dest_dir, which only tests pass. The refusal was removed on purpose in 02ea5b0.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 02ea5b037b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +213 to +215
local_by_key[key] = _unique_local_path(
os.path.dirname(path), os.path.basename(path), used_paths
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reserve required directories before allocating collision suffixes

On a case-sensitive filesystem, a prefix containing audio/A.wav, audio/a.wav, and audio/a_1.wav/part.wav now allocates audio/a_1.wav to the second recording even though it is also a directory required by the third key. All allocations happen before downloading and only leaf paths are reserved, so _download_structured writes that recording and then raises FileExistsError when creating the third object's parent directory. This dataset downloaded successfully before the change. Reserve the required directory paths before assigning suffixes so generated filenames cannot occupy them.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This needs a prefix that holds a directory named with an audio extension (audio/a_1.wav/) whose name also equals the suffix generated for a case-colliding sibling. Real datasets do not name directories *.wav, and the failure is a clear FileExistsError before training starts, not a wrong recording. I am not adding directory reservation for that case.

@DimitriChrysafis DimitriChrysafis Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed — the trigger needs a directory literally named with an audio extension and the failure is loud, not silent. Dropped the reservation commit; branch tip is back at 02ea5b0.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@DimitriChrysafis
DimitriChrysafis force-pushed the studio-s3-distinct-objects branch from b488738 to 02ea5b0 Compare November 6, 2026 21:34
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 02ea5b037b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 02ea5b037b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member

audio/clip.wav and audio//clip.wav are different S3 objects, but _structured_local_paths mapped both to the same local file, so the second download overwrote the first and both manifest rows trained on the same recording. Case-only names on macOS and Windows collided the same way. Paths are now reserved before downloading, and a taken name is saved as clip_1.wav with the manifest rewritten to match.

I pushed 02ea5b0 to drop the Parquet error: relative Parquet audio paths never reach the temp download folder, so that audio isn't used either way, and the error could only block datasets that work today.

I reproduced the mix-up on main, the S3 tests pass on the PR merged with current main, and Mirror CI is clear at 3a0e6f6.

@oobabooga
oobabooga merged commit 41da0b1 into unslothai:main Nov 7, 2026
rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Nov 18, 2026
S3 audio downloads leave paths in Parquet manifests unchanged. A row
pointing to `audio/a.wav` still resolves against the process working
directory after the recording is downloaded into a fresh
`unsloth_s3_dataset_*` directory. Full `s3://` references also bypass
the downloaded copy. JSON, JSONL and CSV manifests already use the local
paths. The Parquet limitation was noted in [the review of
unslothai#16830](unslothai#16830 (comment)).

Apply the existing reference resolver to Parquet rows in batches of
1,024, preserving the Arrow schema and Hugging Face feature metadata.
Paths use the allocator's actual filenames, including collision
suffixes. A completed rewrite replaces the manifest atomically;
ambiguity or write errors leave the original intact and remove the
staged file. A manifest without matching references stays
byte-identical.

The S3 fake now writes real Parquet rather than text under a `.parquet`
filename, so the existing download and collision tests continue to
exercise valid input.

Validation on macOS, Python 3.12.4, datasets 4.3.0 and PyArrow 21.0.0:

- The initial regression run has 12 failures on unchanged main.
- All 138 tests pass across the audio download, general S3, optional-SDK
and runtime suites.
- Coverage includes string paths, undecoded HF Audio structs, lists,
nulls, embedded bytes, prefix/manifest-relative paths, full S3 URIs,
colliding keys, later-batch ambiguity, and an injected full-disk error.
A real `datasets.load_dataset` round trip preserves Audio features and
resolves a WAV whose sample bytes and sample rate are checked.
- Repository-pinned Ruff lint and formatting, duplicate-definition and
import-hoist checks, and `git diff --check` pass.

```sh
python -m pytest studio/backend/tests/test_s3_audio_dataset.py \
  studio/backend/tests/test_s3_dataset.py studio/backend/tests/test_s3_optional.py \
  studio/backend/tests/test_s3_runtime.py -q --tb=short --timeout=60
```

S3 transport is faked. No AWS service call, model training, or GPU run.


Follow-up fixes on this branch:

- Apple Silicon (MLX) training deleted the S3 download right after
loading the manifest, before `_mlx_audio_rows` decoded the clips, so S3
audio training there failed for JSON, JSONL, CSV and Parquet manifests
alike. The worker now keeps an S3 audio download until the run ends.
`test_mlx_s3_audio_download_outlives_the_model_load` fails without this
change.
- The no-manifest error now lists Parquet among the supported manifest
formats.
- A test read the rewritten manifest without an encoding, which failed
on Windows (cp1252). It now reads UTF-8.

---------

Signed-off-by: dimitri <[email protected]>
Co-authored-by: Daniel Han <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants