Repository navigation
Add S3 storage integration tests, and S3 support for webdataset index files #6483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jantonguirao
merged 21 commits into
NVIDIA:main
from
jantonguirao:test/s3-mock-server-ci
Sep 17, 2026
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3bfec12
Add S3 storage integration tests with a mock S3 server
jantonguirao 54484e6
Address S3 test review findings
jantonguirao f447ad1
Drop stale line-number references in S3 test comments
jantonguirao bc0d17e
Simplify _with_loopback with a set
jantonguirao 9c1c720
Read no_proxy from either casing, document DALI_TEST_S3_VERBOSE
jantonguirao 466a27a
Drop the redundant sanitizer skip and another stale line ref
jantonguirao a1ae191
Require boto3/moto rather than skipping when missing
jantonguirao d903011
Exclude test_s3 from suites that don't install boto3/moto
jantonguirao b9037df
Install boto3/moto for conda instead of excluding test_s3, document e…
jantonguirao fbdb223
Require S3 support instead of skipping when it's off
jantonguirao b9a0aaf
Support remote (S3) webdataset index files
jantonguirao 9249de1
Stream webdataset index files instead of loading them in full
jantonguirao d52dc57
Test that a multi-chunk remote index parses correctly
jantonguirao 9389d49
Fix FileStreamBuf to not misread a short read as EOF
jantonguirao ff31c53
Add FileStreamBuf gtest, adversarially verified against 3 historical …
jantonguirao 26c8bf7
Bump copyright years to 2026 on substantially modified files
jantonguirao 045f7c9
Fix cpplint: add missing <utility> include for std::move
jantonguirao 1221b18
Support S3 (and other FileStream schemes) for the MXNet RecordIO inde…
jantonguirao 16f4f09
Drop the MinIO/real-S3 escape hatch from the S3 test utils
jantonguirao 6fc0fa7
Give libdali.so and libdali_operators.so one shared S3ClientManager
jantonguirao 2571a37
Fix mxnet_pipe: wrap fn.readers.mxnet's return in tuple()
jantonguirao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,294 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import io | ||
| import os | ||
| import struct | ||
| import tarfile | ||
| import tempfile | ||
| from concurrent.futures import ThreadPoolExecutor | ||
|
|
||
| import nvidia.dali.fn as fn | ||
| from nvidia.dali import pipeline_def | ||
|
|
||
| import s3_test_utils as s3 | ||
| from nose_utils import assert_raises, attr | ||
| from test_utils import compare_pipelines | ||
|
|
||
|
jantonguirao marked this conversation as resolved.
Resolved
|
||
| batch_size = 4 | ||
| num_threads = 4 | ||
|
|
||
| g_server = None | ||
| g_tmpdir = None | ||
| g_root = None | ||
| g_files = None | ||
| g_tar = None | ||
| g_index = None | ||
| g_rec = None | ||
| g_rec_index = None | ||
| g_client = None | ||
|
|
||
| DATA_PREFIX = f"{s3.PREFIX}/data" | ||
| WDS_PREFIX = f"{s3.PREFIX}/wds" | ||
| MANY_PREFIX = f"{s3.PREFIX}/many" | ||
| REC_PREFIX = f"{s3.PREFIX}/rec" | ||
|
|
||
|
|
||
| def _make_local_dataset(root): | ||
| files = [] | ||
| for cls in ("class_a", "class_b"): | ||
| os.makedirs(os.path.join(root, cls)) | ||
| for i in range(4): | ||
| name = os.path.join(cls, f"{i} x.dat") # space on purpose: NVIDIA/DALI#5521 | ||
| with open(os.path.join(root, name), "w") as f: | ||
| f.write(f"Contents of {name}.\n") | ||
| files.append(name) | ||
| empty = os.path.join("class_a", "empty.dat") # 0-byte -> HeadObject branch | ||
| open(os.path.join(root, empty), "w").close() | ||
| files.append(empty) | ||
| return sorted(files) | ||
|
|
||
|
|
||
| def _make_local_tar(path, num_samples=8): | ||
| with tarfile.open(path, "w") as tar: | ||
| for i in range(num_samples): | ||
| for ext, payload in (("txt", b"sample-%d" % i), ("cls", b"%d" % i)): | ||
| info = tarfile.TarInfo(f"{i:04}.{ext}") | ||
| info.size = len(payload) | ||
| tar.addfile(info, io.BytesIO(payload)) | ||
|
|
||
|
|
||
| def _make_local_recordio(path, index_path, num_samples=8): | ||
| """A minimal MXNet RecordIO shard: magic (0xced7230a) + length/cflag word (cflag=0, no | ||
| continuation) + a 24-byte ImageRecordIOHeader (flag=0 -> a single float label, no extra | ||
| labels) + the sample payload. The index is plain text, "<ignored> <byte offset>" per line - | ||
| RecordIOLoader::ReadIndexFile only reads the second column (dali/operators/reader/loader/ | ||
| recordio_loader.h).""" | ||
| with open(path, "wb") as rec, open(index_path, "w") as idx: | ||
| offset = 0 | ||
| for i in range(num_samples): | ||
| payload = struct.pack("<IfQQ", 0, float(i), 0, 0) + b"sample-%d" % i | ||
| record = struct.pack("<II", 0xCED7230A, len(payload)) + payload | ||
| rec.write(record) | ||
| idx.write(f"{i} {offset}\n") | ||
| offset += len(record) | ||
|
|
||
|
|
||
| def _seed_many_objects(client, count=1100): | ||
| def put(i): | ||
| client.put_object(Bucket=s3.BUCKET, Key=f"{MANY_PREFIX}/c/{i:04}.dat", Body=b"x") | ||
|
|
||
| with ThreadPoolExecutor(16) as ex: | ||
| list(ex.map(put, range(count))) | ||
|
|
||
|
|
||
| def setUpModule(): | ||
| global g_server, g_tmpdir, g_root, g_files, g_tar, g_index, g_rec, g_rec_index, g_client | ||
| s3.require_mock_server() | ||
|
|
||
| g_tmpdir = tempfile.TemporaryDirectory() | ||
| g_root = os.path.join(g_tmpdir.name, "data") | ||
| os.makedirs(g_root) | ||
| g_files = _make_local_dataset(g_root) | ||
| g_tar = os.path.join(g_tmpdir.name, "shard0.tar") | ||
| _make_local_tar(g_tar) | ||
| g_rec = os.path.join(g_tmpdir.name, "train.rec") | ||
| g_rec_index = os.path.join(g_tmpdir.name, "train.idx") | ||
| _make_local_recordio(g_rec, g_rec_index) | ||
|
|
||
| g_server = s3.make_server() | ||
| try: | ||
| endpoint = g_server.start() | ||
| s3.export_s3_env(endpoint) | ||
| s3.require_s3_support() | ||
| g_client = s3.s3_client(endpoint) | ||
| s3.create_bucket(g_client, s3.BUCKET) | ||
| s3.upload_dir(g_client, s3.BUCKET, g_root, DATA_PREFIX) | ||
| g_client.upload_file(g_tar, s3.BUCKET, f"{WDS_PREFIX}/shard0.tar") | ||
| g_client.upload_file(g_rec, s3.BUCKET, f"{REC_PREFIX}/train.rec") | ||
| g_client.upload_file(g_rec_index, s3.BUCKET, f"{REC_PREFIX}/train.idx") | ||
|
|
||
| # everything past the first upload has to stay inside this try: unittest skips | ||
| # tearDownModule once setUpModule raised, so the call below is the only thing that | ||
| # removes what was uploaded | ||
| import webdataset_base as base | ||
|
|
||
| g_index = base.generate_temp_index_file(g_tar) | ||
| g_client.upload_file(g_index.name, s3.BUCKET, f"{WDS_PREFIX}/shard0.index") | ||
| except Exception: | ||
| tearDownModule() | ||
| raise | ||
|
|
||
|
|
||
| def tearDownModule(): | ||
| global g_server, g_tmpdir, g_index, g_client | ||
| try: | ||
| # The mock server, and everything uploaded to it, is thrown away wholesale. | ||
| g_client = None | ||
| if g_server is not None: | ||
| g_server.stop() | ||
| g_server = None | ||
| finally: | ||
| if g_index is not None: | ||
| g_index.close() | ||
| g_index = None | ||
| if g_tmpdir is not None: | ||
| g_tmpdir.cleanup() | ||
| g_tmpdir = None | ||
|
|
||
|
|
||
| @pipeline_def(batch_size=batch_size, num_threads=num_threads, device_id=None) | ||
| def file_pipe(file_root=None, files=None, dont_use_mmap=False): | ||
| return tuple( | ||
|
jantonguirao marked this conversation as resolved.
Resolved
|
||
| fn.readers.file( | ||
| file_root=file_root, | ||
| files=files, | ||
| name="Reader", | ||
| dont_use_mmap=dont_use_mmap, | ||
| file_filters=["*.dat"], | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| @pipeline_def(batch_size=batch_size, num_threads=num_threads, device_id=None) | ||
| def wds_pipe(paths, index_paths=None, dont_use_mmap=False): | ||
| return tuple( | ||
| fn.readers.webdataset( | ||
| paths=paths, index_paths=index_paths, ext=["txt", "cls"], dont_use_mmap=dont_use_mmap | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| @pipeline_def(batch_size=batch_size, num_threads=num_threads, device_id=None) | ||
| def mxnet_pipe(path, index_path, dont_use_mmap=False): | ||
| return tuple(fn.readers.mxnet(path=path, index_path=index_path, dont_use_mmap=dont_use_mmap)) | ||
|
|
||
|
|
||
| def test_file_reader_file_root(): | ||
| compare_pipelines( | ||
| file_pipe(file_root=f"s3://{s3.BUCKET}/{DATA_PREFIX}"), | ||
| file_pipe(file_root=g_root, dont_use_mmap=True), # mmap cannot map the 0-byte file | ||
| batch_size, | ||
| 3, | ||
| ) | ||
|
|
||
|
|
||
| def test_file_reader_files_arg(): | ||
| compare_pipelines( | ||
| file_pipe(files=[f"s3://{s3.BUCKET}/{DATA_PREFIX}/{f}" for f in g_files]), | ||
| file_pipe(files=[os.path.join(g_root, f) for f in g_files], dont_use_mmap=True), | ||
| batch_size, | ||
| 2, | ||
| ) | ||
|
|
||
|
|
||
| @attr("slow") | ||
| def test_file_reader_listing_pagination(): | ||
| # seeded here rather than in setUpModule: it costs ~7 s and nothing else needs it | ||
| _seed_many_objects(s3.s3_client(g_server.endpoint_url)) | ||
| pipe = file_pipe(file_root=f"s3://{s3.BUCKET}/{MANY_PREFIX}") | ||
| pipe.build() | ||
| assert pipe.reader_meta("Reader")["epoch_size"] == 1100, pipe.reader_meta("Reader") | ||
|
|
||
|
|
||
| def test_webdataset_index_inferred(): | ||
| compare_pipelines( | ||
| wds_pipe(paths=f"s3://{s3.BUCKET}/{WDS_PREFIX}/shard0.tar"), | ||
| wds_pipe(paths=g_tar, dont_use_mmap=True), | ||
| batch_size, | ||
| 2, | ||
| ) | ||
|
|
||
|
|
||
| def test_webdataset_local_index(): | ||
| compare_pipelines( | ||
| wds_pipe(paths=f"s3://{s3.BUCKET}/{WDS_PREFIX}/shard0.tar", index_paths=[g_index.name]), | ||
|
jantonguirao marked this conversation as resolved.
Resolved
|
||
| wds_pipe(paths=g_tar, index_paths=[g_index.name], dont_use_mmap=True), | ||
| batch_size, | ||
| 2, | ||
| ) | ||
|
|
||
|
|
||
| def test_webdataset_remote_index(): | ||
| compare_pipelines( | ||
| wds_pipe( | ||
| paths=f"s3://{s3.BUCKET}/{WDS_PREFIX}/shard0.tar", | ||
| index_paths=[f"s3://{s3.BUCKET}/{WDS_PREFIX}/shard0.index"], | ||
| ), | ||
| wds_pipe(paths=g_tar, index_paths=[g_index.name], dont_use_mmap=True), | ||
| batch_size, | ||
| 2, | ||
| ) | ||
|
|
||
|
|
||
| @attr("slow") | ||
| def test_webdataset_remote_index_multi_chunk(): | ||
| # 3000 samples puts the index well past FileStreamBuf's 64 KiB read chunk | ||
| # (webdataset_loader.cc), so building this pipeline exercises multiple Read() round trips | ||
| # against S3 - including the one at the exact end of the index - not just the single-chunk | ||
| # case every other webdataset test here happens to hit. | ||
| import webdataset_base as base | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| big_tar = os.path.join(tmpdir, "big.tar") | ||
| _make_local_tar(big_tar, num_samples=3000) | ||
| big_index = base.generate_temp_index_file(big_tar) | ||
| try: | ||
| prefix = f"{WDS_PREFIX}/big" | ||
| g_client.upload_file(big_tar, s3.BUCKET, f"{prefix}/shard0.tar") | ||
| g_client.upload_file(big_index.name, s3.BUCKET, f"{prefix}/shard0.index") | ||
| compare_pipelines( | ||
| wds_pipe( | ||
| paths=f"s3://{s3.BUCKET}/{prefix}/shard0.tar", | ||
| index_paths=[f"s3://{s3.BUCKET}/{prefix}/shard0.index"], | ||
| ), | ||
| wds_pipe(paths=big_tar, index_paths=[big_index.name], dont_use_mmap=True), | ||
| batch_size, | ||
| 2, | ||
| ) | ||
| finally: | ||
| big_index.close() | ||
|
|
||
|
|
||
| def test_mxnet_reader_local_index(): | ||
| compare_pipelines( | ||
| mxnet_pipe(path=f"s3://{s3.BUCKET}/{REC_PREFIX}/train.rec", index_path=g_rec_index), | ||
| mxnet_pipe(path=g_rec, index_path=g_rec_index, dont_use_mmap=True), | ||
| batch_size, | ||
| 2, | ||
| ) | ||
|
|
||
|
|
||
| def test_mxnet_reader_remote_index(): | ||
| compare_pipelines( | ||
| mxnet_pipe( | ||
| path=f"s3://{s3.BUCKET}/{REC_PREFIX}/train.rec", | ||
| index_path=f"s3://{s3.BUCKET}/{REC_PREFIX}/train.idx", | ||
| ), | ||
| mxnet_pipe(path=g_rec, index_path=g_rec_index, dont_use_mmap=True), | ||
| batch_size, | ||
| 2, | ||
| ) | ||
|
|
||
|
|
||
| def test_file_reader_missing_object(): | ||
| with assert_raises(RuntimeError, glob="*S3 Object not found. bucket=*object=*"): | ||
| pipe = file_pipe(files=[f"s3://{s3.BUCKET}/{DATA_PREFIX}/no-such-object.dat"]) | ||
| pipe.build() | ||
| pipe.run() | ||
|
|
||
|
|
||
| def test_file_reader_missing_bucket(): | ||
| with assert_raises(RuntimeError, glob="*NoSuchBucket*"): | ||
| file_pipe(file_root=f"s3://{s3.MISSING_BUCKET}/data").build() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.