Decode using vLLM with a MaxText model.
(
config: Config,
model: Any,
mesh: jax.sharding.Mesh,
)
| 186 | |
| 187 | |
| 188 | def decode_with_tunix( |
| 189 | config: Config, |
| 190 | model: Any, |
| 191 | mesh: jax.sharding.Mesh, |
| 192 | ) -> None: |
| 193 | """Decode using vLLM with a MaxText model.""" |
| 194 | # Wrap the model for Tunix |
| 195 | tunix_model = TunixMaxTextAdapter(base_model=model) |
| 196 | |
| 197 | # Load the tokenizer |
| 198 | tokenizer = transformers.AutoTokenizer.from_pretrained( |
| 199 | config.tokenizer_path, |
| 200 | token=config.hf_access_token, |
| 201 | model_max_length=config.max_target_length, |
| 202 | ) |
| 203 | tokenizer.bos_token = None |
| 204 | |
| 205 | prompts = [config.prompt] |
| 206 | if config.use_chat_template: |
| 207 | # Format the prompt using chat template if specified |
| 208 | messages = [ |
| 209 | {"role": "user", "content": config.prompt}, |
| 210 | ] |
| 211 | input_with_chat_template = tokenizer.apply_chat_template( |
| 212 | messages, |
| 213 | tokenize=False, # Set to False to get the string |
| 214 | add_generation_prompt=True, |
| 215 | add_special_tokens=False, # Prevent adding special tokens |
| 216 | ) |
| 217 | prompts = [input_with_chat_template] |
| 218 | |
| 219 | max_prompt_length = max(len(tokenizer.encode(p)) for p in prompts) |
| 220 | max_tokens_to_generate = config.max_target_length - max_prompt_length |
| 221 | |
| 222 | # Create vLLM rollout for inference |
| 223 | rollout_config = base_rollout.RolloutConfig( |
| 224 | max_tokens_to_generate=max_tokens_to_generate, |
| 225 | max_prompt_length=max_prompt_length, |
| 226 | temperature=config.decode_sampling_temperature, |
| 227 | top_p=config.decode_sampling_nucleus_p, |
| 228 | top_k=config.decode_sampling_top_k, |
| 229 | ) |
| 230 | vllm_rollout = VllmRollout( |
| 231 | model=tunix_model, |
| 232 | tokenizer=tokenizer, |
| 233 | # The cache_config_or_size sets the absolute maximum sequence length. |
| 234 | # We add 256 as a safety buffer to account for tokens added by |
| 235 | # other special formatting, which is not part of max_prompt_length. |
| 236 | cache_config_or_size=max_prompt_length + max_tokens_to_generate + 256, |
| 237 | mesh=mesh, |
| 238 | model_version=config.tokenizer_path, |
| 239 | hbm_utilization=0.8, |
| 240 | # Initialize vllm model with random weights to speed up bootstrap time. |
| 241 | # Actual model weights will be loaded later. |
| 242 | init_with_random_weights=True, |
| 243 | tpu_backend_type="jax", |
| 244 | ) |
| 245 |
no test coverage detected