class_docstr: 基于图的 parser base class，支持 **贪婪解码** 和 **最大生成树解码**
class_docstr_tok: ["\u57fa\u4e8e\u56fe\u7684","parser","base","class\uff0c\u652f\u6301","*","*","\u8d2a\u5a6a\u89e3\u7801","*","*","\u548c","*","*","\u6700\u5927\u751f\u6210\u6811\u89e3\u7801","*","*"]
docstr: 用最大生成树算法，计算 parsing 结果，保证输出合法的树结构
  
  :param arc_matrix: 输入图矩阵，形状为 ``[batch, seq_len, seq_len]``。
  :param mask: 输入图的padding mask，形状为 ``[batch, seq_len]`` ， 有内容的部分为 **1** ， 否则为 **0** 。
      若为 ``None`` ，则默认为全1向量。
  :return: 每个元素在树中对应的 ``head(parent)`` 预测结果，形状为 ``[batch, seq_len]``。
docstr_tok: ["\u7528\u6700\u5927\u751f\u6210\u6811\u7b97\u6cd5\uff0c\u8ba1\u7b97","parsing","\u7ed3\u679c\uff0c\u4fdd\u8bc1\u8f93\u51fa\u5408\u6cd5\u7684\u6811\u7ed3\u6784",":","param","arc_matrix",":","\u8f93\u5165\u56fe\u77e9\u9635\uff0c\u5f62\u72b6\u4e3a","``","[","batch",",","seq_len",",","seq_len","]","``","\u3002",":","param","mask",":","\u8f93\u5165\u56fe\u7684padding","mask\uff0c\u5f62\u72b6\u4e3a","``","[","batch",",","seq_len","]","``","\uff0c","\u6709\u5185\u5bb9\u7684\u90e8\u5206\u4e3a","*","*","1","*","*","\uff0c","\u5426\u5219\u4e3a","*","*","0","*","*","\u3002","\u82e5\u4e3a","``","None","``","\uff0c\u5219\u9ed8\u8ba4\u4e3a\u51681\u5411\u91cf\u3002",":","return",":","\u6bcf\u4e2a\u5143\u7d20\u5728\u6811\u4e2d\u5bf9\u5e94\u7684","``","head","(","parent",")","``","\u9884\u6d4b\u7ed3\u679c\uff0c\u5f62\u72b6\u4e3a","``","[","batch",",","seq_len","]","``","\u3002"]
code: def mst_decoder(arc_matrix, mask=None):
      r"""
      用最大生成树算法，计算 parsing 结果，保证输出合法的树结构
  
      :param arc_matrix: 输入图矩阵，形状为 ``[batch, seq_len, seq_len]``。
      :param mask: 输入图的padding mask，形状为 ``[batch, seq_len]`` ， 有内容的部分为 **1** ， 否则为 **0** 。
          若为 ``None`` ，则默认为全1向量。
      :return: 每个元素在树中对应的 ``head(parent)`` 预测结果，形状为 ``[batch, seq_len]``。
      """
      batch_size, seq_len, _ = arc_matrix.shape
      matrix = arc_matrix.clone()
      ans = matrix.new_zeros(batch_size, seq_len).long()
      lens = (
          (mask.long()).sum(1)
          if mask is not None
          else torch.zeros(batch_size) + seq_len
      )
      for i, graph in enumerate(matrix):
          len_i = lens[i]
          ans[i, :len_i] = torch.as_tensor(
              _mst(graph.detach()[:len_i, :len_i].cpu().numpy()),
              device=ans.device,
          )
      if mask is not None:
          ans *= mask.long()
      return ans
  
imports: from collections import defaultdict
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from ...core.utils import seq_len_to_mask
  from ...embeddings.torch.utils import get_embeddings
  from ...modules.torch.dropout import TimestepDropout
  from ...modules.torch.encoder.transformer import TransformerEncoder
  from ...modules.torch.encoder.variational_rnn import VarLSTM
label_desc: Use image node_id 3 for calling the GraphParser obj's underlying member method code with example usage: obj.mst_decoder(arc_matrix, mask) and returns: ans