Skip to content
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

Merged
merged 37 commits into from
Oct 9, 2024

Conversation

qubvel
Copy link
Member

@qubvel qubvel commented Aug 7, 2024

What does this PR do?

Add processor to the pipeline and refactor tests a bit to support it. Related to

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

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.

@qubvel qubvel marked this pull request as draft August 7, 2024 21:02
@HuggingFaceDocBuilderDev

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.

Copy link
Member Author

@qubvel qubvel left a 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

Comment on lines +935 to +952
# 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
Copy link
Member Author

@qubvel qubvel Aug 9, 2024

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Piggy-backing on the comment above, this is likely something we want to highlight very clearly in each pipeline's documentation

Comment on lines 817 to 829
# 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
Copy link
Member Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense and I appreciate us being explicit. This repeats what is said above, but this should be extremely clear in the pipelines documentation if possible

Comment on lines +1065 to +1084
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

Copy link
Member Author

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

Copy link
Collaborator

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?

Copy link
Collaborator

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)?

Copy link
Member Author

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.

Copy link
Member Author

@qubvel qubvel Aug 14, 2024

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",
Copy link
Member Author

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.

Comment on lines 162 to 204
# 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]

Copy link
Member Author

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.

Copy link
Collaborator

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.

Copy link
Member Author

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).

Copy link
Member Author

@qubvel qubvel Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified in b6d3bbc

according to @ydshieh suggestion to use mapping only for Processor

Comment on lines +325 to +378
# 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 --------------------

Copy link
Member Author

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.

@qubvel qubvel changed the title Tests refactoring for processor in pipeline Add processor to the pipeline Aug 9, 2024
@qubvel qubvel marked this pull request as ready for review August 9, 2024 13:00
@ydshieh ydshieh self-requested a review August 13, 2024 12:41
@ydshieh ydshieh self-assigned this Aug 13, 2024
@ydshieh
Copy link
Collaborator

ydshieh commented Aug 13, 2024

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?

@qubvel
Copy link
Member Author

qubvel commented Aug 13, 2024

@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.

@qubvel qubvel changed the title Add processor to the pipeline Make pipeline able to load processor Aug 13, 2024
@ydshieh
Copy link
Collaborator

ydshieh commented Aug 14, 2024

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 ProcessorMixin.

Therefore, in src/transformers/pipelines/__init__.py, this part

       # 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 ProcessorMixin instance.

WDYT?

I will share my thoughts regarding the testing part this afternoon, somehow related to the same consideration.

@ydshieh
Copy link
Collaborator

ydshieh commented Aug 14, 2024

continuation of my previous comment

Once we start to have the code using processor, and if we support it for some currently existing pipelines, it should support:

  • either not use the processor at all (i.e. currently implementation): for backward compatibility
  • or completely use the processor only (but need to verify the results are the same)

for testing

Similar consideration to the above. We should test 2 cases:

  • only load a list of tokenizer, image processor and/or feature extractor: test case 1
  • only load a single processor: test case 2

Copy link
Collaborator

@ydshieh ydshieh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments :-)

Comment on lines +1065 to +1084
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

Copy link
Collaborator

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?

Comment on lines +1065 to +1084
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

Copy link
Collaborator

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)?

Comment on lines 162 to 204
# 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]

Copy link
Collaborator

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.

)
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}.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not skip here.

Copy link
Member Author

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}.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not skip here

Copy link
Member Author

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

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}.")

Copy link
Collaborator

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)

tests/test_pipeline_mixin.py Show resolved Hide resolved
tests/test_pipeline_mixin.py Show resolved Hide resolved
@ydshieh
Copy link
Collaborator

ydshieh commented Aug 14, 2024

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).

Yes, you are correct. It is because I treated them (image processor / feature exactor) equally, and later I decided to make processor join into this pipeline testing

I see better now why you need this change. Could you, at least for now, simply use the information from the summary file processor_classes field, and by looking at training part to decide to assign them to one of the tokenizer_names, feature_extractor_names, processor_names etc.?
``

@qubvel
Copy link
Member Author

qubvel commented Aug 22, 2024

