    def save_vocabulary(filepath, vocab):
        """
        Save all tokens to a vocabulary file. The file contains a token per line,
        and the line number would be the index of corresponding token.

        Args:
            filepath (str): File path to be saved to.
            vocab (Vocab|dict): The `Vocab` or `dict` instance to be saved.
        """
        if isinstance(vocab, Vocab):
            tokens = vocab.idx_to_token
        else:
            tokens = sorted(vocab.keys(), key=lambda token: vocab[token])
        with io.open(filepath, 'w', encoding='utf-8') as f:
            for token in tokens:
                f.write(token + '\n')
Save all tokens to a vocabulary file. The file contains a token per line,
and the line number would be the index of corresponding token.

Args:
filepath (str): File path to be saved to.
vocab (Vocab|dict): The `Vocab` or `dict` instance to be saved.
ptstructure/vqa/pytorchnlp/transformers/tokenizer_utils.py