Qwen3CausalLMPreprocessor
classkeras_hub.models.Qwen3CausalLMPreprocessor(
tokenizer, sequence_length=1024, add_start_token=True, add_end_token=True, **kwargs
)
Base class for causal language modeling preprocessing layers.
CausalLMPreprocessor
tasks wrap a keras_hub.tokenizer.Tokenizer
to
create a preprocessing layer for causal language modeling tasks. It is
intended to be paired with a keras.models.CausalLM
task.
All CausalLMPreprocessor
take inputs a single input. This can be a single
string or a batch of strings. See examples below. These inputs
will be tokenized and padded/truncated to a fixed sequence length.
This layer will always output a (x, y, sample_weight)
tuple, where x
is a dictionary with the tokenized inputs, y
contains the tokens from x
offset by 1, and sample_weight
marks where y
contains padded
values. The exact contents of x
will vary depending on the model being
used.
a CausalLMPreprocessor
contains two extra methods, generate_preprocess
and generate_postprocess
for use with generation. See examples below.
All CausalLMPreprocessor
tasks include a from_preset()
constructor
which can be used to load a pre-trained config and vocabularies. You can
call the from_preset()
constructor directly on this base class, in which
case the correct class for you model will be automatically instantiated.
Examples.
preprocessor = keras_hub.models.CausalLMPreprocessor.from_preset(
"bert_base_en_uncased",
sequence_length=256, # Optional.
)
# Tokenize, mask and pack a single sentence.
x = "The quick brown fox jumped."
x, y, sample_weight = preprocessor(x)
# Tokenize and pad/truncate a batch of labeled sentences.
x = ["The quick brown fox jumped.", "Call me Ishmael."]
x, y, sample_weight = preprocessor(x)
# With a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset).
ds = tf.data.Dataset.from_tensor_slices(x)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
# Generate preprocess and postprocess.
x = preprocessor.generate_preprocess(x) # Tokenized numeric inputs.
x = preprocessor.generate_postprocess(x) # Detokenized string outputs.
from_preset
methodQwen3CausalLMPreprocessor.from_preset(
preset, config_file="preprocessor.json", **kwargs
)
Instantiate a keras_hub.models.Preprocessor
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 Preprocessor
subclass, you can run cls.presets.keys()
to
list all built-in presets available on the class.
As there are usually multiple preprocessing classes for a given model,
this method should be called on a specific subclass like
keras_hub.models.BertTextClassifierPreprocessor.from_preset()
.
Arguments
Examples
# Load a preprocessor for Gemma generation.
preprocessor = keras_hub.models.CausalLMPreprocessor.from_preset(
"gemma_2b_en",
)
# Load a preprocessor for Bert classification.
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
"bert_base_en",
)
Preset | Parameters | Description |
---|---|---|
qwen3_0.6b_en | 596.05M | 28-layer Qwen3 model with 596M parameters, optimized for efficiency and fast inference on resource-constrained devices. |
qwen3_1.7b_en | 1.72B | 28-layer Qwen3 model with 1.72B parameters, offering a good balance between performance and resource usage. |
qwen3_4b_en | 4.02B | 36-layer Qwen3 model with 4.02B parameters, offering improved reasoning capabilities and better performance than smaller variants. |
qwen3_8b_en | 8.19B | 36-layer Qwen3 model with 8.19B parameters, featuring enhanced reasoning, coding, and instruction-following capabilities. |
qwen3_14b_en | 14.77B | 40-layer Qwen3 model with 14.77B parameters, featuring advanced reasoning, coding, and multilingual capabilities. |
qwen3_32b_en | 32.76B | 64-layer Qwen3 model with 32.76B parameters, featuring state-of-the-art performance across reasoning, coding, and general language tasks. |
tokenizer
propertykeras_hub.models.Qwen3CausalLMPreprocessor.tokenizer
The tokenizer used to tokenize strings.