|
17 | 17 | ]
|
18 | 18 | )
|
19 | 19 | class QwenCausalLM(CausalLM):
|
| 20 | + """An end-to-end Qwen model for causal language modeling. |
| 21 | +
|
| 22 | + A causal language model (LM) predicts the next token based on previous |
| 23 | + tokens. This task setup can be used to train the model unsupervised on plain |
| 24 | + text input, or to autoregressively generate plain text similar to the data |
| 25 | + used for training. This task can be used for pre-training or fine-tuning a |
| 26 | + Qwen model, simply by calling `fit()`. |
| 27 | +
|
| 28 | + This model has a `generate()` method, which generates text based on a |
| 29 | + prompt. The generation strategy used is controlled by an additional |
| 30 | + `sampler` argument on `compile()`. You can recompile the model with |
| 31 | + different `keras_hub.samplers` objects to control the generation. |
| 32 | + By default, `"greedy"` sampling will be used. |
| 33 | +
|
| 34 | + This model can optionally be configured with a `preprocessor` layer, in |
| 35 | + which case it will automatically apply preprocessing to string inputs during |
| 36 | + `fit()`, `predict()`, `evaluate()`, and `generate()`. This is done by |
| 37 | + default when creating the model with `from_preset()`. |
| 38 | +
|
| 39 | + Args: |
| 40 | + backbone: A `keras_hub.models.QwenBackbone` instance. |
| 41 | + preprocessor: A `keras_hub.models.QwenCausalLMPreprocessor` or |
| 42 | + `None`. If `None`, this model will not apply preprocessing, and |
| 43 | + inputs should be preprocessed before calling the model. |
| 44 | +
|
| 45 | + Examples: |
| 46 | +
|
| 47 | + Use `generate()` to do text generation. |
| 48 | + ```python |
| 49 | + qwen_lm = keras_hub.models.QwenCausalLM.from_preset("qwen2.5_0.5b_en") |
| 50 | + qwen_lm.generate("I want to say", max_length=30) |
| 51 | +
|
| 52 | + # Generate with batched prompts. |
| 53 | + qwen_lm.generate(["This is a", "Where are you"], max_length=30) |
| 54 | + ``` |
| 55 | +
|
| 56 | + Compile the `generate()` function with a custom sampler. |
| 57 | + ```python |
| 58 | + qwen_lm = keras_hub.models.QwenMoeCausalLM.from_preset("qwen2.5_0.5b_en") |
| 59 | + qwen_lm.compile(sampler="top_k") |
| 60 | + qwen_lm.generate("I want to say", max_length=30) |
| 61 | +
|
| 62 | + qwen_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2)) |
| 63 | + qwen_lm.generate("I want to say", max_length=30) |
| 64 | + ``` |
| 65 | +
|
| 66 | + Use `generate()` without preprocessing. |
| 67 | + ```python |
| 68 | + prompt = { |
| 69 | + # Token ids for "<bos> Qwen is". |
| 70 | + "token_ids": np.array([[2, 12345, 678, 0, 0, 0, 0]] * 2), |
| 71 | + # Use `"padding_mask"` to indicate values that should not be overridden. |
| 72 | + "padding_mask": np.array([[1, 1, 1, 0, 0, 0, 0]] * 2), |
| 73 | + } |
| 74 | +
|
| 75 | + qwen_lm = keras_hub.models.QwenMoeCausalLM.from_preset( |
| 76 | + "qwen2.5_0.5b_en", |
| 77 | + preprocessor=None, |
| 78 | + ) |
| 79 | + qwen_lm.generate(prompt) |
| 80 | + ``` |
| 81 | +
|
| 82 | + Call `fit()` on a single batch. |
| 83 | + ```python |
| 84 | + features = ["The quick brown fox jumped.", "I forgot my homework."] |
| 85 | + qwen_lm = keras_hub.models.QwenMoeCausalLM.from_preset("qwen2.5_0.5b_en") |
| 86 | + qwen_lm.fit(x=features, batch_size=2) |
| 87 | + ``` |
| 88 | +
|
| 89 | + Call `fit()` with LoRA fine-tuning enabled. |
| 90 | + ```python |
| 91 | + features = ["The quick brown fox jumped.", "I forgot my homework."] |
| 92 | + qwen_lm = keras_hub.models.QwenMoeCausalLM.from_preset("qwen2.5_0.5b_en") |
| 93 | + qwen_lm.backbone.enable_lora(rank=4) |
| 94 | + qwen_lm.fit(x=features, batch_size=2) |
| 95 | + ``` |
| 96 | +
|
| 97 | + Call `fit()` without preprocessing. |
| 98 | + ```python |
| 99 | + x = { |
| 100 | + # Token ids for "<bos> Qwen is a language model<eos>" |
| 101 | + "token_ids": np.array([[2, 12345, 678, 543, 9876, 1, 0, 0]] * 2), |
| 102 | + "padding_mask": np.array([[1, 1, 1, 1, 1, 1, 0, 0]] * 2), |
| 103 | + } |
| 104 | + y = np.array([[12345, 678, 543, 9876, 1, 0, 0, 0]] * 2) |
| 105 | + sw = np.array([[1, 1, 1, 1, 1, 0, 0, 0]] * 2) |
| 106 | +
|
| 107 | + qwen_lm = keras_hub.models.QwenMoeCausalLM.from_preset( |
| 108 | + "qwen2.5_0.5b_en", |
| 109 | + preprocessor=None, |
| 110 | + ) |
| 111 | + qwen_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2) |
| 112 | + ``` |
| 113 | +
|
| 114 | + Custom backbone and vocabulary. |
| 115 | + ```python |
| 116 | + tokenizer = keras_hub.models.QwenMoeTokenizer( |
| 117 | + proto="qwen_moe_vocab.spm", |
| 118 | + ) |
| 119 | + preprocessor = keras_hub.models.QwenMoeCausalLMPreprocessor( |
| 120 | + tokenizer=tokenizer, |
| 121 | + sequence_length=128, |
| 122 | + ) |
| 123 | + backbone = keras_hub.models.QwenMoeBackbone( |
| 124 | + vocabulary_size=151936, |
| 125 | + num_layers=28, |
| 126 | + num_query_heads=16, |
| 127 | + num_key_value_heads=8, |
| 128 | + hidden_dim=2048, |
| 129 | + intermediate_dim=4096, |
| 130 | + moe_intermediate_dim=128, |
| 131 | + shared_expert_intermediate_dim=4096, |
| 132 | + num_experts=60, |
| 133 | + top_k=4, |
| 134 | + max_sequence_length=4096, |
| 135 | + ) |
| 136 | + qwen_lm = keras_hub.models.QwenMoeCausalLM( |
| 137 | + backbone=backbone, |
| 138 | + preprocessor=preprocessor, |
| 139 | + ) |
| 140 | + qwen_lm.fit(x=features, batch_size=2) |
| 141 | + ``` |
| 142 | + """ |
| 143 | + |
20 | 144 | backbone_cls = QwenBackbone
|
21 | 145 | preprocessor_cls = QwenCausalLMPreprocessor
|
22 | 146 |
|
|
0 commit comments