Skip to content

Commit

Permalink
fix loss and eval_forward for HF models (mosaicml#1597)
Browse files Browse the repository at this point in the history
* fix loss and eval_forward for HF models

* add a comment

Co-authored-by: nik-mosaic <[email protected]>
  • Loading branch information
dskhudia and nik-mosaic committed Oct 6, 2022
1 parent 36bc4e0 commit bf49ed4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions composer/models/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,21 @@ def forward(self, batch):
return output

def loss(self, outputs, batch):
return outputs['loss']
if self.config.use_return_dict:
return outputs['loss']
else:
# loss is at index 0 in the output tuple
return outputs[0]

def eval_forward(self, batch, outputs: Optional[Any] = None):
output = outputs if outputs else self.forward(batch)
if self.use_logits:
self.labels = batch.pop('labels')
output = output['logits']
if self.config.use_return_dict:
output = output['logits']
else:
# logits are at index 1 in the output tuple
output = output[1]

# if we are in the single class case, then remove the classes dimension
if output.shape[1] == 1:
Expand Down

0 comments on commit bf49ed4

Please sign in to comment.