wolof-translate 0.0.2__py3-none-any.whl → 0.0.3__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 +119 -45
- {wolof_translate-0.0.2.dist-info → wolof_translate-0.0.3.dist-info}/METADATA +1 -1
- {wolof_translate-0.0.2.dist-info → wolof_translate-0.0.3.dist-info}/RECORD +5 -5
- {wolof_translate-0.0.2.dist-info → wolof_translate-0.0.3.dist-info}/WHEEL +0 -0
- {wolof_translate-0.0.2.dist-info → wolof_translate-0.0.3.dist-info}/top_level.txt +0 -0
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import torch
|
|
2
1
|
import numpy as np
|
|
3
|
-
from typing import Optional, List, Iterator
|
|
2
|
+
from typing import Optional, List, Iterator, Union
|
|
4
3
|
from torch.utils.data import Sampler
|
|
5
4
|
from math import ceil
|
|
5
|
+
from tqdm import tqdm
|
|
6
|
+
import time
|
|
6
7
|
|
|
7
8
|
class SequenceLengthBatchSampler(Sampler[List[int]]):
|
|
8
9
|
def __init__(
|
|
@@ -18,62 +19,135 @@ class SequenceLengthBatchSampler(Sampler[List[int]]):
|
|
|
18
19
|
self.boundaries = boundaries
|
|
19
20
|
self.batch_sizes = batch_sizes
|
|
20
21
|
self.drop_unique = drop_unique
|
|
21
|
-
self.data_info = {}
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
data = dataset[i]
|
|
26
|
-
if input_key is None or label_key is None:
|
|
27
|
-
length = max(len(data[0]), len(data[2]))
|
|
28
|
-
else:
|
|
29
|
-
length = max(len(data[input_key]), len(data[label_key]))
|
|
30
|
-
self.data_info[i] = {"index": i, "length": length}
|
|
23
|
+
start_time = time.time()
|
|
24
|
+
tqdm.write("Computing sequence lengths...")
|
|
31
25
|
|
|
32
|
-
|
|
26
|
+
# Compute lengths with tqdm progress bar
|
|
27
|
+
self.lengths = np.array([
|
|
28
|
+
max(len(data[0]), len(data[2])) if input_key is None or label_key is None
|
|
29
|
+
else max(len(data[input_key]), len(data[label_key]))
|
|
30
|
+
for data in tqdm(dataset, desc="Lengths", unit="seq")
|
|
31
|
+
])
|
|
33
32
|
|
|
34
|
-
|
|
33
|
+
tqdm.write(f"Sequence lengths computed in {time.time() - start_time:.2f} seconds.")
|
|
34
|
+
|
|
35
|
+
start_time = time.time()
|
|
36
|
+
tqdm.write("Assigning buckets...")
|
|
37
|
+
|
|
38
|
+
# Assign bucket ids using digitize (vectorized)
|
|
39
|
+
self.bucket_ids = np.digitize(self.lengths, bins=self.boundaries, right=True)
|
|
40
|
+
|
|
41
|
+
# Create buckets of indices
|
|
42
|
+
self.buckets = [np.where(self.bucket_ids == i)[0] for i in range(len(boundaries) + 1)]
|
|
43
|
+
|
|
44
|
+
tqdm.write(f"Buckets assigned in {time.time() - start_time:.2f} seconds.")
|
|
45
|
+
|
|
46
|
+
start_time = time.time()
|
|
47
|
+
tqdm.write("Preparing batches...")
|
|
48
|
+
|
|
49
|
+
# Prepare batches from buckets
|
|
35
50
|
self.batches = []
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
for boundary in self.boundaries:
|
|
40
|
-
batch = [i for i in sorted_indices if prev_boundary < self.data_info[i]["length"] <= boundary]
|
|
41
|
-
self.batches.append(batch)
|
|
42
|
-
sorted_indices = [i for i in sorted_indices if i not in batch]
|
|
43
|
-
prev_boundary = boundary
|
|
44
|
-
|
|
45
|
-
# Remaining sequences > last boundary
|
|
46
|
-
self.batches.append(sorted_indices)
|
|
47
|
-
|
|
48
|
-
total_batches = 0
|
|
49
|
-
for batch, batch_size in zip(self.batches, self.batch_sizes):
|
|
50
|
-
n_full_batches = len(batch) // batch_size
|
|
51
|
-
leftover = len(batch) % batch_size
|
|
52
|
-
total_batches += n_full_batches
|
|
53
|
-
if leftover > 0 and (leftover != 1 or not self.drop_unique):
|
|
54
|
-
total_batches += 1
|
|
55
|
-
self.length = total_batches
|
|
51
|
+
for bucket, batch_size in zip(self.buckets, self.batch_sizes):
|
|
52
|
+
bucket = bucket.copy()
|
|
53
|
+
np.random.shuffle(bucket)
|
|
56
54
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
num_batches = len(batch_indices) // batch_size
|
|
55
|
+
n_full_batches = len(bucket) // batch_size
|
|
56
|
+
leftover = len(bucket) % batch_size
|
|
60
57
|
|
|
61
|
-
for i in range(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
yield [self.data_info[idx]["index"] for idx in current_bucket]
|
|
58
|
+
for i in range(n_full_batches):
|
|
59
|
+
batch = bucket[i * batch_size : (i + 1) * batch_size].tolist()
|
|
60
|
+
self.batches.append(batch)
|
|
65
61
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
62
|
+
if leftover > 0 and (leftover != 1 or not self.drop_unique):
|
|
63
|
+
batch = bucket[-leftover:].tolist()
|
|
64
|
+
self.batches.append(batch)
|
|
65
|
+
|
|
66
|
+
self.length = len(self.batches)
|
|
67
|
+
tqdm.write(f"Batches prepared in {time.time() - start_time:.2f} seconds.")
|
|
68
|
+
|
|
69
|
+
def __iter__(self) -> Iterator[List[int]]:
|
|
70
|
+
# Shuffle all batches globally to add randomness between buckets
|
|
71
|
+
np.random.shuffle(self.batches)
|
|
72
|
+
for batch in self.batches:
|
|
73
|
+
yield batch
|
|
71
74
|
|
|
72
75
|
def __len__(self) -> int:
|
|
73
76
|
return self.length
|
|
74
77
|
|
|
75
78
|
|
|
76
79
|
|
|
80
|
+
|
|
81
|
+
# class SequenceLengthBatchSampler(Sampler[List[int]]):
|
|
82
|
+
# def __init__(
|
|
83
|
+
# self,
|
|
84
|
+
# dataset,
|
|
85
|
+
# boundaries: List[int],
|
|
86
|
+
# batch_sizes: List[int],
|
|
87
|
+
# input_key: Optional[int] = None,
|
|
88
|
+
# label_key: Optional[int] = None,
|
|
89
|
+
# drop_unique: bool = True,
|
|
90
|
+
# ):
|
|
91
|
+
# self.dataset = dataset
|
|
92
|
+
# self.boundaries = boundaries
|
|
93
|
+
# self.batch_sizes = batch_sizes
|
|
94
|
+
# self.drop_unique = drop_unique
|
|
95
|
+
# self.data_info = {}
|
|
96
|
+
|
|
97
|
+
# # Extract lengths
|
|
98
|
+
# for i in range(len(dataset)):
|
|
99
|
+
# data = dataset[i]
|
|
100
|
+
# if input_key is None or label_key is None:
|
|
101
|
+
# length = max(len(data[0]), len(data[2]))
|
|
102
|
+
# else:
|
|
103
|
+
# length = max(len(data[input_key]), len(data[label_key]))
|
|
104
|
+
# self.data_info[i] = {"index": i, "length": length}
|
|
105
|
+
|
|
106
|
+
# self.calculate_length()
|
|
107
|
+
|
|
108
|
+
# def calculate_length(self):
|
|
109
|
+
# self.batches = []
|
|
110
|
+
# sorted_indices = sorted(self.data_info.keys(), key=lambda i: self.data_info[i]["length"])
|
|
111
|
+
|
|
112
|
+
# prev_boundary = 0
|
|
113
|
+
# for boundary in self.boundaries:
|
|
114
|
+
# batch = [i for i in sorted_indices if prev_boundary < self.data_info[i]["length"] <= boundary]
|
|
115
|
+
# self.batches.append(batch)
|
|
116
|
+
# sorted_indices = [i for i in sorted_indices if i not in batch]
|
|
117
|
+
# prev_boundary = boundary
|
|
118
|
+
|
|
119
|
+
# # Remaining sequences > last boundary
|
|
120
|
+
# self.batches.append(sorted_indices)
|
|
121
|
+
|
|
122
|
+
# total_batches = 0
|
|
123
|
+
# for batch, batch_size in zip(self.batches, self.batch_sizes):
|
|
124
|
+
# n_full_batches = len(batch) // batch_size
|
|
125
|
+
# leftover = len(batch) % batch_size
|
|
126
|
+
# total_batches += n_full_batches
|
|
127
|
+
# if leftover > 0 and (leftover != 1 or not self.drop_unique):
|
|
128
|
+
# total_batches += 1
|
|
129
|
+
# self.length = total_batches
|
|
130
|
+
|
|
131
|
+
# def __iter__(self) -> Iterator[List[int]]:
|
|
132
|
+
# for batch_indices, batch_size in zip(self.batches, self.batch_sizes):
|
|
133
|
+
# num_batches = len(batch_indices) // batch_size
|
|
134
|
+
|
|
135
|
+
# for i in range(num_batches):
|
|
136
|
+
# current_bucket = batch_indices[i * batch_size: (i + 1) * batch_size]
|
|
137
|
+
# np.random.shuffle(current_bucket)
|
|
138
|
+
# yield [self.data_info[idx]["index"] for idx in current_bucket]
|
|
139
|
+
|
|
140
|
+
# remaining = len(batch_indices) % batch_size
|
|
141
|
+
# if remaining > 0 and (remaining != 1 or not self.drop_unique):
|
|
142
|
+
# current_bucket = batch_indices[-remaining:]
|
|
143
|
+
# np.random.shuffle(current_bucket)
|
|
144
|
+
# yield [self.data_info[idx]["index"] for idx in current_bucket]
|
|
145
|
+
|
|
146
|
+
# def __len__(self) -> int:
|
|
147
|
+
# return self.length
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
77
151
|
class BucketSampler(Sampler):
|
|
78
152
|
def __init__(self, dataset, batch_size, sort_key=lambda x, index_1, index_2: max(len(x[index_1]), len(x[index_2])), input_key: Union[str, int] = 0, label_key: Union[str, int] = 1):
|
|
79
153
|
self.dataset = dataset
|
|
@@ -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=JctNzZI1yU1FjZUKY-zNiRMXS3JpRiXOOSKdObRdPbg,8511
|
|
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.3.dist-info/METADATA,sha256=IztySbGGsGKbrtFqCcLpppA07bO8kt2_dxdd4hdMVOI,818
|
|
47
|
+
wolof_translate-0.0.3.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
|
|
48
|
+
wolof_translate-0.0.3.dist-info/top_level.txt,sha256=YG-kBnOwUZyQ7SofNvMxNYjzCreH2PVcW2UaEg1-Reg,16
|
|
49
|
+
wolof_translate-0.0.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|