class_docstr: The model which defines heads in syntax tree and dependencies for text tokens.
  Text token ids are fed into Transformer encoder, hidden states are passed into dense layers followed by
  two biaffine layers (first for prediction of pairwise probabilities of a token to be the head for other token,
  second - for prediction of syntax dependency of a token).
class_docstr_tok: ["The","model","which","defines","heads","in","syntax","tree","and","dependencies","for","text","tokens",".","Text","token","ids","are","fed","into","Transformer","encoder",",","hidden","states","are","passed","into","dense","layers","followed","by","two","biaffine","layers","(","first","for","prediction","of","pairwise","probabilities","of","a","token","to","be","the","head","for","other","token",",","second","-","for","prediction","of","syntax","dependency","of","a","token",")","."]
code: def forward(
      self,
      input_ids,
      attention_mask,
      subtoken_mask,
      y_heads=None,
      y_dep=None,
  ):
      input_ids = torch.from_numpy(input_ids).to(self.device)
      attention_mask = torch.from_numpy(attention_mask).to(self.device)
      subtoken_mask = torch.from_numpy(subtoken_mask)
  
      outputs = self.encoder(
          input_ids, attention_mask, output_hidden_states=True
      )
      hidden_states = outputs.hidden_states
      layer_output_list = []
      for layer_id in self.encoder_layer_ids:
          layer_id = layer_id + 1 if layer_id != -1 else layer_id
          layer_output_list.append(hidden_states[layer_id])
      layer_output = torch.stack(layer_output_list)
      layer_output = torch.sum(layer_output, dim=0)
  
      layer_output = token_from_subtoken(layer_output, subtoken_mask)
      bs, seq_len, dim = layer_output.size()
  
      layer_output = layer_output.float().to(self.device)
      lengths = torch.sum(subtoken_mask, dim=-1)
  
      head1 = self.head_embs1(layer_output)
      dep1 = self.dep_embs1(layer_output)
      dep1_zero = [self.zero_emb1 for _ in range(bs)]
      dep1_zero = torch.stack(dep1_zero).unsqueeze(1).to(self.device)
      dep1 = torch.cat([dep1_zero, dep1], dim=1)
  
      head2 = self.head_embs2(layer_output)
      dep2 = self.dep_embs2(layer_output)
      dep2_zero = [self.zero_emb2 for _ in range(bs)]
      dep2_zero = torch.stack(dep2_zero).unsqueeze(1).to(self.device)
      dep2 = torch.cat([dep2_zero, dep2], dim=1)
  
      head1 = self.dropout(head1)
      dep1 = self.dropout(dep1)
      head2 = self.dropout(head2)
      dep2 = self.dropout(dep2)
  
      logits_head_init = self.biaf_head(head1, dep1).squeeze_(3)
      logits_deprel = self.biaf_dep(head2, dep2)
      mask = mask_arc(lengths, mask_diag=False)
      if mask is not None:
          logits_head_init.masked_fill_(
              mask.logical_not().to(logits_head_init.device), -10.0
          )
      logits_head = F.softmax(logits_head_init, dim=-1)
  
      head_loss, dep_loss = None, None
      if y_heads is not None:
          y_heads = tuple(
              torch.LongTensor(yh).to(self.device) for yh in y_heads
          )
          y_heads_pd = nn.utils.rnn.pad_sequence(
              y_heads, batch_first=True, padding_value=-1
          )
  
          logits_head_flatten = logits_head.contiguous().view(
              -1, logits_head.size(-1)
          )
          y_heads_flatten = y_heads_pd.contiguous().view(-1)
          head_loss = F.cross_entropy(
              logits_head_flatten,
              y_heads_flatten,
              ignore_index=-1,
              reduction="sum",
          )
          head_loss.div_((y_heads_flatten != -1).sum())
  
          y_dep = tuple(
              torch.LongTensor(ydp).to(self.device) for ydp in y_dep
          )
          y_dep_pd = nn.utils.rnn.pad_sequence(
              y_dep, batch_first=True, padding_value=-1
          )
          y_heads_new = y_heads_pd.masked_fill(y_heads_pd == -1, 0)
          gather_index = y_heads_new.view(
              *y_heads_new.size(), 1, 1
          ).expand(-1, -1, -1, logits_deprel.size(-1))
  
          logits_deprel = torch.gather(
              logits_deprel, dim=2, index=gather_index
          )
          logits_deprel_flatten = logits_deprel.contiguous().view(
              -1, logits_deprel.size(-1)
          )
          y_dep_flatten = y_dep_pd.contiguous().view(-1)
          dep_loss = F.cross_entropy(
              logits_deprel_flatten,
              y_dep_flatten,
              ignore_index=-1,
              reduction="sum",
          )
          dep_loss.div_((y_dep_flatten != -1).sum())
      else:
          logits_head = logits_head.detach().cpu().numpy()
          head_ids = np.argmax(logits_head, axis=-1).tolist()
  
          head_ids_new = torch.LongTensor(head_ids)
          steps = torch.arange(head_ids_new.size(1))
          logits_deprel = [
              logits_deprel[i, steps, heads]
              for i, heads in enumerate(head_ids_new)
          ]
          logits_deprel = torch.stack(logits_deprel, dim=0)
          deprels = (
              logits_deprel.argmax(dim=2)
              .detach()
              .cpu()
              .numpy()
              .tolist()
          )
  
          head_probas = [
              head_probas_list[:l, : l + 1]
              for l, head_probas_list in zip(lengths, logits_head)
          ]
          deprels = [deprel[:l] for l, deprel in zip(lengths, deprels)]
  
      if y_heads is not None:
          return head_loss + dep_loss
      else:
          return head_probas, deprels
  
imports: import math
  from logging import getLogger
  from pathlib import Path
  from typing import List, Dict, Union, Optional, Tuple
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from transformers import AutoConfig, AutoModel
  from deeppavlov.core.commands.utils import expand_path
  from deeppavlov.core.common.errors import ConfigError
  from deeppavlov.core.common.registry import register
  from deeppavlov.core.models.torch_model import TorchModel
  from deeppavlov.models.torch_bert.torch_transformers_sequence_tagger import token_from_subtoken
label_desc: Use image node_id 2 for calling the SyntaxParserNetwork obj's underlying member method code with example usage: obj.forward(input_ids, attention_mask, subtoken_mask, y_heads, y_dep) and returns: unknown, head_probas,  deprels