Skip to main content

SpmPrefixScheme

Enum SpmPrefixScheme 

Source
pub enum SpmPrefixScheme {
    Once,
    AfterEachSpecial,
}
Expand description

Where the dummy prefix (add_dummy_prefix / add_space_prefix) is placed once the input is split on added tokens.

The two reference implementations of this same vocabulary format genuinely disagree, and both were measured rather than inferred — so neither is “the” behavior and the loader that built the tokenizer has to say which one its vocabulary was produced for. Encoding "[INST]Write" and "a[INST]b":

Once (HF)AfterEachSpecial (llama.cpp)
[INST]Write, [INST], Write[INST], ▁Write
a[INST]b▁a, [INST], b▁a, [INST], ▁b

Getting this wrong is invisible from the outside — every id stays in range and decodes back to the original string — while every chat prompt (which is exactly a text/marker alternation) reaches the model as pieces it was not trained on.

The default is AfterEachSpecial, because SpmTokenizer::new takes a GGUF-style vocabulary and llama.cpp is the reference for those. A vocabulary lifted out of a HuggingFace tokenizer.model is the case that has to be declared, and its one loader does declare it.

Variants§

§

Once

Prefix the whole text once, before splitting on added tokens (HuggingFace / sentencepiece).

SentencePiece normalizes — and therefore prefixes — the input and only then splits, so only the stretch beginning at byte 0 can carry a marker. A leading added token leaves the marker standing alone, with no text to attach to. Measured with AutoTokenizer.from_pretrained("mistral-7b-v0.3", use_fast=False) and add_special_tokens=False: "[INST]Write" -> [29473, 3, 6006] (, [INST], bare Write) and "a[INST]b" -> [1032, 3, 29494] (▁a, [INST], bare b).

This is HuggingFace’s corrected behavior (legacy = false in tokenizer_config.json). Which scheme a bundled .spm vocabulary needs is not determined by the fact that it came from a tokenizer.model — it is that per-checkpoint legacy flag: Mistral V2 sets legacy = false and needs Once, but Mistral V1 sets legacy = true and needs AfterEachSpecial despite also being extracted from a tokenizer.model — see spm_prefix_scheme in pretrained.rs, which reads that flag off per vocabulary rather than assuming it.

§

AfterEachSpecial

Prefix the first stretch and every stretch that follows an added token (llama.cpp’s is_prev_special).

llama-vocab.cpp’s LLAMA_VOCAB_TYPE_SPM arm walks the fragment buffer with bool is_prev_special = true (“prefix with space if first token”), prepending ' ' to a raw-text fragment whenever the flag is set and re-arming it on every special-token fragment. A special token at the very start therefore emits no standalone marker — there is no text fragment before it to prefix.

Correct for every GGUF-loaded vocabulary, because llama.cpp is what actually runs those files — and so the default, see the type’s docs. Also correct for a bundled .spm vocabulary whose checkpoint declares legacy = true (Mistral V1) — see Once’s docs.

Trait Implementations§

Source§

impl Clone for SpmPrefixScheme

Source§

fn clone(&self) -> SpmPrefixScheme

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for SpmPrefixScheme

Source§

impl Debug for SpmPrefixScheme

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SpmPrefixScheme

Source§

fn default() -> SpmPrefixScheme

Returns the “default value” for a type. Read more
Source§

impl Eq for SpmPrefixScheme

Source§

impl PartialEq for SpmPrefixScheme

Source§

fn eq(&self, other: &SpmPrefixScheme) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SpmPrefixScheme

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.