-
Notifications
You must be signed in to change notification settings - Fork 364
[Model] Pixtral Support #253
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
Open
AndreSlavescu
wants to merge
7
commits into
linkedin:main
Choose a base branch
from
AndreSlavescu:pixtral
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7318fe
pixtral initial
AndreSlavescu cf98362
progress
AndreSlavescu 3d05cd9
pixtral support
AndreSlavescu 30a5940
bf16 tests
AndreSlavescu 69a9d1a
Merge branch 'main' into pixtral
AndreSlavescu ca34ab1
Merge branch 'main' into pixtral
AndreSlavescu 5efc338
Merge branch 'main' into pixtral
AndreSlavescu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from typing import Optional | ||
from typing import Tuple | ||
from typing import Union | ||
|
||
import torch | ||
|
||
from transformers.modeling_outputs import BaseModelOutput | ||
|
||
|
||
def lce_forward( | ||
self, | ||
inputs_embeds, | ||
attention_mask: Optional[torch.Tensor] = None, | ||
position_embeddings: Optional[torch.Tensor] = None, | ||
output_attentions: Optional[bool] = None, | ||
output_hidden_states: Optional[bool] = None, | ||
return_dict: Optional[bool] = None, | ||
**loss_kwargs, | ||
) -> Union[Tuple, BaseModelOutput]: | ||
r""" | ||
Copy paste Pixtral's forward from transformers v4.44.2 but replace torch cross entropy with liger fused linear cross entropy | ||
|
||
Args: | ||
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): | ||
Embeddings which serve as input to the Transformer. | ||
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): | ||
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: | ||
|
||
- 1 for tokens that are **not masked**, | ||
- 0 for tokens that are **masked**. | ||
|
||
[What are attention masks?](../glossary#attention-mask) | ||
output_attentions (`bool`, *optional*): | ||
Whether or not to return the attentions tensors of all attention layers. See `attentions` under | ||
returned tensors for more detail. | ||
output_hidden_states (`bool`, *optional*): | ||
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors | ||
for more detail. | ||
return_dict (`bool`, *optional*): | ||
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. | ||
""" | ||
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions | ||
output_hidden_states = ( | ||
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states | ||
) | ||
return_dict = return_dict if return_dict is not None else self.config.use_return_dict | ||
|
||
outputs = self.model( | ||
inputs_embeds, | ||
attention_mask=attention_mask, | ||
position_embeddings=position_embeddings, | ||
output_attentions=output_attentions, | ||
output_hidden_states=output_hidden_states, | ||
return_dict=return_dict, | ||
) | ||
|
||
hidden_states = outputs[0] | ||
|
||
encoder_states = () if output_hidden_states else None | ||
all_attentions = () if output_attentions else None | ||
for encoder_layer in self.layers: | ||
if output_hidden_states: | ||
encoder_states = encoder_states + (hidden_states,) | ||
if self.gradient_checkpointing and self.training: | ||
layer_outputs = self._gradient_checkpointing_func( | ||
encoder_layer.__call__, | ||
hidden_states, | ||
attention_mask, | ||
position_embeddings, | ||
output_attentions, | ||
) | ||
else: | ||
layer_outputs = encoder_layer( | ||
hidden_states, | ||
attention_mask, | ||
position_embeddings=position_embeddings, | ||
output_attentions=output_attentions, | ||
) | ||
|
||
hidden_states = layer_outputs[0] | ||
|
||
if output_attentions: | ||
all_attentions = all_attentions + (layer_outputs[1],) | ||
|
||
if output_hidden_states: | ||
encoder_states = encoder_states + (hidden_states,) | ||
|
||
if not return_dict: | ||
return tuple(v for v in [hidden_states, encoder_states, all_attentions] if v is not None) | ||
|
||
return BaseModelOutput( | ||
last_hidden_states=hidden_states, | ||
hidden_states=encoder_states, | ||
attentions=all_attentions, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm not familiar with pixtral but it looks like it's just a base model. The loss isn't computed in the forward pass, so there's no need to patch CrossEntropy and FusedLinearCrossEntropy.