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

Use minimal softmax mask values based on the datatype #966

Merged

Conversation

bentherien
Copy link
Contributor

@bentherien bentherien commented Jun 4, 2023

The current GPT-NeoX implementation uses a hardcoded value of -10000.0 mask values in its softmax function. While this works fine most of the time, it can become problematic if unnormalized softmax values computed by the model become very small. Therefore, it is always best practice to take the smallest possible (based on the datatype) numerical value for softmax masking. This change does exactly this, following the implementation at https://github.com/huggingface/transformers/blob/539e2281cd97c35ef4122757f26c88f44115fa94/src/transformers/models/gpt_neox/modeling_gpt_neox.py#L226

@bentherien bentherien requested a review from a team as a code owner June 4, 2023 17:18
mask_value = torch.finfo(attention_scores.dtype).min
# Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`.
# Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device`
mask_value = torch.tensor(mask_value, dtype=attention_scores.dtype).to(attention_scores.device)
Copy link
Member

Choose a reason for hiding this comment

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

It's always best to initialize tensors directly on the target device. Using .to necessitates a copy first on the torch default device ('cpu' unless explicitly set).

Change to:
mask_value = torch.tensor(mask_value, dtype=attention_scores.dtype, device=attention_scores.device)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, fixed below

@Quentin-Anthony Quentin-Anthony merged commit a6065b4 into EleutherAI:main Jun 5, 2023
2 of 5 checks passed
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.

None yet

2 participants