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

Avoid import sentencepiece_model_pb2 in utils.__init__.py #24689

Merged
merged 1 commit into from
Jul 6, 2023
Merged

Conversation

ydshieh
Copy link
Collaborator

@ydshieh ydshieh commented Jul 6, 2023

What does this PR do?

Otherwise, trying to import anything from utils will fail if protobuf is not installed.

More details in the comment.

@@ -184,9 +184,6 @@
from . import sentencepiece_model_pb2
else:
from . import sentencepiece_model_pb2_new as sentencepiece_model_pb2
else:
# just to get the expected `No module named 'google.protobuf'` error
from . import sentencepiece_model_pb2
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added this as I would like to keep the following usage

from (transformers).utils import sentencepiece_model_pb2 as model_pb2

gives the same error

No module named 'google.protobuf'

as before 24599, when protobuf is not in the env.

It turns out that, if we just ignore this else block, the above import statement (when protobuf is missing) will find the corresponding file, and import it directly, and we wil get the expected error as before.

Copy link
Collaborator

Choose a reason for hiding this comment

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

huh, interesting. Just to make sure I've understood, without this else statement, we still get the correct No module named 'google.protobuf' error still triggers if protobuf isn't in the env?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. The __init__ won't have the name sentencepiece_model_pb2, but the file utils/sentencepiece_model_pb2.py exist. So the import will look at that file and try to import.

(I was overthinking, partially I was thinking to have sentencepiece_model_pb2_old.py and sentencepiece_model_pb2_new.py (and need to keep the name sentencepiece_model_pb2 available in the __init__ for backward compatibility)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for explaining 🤗

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jul 6, 2023

The documentation is not available anymore as the PR was closed or merged.

@ydshieh ydshieh requested a review from amyeroberts July 6, 2023 13:38
Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for updating!

@ydshieh ydshieh merged commit bbf3090 into main Jul 6, 2023
4 checks passed
@ydshieh ydshieh deleted the fix_import_2 branch July 6, 2023 14:30
blbadger pushed a commit to blbadger/transformers that referenced this pull request Nov 8, 2023
…gface#24689)

fix

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
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.

3 participants