|
| 1 | +<!--Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +
|
| 12 | +⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 | +rendered properly in your Markdown viewer. |
| 14 | +
|
| 15 | +--> |
| 16 | + |
| 17 | +<div style="float: right;"> |
| 18 | + <div class="flex flex-wrap space-x-1"> |
| 19 | + <img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 20 | + <img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat"> |
| 21 | + <img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 22 | + <img alt="Tensor parallelism" src="https://img.shields.io/badge/Tensor%20parallelism-06b6d4?style=flat&logoColor=white"> |
| 23 | + </div> |
| 24 | +</div> |
| 25 | + |
| 26 | +# Ernie 4.5 MoE |
| 27 | + |
| 28 | +## Overview |
| 29 | + |
| 30 | +The Ernie 4.5 MoE model was released in the [Ernie 4.5 Model Family](https://ernie.baidu.com/blog/posts/ernie4.5/) release by baidu. |
| 31 | +This family of models contains multiple different architectures and model sizes. This model in specific targets the base text |
| 32 | +model with mixture of experts (moe) - one with 21B total, 3B active parameters and another one with 300B total, 47B active parameters. |
| 33 | +It uses the standard [Llama](./llama.md) at its core combined with a specialized MoE based on [Mixtral](./mixtral.md) with additional shared |
| 34 | +experts. |
| 35 | + |
| 36 | +Other models from the family can be found at [Ernie 4.5](./ernie4_5.md). |
| 37 | + |
| 38 | +<div class="flex justify-center"> |
| 39 | + <img src="https://ernie.baidu.com/blog/posts/ernie4.5/overview.png"/> |
| 40 | +</div> |
| 41 | + |
| 42 | + |
| 43 | +## Usage Tips |
| 44 | + |
| 45 | +### Generate text |
| 46 | + |
| 47 | +```python |
| 48 | +import torch |
| 49 | +from transformers import AutoModelForCausalLM, AutoTokenizer |
| 50 | + |
| 51 | +model_name = "baidu/ERNIE-4.5-21B-A3B-PT" |
| 52 | + |
| 53 | +# load the tokenizer and the model |
| 54 | +tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 55 | +model = AutoModelForCausalLM.from_pretrained( |
| 56 | + model_name, |
| 57 | + device_map="auto", |
| 58 | + torch_dtype=torch.bfloat16, |
| 59 | +) |
| 60 | + |
| 61 | +# prepare the model input |
| 62 | +inputs = tokenizer("Hey, are you conscious? Can you talk to me?", return_tensors="pt") |
| 63 | +prompt = "Hey, are you conscious? Can you talk to me?" |
| 64 | +messages = [ |
| 65 | + {"role": "user", "content": prompt} |
| 66 | +] |
| 67 | +text = tokenizer.apply_chat_template( |
| 68 | + messages, |
| 69 | + tokenize=False, |
| 70 | + add_generation_prompt=True |
| 71 | +) |
| 72 | +model_inputs = tokenizer([text], add_special_tokens=False, return_tensors="pt").to(model.device) |
| 73 | + |
| 74 | +# conduct text completion |
| 75 | +generated_ids = model.generate( |
| 76 | + **model_inputs, |
| 77 | + max_new_tokens=32, |
| 78 | +) |
| 79 | +output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() |
| 80 | + |
| 81 | +# decode the generated ids |
| 82 | +generate_text = tokenizer.decode(output_ids, skip_special_tokens=True) |
| 83 | +``` |
| 84 | + |
| 85 | +### Distributed Generation with Tensor Parallelism |
| 86 | + |
| 87 | +```python |
| 88 | +import torch |
| 89 | +from transformers import AutoModelForCausalLM, AutoTokenizer |
| 90 | + |
| 91 | +model_name = "baidu/ERNIE-4.5-21B-A3B-PT" |
| 92 | + |
| 93 | +# load the tokenizer and the model |
| 94 | +tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 95 | +model = AutoModelForCausalLM.from_pretrained( |
| 96 | + model_name, |
| 97 | + device_map="auto", |
| 98 | + torch_dtype=torch.bfloat16, |
| 99 | + tp_plan="auto", |
| 100 | +) |
| 101 | + |
| 102 | +# prepare the model input |
| 103 | +inputs = tokenizer("Hey, are you conscious? Can you talk to me?", return_tensors="pt") |
| 104 | +prompt = "Hey, are you conscious? Can you talk to me?" |
| 105 | +messages = [ |
| 106 | + {"role": "user", "content": prompt} |
| 107 | +] |
| 108 | +text = tokenizer.apply_chat_template( |
| 109 | + messages, |
| 110 | + tokenize=False, |
| 111 | + add_generation_prompt=True |
| 112 | +) |
| 113 | +model_inputs = tokenizer([text], add_special_tokens=False, return_tensors="pt").to(model.device) |
| 114 | + |
| 115 | +# conduct text completion |
| 116 | +generated_ids = model.generate( |
| 117 | + **model_inputs, |
| 118 | + max_new_tokens=32, |
| 119 | +) |
| 120 | +output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() |
| 121 | + |
| 122 | +# decode the generated ids |
| 123 | +generate_text = tokenizer.decode(output_ids, skip_special_tokens=True) |
| 124 | +``` |
| 125 | + |
| 126 | +### Quantization with Bitsandbytes |
| 127 | + |
| 128 | +```python |
| 129 | +import torch |
| 130 | +from transformers import BitsAndBytesConfig, AutoModelForCausalLM, AutoTokenizer |
| 131 | + |
| 132 | +model_name = "baidu/ERNIE-4.5-21B-A3B-PT" |
| 133 | + |
| 134 | +# load the tokenizer and the model |
| 135 | +tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 136 | +model = AutoModelForCausalLM.from_pretrained( |
| 137 | + model_name, |
| 138 | + device_map="auto", |
| 139 | + quantization_config=BitsAndBytesConfig(load_in_4bit=True), |
| 140 | +) |
| 141 | + |
| 142 | +# prepare the model input |
| 143 | +inputs = tokenizer("Hey, are you conscious? Can you talk to me?", return_tensors="pt") |
| 144 | +prompt = "Hey, are you conscious? Can you talk to me?" |
| 145 | +messages = [ |
| 146 | + {"role": "user", "content": prompt} |
| 147 | +] |
| 148 | +text = tokenizer.apply_chat_template( |
| 149 | + messages, |
| 150 | + tokenize=False, |
| 151 | + add_generation_prompt=True |
| 152 | +) |
| 153 | +model_inputs = tokenizer([text], add_special_tokens=False, return_tensors="pt").to(model.device) |
| 154 | + |
| 155 | +# conduct text completion |
| 156 | +generated_ids = model.generate( |
| 157 | + **model_inputs, |
| 158 | + max_new_tokens=32, |
| 159 | +) |
| 160 | +output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() |
| 161 | + |
| 162 | +# decode the generated ids |
| 163 | +generate_text = tokenizer.decode(output_ids, skip_special_tokens=True) |
| 164 | +``` |
| 165 | + |
| 166 | +This model was contributed by [Anton Vlasjuk](https://huggingface.co/AntonV). |
| 167 | +The original code can be found [here](https://github.com/PaddlePaddle/ERNIE). |
| 168 | + |
| 169 | + |
| 170 | +## Ernie4_5_MoEConfig |
| 171 | + |
| 172 | +[[autodoc]] Ernie4_5_MoEConfig |
| 173 | + |
| 174 | +## Ernie4_5_MoEModel |
| 175 | + |
| 176 | +[[autodoc]] Ernie4_5_MoEModel |
| 177 | + - forward |
| 178 | + |
| 179 | +## Ernie4_5_MoEForCausalLM |
| 180 | + |
| 181 | +[[autodoc]] Ernie4_5_MoEForCausalLM |
| 182 | + - forward |
| 183 | + - generate |
0 commit comments