-
Notifications
You must be signed in to change notification settings - Fork 26.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make pipeline
able to load processor
#32514
base: main
Are you sure you want to change the base?
Conversation
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main changes for pipeline code:
- Added loading of
processor
(previously only tokenizer, image_processor and feature_extractor) - Added Pipeline class args to control loading
The main changes
- Refactored test signatures to support
processor
pass - Changed the way how class names are obtained for processors of the testing tiny models in a pipeline. Instead of getting them from JSON file, the MAPPINGs are used.
I highlighted these changes below, please see the comments
cc @ydshieh for initial review
# Check that pipeline class required loading | ||
load_tokenizer = load_tokenizer and pipeline_class._load_tokenizer | ||
load_feature_extractor = load_feature_extractor and pipeline_class._load_feature_extractor | ||
load_image_processor = load_image_processor and pipeline_class._load_image_processor | ||
load_processor = load_processor and pipeline_class._load_processor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For backward compatibility, we can control with Pipeline
class if we need to load specific processors/tokenizers. For example, for zero-shot object detection, we will need to load only the processor
, and do not need to load image_processor
and tokenizer separately. Other legacy pipelines might load only tokenizer and image_processor, even if they have processor class.
# Previously, pipelines support only `tokenizer`, `feature_extractor`, and `image_processor`. | ||
# As we start adding `processor`, we want to avoid loading processor for some pipelines, that don't required it, | ||
# because, for example, use `image_processor` and `tokenizer` separately. | ||
# However, we want to enable it for new pipelines. Moreover, this allow us to granularly control loading components | ||
# and avoid loading tokenizer/image_processor/feature_extractor twice: once as a separate object | ||
# and once in the processor. The following flags a set this way for backward compatibility ans might be overridden | ||
# in specific Pipeline class. | ||
_load_processor = False | ||
_load_image_processor = True | ||
_load_feature_extractor = True | ||
_load_tokenizer = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
granular control for loading, see comment in the code
def is_pipeline_test_to_skip( | ||
self, | ||
pipeline_test_case_name, | ||
config_class, | ||
model_architecture, | ||
tokenizer_name, | ||
image_processor_name, | ||
feature_extractor_name, | ||
processor_name, | ||
): | ||
if tokenizer_name is None: | ||
return True | ||
|
||
# `MT5EncoderOnlyModelTest` is not working well with slow tokenizers (for some models) and we don't want to touch the file | ||
# `src/transformers/data/processors/squad.py` (where this test fails for this model) | ||
if pipeline_test_case_name == "TokenClassificationPipelineTests" and not tokenizer_name.endswith("Fast"): | ||
return True | ||
|
||
return False | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is skipped on the main but was not skipped on this branch, so I added a skip rule here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is skipped on the main
Could you explain a bit what this means?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean, in the job run's result, it shows it is skipped (on the main branch)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this test was not running on the main branch. One of the problem of current tests: we are testing a set of cases, and if one of the cases (e.g. the first one) falls under skipTest(...) the rest test cases are not running. Probably it's better to return case status, and then aggregate statuses in the main test function running on multiple cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A simple example to reproduce, this test will be skipped
import unittest
class TestCases(unittest.TestCase):
def test_multiple_cases(self):
for i in range(10):
self.run_test(i)
def run_test(self, i):
if i == 0:
self.skipTest("Skip this test")
elif i == 5:
raise ValueError("This test failed")
@@ -413,6 +431,7 @@ def run_pipeline_test(self, text_generator, _): | |||
"XGLMForCausalLM", | |||
"GPTNeoXForCausalLM", | |||
"FuyuForCausalLM", | |||
"LlamaForCausalLM", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Llama no longer raises exception for long generation, so I added it to exclude list in the test. This test was skipped on main.
tests/test_pipeline_mixin.py
Outdated
# tokenizers are mapped to tuple, e.g. ("XxxTokenizer", None) | ||
tokenizer_names = TOKENIZER_MAPPING_NAMES.get(model_type, [None]) | ||
if len(tokenizer_names) > 1: | ||
tokenizer_names = [name for name in tokenizer_names if name is not None] | ||
|
||
# image processors are mapped to tuple, e.g. ("XxxImageProcessor", None) | ||
image_processor_names = IMAGE_PROCESSOR_MAPPING_NAMES.get(model_type, [None]) | ||
if len(image_processor_names) > 1: | ||
image_processor_names = [name for name in image_processor_names if name is not None] | ||
|
||
# feature extractors are mapped to instance, e.g. "XxxFeatureExtractor" | ||
feature_extractor_names = FEATURE_EXTRACTOR_MAPPING_NAMES.get(model_type, [None]) | ||
if not isinstance(feature_extractor_names, (list, tuple)): | ||
feature_extractor_names = [feature_extractor_names] | ||
|
||
# processors are mapped to instance, e.g. "XxxProcessor" | ||
processor_names = PROCESSOR_MAPPING_NAMES.get(model_type, [None]) | ||
if not isinstance(processor_names, (list, tuple)): | ||
processor_names = [processor_names] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, tokenizer, image processor and feature extractor class names were taken from JSON file of tiny models, however, there is no *Processor
class there. Now MAPPING lists are used to get class names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not in favor of using MAPPING
for anything except processor
.
The pipeline testing is highly tighten to the tiny model on the Hub, and we should use the actual classes that are used on the Hub repositories (to avoid any surprising).
For processor
, it's OK to use MAPPING
, but we can also change the json file's structure for the long term.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it because in the original tiny-models JSON file we do not have separate lists for image processors and feature extractors, I found they are stored in the same list (even processors are sometimes in this list).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# TODO: We should check if a model file is on the Hub repo. instead. | ||
try: | ||
model = model_architecture.from_pretrained(repo_id, revision=commit) | ||
except Exception: | ||
logger.warning( | ||
f"{self.__class__.__name__}::test_pipeline_{task.replace('-', '_')}_{torch_dtype} is skipped: Could not find or load " | ||
f"the model from `{repo_id}` with `{model_architecture}`." | ||
) | ||
self.skipTest(f"Could not find or load the model from {repo_id} with {model_architecture}.") | ||
|
||
# -------------------- Load tokenizer -------------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order of loading is a bit changed in this test, now the model is loaded first to check if it exists, otherwise, the test is skipped.
processor
in pipelineprocessor
to the pipeline
Hi @qubvel . IIRC, that PR is more about "make pipeline able to accept loading processor", but no currently existing pipeline code is changed to use processor yet (even if we specify to load it), right? |
@ydshieh yes, you are right, the following PR will add processor to the ZeroShotObjectDetection pipeline. I decided to split it to simplify the review a bit. |
processor
to the pipelinepipeline
able to load processor
Hi @qubvel I have a general question. The process class could load also a single tokenizer, image processor or feature extractor (if not all components are on a Hub repository). For example, from transformers import AutoProcessor
AutoProcessor.from_pretrained("bert-base-uncased") will turn out to be a tokenizer instead of an instance of Therefore, in # Instantiate processor if needed
if isinstance(processor, (str, tuple)):
processor = AutoProcessor.from_pretrained(processor, _from_pipeline=task, **hub_kwargs, **model_kwargs) should have some guard to make sure we really get a WDYT? I will share my thoughts regarding the testing part this afternoon, somehow related to the same consideration. |
continuation of my previous commentOnce we start to have the code using
for testingSimilar consideration to the above. We should test 2 cases:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments :-)
def is_pipeline_test_to_skip( | ||
self, | ||
pipeline_test_case_name, | ||
config_class, | ||
model_architecture, | ||
tokenizer_name, | ||
image_processor_name, | ||
feature_extractor_name, | ||
processor_name, | ||
): | ||
if tokenizer_name is None: | ||
return True | ||
|
||
# `MT5EncoderOnlyModelTest` is not working well with slow tokenizers (for some models) and we don't want to touch the file | ||
# `src/transformers/data/processors/squad.py` (where this test fails for this model) | ||
if pipeline_test_case_name == "TokenClassificationPipelineTests" and not tokenizer_name.endswith("Fast"): | ||
return True | ||
|
||
return False | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is skipped on the main
Could you explain a bit what this means?
def is_pipeline_test_to_skip( | ||
self, | ||
pipeline_test_case_name, | ||
config_class, | ||
model_architecture, | ||
tokenizer_name, | ||
image_processor_name, | ||
feature_extractor_name, | ||
processor_name, | ||
): | ||
if tokenizer_name is None: | ||
return True | ||
|
||
# `MT5EncoderOnlyModelTest` is not working well with slow tokenizers (for some models) and we don't want to touch the file | ||
# `src/transformers/data/processors/squad.py` (where this test fails for this model) | ||
if pipeline_test_case_name == "TokenClassificationPipelineTests" and not tokenizer_name.endswith("Fast"): | ||
return True | ||
|
||
return False | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean, in the job run's result, it shows it is skipped (on the main branch)?
tests/test_pipeline_mixin.py
Outdated
# tokenizers are mapped to tuple, e.g. ("XxxTokenizer", None) | ||
tokenizer_names = TOKENIZER_MAPPING_NAMES.get(model_type, [None]) | ||
if len(tokenizer_names) > 1: | ||
tokenizer_names = [name for name in tokenizer_names if name is not None] | ||
|
||
# image processors are mapped to tuple, e.g. ("XxxImageProcessor", None) | ||
image_processor_names = IMAGE_PROCESSOR_MAPPING_NAMES.get(model_type, [None]) | ||
if len(image_processor_names) > 1: | ||
image_processor_names = [name for name in image_processor_names if name is not None] | ||
|
||
# feature extractors are mapped to instance, e.g. "XxxFeatureExtractor" | ||
feature_extractor_names = FEATURE_EXTRACTOR_MAPPING_NAMES.get(model_type, [None]) | ||
if not isinstance(feature_extractor_names, (list, tuple)): | ||
feature_extractor_names = [feature_extractor_names] | ||
|
||
# processors are mapped to instance, e.g. "XxxProcessor" | ||
processor_names = PROCESSOR_MAPPING_NAMES.get(model_type, [None]) | ||
if not isinstance(processor_names, (list, tuple)): | ||
processor_names = [processor_names] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not in favor of using MAPPING
for anything except processor
.
The pipeline testing is highly tighten to the tiny model on the Hub, and we should use the actual classes that are used on the Hub repositories (to avoid any surprising).
For processor
, it's OK to use MAPPING
, but we can also change the json file's structure for the long term.
tests/test_pipeline_mixin.py
Outdated
) | ||
self.skipTest(f"Could not load the processor from {repo_id} with {processor_name}.") | ||
self.skipTest(f"Could not load the tokenizer from {repo_id} with {tokenizer_name}.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should not skip here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 8e3eb2b
f"{self.__class__.__name__}::test_pipeline_{task.replace('-', '_')}_{torch_dtype} is skipped: " | ||
f"Could not load the {key} from `{repo_id}` with `{name}`." | ||
) | ||
self.skipTest(f"Could not load the {key} from {repo_id} with {name}.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should not skip here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is designed the same way on the main
now, according to the comment some instances might not be loaded due to optional dependencies
transformers/tests/test_pipeline_mixin.py
Lines 256 to 267 in af638c4
processor = None | |
if processor_name is not None: | |
processor_class = getattr(transformers_module, processor_name) | |
# If the required packages (like `Pillow` or `torchaudio`) are not installed, this will fail. | |
try: | |
processor = processor_class.from_pretrained(repo_id, revision=commit) | |
except Exception: | |
logger.warning( | |
f"{self.__class__.__name__}::test_pipeline_{task.replace('-', '_')}_{torch_dtype} is skipped: Could not load the " | |
f"processor from `{repo_id}` with `{processor_name}`." | |
) | |
self.skipTest(f"Could not load the processor from {repo_id} with {processor_name}.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently on main
, this run_pipeline_test
only take a single tokenizer_name
and a single processor_name
, and try to load them. So if a specified processor_name
could not be loaded, we can skip it.
However, here in this PR, we try to collect the processors and put them into the dictionary processors
. So logically it's better not to skip the test during this collection.
(But in practice, this may not produce any difference)
Yes, you are correct. It is because I treated them (image processor / feature exactor) equally, and later I decided to make I see better now why you need this change. Could you, at least for now, simply use the information from the summary file |
@ydshieh Thanks for reviewing, I addressed the comments, please have a look!
This is what I tried to achieve with granular control for each pipeline class by adding |
@ydshieh could you please have a look once again? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few nits but overall good!
Let me know if my comments are clear 🙏
f"{self.__class__.__name__}::test_pipeline_{task.replace('-', '_')}_{torch_dtype} is skipped: " | ||
f"Could not load the {key} from `{repo_id}` with `{name}`." | ||
) | ||
self.skipTest(f"Could not load the {key} from {repo_id} with {name}.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently on main
, this run_pipeline_test
only take a single tokenizer_name
and a single processor_name
, and try to load them. So if a specified processor_name
could not be loaded, we can skip it.
However, here in this PR, we try to collect the processors and put them into the dictionary processors
. So logically it's better not to skip the test during this collection.
(But in practice, this may not produce any difference)
@@ -236,67 +324,82 @@ def run_pipeline_test( | |||
A subclass of `PretrainedModel` or `PretrainedModel`. | |||
tokenizer_name (`str`): | |||
The name of a subclass of `PreTrainedTokenizerFast` or `PreTrainedTokenizer`. | |||
processor_name (`str`): | |||
image_processor_or_feature_extractor_name (`str`): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like not the correct docstring?
# Adding `None` (if empty) so we can generate tests | ||
tokenizer_names = [None] if len(tokenizer_names) == 0 else tokenizer_names | ||
processor_names = [None] if len(processor_names) == 0 else processor_names | ||
if model_arch_name in tiny_model_summary and "sha" in tiny_model_summary[model_arch_name]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the first condition is not necessary at this point.
tokenizer_names = tiny_model_summary[model_arch_name]["tokenizer_classes"] | ||
|
||
# Sort image processors and feature extractors from tiny-models json file | ||
image_processor_names = [] | ||
feature_extractor_names = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we don't add None
later in this method (while it's empty), then in run_model_pipeline_tests
,
test_cases = [ ....]
will be empty if tokenizer_names
is empty or image_processor_names
is empty or ... (etc).
But we should allow the test case to exist even if only one of them present: for example, a text only model or a vision only model.
Hi @yonigozlan, just a lack of time 🙂 I will address comments this week, and hopefully it can be merged then! |
cc @Rocketknight1 as well (please always ping Matt for pipelines 🤗) |
What does this PR do?
Add
processor
to thepipeline
and refactor tests a bit to support it. Related toFixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.