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

[several models] improve readability #24585

Merged
merged 3 commits into from
Jun 30, 2023
Merged

[several models] improve readability #24585

merged 3 commits into from
Jun 30, 2023

Conversation

stas00
Copy link
Contributor

@stas00 stas00 commented Jun 30, 2023

Honestly I had no idea what torch.ones([]) * self.config.logit_scale_init_value would return - it's not documented either.

Proposing to change it to a very clear torch.tensor(1.0) which leaves no doubt to what it does.

Proposing to change it to a very clear torch.tensor(self.config.logit_scale_init_value) which leaves no doubt to what it does.

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jun 30, 2023

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

@stas00 stas00 marked this pull request as ready for review June 30, 2023 06:02
@stas00 stas00 requested a review from sgugger June 30, 2023 06:02
@ydshieh
Copy link
Collaborator

ydshieh commented Jun 30, 2023

size ([int] – a sequence of integers defining the shape of the output tensor.

It's actually mentioned, but I agree it's a bit less explicit than torch.tensor(1).

@@ -977,7 +977,7 @@ def __init__(self, config: CLIPConfig):

self.visual_projection = nn.Linear(self.vision_embed_dim, self.projection_dim, bias=False)
self.text_projection = nn.Linear(self.text_embed_dim, self.projection_dim, bias=False)
self.logit_scale = nn.Parameter(torch.ones([]) * self.config.logit_scale_init_value)
self.logit_scale = nn.Parameter(torch.tensor(1.0) * self.config.logit_scale_init_value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's change all such occurrences? Most of them are in clip-family models.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Thanks

@stas00
Copy link
Contributor Author

stas00 commented Jun 30, 2023

Gentlemen, sleeping more on it, actually it just came to me that the cleanest most readable solution is just:

self.logit_scale = nn.Parameter(torch.tensor(self.config.logit_scale_init_value))

torch.ones isn't even needed ;)

Do you agree?

and yes, I can then fix other places.

@stas00
Copy link
Contributor Author

stas00 commented Jun 30, 2023

ok, I have used:

self.logit_scale = nn.Parameter(torch.tensor(self.config.logit_scale_init_value))

pattern everywhere I found torch.ones([]), plus in a few places I used from_numpy where the input was numpy, so I touched on other models as well.

Please have a look.

@stas00 stas00 changed the title [modeling_clip.py] improve readability [several models] improve readability Jun 30, 2023
Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

@stas00
Copy link
Contributor Author

stas00 commented Jun 30, 2023

from_numpy didn't quite work in 2 models where I tried it, so going to use the normal torch.tensor constructor like everywhere else in this PR.

@ydshieh
Copy link
Collaborator

ydshieh commented Jun 30, 2023

Nice finding :-)

@stas00 stas00 merged commit 49e812d into main Jun 30, 2023
4 checks passed
@stas00 stas00 deleted the stas00-patch-1 branch June 30, 2023 18:27
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.

4 participants