Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert vocabulary for the new tokenizer to JSON #107

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

koute
Copy link

@koute koute commented May 1, 2023

The way the vocabulary is currently stored is a huge pain to parse in any language other than Python, so I propose we use something easier to parse but still as simple, like e.g. JSON.

I've made it so that the vocabulary is now encoded as a single map where the key is the token index and the value is the string to which the token corresponds to, or an array for those tokens which do not correspond to valid UTF-8 (basically only the 0x80-0xFF range).

This is how the file with the vocabulary looks like now:

{
    "1": "\u0000",
    "2": "\u0001",
    "3": "\u0002",
    "4": "\u0003",
// ...
    "124": "{",
    "125": "|",
    "126": "}",
    "127": "~",
    "128": "\u007f",
    "129": [
        128
    ],
    "130": [
        129
    ],
// ...
    "255": [
        254
    ],
    "256": [
        255
    ],
    "257": "\t\t",
    "258": "\t\n",
    "259": "\t ",
// ...
}

This way I can use the tokenizer vocabulary as-is in my native tokenizer without having to first manually convert it. (And in fact, I updated my tokenizer so now it can load the vocabulary at runtime instead of it being hardcoded.)


Script used to convert the .txt into .json:

#!/usr/bin/python

import json

I_TO_TOKEN = {}
lines = open("rwkv_vocab_v20230424.txt", "r", encoding="utf-8").readlines()
for l in lines:
    idx = int(l[:l.index(' ')])
    x = eval(l[l.index(' '):l.rindex(' ')])
    if not isinstance(x, str):
        x = list(x)
    I_TO_TOKEN[idx] = x

out = open("rwkv_vocab_v20230424.json", "w")
out.write(json.dumps(I_TO_TOKEN, indent=4))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant