3636from absl import flags
3737from flax .linen import partitioning as nn_partitioning
3838import jax
39- import transformers
40-
41- from maxtext .utils import model_creation_utils
42- from maxtext .utils import max_logging
43- from maxtext .utils .globals import MAXTEXT_CONFIGS_DIR
44- from maxtext .common .common_types import Config
4539from maxtext .common import profiler
40+ from maxtext .common .common_types import Config
41+ from maxtext .configs import pyconfig
4642from maxtext .integration .tunix .tunix_adapter import TunixMaxTextAdapter
43+ import maxtext .integration .vllm .maxtext_vllm_adapter as adapter
44+ from maxtext .utils import lora_utils
45+ from maxtext .utils import max_logging
46+ from maxtext .utils import model_creation_utils
47+ from maxtext .utils .globals import MAXTEXT_CONFIGS_DIR
48+ import transformers
4749from tunix .rl .rollout import base_rollout
4850from tunix .rl .rollout .vllm_rollout import VllmRollout
4951from vllm import LLM
50- from vllm .sampling_params import SamplingParams
51- from maxtext .configs import pyconfig
52- import maxtext .integration .vllm .maxtext_vllm_adapter as adapter
53-
5452# Force uses_mrope to False to disable 3D multimodal position IDs in text-only runs.
5553# TODO(b/520142315): Class-level monkey patching is required here because ModelConfig.uses_mrope
5654# is evaluated during the internal initialization of the LLM engine, making instance-level
5755# configuration impossible. Check if a cleaner configuration option can be added in upstream vLLM.
5856from vllm .config import ModelConfig
57+ from vllm .sampling_params import SamplingParams
5958
6059ModelConfig .uses_mrope = property (lambda _ : False )
6160
@@ -94,11 +93,13 @@ def decode_with_vllm(config: Config) -> None:
9493 "additional_config" : {
9594 "maxtext_config" : {
9695 "model_name" : config .model_name ,
97- "weight_dtype" : "bfloat16" ,
96+ "weight_dtype" : config . weight_dtype ,
9897 "allow_split_physical_axes" : True ,
9998 "debug_sharding" : config .debug_sharding ,
10099 "prefuse_moe_weights" : config .prefuse_moe_weights ,
101100 "scan_layers" : config .scan_layers ,
101+ "enable_nnx" : config .enable_nnx ,
102+ "pure_nnx_decoder" : config .pure_nnx_decoder ,
102103 },
103104 "sharding" : {
104105 "sharding_strategy" : {
@@ -195,7 +196,20 @@ def decode_with_tunix(
195196 mesh: The JAX mesh for parallelism.
196197 """
197198 # Wrap the model for Tunix
198- tunix_model = TunixMaxTextAdapter (base_model = model )
199+ use_no_op_mappings = False
200+ if hasattr (config , "vllm_hf_overrides" ) and config .vllm_hf_overrides :
201+ overrides = config .vllm_hf_overrides
202+ if isinstance (overrides , str ) and "MaxTextForCausalLM" in overrides :
203+ use_no_op_mappings = True
204+ elif isinstance (overrides , dict ) and "MaxTextForCausalLM" in overrides .get (
205+ "architectures" , []
206+ ):
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,62 @@ 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 = (
242+ config .decode_sampling_nucleus_p
243+ if config .decode_sampling_nucleus_p > 0
244+ else 1.0
245+ )
246+ top_k = (
247+ config .decode_sampling_top_k if config .decode_sampling_top_k > 0 else - 1
248+ )
249+
250+ rollout_vllm_additional_config = {
251+ "maxtext_config" : {
252+ "model_name" : config .model_name ,
253+ "weight_dtype" : config .weight_dtype ,
254+ "allow_split_physical_axes" : True ,
255+ "debug_sharding" : config .debug_sharding ,
256+ "prefuse_moe_weights" : config .prefuse_moe_weights ,
257+ "scan_layers" : config .scan_layers ,
258+ "enable_nnx" : config .enable_nnx ,
259+ "pure_nnx_decoder" : config .pure_nnx_decoder ,
260+ }
261+ }
262+
263+ if config .lora .enable_lora :
264+ rollout_vllm_additional_config ["maxtext_config" ]["lora" ] = {
265+ "enable_lora" : config .lora .enable_lora ,
266+ "lora_restore_path" : config .lora .lora_restore_path ,
267+ "lora_rank" : config .lora .lora_rank ,
268+ "lora_alpha" : config .lora .lora_alpha ,
269+ "lora_module_path" : config .lora .lora_module_path ,
270+ }
271+
226272 # Create vLLM rollout for inference
227273 rollout_config = base_rollout .RolloutConfig (
228274 max_tokens_to_generate = max_tokens_to_generate ,
229275 max_prompt_length = max_prompt_length ,
230276 temperature = config .decode_sampling_temperature ,
231- top_p = config .decode_sampling_nucleus_p ,
232- top_k = config .decode_sampling_top_k ,
277+ top_p = top_p ,
278+ top_k = top_k ,
279+ rollout_vllm_model_version = config .tokenizer_path ,
280+ rollout_vllm_hbm_utilization = config .hbm_utilization_vllm ,
281+ rollout_vllm_init_with_random_weights = True ,
282+ rollout_vllm_tpu_backend_type = "jax" ,
283+ tensor_parallel_size = (
284+ config .ici_tensor_parallelism
285+ if config .ici_tensor_parallelism > 0
286+ else 1
287+ ),
288+ data_parallel_size = jax .device_count ()
289+ // (
290+ config .ici_tensor_parallelism
291+ if config .ici_tensor_parallelism > 0
292+ else 1
293+ ),
294+ rollout_vllm_additional_config = rollout_vllm_additional_config ,
295+ rollout_vllm_kwargs = {"hf_overrides" : config .vllm_hf_overrides },
233296 )
234297 vllm_rollout = VllmRollout (
235298 model = tunix_model ,
@@ -239,12 +302,7 @@ def decode_with_tunix(
239302 # other special formatting, which is not part of max_prompt_length.
240303 cache_config_or_size = max_prompt_length + max_tokens_to_generate + 256 ,
241304 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" ,
305+ rollout_config = rollout_config ,
248306 )
249307
250308 # Generate text
@@ -271,6 +329,12 @@ def main(argv: Sequence[str]) -> None:
271329
272330 if FLAGS .use_tunix :
273331 maxtext_model , mesh = model_creation_utils .from_pretrained (config )
332+ if config .lora .enable_lora :
333+ maxtext_model = lora_utils .apply_lora_to_model (
334+ maxtext_model , mesh , config
335+ )
336+ if config .lora .lora_restore_path :
337+ lora_utils .restore_lora_from_path (maxtext_model , config )
274338 decode_with_tunix (config , model = maxtext_model , mesh = mesh )
275339 else :
276340 decode_with_vllm (config )
0 commit comments