StopIteration error when using HuggingFace Transformer models
Author: h56cho
Pull request: no
Labels: name: wontfix

Hello,

I am trying to use the RobertaForMultipleChoice model, and when I try to compute the mc_loss, the following StopIteration error is generated:

```python
>>> mc_loss = model(input_ids = input_ids, attention_mask = attention_mask, labels = mc_labels)[0]

Traceback (most recent call last):
  File "STAT946_final_project_code_v4.py", line 625, in <module>
    success_rate_list_diag_normal = main_function_diag_normal('/home/ec2-user/test.txt', 'test_ans_num.txt', num_iter, log_interval)
  File "STAT946_final_project_code_v4.py", line 415, in main_function_diag_normal
    best_model_RobertaForMultipleChoice_diag_normal = train_loop(model_RobertaForMultipleChoice, tokenizer, optimizer_1, scheduler_1, log_interval, svi_diag_normal, guide_diag_normal, best_model_RobertaForMultipleChoice_diag_normal)
  File "STAT946_final_project_code_v4.py", line 342, in train_loop
    optimizer, scheduler, log_interval, svi, guide, epoch)
  File "STAT946_final_project_code_v4.py", line 237, in train_mc_head
    mc_loss = model(input_ids = input_ids, attention_mask = attention_mask, labels = mc_labels)[0]
  File "/home/ec2-user/anaconda3/lib/python3.7/site-packages/pyro/nn/module.py", line 413, in __call__
    return super().__call__(*args, **kwargs)
  File "/home/ec2-user/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/ec2-user/anaconda3/lib/python3.7/site-packages/transformers/modeling_roberta.py", line 441, in forward
    output_hidden_states=output_hidden_states,
  File "/home/ec2-user/anaconda3/lib/python3.7/site-packages/pyro/nn/module.py", line 413, in __call__
    return super().__call__(*args, **kwargs)
  File "/home/ec2-user/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/ec2-user/anaconda3/lib/python3.7/site-packages/transformers/modeling_bert.py", line 732, in forward
    extended_attention_mask: torch.Tensor = self.get_extended_attention_mask(attention_mask, input_shape, device)
  File "/home/ec2-user/anaconda3/lib/python3.7/site-packages/transformers/modeling_utils.py", line 228, in get_extended_attention_mask
    extended_attention_mask = extended_attention_mask.to(dtype=self.dtype)  # fp16 compatibility
  File "/home/ec2-user/anaconda3/lib/python3.7/site-packages/transformers/modeling_utils.py", line 159, in dtype
    first_tuple = next(gen)
StopIteration
```

The error seem to be generated from the HuggingFace code below:

```python
@property
def device(self) -> device:

     try:
        return next(self.parameters()).device

     except StopIteration:
          
       # For nn.DataParallel compatibility in PyTorch 1.5

       def find_tensor_attributes(module: nn.Module) -> List[Tuple[str, Tensor]]:
              tuples = [(k, v) for k, v in module.__dict__.items() if torch.is_tensor(v)]
              return tuples

       gen = self._named_members(get_members_fn=find_tensor_attributes)
       first_tuple = next(gen)
       return first_tuple[1].device
```

What is the cause of this error? and how can I fix it?

Thank you

Comments:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.


Hi @h56cho  the problem has been solved? 

It's been 4 years, pretty sure it has been 😉