Refills the internal buffer. - max_items_to_add: an amount less than or equal to the number of items to add Returns: the number of items actually added. The default implementation of this simply extends to src.buffer, which is initialized as a list in __in
(self, max_items_to_add=float("inf"))
| 194 | return None |
| 195 | |
| 196 | def _fill_buffer(self, max_items_to_add=float("inf")) -> int: |
| 197 | """ |
| 198 | Refills the internal buffer. |
| 199 | |
| 200 | - max_items_to_add: an amount less than or equal to the number of items to add |
| 201 | |
| 202 | Returns: the number of items actually added. |
| 203 | |
| 204 | The default implementation of this simply extends to src.buffer, which is |
| 205 | initialized as a list in __init__. Subclasses which want to use a different data |
| 206 | structure for internal buffering should override this method and also add |
| 207 | code in __init__ to initialize src.buffer appropriately. |
| 208 | |
| 209 | Any implementation of this MUST never place more than self.buffer_size items |
| 210 | in the internal buffer. |
| 211 | """ |
| 212 | items_added = 0 |
| 213 | # NOTE: this should be >=, kept as is to match model training code |
| 214 | # TODO: change if training a new model |
| 215 | while (self.buffer_size - len(self.buffer)) > self.src_batch_size: |
| 216 | try: |
| 217 | # if pulling another batch would fetch more than the requested max, stop |
| 218 | if max_items_to_add < float("inf"): |
| 219 | if (items_added + self.src_batch_size) > max_items_to_add: |
| 220 | # print("Not adding, because of max_items_to_fetch") |
| 221 | break |
| 222 | incoming_batch = next(self.src_iterator) |
| 223 | assert ( |
| 224 | len(incoming_batch) <= self.src_batch_size |
| 225 | ), f"expected {len(incoming_batch)=} <= {self.src_batch_size=}" |
| 226 | for item in incoming_batch: |
| 227 | if len(item["input_ids"]) > 0: # ignore empty sequences |
| 228 | self.buffer.append(item["input_ids"]) |
| 229 | items_added += 1 |
| 230 | self._seqs_consumed += 1 |
| 231 | except StopIteration: |
| 232 | break |
| 233 | return items_added |
| 234 | |
| 235 | def _generate_batches(self): |
| 236 | """ |