code: def convert_vocab(args, fairseq_model):
      print("converting vocab")
      origin_spm_path = os.path.join(
          args.fairseq_model_path, "sentencepiece.bpe.model"
      )
      assert os.path.exists(origin_spm_path)
      new_spm_path = os.path.join(args.save_dir, "sentencepiece.model")
      fairseq_vocab = fairseq_model.task.dictionary
      # bos_word attr missing in fairseq_vocab
      fairseq_vocab.bos_word = fairseq_vocab[fairseq_vocab.bos_index]
  
      # model.pieces: <unk> <s> </s> other_tokens ->
      # model.pieces: <s> <pad> </s> <unk> other_tokens <mask>
      model = sentencepiece_model_pb2.ModelProto()
      with open(origin_spm_path, "rb") as f_m:
          model.ParseFromString(f_m.read())
      p0 = model.pieces[0]
      p1 = model.pieces[1]
      p2 = model.pieces[2]
  
      pad_piece = copy.deepcopy(p0)
      pad_piece.piece = fairseq_vocab.pad_word
      pad_piece.type = pad_piece.CONTROL
      mask_piece = copy.deepcopy(p0)
      mask_piece.piece = "<mask>"
      mask_piece.type = mask_piece.CONTROL
  
      p0.type = p0.CONTROL
      p0.piece = fairseq_vocab.bos_word
      p1.type = p1.CONTROL
      p1.piece = fairseq_vocab.eos_word
      p2.type = p2.UNKNOWN
      p2.piece = fairseq_vocab.unk_word
      model.pieces.insert(fairseq_vocab.pad_index, pad_piece)
      model.pieces.append(mask_piece)
  
      model.trainer_spec.vocab_size = len(fairseq_vocab)
      model.trainer_spec.unk_id = fairseq_vocab.unk_index
      model.trainer_spec.bos_id = fairseq_vocab.bos_index
      model.trainer_spec.eos_id = fairseq_vocab.eos_index
      model.trainer_spec.pad_id = fairseq_vocab.pad_index
  
      with open(new_spm_path, "wb") as f:
          f.write(model.SerializeToString())
  
      gluon_tokenizer = SentencepieceTokenizer(new_spm_path)
      if args.test:
          test_vocab(
              fairseq_model, gluon_tokenizer, check_all_tokens=True
          )
  
      vocab_size = len(fairseq_model.task.dictionary)
      print("| converted dictionary: {} types".format(vocab_size))
      return vocab_size
  
file_path: gluon-nlp/docs/model_zoo/conversion_toolkits/convert_fairseq_xlmr.py
imports: import os
  import copy
  import logging
  import argparse
  import mxnet
  from gluonnlp.utils.misc import logging_config
  from gluonnlp.models.xlmr import XLMRModel, XLMRForMLM
  from gluonnlp.third_party import sentencepiece_model_pb2
  from fairseq.models.roberta import XLMRModel
  from convert_fairseq_roberta import rename, test_model, test_vocab, convert_config, convert_params
  from gluonnlp.data.tokenizers import SentencepieceTokenizer
label_desc: Use image node_id 2 for calling a global function with example usage: convert_vocab(args, fairseq_model) and returns: vocab_size