-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Dataset mixture at the shard level #425
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e8484cf
Add support for dataset mixtures with different sampling weights
gabrielilharco cfb5758
Handle re-weighting at the sample level instead of the shard level
gabrielilharco ccddc2d
cleaning up
gabrielilharco 9fa4349
adding documentation
gabrielilharco 09a68fe
iterator fix
gabrielilharco 90e804f
sampling at the shard level instead of the sample level
gabrielilharco 12c86ab
merging
gabrielilharco ef2724a
remove script
gabrielilharco f0668fc
removing scripts
gabrielilharco e62d830
fixing bad merge
gabrielilharco 5566005
clarifying readme
gabrielilharco 6bc024b
changing defaults, adding tests and renaming flag
gabrielilharco 82cc840
test data is created on the fly
gabrielilharco 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,149 @@ | ||
| import os | ||
| import pytest | ||
| import util_test | ||
| import collections | ||
| import tarfile | ||
| import io | ||
| from PIL import Image | ||
|
|
||
| from training.data import get_wds_dataset | ||
| from training.params import parse_args | ||
| from training.main import random_seed | ||
|
|
||
| TRAIN_NUM_SAMPLES = 10_000 | ||
| RTOL = 0.2 | ||
|
|
||
| # NOTE: we use two test tar files, which are created on the fly and saved to data/input. | ||
| # 000.tar has 10 samples, and the captions are 000_0, 000_1, ..., 000_9 | ||
| # 001.tar has 5 samples, and the captions are 001_0, 001_1, ..., 001_4 | ||
| def build_inputs(test_name): | ||
| base_input_dir, _ = util_test.get_data_dirs() | ||
| input_dir = os.path.join(base_input_dir, test_name) | ||
| os.makedirs(input_dir, exist_ok=True) | ||
|
|
||
| def save_tar(idx, num_samples): | ||
| filename = os.path.join(input_dir, f'test_data_{idx:03d}.tar') | ||
| tar = tarfile.open(filename, 'w') | ||
|
|
||
| for sample_idx in range(num_samples): | ||
| # Image | ||
| image = Image.new('RGB', (32, 32)) | ||
| info = tarfile.TarInfo(f'{sample_idx}.png') | ||
| bio = io.BytesIO() | ||
| image.save(bio, format='png') | ||
| size = bio.tell() | ||
| bio.seek(0) | ||
| info.size = size | ||
| tar.addfile(info, bio) | ||
|
|
||
| # Caption | ||
| info = tarfile.TarInfo(f'{sample_idx}.txt') | ||
| bio = io.BytesIO() | ||
| bio.write(f'{idx:03d}_{sample_idx}'.encode('utf-8')) | ||
| size = bio.tell() | ||
| bio.seek(0) | ||
| info.size = size | ||
| tar.addfile(info, bio) | ||
|
|
||
| tar.close() | ||
|
|
||
| save_tar(0, 10) | ||
| save_tar(1, 5) | ||
|
|
||
| return input_dir | ||
|
|
||
|
|
||
| def build_params(input_shards, seed=0): | ||
| args = parse_args([]) | ||
| args.train_data = input_shards | ||
| args.train_num_samples = TRAIN_NUM_SAMPLES | ||
| args.dataset_resampled = True | ||
| args.seed = seed | ||
| args.workers = 1 | ||
| args.world_size = 1 | ||
| args.batch_size = 1 | ||
| random_seed(seed) | ||
|
|
||
| preprocess_img = lambda x: x | ||
| tokenizer = lambda x: [x.strip()] | ||
|
|
||
| return args, preprocess_img, tokenizer | ||
|
|
||
|
|
||
| def get_dataloader(input_shards): | ||
| args, preprocess_img, tokenizer = build_params(input_shards) | ||
| dataset = get_wds_dataset(args, preprocess_img, is_train=True, tokenizer=tokenizer) | ||
| dataloader = dataset.dataloader | ||
| return dataloader | ||
|
|
||
|
|
||
| def test_single_source(): | ||
| """Test webdataset with a single tar file.""" | ||
| input_dir = build_inputs('single_source') | ||
| input_shards = os.path.join(input_dir, 'test_data_000.tar') | ||
| dataloader = get_dataloader(input_shards) | ||
|
|
||
| counts = collections.defaultdict(int) | ||
| for sample in dataloader: | ||
| txts = sample[1] | ||
| for txt in txts: | ||
| counts[txt] += 1 | ||
|
|
||
| for key, count in counts.items(): | ||
| assert count == pytest.approx(TRAIN_NUM_SAMPLES / 10, RTOL) | ||
|
|
||
|
|
||
| def test_two_sources(): | ||
| """Test webdataset with a single two tar files.""" | ||
| input_dir = build_inputs('two_sources') | ||
| input_shards = os.path.join(input_dir, 'test_data_{000..001}.tar') | ||
| dataloader = get_dataloader(input_shards) | ||
|
|
||
| counts = collections.defaultdict(int) | ||
| for sample in dataloader: | ||
| txts = sample[1] | ||
| for txt in txts: | ||
| counts[txt] += 1 | ||
|
|
||
| for key, count in counts.items(): | ||
| assert count == pytest.approx(TRAIN_NUM_SAMPLES / 15, RTOL), f'{key}, {count}' | ||
|
|
||
|
|
||
| def test_two_sources_same_weights(): | ||
| """Test webdataset with a two tar files, using --train-data-weights=1::1.""" | ||
| input_dir = build_inputs('two_sources_same_weights') | ||
| input_shards = f"{os.path.join(input_dir, 'test_data_000.tar')}::{os.path.join(input_dir, 'test_data_001.tar')}" | ||
| args, preprocess_img, tokenizer = build_params(input_shards) | ||
| args.train_data_upsampling_factors = '1::1' | ||
| dataset = get_wds_dataset(args, preprocess_img, is_train=True, tokenizer=tokenizer) | ||
| dataloader = dataset.dataloader | ||
|
|
||
| counts = collections.defaultdict(int) | ||
| for sample in dataloader: | ||
| txts = sample[1] | ||
| for txt in txts: | ||
| counts[txt] += 1 | ||
|
|
||
| for key, count in counts.items(): | ||
| assert count == pytest.approx(TRAIN_NUM_SAMPLES / 15, RTOL), f'{key}, {count}' | ||
|
|
||
| def test_two_sources_with_upsampling(): | ||
| """Test webdataset with a two tar files with upsampling.""" | ||
| input_dir = build_inputs('two_sources_with_upsampling') | ||
| input_shards = f"{os.path.join(input_dir, 'test_data_000.tar')}::{os.path.join(input_dir, 'test_data_001.tar')}" | ||
| args, preprocess_img, tokenizer = build_params(input_shards) | ||
| args.train_data_upsampling_factors = '1::2' | ||
| dataset = get_wds_dataset(args, preprocess_img, is_train=True, tokenizer=tokenizer) | ||
| dataloader = dataset.dataloader | ||
|
|
||
| counts = collections.defaultdict(int) | ||
| for sample in dataloader: | ||
| txts = sample[1] | ||
| for txt in txts: | ||
| counts[txt] += 1 | ||
|
|
||
| for key, count in counts.items(): | ||
| if key.startswith('000'): | ||
| assert count == pytest.approx(TRAIN_NUM_SAMPLES / 20, RTOL), f'{key}, {count}' | ||
| else: | ||
| assert count == pytest.approx(TRAIN_NUM_SAMPLES / 10, RTOL), f'{key}, {count}' |
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.