|
6 | 6 | from typing import TypeVar |
7 | 7 |
|
8 | 8 | import torch |
| 9 | +import torchax |
9 | 10 | from torch import nn |
10 | 11 |
|
11 | 12 | from vllm.config import VllmConfig |
@@ -481,101 +482,102 @@ def create_dummy_lora( |
481 | 482 | ) -> LoRAModel: |
482 | 483 | """Create zero-initialized LoRAModel for warmup.""" |
483 | 484 | model = LoRAModel(lora_id, rank, {}) |
484 | | - for module_name, module in self.model.named_modules(): |
485 | | - if ( |
486 | | - not self._match_target_modules(module_name) |
487 | | - or not isinstance(module, BaseLayerWithLoRA) |
488 | | - or self._get_punica_wrapper(module_name) is None |
489 | | - ): |
490 | | - continue |
491 | | - parts = module_name.split(".") |
492 | | - if module_name not in self.packed_modules: |
493 | | - assert embedding_modules is not None |
494 | | - if parts[-1] in embedding_modules: |
495 | | - # Special-case lm_head: wrapped by LogitsProcessorWithLoRA. |
496 | | - # LoRA input dim is hidden_size, output dim is vocab size. |
497 | | - # LogitsProcessorWithLoRA handles extra vocab size directly. |
498 | | - if parts[-1] == "lm_head": |
499 | | - input_dim = module.lora_a_stacked[0].shape[-1] |
500 | | - output_dim = module.lora_b_stacked[0].shape[-2] |
501 | | - else: |
502 | | - input_dim = ( |
503 | | - module.base_layer.org_vocab_size |
504 | | - if hasattr(module.base_layer, "org_vocab_size") |
505 | | - else module.base_layer.weight.shape[1] |
| 485 | + with torchax.default_env(): |
| 486 | + for module_name, module in self.model.named_modules(): |
| 487 | + if ( |
| 488 | + not self._match_target_modules(module_name) |
| 489 | + or not isinstance(module, BaseLayerWithLoRA) |
| 490 | + or self._get_punica_wrapper(module_name) is None |
| 491 | + ): |
| 492 | + continue |
| 493 | + parts = module_name.split(".") |
| 494 | + if module_name not in self.packed_modules: |
| 495 | + assert embedding_modules is not None |
| 496 | + if parts[-1] in embedding_modules: |
| 497 | + # Special-case lm_head: wrapped by LogitsProcessorWithLoRA. |
| 498 | + # LoRA input dim is hidden_size, output dim is vocab size. |
| 499 | + # LogitsProcessorWithLoRA handles extra vocab size directly. |
| 500 | + if parts[-1] == "lm_head": |
| 501 | + input_dim = module.lora_a_stacked[0].shape[-1] |
| 502 | + output_dim = module.lora_b_stacked[0].shape[-2] |
| 503 | + else: |
| 504 | + input_dim = ( |
| 505 | + module.base_layer.org_vocab_size |
| 506 | + if hasattr(module.base_layer, "org_vocab_size") |
| 507 | + else module.base_layer.weight.shape[1] |
| 508 | + ) |
| 509 | + output_dim = ( |
| 510 | + module.base_layer.embedding_dim |
| 511 | + if hasattr(module.base_layer, "embedding_dim") |
| 512 | + else module.base_layer.weight.shape[0] |
| 513 | + ) |
| 514 | + lora = LoRALayerWeights.create_dummy_lora_weights( |
| 515 | + module_name, |
| 516 | + input_dim, |
| 517 | + output_dim, |
| 518 | + rank, |
| 519 | + module.lora_a_stacked[0].dtype, |
| 520 | + "cpu", |
506 | 521 | ) |
507 | | - output_dim = ( |
508 | | - module.base_layer.embedding_dim |
509 | | - if hasattr(module.base_layer, "embedding_dim") |
510 | | - else module.base_layer.weight.shape[0] |
| 522 | + model.loras[module_name] = lora |
| 523 | + elif module.__class__.__name__ == "FusedMoE3DWithLoRA": |
| 524 | + # Case for 3D moe model |
| 525 | + # w2 |
| 526 | + lora = LoRALayerWeights.create_dummy_lora_weights( |
| 527 | + module_name, |
| 528 | + module.w2_input_size, |
| 529 | + module.w2_output_size, |
| 530 | + rank * module.w2_lora_a_stacked[0].shape[1], # rank*num_experts |
| 531 | + module.w2_lora_a_stacked[0].dtype, |
| 532 | + "cpu", |
511 | 533 | ) |
512 | | - lora = LoRALayerWeights.create_dummy_lora_weights( |
513 | | - module_name, |
514 | | - input_dim, |
515 | | - output_dim, |
516 | | - rank, |
517 | | - module.lora_a_stacked[0].dtype, |
518 | | - "cpu", |
519 | | - ) |
520 | | - model.loras[module_name] = lora |
521 | | - elif module.__class__.__name__ == "FusedMoE3DWithLoRA": |
522 | | - # Case for 3D moe model |
523 | | - # w2 |
524 | | - lora = LoRALayerWeights.create_dummy_lora_weights( |
525 | | - module_name, |
526 | | - module.w2_input_size, |
527 | | - module.w2_output_size, |
528 | | - rank * module.w2_lora_a_stacked[0].shape[1], # rank*num_experts |
529 | | - module.w2_lora_a_stacked[0].dtype, |
530 | | - "cpu", |
531 | | - ) |
532 | | - model.loras[module_name] = lora |
533 | | - # w13 |
534 | | - lora = LoRALayerWeights.create_dummy_lora_weights( |
535 | | - module_name, |
536 | | - module.w13_input_size, |
537 | | - module.w13_output_size, |
538 | | - rank |
539 | | - * module.w13_lora_a_stacked[0].shape[1], # rank*num_experts |
540 | | - module.w13_lora_a_stacked[0].dtype, |
541 | | - "cpu", |
542 | | - ) |
543 | | - model.loras[module_name + ".base_layer"] = lora |
| 534 | + model.loras[module_name] = lora |
| 535 | + # w13 |
| 536 | + lora = LoRALayerWeights.create_dummy_lora_weights( |
| 537 | + module_name, |
| 538 | + module.w13_input_size, |
| 539 | + module.w13_output_size, |
| 540 | + rank |
| 541 | + * module.w13_lora_a_stacked[0].shape[1], # rank*num_experts |
| 542 | + module.w13_lora_a_stacked[0].dtype, |
| 543 | + "cpu", |
| 544 | + ) |
| 545 | + model.loras[module_name + ".base_layer"] = lora |
| 546 | + else: |
| 547 | + lora = LoRALayerWeights.create_dummy_lora_weights( |
| 548 | + module_name, |
| 549 | + module.lora_a_stacked[0].shape[-1], |
| 550 | + module.lora_b_stacked[0].shape[-2], |
| 551 | + rank, |
| 552 | + module.lora_a_stacked[0].dtype, |
| 553 | + "cpu", |
| 554 | + ) |
| 555 | + model.loras[module_name] = lora |
544 | 556 | else: |
545 | | - lora = LoRALayerWeights.create_dummy_lora_weights( |
546 | | - module_name, |
547 | | - module.lora_a_stacked[0].shape[-1], |
548 | | - module.lora_b_stacked[0].shape[-2], |
549 | | - rank, |
550 | | - module.lora_a_stacked[0].dtype, |
551 | | - "cpu", |
552 | | - ) |
| 557 | + parts = module_name.split(".") |
| 558 | + replacements = self.packed_modules_mapping[parts[-1]] |
| 559 | + subloras: list[LoRALayerWeights | None] = [] |
| 560 | + for i, r in enumerate(replacements): |
| 561 | + lora = LoRALayerWeights.create_dummy_lora_weights( |
| 562 | + module_name + "." + r, |
| 563 | + module.lora_a_stacked[i].shape[-1], |
| 564 | + module.lora_b_stacked[i].shape[-2], |
| 565 | + rank, |
| 566 | + module.lora_a_stacked[i].dtype, |
| 567 | + "cpu", |
| 568 | + ) |
| 569 | + subloras.append(lora) |
| 570 | + if module.__class__.__name__ == "FusedMoEWithLoRA": |
| 571 | + # For non-gated MoE, pad subloras to 3 elements per expert |
| 572 | + # to match pack_moe expectations (w1, w2, None for w3) |
| 573 | + if self._is_non_gated_moe and len(subloras) > 0: |
| 574 | + subloras = self._pad_lora_pairs_to_triplets(subloras) |
| 575 | + lora = PackedLoRALayerWeights.pack_moe( |
| 576 | + subloras, module_name, is_non_gated_moe=self._is_non_gated_moe |
| 577 | + ) |
| 578 | + else: |
| 579 | + lora = PackedLoRALayerWeights.pack(subloras) |
553 | 580 | model.loras[module_name] = lora |
554 | | - else: |
555 | | - parts = module_name.split(".") |
556 | | - replacements = self.packed_modules_mapping[parts[-1]] |
557 | | - subloras: list[LoRALayerWeights | None] = [] |
558 | | - for i, r in enumerate(replacements): |
559 | | - lora = LoRALayerWeights.create_dummy_lora_weights( |
560 | | - module_name + "." + r, |
561 | | - module.lora_a_stacked[i].shape[-1], |
562 | | - module.lora_b_stacked[i].shape[-2], |
563 | | - rank, |
564 | | - module.lora_a_stacked[i].dtype, |
565 | | - "cpu", |
566 | | - ) |
567 | | - subloras.append(lora) |
568 | | - if module.__class__.__name__ == "FusedMoEWithLoRA": |
569 | | - # For non-gated MoE, pad subloras to 3 elements per expert |
570 | | - # to match pack_moe expectations (w1, w2, None for w3) |
571 | | - if self._is_non_gated_moe and len(subloras) > 0: |
572 | | - subloras = self._pad_lora_pairs_to_triplets(subloras) |
573 | | - lora = PackedLoRALayerWeights.pack_moe( |
574 | | - subloras, module_name, is_non_gated_moe=self._is_non_gated_moe |
575 | | - ) |
576 | | - else: |
577 | | - lora = PackedLoRALayerWeights.pack(subloras) |
578 | | - model.loras[module_name] = lora |
579 | 581 | return model |
580 | 582 |
|
581 | 583 | def _match_target_modules(self, module_name: str) -> bool: |
|
0 commit comments