MCPcopy Create free account
hub / github.com/THUDM/GLM / bytes_to_unicode

Function bytes_to_unicode

data_utils/tokenization_gpt2.py:54–74  ·  view source on GitHub ↗

Returns list of utf-8 byte and a corresponding list of unicode strings. The reversible bpe codes work on unicode strings. This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. When you're at something like a 10B token dataset you end up needing

()

Source from the content-addressed store, hash-verified

52
53@lru_cache()
54def bytes_to_unicode():
55 """
56 Returns list of utf-8 byte and a corresponding list of unicode strings.
57 The reversible bpe codes work on unicode strings.
58 This means you need a large # of unicode characters in your vocab if you want to avoid UNKs.
59 When you're at something like a 10B token dataset you end up needing around 5K for decent coverage.
60 This is a signficant percentage of your normal, say, 32K bpe vocab.
61 To avoid that, we want lookup tables between utf-8 bytes and unicode strings.
62 And avoids mapping to whitespace/control characters the bpe code barfs on.
63 """
64 _chr = unichr if sys.version_info[0] == 2 else chr
65 bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1))
66 cs = bs[:]
67 n = 0
68 for b in range(2**8):
69 if b not in bs:
70 bs.append(b)
71 cs.append(2**8+n)
72 n += 1
73 cs = [_chr(n) for n in cs]
74 return dict(zip(bs, cs))
75
76def get_pairs(word):
77 """Return set of symbol pairs in a word.

Callers 1

__init__Method · 0.85

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected