MoonshineTokenizer
classkeras_hub.tokenizers.MoonshineTokenizer(proto, **kwargs)
Moonshine tokenizer layer based on keras_hub.models.LlamaTokenizer
.
This tokenizer class is an alias of LlamaTokenizer
but for the Moonshine
model. It uses a SentencePiece vocabulary to handle tokenization.
Arguments
str
or bytes
. Either a string path to a SentencePiece proto
file or a bytes object containing a serialized SentencePiece proto.
See the SentencePiece repository
for details on the format.LlamaTokenizer
.Examples
from keras_hub.tokenizers import MoonshineTokenizer
# Initialize tokenizer.
tokenizer = MoonshineTokenizer(
"keras_hub/src/tests/test_data/llama_test_vocab.spm"
)
# Single input example.
single_input = "the quick brown fox"
single_tokens = tokenizer(single_input)
print("Single input tokenization:")
print(f"Input text: {single_input}")
print(f"Tokenized: {single_tokens}")
# Batched input example.
batch_input = ["the quick brown fox", "the earth is round"]
batch_tokens = tokenizer(batch_input)
print("Batch input tokenization:")
print(f"Input texts: {batch_input}")
print(f"Tokenized: {batch_tokens}")
# Detokenization example.
encoded = tokenizer(single_input)
decoded = tokenizer.detokenize(encoded)
print("Detokenization:")
print(f"Original text: {single_input}")
print(f"Encoded: {encoded}")
print(f"Decoded: {decoded}")
from_preset
methodMoonshineTokenizer.from_preset(preset, config_file="tokenizer.json", **kwargs)
Instantiate a keras_hub.models.Tokenizer
from a model preset.
A preset is a directory of configs, weights and other file assets used
to save and load a pre-trained model. The preset
can be passed as
one of:
'bert_base_en'
'kaggle://user/bert/keras/bert_base_en'
'hf://user/bert_base_en'
'./bert_base_en'
For any Tokenizer
subclass, you can run cls.presets.keys()
to list
all built-in presets available on the class.
This constructor can be called in one of two ways. Either from the base
class like keras_hub.models.Tokenizer.from_preset()
, or from
a model class like keras_hub.models.GemmaTokenizer.from_preset()
.
If calling from the base class, the subclass of the returning object
will be inferred from the config in the preset directory.
Arguments
True
, the weights will be loaded into the
model architecture. If False
, the weights will be randomly
initialized.Examples
# Load a preset tokenizer.
tokenizer = keras_hub.tokenizer.Tokenizer.from_preset("bert_base_en")
# Tokenize some input.
tokenizer("The quick brown fox tripped.")
# Detokenize some input.
tokenizer.detokenize([5, 6, 7, 8, 9])
Preset | Parameters | Description |
---|---|---|
llama2_7b_en | 6.74B | 7 billion parameter, 32-layer, base LLaMA 2 model. |
llama2_instruct_7b_en | 6.74B | 7 billion parameter, 32-layer, instruction tuned LLaMA 2 model. |
vicuna_1.5_7b_en | 6.74B | 7 billion parameter, 32-layer, instruction tuned Vicuna v1.5 model. |
llama2_7b_en_int8 | 6.74B | 7 billion parameter, 32-layer, base LLaMA 2 model with activation and weights quantized to int8. |
llama2_instruct_7b_en_int8 | 6.74B | 7 billion parameter, 32-layer, instruction tuned LLaMA 2 model with activation and weights quantized to int8. |
llama3.2_1b | 1.50B | 1 billion parameter, 16-layer, based LLaMA 3.2 model. |
llama3.2_instruct_1b | 1.50B | 1 billion parameter, 16-layer, instruction tuned LLaMA 3.2. |
llama3.2_guard_1b | 1.50B | 1 billion parameter, 16-layer, based LLaMA 3.2 model fine-tuned for consent safety classification. |
llama3.2_3b | 3.61B | 3 billion parameter, 26-layer, based LLaMA 3.2 model. |
llama3.2_instruct_3b | 3.61B | 3 billion parameter, 28-layer, instruction tuned LLaMA 3.2. |
llama3_8b_en | 8.03B | 8 billion parameter, 32-layer, base LLaMA 3 model. |
llama3_instruct_8b_en | 8.03B | 8 billion parameter, 32-layer, instruction tuned LLaMA 3 model. |
llama3.1_8b | 8.03B | 8 billion parameter, 32-layer, based LLaMA 3.1 model. |
llama3.1_instruct_8b | 8.03B | 8 billion parameter, 32-layer, instruction tuned LLaMA 3.1. |
llama3.1_guard_8b | 8.03B | 8 billion parameter, 32-layer, LLaMA 3.1 fine-tuned for consent safety classification. |
llama3_8b_en_int8 | 8.03B | 8 billion parameter, 32-layer, base LLaMA 3 model with activation and weights quantized to int8. |
llama3_instruct_8b_en_int8 | 8.03B | 8 billion parameter, 32-layer, instruction tuned LLaMA 3 model with activation and weights quantized to int8. |