@ydshieh Thanks for reviewing, I addressed the comments, please have a look!

Once we start to have the code using processor, and if we support it for some currently existing pipelines, it should support:

  • either not use the processor at all (i.e. currently implementation): for backward compatibility
  • or completely use the processor only (but need to verify the results are the same)

This is what I tried to achieve with granular control for each pipeline class by adding _load_* attributes
#32514 (comment)

@qubvel
Copy link
Member Author

qubvel commented Aug 27, 2024

@ydshieh could you please have a look once again?

Copy link
Collaborator

@ydshieh ydshieh left a 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}.")
Copy link
Collaborator

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)

tests/test_pipeline_mixin.py Outdated Show resolved Hide resolved
tests/test_pipeline_mixin.py Show resolved Hide resolved
# 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]:
Copy link
Collaborator

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise we can get an error while doing tiny_model_summary[model_arch_name]

tests/test_pipeline_mixin.py Show resolved Hide resolved
@yonigozlan
Copy link
Member

Hi @qubvel @ydshieh ! Just wondering if anything is blocking this PR from getting merged, as it is needed for the image-text-to-text pipeline. Thanks!

@qubvel
Copy link
Member Author

qubvel commented Sep 17, 2024

Hi @yonigozlan, just a lack of time 🙂 I will address comments this week, and hopefully it can be merged then!

@LysandreJik
Copy link
Member

cc @Rocketknight1 as well (please always ping Matt for pipelines 🤗)

@HuggingFaceDocBuilderDev

Hey! 🤗 Thanks for your contribution to the transformers library!

Before merging this pull request, slow tests CI should be triggered. To enable this:

  • Add the run-slow label to the PR
  • When your PR is ready for merge and all reviewers' comments have been addressed, push an empty commit with the command [run-slow] followed by a comma separated list of all the models to be tested, i.e. [run_slow] model_to_test_1, model_to_test_2
    • If the pull request affects a lot of models, put at most 10 models in the commit message
  • A transformers maintainer will then approve the workflow to start the tests

(For maintainers) The documentation for slow tests CI on PRs is here.

@qubvel qubvel force-pushed the add-processor-to-pipeline branch from 9b88faa to e06053d Compare October 1, 2024 12:48
@qubvel
Copy link
Member Author

qubvel commented Oct 1, 2024

@Rocketknight1 can you please review the PR, if that aligns with plans for pipeline refactoring/standardization

@Rocketknight1
Copy link
Member

I'm sorry, I missed that ping somehow! Reviewing now.

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just finished the review, with a few comments.

Overall summary for core maintainers:

  • This is a nice PR! It's clean, and although it seems very large, almost all of the changes are boilerplate adding kwargs and fixing spelling mistakes in a lot of files.
  • The actual changes you need to look at are __init__.py, base.py and test_pipeline_mixin.py
  • I made some comments, but they're nits and shouldn't affect any of the PR logic.

cc @LysandreJik for core maintainer review!

