wolof-translate 0.0.3__py3-none-any.whl → 0.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wolof_translate/utils/bucket_iterator.py +40 -9
- {wolof_translate-0.0.3.dist-info → wolof_translate-0.0.4.dist-info}/METADATA +1 -1
- {wolof_translate-0.0.3.dist-info → wolof_translate-0.0.4.dist-info}/RECORD +5 -5
- {wolof_translate-0.0.3.dist-info → wolof_translate-0.0.4.dist-info}/WHEEL +0 -0
- {wolof_translate-0.0.3.dist-info → wolof_translate-0.0.4.dist-info}/top_level.txt +0 -0
|
@@ -5,29 +5,43 @@ from math import ceil
|
|
|
5
5
|
from tqdm import tqdm
|
|
6
6
|
import time
|
|
7
7
|
|
|
8
|
+
|
|
8
9
|
class SequenceLengthBatchSampler(Sampler[List[int]]):
|
|
9
10
|
def __init__(
|
|
10
11
|
self,
|
|
11
12
|
dataset,
|
|
12
13
|
boundaries: List[int],
|
|
13
14
|
batch_sizes: List[int],
|
|
14
|
-
input_key: Optional[int] = None,
|
|
15
|
-
label_key: Optional[int] = None,
|
|
15
|
+
input_key: Optional[Union[int, str]] = None,
|
|
16
|
+
label_key: Optional[Union[int, str]] = None,
|
|
16
17
|
drop_unique: bool = True,
|
|
17
18
|
):
|
|
19
|
+
"""
|
|
20
|
+
Sampler that batches sequences of similar lengths together to minimize padding.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
dataset: Dataset to sample from.
|
|
24
|
+
boundaries: List of length boundaries to bucket sequences.
|
|
25
|
+
batch_sizes: List of batch sizes per bucket (length must be len(boundaries)+1).
|
|
26
|
+
input_key: Key or index to access input sequence in dataset item.
|
|
27
|
+
label_key: Key or index to access label sequence in dataset item.
|
|
28
|
+
drop_unique: Whether to drop batches with a single leftover element.
|
|
29
|
+
"""
|
|
18
30
|
self.dataset = dataset
|
|
19
31
|
self.boundaries = boundaries
|
|
20
32
|
self.batch_sizes = batch_sizes
|
|
21
33
|
self.drop_unique = drop_unique
|
|
22
34
|
|
|
35
|
+
assert len(batch_sizes) == len(boundaries) + 1, (
|
|
36
|
+
f"batch_sizes length ({len(batch_sizes)}) must be one more than boundaries length ({len(boundaries)})"
|
|
37
|
+
)
|
|
38
|
+
|
|
23
39
|
start_time = time.time()
|
|
24
40
|
tqdm.write("Computing sequence lengths...")
|
|
25
41
|
|
|
26
|
-
# Compute lengths with tqdm progress bar
|
|
27
42
|
self.lengths = np.array([
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
for data in tqdm(dataset, desc="Lengths", unit="seq")
|
|
43
|
+
self._get_length(data, input_key, label_key)
|
|
44
|
+
for data in tqdm(self.dataset, desc="Lengths", unit="seq")
|
|
31
45
|
])
|
|
32
46
|
|
|
33
47
|
tqdm.write(f"Sequence lengths computed in {time.time() - start_time:.2f} seconds.")
|
|
@@ -35,7 +49,7 @@ class SequenceLengthBatchSampler(Sampler[List[int]]):
|
|
|
35
49
|
start_time = time.time()
|
|
36
50
|
tqdm.write("Assigning buckets...")
|
|
37
51
|
|
|
38
|
-
# Assign bucket ids
|
|
52
|
+
# Assign bucket ids (0-based)
|
|
39
53
|
self.bucket_ids = np.digitize(self.lengths, bins=self.boundaries, right=True)
|
|
40
54
|
|
|
41
55
|
# Create buckets of indices
|
|
@@ -46,7 +60,6 @@ class SequenceLengthBatchSampler(Sampler[List[int]]):
|
|
|
46
60
|
start_time = time.time()
|
|
47
61
|
tqdm.write("Preparing batches...")
|
|
48
62
|
|
|
49
|
-
# Prepare batches from buckets
|
|
50
63
|
self.batches = []
|
|
51
64
|
for bucket, batch_size in zip(self.buckets, self.batch_sizes):
|
|
52
65
|
bucket = bucket.copy()
|
|
@@ -66,8 +79,26 @@ class SequenceLengthBatchSampler(Sampler[List[int]]):
|
|
|
66
79
|
self.length = len(self.batches)
|
|
67
80
|
tqdm.write(f"Batches prepared in {time.time() - start_time:.2f} seconds.")
|
|
68
81
|
|
|
82
|
+
def _get_length(self, data, input_key, label_key) -> int:
|
|
83
|
+
"""
|
|
84
|
+
Helper to get the max length of input and label sequences in a dataset item.
|
|
85
|
+
|
|
86
|
+
Supports dict-like or tuple/list-like dataset items.
|
|
87
|
+
"""
|
|
88
|
+
try:
|
|
89
|
+
if input_key is None or label_key is None:
|
|
90
|
+
# Assume tuple/list with input at 0, label at 2
|
|
91
|
+
input_seq = data[0]
|
|
92
|
+
label_seq = data[2]
|
|
93
|
+
else:
|
|
94
|
+
input_seq = data[input_key]
|
|
95
|
+
label_seq = data[label_key]
|
|
96
|
+
return max(len(input_seq), len(label_seq))
|
|
97
|
+
except Exception as e:
|
|
98
|
+
raise ValueError(f"Error accessing lengths with input_key={input_key}, label_key={label_key}: {e}")
|
|
99
|
+
|
|
69
100
|
def __iter__(self) -> Iterator[List[int]]:
|
|
70
|
-
# Shuffle
|
|
101
|
+
# Shuffle batches globally for randomness
|
|
71
102
|
np.random.shuffle(self.batches)
|
|
72
103
|
for batch in self.batches:
|
|
73
104
|
yield batch
|
|
@@ -22,7 +22,7 @@ wolof_translate/trainers/transformer_trainer_custom.py,sha256=hHUBcU4YK6wuRUMiwX
|
|
|
22
22
|
wolof_translate/trainers/transformer_trainer_ml.py,sha256=WgggaugkVHSJlwIAZT-QwI90Fl-_zT8Clhb-7M0m8gM,33561
|
|
23
23
|
wolof_translate/trainers/transformer_trainer_ml_.py,sha256=QaN9DB5pqhBxV4WlFmJCmUyfwlX-UyAzKRwL6rVEr4Q,38199
|
|
24
24
|
wolof_translate/utils/__init__.py,sha256=Nl3300H-Xd3uTHDR8y-rYa-UUR9FqbqZPwUKJUpQOb4,64
|
|
25
|
-
wolof_translate/utils/bucket_iterator.py,sha256=
|
|
25
|
+
wolof_translate/utils/bucket_iterator.py,sha256=sGSBCGPn8NzZ32mfEKh0cfH1Z0WbNJWbDjghR8-u5tU,9847
|
|
26
26
|
wolof_translate/utils/database_manager.py,sha256=7yhgBN1LvVFNEQikxCjSCva82h5nX44Nx2zh8cpFWyA,3543
|
|
27
27
|
wolof_translate/utils/display_predictions.py,sha256=y5H5lfgIODl6E5Zfb1YIwiAxIlHUxRBoChfQR5kjh24,5145
|
|
28
28
|
wolof_translate/utils/download_model.py,sha256=x92KpfVPvNK8Suen1qnOcPtZOlB4kXTfqWgoVuuMUEM,1241
|
|
@@ -43,7 +43,7 @@ wolof_translate/utils/training.py,sha256=5vPVuqHL6_gqLkh4PTxXqW4UvAJBWNWVDDXC9Fk
|
|
|
43
43
|
wolof_translate/utils/trunc_hg_training.py,sha256=mMGrU7Mjr9vYd7eLc8nbFRhRXwSWMKyg35lGf0L6RtQ,6418
|
|
44
44
|
wolof_translate/utils/improvements/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
45
45
|
wolof_translate/utils/improvements/end_marks.py,sha256=scmhMMYguZmrZTPozx1ZovizKrrPfPpMLXbU2-IOdGs,1194
|
|
46
|
-
wolof_translate-0.0.
|
|
47
|
-
wolof_translate-0.0.
|
|
48
|
-
wolof_translate-0.0.
|
|
49
|
-
wolof_translate-0.0.
|
|
46
|
+
wolof_translate-0.0.4.dist-info/METADATA,sha256=Adyexcw1wpc80mDnoeLxFuaWe4Pg0ZAZroqx6eHnSz4,818
|
|
47
|
+
wolof_translate-0.0.4.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
|
|
48
|
+
wolof_translate-0.0.4.dist-info/top_level.txt,sha256=YG-kBnOwUZyQ7SofNvMxNYjzCreH2PVcW2UaEg1-Reg,16
|
|
49
|
+
wolof_translate-0.0.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|