Skip to content

Commit 0ebe893

Browse files
Fix braceexpand memory explosion for complex urls (#336)
* fix braceexpand memory explosion for complex urls * add tests
1 parent 934d008 commit 0ebe893

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎src/training/data.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from dataclasses import dataclass
1010
from multiprocessing import Value
1111

12-
import braceexpand
1312
import numpy as np
1413
import pandas as pd
1514
import torch
@@ -73,8 +72,8 @@ def set_epoch(self, epoch):
7372

7473

7574
def get_dataset_size(shards):
76-
shards_list = list(braceexpand.braceexpand(shards))
77-
dir_path = os.path.dirname(shards)
75+
shards_list = wds.shardlists.expand_urls(shards)
76+
dir_path = os.path.dirname(shards_list[0])
7877
sizes_filename = os.path.join(dir_path, 'sizes.json')
7978
len_filename = os.path.join(dir_path, '__len__')
8079
if os.path.exists(sizes_filename):

‎tests/test_num_shards.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
from training.data import get_dataset_size
4+
5+
@pytest.mark.parametrize(
6+
"shards,expected_size",
7+
[
8+
('/path/to/shard.tar', 1),
9+
('/path/to/shard_{000..000}.tar', 1),
10+
('/path/to/shard_{000..009}.tar', 10),
11+
('/path/to/shard_{000..009}_{000..009}.tar', 100),
12+
('/path/to/shard.tar::/path/to/other_shard_{000..009}.tar', 11),
13+
('/path/to/shard_{000..009}.tar::/path/to/other_shard_{000..009}.tar', 20),
14+
(['/path/to/shard.tar'], 1),
15+
(['/path/to/shard.tar', '/path/to/other_shard.tar'], 2),
16+
]
17+
)
18+
def test_num_shards(shards, expected_size):
19+
_, size = get_dataset_size(shards)
20+
assert size == expected_size, f'Expected {expected_size} for {shards} but found {size} instead.'

0 commit comments

Comments
 (0)