(
self,
input_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
attention_mask: Optional[torch.FloatTensor] = None,
token_type_ids: Optional[torch.LongTensor] = None,
position_ids: Optional[torch.LongTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
)
| 420 | config_class=_CONFIG_FOR_DOC, |
| 421 | ) |
| 422 | def forward( |
| 423 | self, |
| 424 | input_ids: Optional[torch.LongTensor] = None, |
| 425 | past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, |
| 426 | attention_mask: Optional[torch.FloatTensor] = None, |
| 427 | token_type_ids: Optional[torch.LongTensor] = None, |
| 428 | position_ids: Optional[torch.LongTensor] = None, |
| 429 | head_mask: Optional[torch.FloatTensor] = None, |
| 430 | inputs_embeds: Optional[torch.FloatTensor] = None, |
| 431 | use_cache: Optional[bool] = None, |
| 432 | output_attentions: Optional[bool] = None, |
| 433 | output_hidden_states: Optional[bool] = None, |
| 434 | return_dict: Optional[bool] = None, |
| 435 | ) -> Union[Tuple, BaseModelOutputWithPast]: |
| 436 | output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions |
| 437 | output_hidden_states = ( |
| 438 | output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states |
| 439 | ) |
| 440 | use_cache = use_cache if use_cache is not None else self.config.use_cache |
| 441 | return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
| 442 | |
| 443 | if input_ids is not None and inputs_embeds is not None: |
| 444 | raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") |
| 445 | elif input_ids is not None: |
| 446 | input_shape = input_ids.size() |
| 447 | input_ids = input_ids.view(-1, input_shape[-1]) |
| 448 | batch_size = input_ids.shape[0] |
| 449 | elif inputs_embeds is not None: |
| 450 | input_shape = inputs_embeds.size()[:-1] |
| 451 | batch_size = inputs_embeds.shape[0] |
| 452 | else: |
| 453 | raise ValueError("You have to specify either input_ids or inputs_embeds") |
| 454 | |
| 455 | device = input_ids.device if input_ids is not None else inputs_embeds.device |
| 456 | |
| 457 | if token_type_ids is not None: |
| 458 | token_type_ids = token_type_ids.view(-1, input_shape[-1]) |
| 459 | |
| 460 | if position_ids is not None: |
| 461 | position_ids = position_ids.view(-1, input_shape[-1]).long() |
| 462 | |
| 463 | if past_key_values is None: |
| 464 | past_length = 0 |
| 465 | past_key_values = tuple([None] * len(self.h)) |
| 466 | else: |
| 467 | past_length = past_key_values[0][0].size(-2) |
| 468 | |
| 469 | if position_ids is None: |
| 470 | position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device) |
| 471 | position_ids = position_ids.unsqueeze(0).view(-1, input_shape[-1]) |
| 472 | |
| 473 | # Attention mask. |
| 474 | if attention_mask is not None: |
| 475 | if batch_size <= 0: |
| 476 | raise ValueError("batch_size has to be defined and > 0") |
| 477 | attention_mask = attention_mask.view(batch_size, -1) |
| 478 | # We create a 3D attention mask from a 2D tensor mask. |
| 479 | # Sizes are [batch_size, 1, 1, to_seq_length] |
nothing calls this directly
no outgoing calls
no test coverage detected