-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
NeMo-UX: use input_ids instead of tokens in HfAutoModelForCausalLM #11340
Draft
akoumpa
wants to merge
7
commits into
main
Choose a base branch
from
akoumparouli/nemo_ux_fix_hf_auto_model_param_names
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.
+29
−24
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7be4740
use input_ids instead of tokens
akoumpa 7f9caf5
fix
akoumpa 403e566
update peft example
akoumpa fa520a9
Apply isort and black reformatting
akoumpa 4ba13b5
add missing import
akoumpa d97daa3
add hf peft test
akoumpa 0bf69ea
Apply isort and black reformatting
akoumpa 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 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 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 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 |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Optional | ||
|
||
import lightning.pytorch as pl | ||
import torch | ||
import torch.nn.functional as F | ||
|
@@ -82,41 +84,31 @@ def configure_model(self): | |
self.model = AutoModelForCausalLM.from_config(config, trust_remote_code=self.trust_remote_code) | ||
self.model.train() | ||
|
||
def forward(self, input_ids, attention_mask=None, labels=None, loss_mask=None): | ||
def forward( | ||
self, | ||
input_ids: torch.Tensor, | ||
attention_mask: Optional[torch.Tensor] = None, | ||
position_ids: torch.Tensor = None, | ||
labels: Optional[torch.Tensor] = None, | ||
**kwargs, | ||
): | ||
outputs = self.model( | ||
input_ids=input_ids.to(self.model.device), | ||
attention_mask=attention_mask, | ||
attention_mask=attention_mask.to(self.model.device) if attention_mask is not None else attention_mask, | ||
) | ||
labels = labels.to(self.model.device) | ||
if loss_mask is not None: | ||
loss_mask = loss_mask.to(self.model.device).view(-1) | ||
n_cls = outputs.logits.shape[-1] | ||
outputs.loss = self.loss_fn(outputs.logits.view(-1, n_cls), labels.view(-1), loss_mask) | ||
return outputs | ||
return self.loss_fn(outputs.logits.view(-1, n_cls), labels.view(-1), loss_mask) | ||
|
||
def training_step(self, batch): | ||
tokens = batch['tokens'] | ||
labels = batch['labels'] | ||
loss_mask = batch.get('loss_mask', None) | ||
output = self.forward( | ||
input_ids=tokens, | ||
labels=labels, | ||
loss_mask=loss_mask, | ||
) | ||
|
||
loss = output.loss | ||
loss = self.forward(**batch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please update the https://github.com/NVIDIA/NeMo/blob/akoumparouli/nemo_ux_fix_hf_auto_model_param_names/examples/llm/sft/hf.py as well? |
||
self.log('train_log', loss, on_step=True, on_epoch=True, prog_bar=True) | ||
return loss | ||
|
||
def validation_step(self, batch, batch_idx): | ||
tokens = batch['tokens'] | ||
labels = batch['labels'] | ||
output = self.forward( | ||
input_ids=tokens, | ||
labels=labels, | ||
) | ||
|
||
loss = output.loss | ||
loss = self.forward(**batch) | ||
self.log('val_loss', loss, on_step=True, on_epoch=True, prog_bar=True) | ||
|
||
def save_pretrained(self, path): | ||
|
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.
Can you pass this position_ids to the model as well?