Comments (6)
I'm currently working on optimizing the tokenizer and the token counter (on the Java implementation at https://github.com/knuddelsgmbh/jtokkit, but most of the tricks should be applicable to other implementations as well).
Benchmark (dataFolderPath) Mode Cnt Score Error Units
SingleThreadedBenchmark.benchmarkCl100kBaseTokenCountOriginal data ss 10 6.503 ± 0.053 s/op
SingleThreadedBenchmark.benchmarkCl100kBaseTokenCount data ss 10 2.094 ± 0.042 s/op
So far it's 3x faster, but I still have a few ideas left.
I'll check after, if the recommendations here are applicable or not.
from tiktoken.
I had a version using a trie to do single-pass-ish encoding of an input, but it wasn't correct. I'm not certain how fast a correct version of that trie would be.
from tiktoken.
Thanks, those are really nice results!
- Last time I checked, regex splitting was the majority of the time — I'd be interested in benchmarking the splitting part if easy. I'm potentially interested in specialised code, but we do vary the splitting regexes. Hopefully the PCRE approach demonstrated in the original version of #31 is viable and closes most of the gap.
- Nice! The repeated bytes hashing that tiktoken does is clearly not efficient (I was surprised it was viable). Skip list seems like a good way to avoid the O(n) deletes in the loop.
- Yeah, tiktoken does this and it was a big piece in ensuring good perf.
- I thought about this. I wouldn't want caller to provide array because it's annoying to size. But even just returning an numpy array to get rid of the overhead of going from Rust vec to Python list could be good (PyO3 has numpy bindings) — but I haven't benchmarked.
- If you figure out how to do this, let me know! I don't see a way. I'm always uncertain about the perf characteristics of tries, since they're not CPU cache friendly.
from tiktoken.
- Definitely easier if PCRE is fast enough, but if there's still a significant speed gain from hand-writing the most common regexps, could be worth it.
Previous script, full encode: csh_bpe 16592057.7610188 bytes / s => 60.3 ns/byte
Previous script, splitting only (commented out the bigram part): csh_bpe 104345021.38634916 bytes / s => 9.6 ns/byte
- The caller supplies an array that is the length of the input,
out_tokens = np.empty(input.shape, dtype=np.int32)
. This does cost more memory during encoding, though the caller can copy the used part of the array afterward if they want. Also unclear to me if this has any measurable performance advantage. - Yeah, the cache unfriendlyness is worrying, but definitely having a correct trie is the first step. It's not obviously impossible to me, but the correct trie could be gigantic.
from tiktoken.
Feel free to close this if the ideas have been ideated.
from tiktoken.
Hello,
It seems that the slow performance is due to an ineffective implementation of the negative lookahead clause ("\s+(?!\S)") in the fancy_regex library.
A possible solution to mimic the negative lookahead functionality is to remove it from the regex and manually re-add spaces to the matched parts, such as words or numbers. Although this approach achieves the same performance as pcre2, it may not be the most elegant solution.
from tiktoken.
Related Issues (20)
- how to convert qwen.tiktoken to tokenzier.model
- Optimize _byte_pair_merge function in BPE implementation
- Tiktoken not installing on a macbook pro with m2 chip HOT 2
- Exception has occurred: ConnectionError HTTPSConnectionPool(host='openaipublic.blob.core.windows.net', port=443): Max retries exceeded with url: /encodings/cl100k_base.tiktoken (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001F4D42B0EE0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
- Understanding the intended behaviour of `_encode_bytes`
- Custom tokenizer fails to encode despite characters being in mergeable_ranks HOT 2
- Use a custom exception ValueError subclass for the special tokens warning
- Error
- Combining marks and indic vowel marks within words are being split breaking all indic languages and most languages except English and CJKs HOT 4
- I need tiktoken win32 python3.8 version, can anyone provide it? HOT 1
- A character is splited into two tokens HOT 1
- gpt-4o tokenizer HOT 1
- Or
- GPT4o出现低级bug:发现最新token中的垃圾语料及实测GPT4o胡言乱语出现幻觉 HOT 3
- `o200k_base` pretokenizer - regex error? HOT 2
- Tiktoken educational BPE trainer takes long time to train with vocab size 30k HOT 2
- tiktoken 0.7.0 isn't compatible with python 3.11.* HOT 3
- Unknown encoding gpt2 HOT 1
- I want to modify the code in self._core_bpe.decode_bytes(tokens).decode("utf-8", errors=errors) HOT 1
- TikToken Tokenizer from scratch ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tiktoken.