Adding support for raw python generator in addition to Dataset for pipelines (#14352)
* Adding support for raw python `generator` in addition to `Dataset` The main goal is to ease the create of streaming data to the pipe. `Dataset` is more involved and pytorch specific. This PR, provides a way to use a python iterator too. This enabled #14250 but can be proposed as a standalone PR. ```python from transformers import pipeline def read_data(filename): with open(filename, 'r') as f: for line in f: yield f pipe = pipeline("text-classification") for classified in pipe(read_data("large_file.txt")): print("Success ! ", classified) ``` The main caveat of this, is the interaction with `DataLoader` with `num_workers>1`. When you have multiple workers, each receive a copy of the generator (like `IterableDataset`). That means the naive Iterator will fail since all workers iterate on all items of the generator. There are ways to do clever "skipping", but it could be bad still because all workers still do have to pass through all items of the generator (they just ignore items they don't handle), depending on the case it might be bad. Using `num_workers=1` is the simplest fix and if the cost of loading your data is small enough should be good enough. In the above example trying to do smart tricks to skip some lines is unlikely to be a net positive for instance. If there are better ways to do "jumps" on some data, then using `Dataset` is more advised (since then differents workers can just jump themselves). * Adding iterator support for `tf` too.
This commit is contained in:
@@ -34,7 +34,7 @@ from transformers import (
|
||||
)
|
||||
from transformers.pipelines import get_task
|
||||
from transformers.pipelines.base import _pad
|
||||
from transformers.testing_utils import is_pipeline_test, require_torch
|
||||
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -286,6 +286,42 @@ class CommonPipelineTest(unittest.TestCase):
|
||||
# Wrong framework
|
||||
get_task("espnet/siddhana_slurp_entity_asr_train_asr_conformer_raw_en_word_valid.acc.ave_10best")
|
||||
|
||||
@require_torch
|
||||
def test_iterator_data(self):
|
||||
def data(n: int):
|
||||
for _ in range(n):
|
||||
yield "This is a test"
|
||||
|
||||
pipe = pipeline(model="Narsil/tiny-distilbert-sequence-classification")
|
||||
|
||||
results = []
|
||||
for out in pipe(data(10)):
|
||||
self.assertEqual(nested_simplify(out), {"label": "LABEL_1", "score": 0.502})
|
||||
results.append(out)
|
||||
self.assertEqual(len(results), 10)
|
||||
|
||||
# When using multiple workers on streamable data it should still work
|
||||
# This will force using `num_workers=1` with a warning for now.
|
||||
results = []
|
||||
for out in pipe(data(10), num_workers=2):
|
||||
self.assertEqual(nested_simplify(out), {"label": "LABEL_1", "score": 0.502})
|
||||
results.append(out)
|
||||
self.assertEqual(len(results), 10)
|
||||
|
||||
@require_tf
|
||||
def test_iterator_data_tf(self):
|
||||
def data(n: int):
|
||||
for _ in range(n):
|
||||
yield "This is a test"
|
||||
|
||||
pipe = pipeline(model="Narsil/tiny-distilbert-sequence-classification", framework="tf")
|
||||
out = pipe("This is a test")
|
||||
results = []
|
||||
for out in pipe(data(10)):
|
||||
self.assertEqual(nested_simplify(out), {"label": "LABEL_1", "score": 0.502})
|
||||
results.append(out)
|
||||
self.assertEqual(len(results), 10)
|
||||
|
||||
|
||||
@is_pipeline_test
|
||||
class PipelinePadTest(unittest.TestCase):
|
||||
|
||||
Reference in New Issue
Block a user