-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clip_tokenizer.h
64 lines (56 loc) · 2.04 KB
/
clip_tokenizer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <set>
#include <unordered_map>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <regex>
#include <locale>
#include <codecvt>
#include <memory>
#include <unicode/unistr.h>
#include <unicode/ustream.h>
#include <unicode/locid.h>
#include <unicode/uchar.h>
#include <unicode/regex.h>
#include <unicode/normalizer2.h>
#include <unicode/translit.h>
#include <unicode/ustream.h>
namespace std
{
template<>
class hash<icu::UnicodeString> {
public:
size_t operator()(const icu::UnicodeString &s) const
{
return (size_t) s.hashCode();
}
};
};
struct TokenizerResult {
std::vector<std::vector<int>> tokens;
std::vector<std::vector<int>> attention_mask;
};
class CLIPTokenizer {
private:
std::unordered_map<int, UChar32> bytes_to_unicode();
std::set<std::pair<icu::UnicodeString, icu::UnicodeString>> get_pairs(const std::vector<icu::UnicodeString>& word);
std::vector<std::tuple<std::string, std::string>> get_merges(const std::string& file_path);
icu::UnicodeString whitespace_clean(const icu::UnicodeString& text);
std::vector<icu::UnicodeString> bpe(const icu::UnicodeString& text);
std::unordered_map<int, UChar32> byte_encoder;
std::unordered_map<UChar32, int> byte_decoder;
std::vector<icu::UnicodeString> vocab;
std::unordered_map<icu::UnicodeString, std::vector<icu::UnicodeString>> cache;
std::unique_ptr<icu::RegexMatcher> matcher;
std::unordered_map<icu::UnicodeString, int, std::hash<icu::UnicodeString>> bpe_ranks;
std::unordered_map<icu::UnicodeString, int, std::hash<icu::UnicodeString>> encoder;
std::unordered_map<int, icu::UnicodeString, std::hash<icu::UnicodeString>> decoder;
UErrorCode status = U_ZERO_ERROR;
std::vector<int> encode(icu::UnicodeString text);
size_t MAX_LEN = 77;
public:
CLIPTokenizer(const std::string& vocab_file);
TokenizerResult tokenize(const std::vector<std::string>& texts);
};