4040
4141from maxtext .utils import model_creation_utils
4242from maxtext .utils import max_logging
43+ from maxtext .utils import lora_utils
4344from maxtext .utils .globals import MAXTEXT_CONFIGS_DIR
4445from maxtext .common .common_types import Config
4546from maxtext .common import profiler
@@ -94,11 +95,13 @@ def decode_with_vllm(config: Config) -> None:
9495 "additional_config" : {
9596 "maxtext_config" : {
9697 "model_name" : config .model_name ,
97- "weight_dtype" : "bfloat16" ,
98+ "weight_dtype" : config . weight_dtype ,
9899 "allow_split_physical_axes" : True ,
99100 "debug_sharding" : config .debug_sharding ,
100101 "prefuse_moe_weights" : config .prefuse_moe_weights ,
101102 "scan_layers" : config .scan_layers ,
103+ "enable_nnx" : config .enable_nnx ,
104+ "pure_nnx_decoder" : config .pure_nnx_decoder ,
102105 },
103106 "sharding" : {
104107 "sharding_strategy" : {
@@ -195,7 +198,18 @@ def decode_with_tunix(
195198 mesh: The JAX mesh for parallelism.
196199 """
197200 # Wrap the model for Tunix
198- tunix_model = TunixMaxTextAdapter (base_model = model )
201+ use_no_op_mappings = False
202+ if hasattr (config , "vllm_hf_overrides" ) and config .vllm_hf_overrides :
203+ overrides = config .vllm_hf_overrides
204+ if isinstance (overrides , str ) and "MaxTextForCausalLM" in overrides :
205+ use_no_op_mappings = True
206+ elif isinstance (overrides , dict ) and "MaxTextForCausalLM" in overrides .get ("architectures" , []):
207+ use_no_op_mappings = True
208+
209+ tunix_model = TunixMaxTextAdapter (
210+ base_model = model ,
211+ use_no_op_mappings = use_no_op_mappings ,
212+ )
199213
200214 # Load the tokenizer
201215 tokenizer = transformers .AutoTokenizer .from_pretrained (
@@ -223,13 +237,48 @@ def decode_with_tunix(
223237 f"max_target_length ({ config .max_target_length } ) must be greater than max_prompt_length ({ max_prompt_length } )"
224238 )
225239
240+ # MaxText uses -1 to mean "disabled"; vLLM requires top_p in (0, 1].
241+ top_p = config .decode_sampling_nucleus_p if config .decode_sampling_nucleus_p > 0 else 1.0
242+ top_k = config .decode_sampling_top_k if config .decode_sampling_top_k > 0 else - 1
243+
244+ rollout_vllm_additional_config = {
245+ "maxtext_config" : {
246+ "model_name" : config .model_name ,
247+ "weight_dtype" : config .weight_dtype ,
248+ "allow_split_physical_axes" : True ,
249+ "debug_sharding" : config .debug_sharding ,
250+ "prefuse_moe_weights" : config .prefuse_moe_weights ,
251+ "scan_layers" : config .scan_layers ,
252+ "enable_nnx" : config .enable_nnx ,
253+ "pure_nnx_decoder" : config .pure_nnx_decoder ,
254+ }
255+ }
256+
257+ if config .lora .enable_lora :
258+ rollout_vllm_additional_config ["maxtext_config" ]["lora" ] = {
259+ "enable_lora" : config .lora .enable_lora ,
260+ "lora_restore_path" : config .lora .lora_restore_path ,
261+ "lora_rank" : config .lora .lora_rank ,
262+ "lora_alpha" : config .lora .lora_alpha ,
263+ "lora_module_path" : config .lora .lora_module_path ,
264+ }
265+
226266 # Create vLLM rollout for inference
227267 rollout_config = base_rollout .RolloutConfig (
228268 max_tokens_to_generate = max_tokens_to_generate ,
229269 max_prompt_length = max_prompt_length ,
230270 temperature = config .decode_sampling_temperature ,
231- top_p = config .decode_sampling_nucleus_p ,
232- top_k = config .decode_sampling_top_k ,
271+ top_p = top_p ,
272+ top_k = top_k ,
273+ rollout_vllm_model_version = config .tokenizer_path ,
274+ rollout_vllm_hbm_utilization = config .hbm_utilization_vllm ,
275+ rollout_vllm_init_with_random_weights = True ,
276+ rollout_vllm_tpu_backend_type = "jax" ,
277+ tensor_parallel_size = config .ici_tensor_parallelism if config .ici_tensor_parallelism > 0 else 1 ,
278+ data_parallel_size = jax .device_count ()
279+ // (config .ici_tensor_parallelism if config .ici_tensor_parallelism > 0 else 1 ),
280+ rollout_vllm_additional_config = rollout_vllm_additional_config ,
281+ rollout_vllm_kwargs = {"hf_overrides" : config .vllm_hf_overrides },
233282 )
234283 vllm_rollout = VllmRollout (
235284 model = tunix_model ,
@@ -239,12 +288,7 @@ def decode_with_tunix(
239288 # other special formatting, which is not part of max_prompt_length.
240289 cache_config_or_size = max_prompt_length + max_tokens_to_generate + 256 ,
241290 mesh = mesh ,
242- model_version = config .tokenizer_path ,
243- hbm_utilization = 0.8 ,
244- # Initialize vllm model with random weights to speed up bootstrap time.
245- # Actual model weights will be loaded later.
246- init_with_random_weights = True ,
247- tpu_backend_type = "jax" ,
291+ rollout_config = rollout_config ,
248292 )
249293
250294 # Generate text
@@ -271,6 +315,10 @@ def main(argv: Sequence[str]) -> None:
271315
272316 if FLAGS .use_tunix :
273317 maxtext_model , mesh = model_creation_utils .from_pretrained (config )
318+ if config .lora .enable_lora :
319+ maxtext_model = lora_utils .apply_lora_to_model (maxtext_model , mesh , config )
320+ if config .lora .lora_restore_path :
321+ lora_utils .restore_lora_from_path (maxtext_model , config )
274322 decode_with_tunix (config , model = maxtext_model , mesh = mesh )
275323 else :
276324 decode_with_vllm (config )
0 commit comments