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
()
| 14 | |
| 15 | @lru_cache() |
| 16 | def bytes_to_unicode(): |
| 17 | """ |
| 18 | Returns list of utf-8 byte and a corresponding list of unicode strings. |
| 19 | The reversible bpe codes work on unicode strings. |
| 20 | This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. |
| 21 | When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. |
| 22 | This is a signficant percentage of your normal, say, 32K bpe vocab. |
| 23 | To avoid that, we want lookup tables between utf-8 bytes and unicode strings. |
| 24 | And avoids mapping to whitespace/control characters the bpe code barfs on. |
| 25 | """ |
| 26 | bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1)) |
| 27 | cs = bs[:] |
| 28 | n = 0 |
| 29 | for b in range(2**8): |
| 30 | if b not in bs: |
| 31 | bs.append(b) |
| 32 | cs.append(2**8+n) |
| 33 | n += 1 |
| 34 | cs = [chr(n) for n in cs] |
| 35 | return dict(zip(bs, cs)) |
| 36 | |
| 37 | |
| 38 | def get_pairs(word): |