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

feat: Add OPT Tokenizer. #66

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions test/processor/OPTTokenizerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Created by 咸的鱼 on 2024/2/23.
//
#include "gtest/gtest.h"
#include "TokenizorTest.hpp"
#include "tokenizers/BPE/Bpe.hpp"
TEST_F(TokenizerTest, OPTTokenizerTest) {
GTEST_SKIP();
auto bpe = new mllm::BPETokenizer("./vocab_opt.mllm");
std::unordered_map<string,unsigned> merge_rank;
auto merge_file = std::ifstream("./merges.txt");
std::string line;
unsigned rank=0;
while (std::getline(merge_file, line)) {
if (line.empty()) {
continue;
}
if (line[0]=='#'){
continue;
}
merge_rank[line]=rank;
rank++;
}
bpe->setMergeRank(merge_rank);
std::cout<<bpe->getVocabSize()<<std::endl;
vector<mllm::token_id_t> tokens={};
string text="Hello, world!";
text = mllm::Tokenizer::replaceString(text,' ',"Ġ");
bpe->setSpecialToken("</s>","");
bpe->tokenize(text,tokens,true);

for (auto token:tokens){
std::cout<<token<< " ";
}


}
8 changes: 7 additions & 1 deletion tools/convertor/vocab.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import struct
from typing import Iterable, Tuple
import argparse
import os

MAGIC_NUM = 23333
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -89,7 +90,7 @@ def write_unigram(vocab_file, tokenizer_config):
args = parser.parse_args()
output_file = args.output_file
input_file = args.input_file

added_vocab = []
with open(output_file, "wb+") as vocab_file:
vocab_file.write(struct.pack("<i", MAGIC_NUM))

Expand All @@ -98,6 +99,11 @@ def write_unigram(vocab_file, tokenizer_config):

sentencepiece_tokenizer = SentencePieceProcessor(str(input_file))
write_vocab(vocab_file, sentencepiece_tokenizer)
elif args.type == "BPE" and os.path.basename(input_file) =="vocab.json":
vocabs = json.load(open(input_file, "r"))
config = {"vocab": vocabs, "type": "BPE"}
write_unigram(vocab_file, config)

else:
tokenizer_config = json.load(open(input_file, "r"))

Expand Down
Loading