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
()
| 74 | |
| 75 | @lru_cache() |
| 76 | def bytes_to_unicode(): |
| 77 | """ |
| 78 | Returns list of utf-8 byte and a corresponding list of unicode strings. |
| 79 | The reversible bpe codes work on unicode strings. |
| 80 | This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. |
| 81 | When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. |
| 82 | This is a significant percentage of your normal, say, 32K bpe vocab. |
| 83 | To avoid that, we want lookup tables between utf-8 bytes and unicode strings. |
| 84 | And avoids mapping to whitespace/control characters the bpe code barfs on. |
| 85 | """ |
| 86 | bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1)) |
| 87 | cs = bs[:] |
| 88 | n = 0 |
| 89 | for b in range(2**8): |
| 90 | if b not in bs: |
| 91 | bs.append(b) |
| 92 | cs.append(2**8+n) |
| 93 | n += 1 |
| 94 | cs = [chr(n) for n in cs] |
| 95 | return dict(zip(bs, cs)) |
| 96 | |
| 97 | |
| 98 | def get_pairs(word): |