code: def convert_vocab(args, fairseq_model):
      print("converting vocab")
      fairseq_dict_path = os.path.join(
          args.fairseq_model_path, "dict.txt"
      )
      merges_save_path = os.path.join(args.save_dir, "gpt2.merges")
      vocab_save_path = os.path.join(args.save_dir, "gpt2.vocab")
      fairseq_vocab = fairseq_model.task.dictionary
      # bos_word attr missing in fairseq_vocab
      fairseq_vocab.bos_word = fairseq_vocab[fairseq_vocab.bos_index]
  
      assert os.path.exists(fairseq_dict_path), "{} not found".format(
          fairseq_dict_path
      )
      from mxnet.gluon.utils import download
  
      temp_vocab_file = download(
          "https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json"
      )
      temp_merges_file = download(
          "https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpe"
      )
      # copy merges directy
      shutil.copy(temp_merges_file, merges_save_path)
  
      # build vocab
      transfer_dict = []
      with open(fairseq_dict_path, "r", encoding="utf-8") as f_dict:
          for line in f_dict:
              word_id, count = line.split(" ", 1)
              transfer_dict.append(word_id)
      transfer_dict = {
          transfer_dict[i]: i for i in range(len(transfer_dict))
      }
      with open(temp_vocab_file, "r", encoding="utf-8") as f_v:
          inter_vocab = json.load(f_v)
      # transfer by dict
      for k in inter_vocab:
          inter_vocab[k] = transfer_dict[str(inter_vocab[k])]
      inter_vocab = list(inter_vocab.items())
      inter_vocab = sorted(inter_vocab, key=lambda x: x[1])
      tokens = [e[0] for e in inter_vocab]
  
      tail = [
          vocab
          for vocab in fairseq_vocab.indices.keys()
          if re.match(r"^madeupword[\d]{4}$", vocab) is not None
      ]
      all_tokens = (
          ["<s>", "<pad>", "</s>", "<unk>"] + tokens + tail + ["<mask>"]
      )
  
      gluon_vocab = gluon_Vocab(
          all_tokens,
          unk_token=fairseq_vocab.unk_word,
          pad_token=fairseq_vocab.pad_word,
          eos_token=fairseq_vocab.eos_word,
          bos_token=fairseq_vocab.bos_word,
          mask_token=fairseq_vocab[-1],
      )
      gluon_vocab.save(vocab_save_path)
      os.remove(temp_vocab_file)
      os.remove(temp_merges_file)
  
      gluon_tokenizer = HuggingFaceByteBPETokenizer(
          merges_save_path, vocab_save_path
      )
  
      if args.test:
          test_vocab(fairseq_model, gluon_tokenizer)
  
      vocab_size = len(fairseq_vocab)
      print("| converted dictionary: {} types".format(vocab_size))
      return vocab_size
  
imports: import os
  import re
  import sys
  import json
  import shutil
  import logging
  import argparse
  import mxnet
  import numpy
  from numpy.testing import assert_allclose
  import torch
  from gluonnlp.data.vocab import Vocab
  from gluonnlp.utils.misc import sha1sum, logging_config, naming_convention
  from fairseq.models.roberta import RobertaModel
  from gluonnlp.models.roberta import RobertaModel, RobertaForMLM
  from gluonnlp.data.tokenizers import HuggingFaceByteBPETokenizer
label_desc: Use image node_id 2 for calling a global function with example usage: convert_vocab(args, fairseq_model) and returns: vocab_size