Comment on lines 938 to 939
or feature_extractor is not None
or isinstance(feature_extractor, str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this conditional is redundant - if feature_extractor is a str then it will always be not None as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in d553dab

src/transformers/pipelines/__init__.py Outdated Show resolved Hide resolved
src/transformers/pipelines/__init__.py Outdated Show resolved Hide resolved
Comment on lines +274 to +285
test_cases = [
{
"tokenizer_name": tokenizer_name,
"image_processor_name": image_processor_name,
"feature_extractor_name": feature_extractor_name,
"processor_name": processor_name,
}
for tokenizer_name in tokenizer_names
for image_processor_name in image_processor_names
for feature_extractor_name in feature_extractor_names
for processor_name in processor_names
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for other reviewers: When a model doesn't support one of these preprocessor types, the list of names will be [None], not an empty list. When I first looked at this quadruple-loop I was worried that test_cases would be empty if any of the four name lists was empty, but that should never occur. For safety, we might want to add a check for that, though, since it depends on behaviour in a different function that might be modified later!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, let's have this check closer to the loop to ensure we always have something to test
6ff4e68

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, you can also remove

            if not isinstance(processor_names, (list, tuple)):
                processor_names = [processor_names]

(which was there for the same reason)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it there to make sure we do not have str processor_names

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! I'm mostly concerned about having good and clear documentation and removing unnecessary warnings.

Pipelines are the very first transformers object many users interact with -> ensuring that they're aware of the info they need, without being hammered by warnigns should be an absolute goal of ours.

Thanks for working on this important change!

src/transformers/pipelines/__init__.py Outdated Show resolved Hide resolved
Comment on lines +651 to +668
The image processor that will be used by the pipeline to preprocess images for the model. This can be a
model identifier or an actual image processor inheriting from [`BaseImageProcessor`].

Image processors are used for Vision models and multi-modal models that require image inputs. Multi-modal
models will also require a tokenizer to be passed.

If not provided, the default image processor for the given `model` will be loaded (if it is a string). If
`model` is not specified or not a string, then the default image processor for `config` is loaded (if it is
a string).
processor (`str` or [`ProcessorMixin`], *optional*):
The processor that will be used by the pipeline to preprocess data for the model. This can be a model
identifier or an actual processor inheriting from [`ProcessorMixin`].

Processors are used for multi-modal models that require multi-modal inputs, for example, a model that
requires both text and image inputs.

If not provided, the default processor for the given `model` will be loaded (if it is a string). If `model`
is not specified or not a string, then the default processor for `config` is loaded (if it is a string).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are now a few overlapping inputs:

  • tokenizer
  • feature extractor
  • image processor
  • processor

I believe it would be nice to highlight somewhere visible (like in the documentation above) what attribute is necessary for what: at no point should a user specify all four of them, for example.

Copy link
Member Author

@qubvel qubvel Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a separate Note section to highlight that we should not provide all types of processors at once
e712717 and refer to a specific pipeline in case one would like to provide them explicitly.

For each specific pipeline we have only required processors args in docs section configured with docs decorator. e.g. here

build_pipeline_init_args(has_image_processor=True),

Copy link
Member Author

@qubvel qubvel Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, updated pipeline doc to more relevant one in 6996695

Comment on lines +935 to +952
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Piggy-backing on the comment above, this is likely something we want to highlight very clearly in each pipeline's documentation

Comment on lines 1122 to 1126
warnings.warn(
f"Processor will be not loaded, because {processor} is not an instance of `ProcessorMixin`. "
f"Got type `{type(processor)}` instead.",
UserWarning,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With transformers already having too many warnings, I'd be cautious about the ones we add.

What purpose does this warning serve? Is it sufficiently actionable? Does it concern users (the ones that will see it), or repo owners/creators that have not configured their processors/feature extractors correctly (that will likely not see this warning)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the questions!
There was a discussion here. The main idea is that AutoProcessor can load not only processors but also basic processing classes, like tokenizer.

Indeed, a misconfiguration in the model-pipeline-processor setup could trigger this and raising a warning + dropping the processor might be sufficient only when the processor isn't needed in the pipeline at all.

However, with granular control, such a case shouldn't occur. Therefore, it seems more appropriate to replace the warning with an error to clearly indicate a misconfiguration. Otherwise, the error will happen later with a less clear message because the processor is None.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to error raise here bcce4dc

src/transformers/pipelines/base.py Outdated Show resolved Hide resolved
@@ -805,6 +816,18 @@ class Pipeline(_ScikitCompat, PushToHubMixin):
constructor argument. If set to `True`, the output will be stored in the pickle format.
"""

# 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get this comment 😁

Copy link
Member Author

@qubvel qubvel Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to make it clearer in 9793e01

Comment on lines 817 to 829
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense and I appreciate us being explicit. This repeats what is said above, but this should be extremely clear in the pipelines documentation if possible

@qubvel
Copy link
Member Author

qubvel commented Oct 7, 2024

@LysandreJik thanks for reviewing, I addressed the comment: made clearer docs and removed the warning. Please have a look whenever you have bandwidth.

P.S. The test failure looks unrelated.

@qubvel qubvel requested a review from LysandreJik October 7, 2024 15:14
Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok great, thanks for the changes @qubvel!

@qubvel qubvel merged commit 48461c0 into huggingface:main Oct 9, 2024
25 checks passed
@qubvel qubvel mentioned this pull request Oct 9, 2024
NielsRogge pushed a commit to NielsRogge/transformers that referenced this pull request Oct 21, 2024
* Refactor get_test_pipeline

* Fixup

* Fixing tests

* Add processor loading in tests

* Restructure processors loading

* Add processor to the pipeline

* Move model loading on tom of the test

* Update `get_test_pipeline`

* Fixup

* Add class-based flags for loading processors

* Change `is_pipeline_test_to_skip` signature

* Skip t5 failing test for slow tokenizer

* Fixup

* Fix copies for T5

* Fix typo

* Add try/except for tokenizer loading (kosmos-2 case)

* Fixup

* Llama not fails for long generation

* Revert processor pass in text-generation test

* Fix docs

* Switch back to json file for image processors and feature extractors

* Add processor type check

* Remove except for tokenizers

* Fix docstring

* Fix empty lists for tests

* Fixup

* Fix load check

* Ensure we have non-empty test cases

* Update src/transformers/pipelines/__init__.py

Co-authored-by: Lysandre Debut <hi@lysand.re>

* Update src/transformers/pipelines/base.py

Co-authored-by: Lysandre Debut <hi@lysand.re>

* Rework comment

* Better docs, add note about pipeline components

* Change warning to error raise

* Fixup

* Refine pipeline docs

---------

Co-authored-by: Lysandre Debut <hi@lysand.re>
BernardZach pushed a commit to BernardZach/transformers that referenced this pull request Dec 5, 2024
* Refactor get_test_pipeline

* Fixup

* Fixing tests

* Add processor loading in tests

* Restructure processors loading

* Add processor to the pipeline

* Move model loading on tom of the test

* Update `get_test_pipeline`

* Fixup

* Add class-based flags for loading processors

* Change `is_pipeline_test_to_skip` signature

* Skip t5 failing test for slow tokenizer

* Fixup

* Fix copies for T5

* Fix typo

* Add try/except for tokenizer loading (kosmos-2 case)

* Fixup

* Llama not fails for long generation

* Revert processor pass in text-generation test

* Fix docs

* Switch back to json file for image processors and feature extractors

* Add processor type check

* Remove except for tokenizers

* Fix docstring

* Fix empty lists for tests

* Fixup

* Fix load check

* Ensure we have non-empty test cases

* Update src/transformers/pipelines/__init__.py

Co-authored-by: Lysandre Debut <hi@lysand.re>

* Update src/transformers/pipelines/base.py

Co-authored-by: Lysandre Debut <hi@lysand.re>

* Rework comment

* Better docs, add note about pipeline components

* Change warning to error raise

* Fixup

* Refine pipeline docs

---------

Co-authored-by: Lysandre Debut <hi@lysand.re>
BernardZach pushed a commit to innovationcore/transformers that referenced this pull request Dec 6, 2024
* Refactor get_test_pipeline

* Fixup

* Fixing tests

* Add processor loading in tests

* Restructure processors loading

* Add processor to the pipeline

* Move model loading on tom of the test

* Update `get_test_pipeline`

* Fixup

* Add class-based flags for loading processors

* Change `is_pipeline_test_to_skip` signature

* Skip t5 failing test for slow tokenizer

* Fixup

* Fix copies for T5

* Fix typo

* Add try/except for tokenizer loading (kosmos-2 case)

* Fixup

* Llama not fails for long generation

* Revert processor pass in text-generation test

* Fix docs

* Switch back to json file for image processors and feature extractors

* Add processor type check

* Remove except for tokenizers

* Fix docstring

* Fix empty lists for tests

* Fixup

* Fix load check

* Ensure we have non-empty test cases

* Update src/transformers/pipelines/__init__.py

Co-authored-by: Lysandre Debut <hi@lysand.re>

* Update src/transformers/pipelines/base.py

Co-authored-by: Lysandre Debut <hi@lysand.re>

* Rework comment

* Better docs, add note about pipeline components

* Change warning to error raise

* Fixup

* Refine pipeline docs

---------

Co-authored-by: Lysandre Debut <hi@lysand.re>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants