-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial release #22
Merged
Merged
Initial release #22
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1bca946
Add fields to crate manifest
hendrikvanantwerpen 68bf766
Change serialization to make embedded files smaller
hendrikvanantwerpen 1c2506d
Merge remote-tracking branch 'origin/main' into initial-release
hendrikvanantwerpen bae9f01
Generate serialized data in build script
hendrikvanantwerpen a83b250
clippy
hendrikvanantwerpen 3c95078
Add README
hendrikvanantwerpen fa4edb5
Bump versions
hendrikvanantwerpen 0b4cae9
Add other token sets as well
hendrikvanantwerpen d87b55e
Disable benchmark as a test target
hendrikvanantwerpen b1e3739
Typos
hendrikvanantwerpen 61d4fd2
Merge pull request #24 from github/generate-bpe-data
hendrikvanantwerpen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "bpe-openai" | ||
version = "0.1.0" | ||
edition = "2021" | ||
description = "Prebuilt fast byte-pair encoders for OpenAI." | ||
repository = "https://github.com/github/rust-gems" | ||
license = "MIT" | ||
keywords = ["tokenizer", "algorithm", "encoding", "bpe"] | ||
categories = ["algorithms", "data-structures", "encoding", "science"] | ||
|
||
[lib] | ||
crate-type = ["lib", "staticlib"] | ||
bench = false | ||
|
||
[dependencies] | ||
bpe = { version = "0.1.0", path = "../bpe" } | ||
rmp-serde = "1" | ||
serde = { version = "1" } | ||
|
||
[build-dependencies] | ||
bpe = { version = "0.1.0", path = "../bpe", features = ["tiktoken-rs"] } | ||
rmp-serde = "1" | ||
tiktoken-rs = { version = "0.5" } | ||
serde = { version = "1" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# OpenAI Byte Pair Encoders | ||
|
||
Fast tokenizers for OpenAI token sets based on the [bpe](https://crates.io/crates/bpe) crate. | ||
Serialized BPE instances are generated during build and lazily loaded at runtime as static values. | ||
The overhead of loading the tokenizers is small because it happens only once per process and only requires deserialization (as opposed to actually building the internal data structures). | ||
For convencience it re-exports the `bpe` crate so that depending on this crate is enough to use these tokenizers. | ||
|
||
Supported token sets: | ||
|
||
- r50k | ||
- p50k | ||
- cl100k | ||
- o200k | ||
|
||
## Usage | ||
|
||
Add a dependency by running | ||
|
||
```sh | ||
cargo add bpe-openai | ||
``` | ||
|
||
or by adding the following to `Cargo.toml` | ||
|
||
```toml | ||
[dependencies] | ||
bpe-openai = "0.1" | ||
``` | ||
|
||
Counting tokens is as simple as: | ||
|
||
```rust | ||
use bpe_openai::cl100k; | ||
|
||
fn main() { | ||
let bpe = cl100k(); | ||
let count = bpe.count("Hello, world!"); | ||
println!("{tokens}"); | ||
} | ||
``` | ||
|
||
For more detailed documentation we refer to [bpe](https://crates.io/crates/bpe). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::env; | ||
use std::fs::File; | ||
use std::path::PathBuf; | ||
|
||
use bpe::byte_pair_encoding::BytePairEncoding; | ||
use serde::Serialize; | ||
use tiktoken_rs::CoreBPE; | ||
|
||
fn main() { | ||
serialize_tokens( | ||
"r50k", | ||
&tiktoken_rs::r50k_base().expect("tiktoken initialization must not fail!"), | ||
50256, | ||
1, | ||
); | ||
serialize_tokens( | ||
"p50k", | ||
&tiktoken_rs::p50k_base().expect("tiktoken initialization must not fail!"), | ||
50280, | ||
1, | ||
); | ||
serialize_tokens( | ||
"cl100k", | ||
&tiktoken_rs::cl100k_base().expect("tiktoken initialization must not fail!"), | ||
100256, | ||
17846336922010275747, | ||
); | ||
serialize_tokens( | ||
"cl100k", | ||
&tiktoken_rs::cl100k_base().expect("tiktoken initialization must not fail!"), | ||
100256, | ||
17846336922010275747, | ||
); | ||
serialize_tokens( | ||
"o200k", | ||
&tiktoken_rs::o200k_base().expect("tiktoken initialization must not fail!"), | ||
199998, | ||
17846336922010275747, | ||
); | ||
println!("cargo::rerun-if-changed=build.rs"); | ||
} | ||
|
||
fn serialize_tokens(name: &str, bpe: &CoreBPE, num_tokens: usize, hash_factor: u64) { | ||
let mut path = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is set during build")); | ||
path.push(format!("bpe_{name}.dict")); | ||
let file = File::create(path).expect("can create output file"); | ||
let mut serializer = rmp_serde::Serializer::new(file); | ||
let bpe = BytePairEncoding::from_tiktoken(bpe, num_tokens, Some(hash_factor)); | ||
bpe.serialize(&mut serializer) | ||
.expect("serialization succeeds"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::sync::LazyLock; | ||
|
||
use bpe::byte_pair_encoding::BytePairEncoding; | ||
|
||
static BPE_R50K: LazyLock<BytePairEncoding> = LazyLock::new(|| { | ||
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/bpe_r50k.dict")); | ||
rmp_serde::from_slice(bytes).expect("") | ||
}); | ||
|
||
static BPE_P50K: LazyLock<BytePairEncoding> = LazyLock::new(|| { | ||
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/bpe_p50k.dict")); | ||
rmp_serde::from_slice(bytes).expect("") | ||
}); | ||
|
||
static BPE_CL100K: LazyLock<BytePairEncoding> = LazyLock::new(|| { | ||
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/bpe_cl100k.dict")); | ||
rmp_serde::from_slice(bytes).expect("") | ||
}); | ||
|
||
static BPE_O200K: LazyLock<BytePairEncoding> = LazyLock::new(|| { | ||
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/bpe_o200k.dict")); | ||
rmp_serde::from_slice(bytes).expect("") | ||
}); | ||
|
||
pub use bpe::*; | ||
|
||
pub fn r50k() -> &'static BytePairEncoding { | ||
&BPE_R50K | ||
} | ||
|
||
pub fn p50k() -> &'static BytePairEncoding { | ||
&BPE_P50K | ||
} | ||
|
||
pub fn cl100k() -> &'static BytePairEncoding { | ||
&BPE_CL100K | ||
} | ||
|
||
pub fn o200k() -> &'static BytePairEncoding { | ||
&BPE_O200K | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn can_load_r50k() { | ||
r50k().count("".as_bytes()); | ||
} | ||
|
||
#[test] | ||
fn can_load_p50k() { | ||
p50k().count("".as_bytes()); | ||
} | ||
|
||
#[test] | ||
fn can_load_cl100k() { | ||
cl100k().count("".as_bytes()); | ||
} | ||
|
||
#[test] | ||
fn can_load_o200k() { | ||
o200k().count("".as_bytes()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there should be a warning that this is crate is NOT replicating the regex "word splitting" used by openAI.
Therefore, results will differ!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the warning and also a test that shows an example of the issue.