pub struct CharTokenizer { /* private fields */ }Implementations§
Source§impl CharTokenizer
impl CharTokenizer
Sourcepub fn from_corpus(text: &str) -> Self
pub fn from_corpus(text: &str) -> Self
Build the vocab from the sorted unique characters of text
(nanoGPT’s rule). Ids are indices into that sorted order.
Sourcepub fn unknown_chars(&self, text: &str) -> Vec<char>
pub fn unknown_chars(&self, text: &str) -> Vec<char>
Characters of text that are outside the vocabulary, deduplicated and
in first-appearance order. Callers that want to warn a user before
CharTokenizer::encode rejects the input use this.
Sourcepub fn encode_lossy(&self, text: &str) -> Vec<u32>
pub fn encode_lossy(&self, text: &str) -> Vec<u32>
Encode, silently dropping characters outside the vocabulary. Pair with
CharTokenizer::unknown_chars to tell the user what was dropped —
the strict CharTokenizer::encode is the default for good reason.
Sourcepub fn to_json(&self) -> String
pub fn to_json(&self) -> String
{"<char>": id} — the same shape as GPT-2’s vocab.json, so the same
loader path and the same filename work for both tokenizers.
pub fn from_json(s: &str) -> Result<Self>
Sourcepub fn save_json(&self, path: impl AsRef<Path>) -> Result<()>
pub fn save_json(&self, path: impl AsRef<Path>) -> Result<()>
Write vocab.json next to a checkpoint. Re-deriving the vocab at
inference time from a different corpus would silently shift every id,
so it ships with the weights.
pub fn from_json_file(path: impl AsRef<Path>) -> Result<Self>
Trait Implementations§
Source§impl Tokenizer for CharTokenizer
impl Tokenizer for CharTokenizer
Source§fn encode(&self, text: &str) -> Result<Vec<u32>>
fn encode(&self, text: &str) -> Result<Vec<u32>>
Unknown characters are an error rather than a silent drop: a prompt
containing them would otherwise generate a plausible continuation of
something the user did not type. See CharTokenizer::encode_lossy.
Source§fn decode_bytes(&self, ids: &[u32]) -> Vec<u8> ⓘ
fn decode_bytes(&self, ids: &[u32]) -> Vec<u8> ⓘ
A char vocab has no partial-UTF-8 problem — every token is a whole character — but the streaming path calls this for both tokenizers.
fn decode(&self, ids: &[u32]) -> String
fn vocab_size(&self) -> usize
Auto Trait Implementations§
impl Freeze for CharTokenizer
impl RefUnwindSafe for CharTokenizer
impl Send for CharTokenizer
impl Sync for CharTokenizer
impl Unpin for CharTokenizer
impl UnsafeUnpin for CharTokenizer
impl UnwindSafe for CharTokenizer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more