MCPcopy Create free account
hub / github.com/WangHanLinHenry/SPA-RL-Agent / load_model

Method load_model

fastchat/model/model_adapter.py:559–606  ·  view source on GitHub ↗

Loads the base model then the (peft) adapter weights

(self, model_path: str, from_pretrained_kwargs: dict)

Source from the content-addressed store, hash-verified

557 return "peft" in model_path.lower()
558
559 def load_model(self, model_path: str, from_pretrained_kwargs: dict):
560 """Loads the base model then the (peft) adapter weights"""
561 from peft import PeftConfig, PeftModel
562
563 config = PeftConfig.from_pretrained(model_path)
564 base_model_path = config.base_model_name_or_path
565 if "peft" in base_model_path:
566 raise ValueError(
567 f"PeftModelAdapter cannot load a base model with 'peft' in the name: {config.base_model_name_or_path}"
568 )
569
570 # Basic proof of concept for loading peft adapters that share the base
571 # weights. This is pretty messy because Peft re-writes the underlying
572 # base model and internally stores a map of adapter layers.
573 # So, to make this work we:
574 # 1. Cache the first peft model loaded for a given base models.
575 # 2. Call `load_model` for any follow on Peft models.
576 # 3. Make sure we load the adapters by the model_path. Why? This is
577 # what's accessible during inference time.
578 # 4. In get_generate_stream_function, make sure we load the right
579 # adapter before doing inference. This *should* be safe when calls
580 # are blocked the same semaphore.
581 if peft_share_base_weights:
582 if base_model_path in peft_model_cache:
583 model, tokenizer = peft_model_cache[base_model_path]
584 # Super important: make sure we use model_path as the
585 # `adapter_name`.
586 model.load_adapter(model_path, adapter_name=model_path)
587 else:
588 base_adapter = get_model_adapter(base_model_path)
589 base_model, tokenizer = base_adapter.load_model(
590 base_model_path, from_pretrained_kwargs
591 )
592 # Super important: make sure we use model_path as the
593 # `adapter_name`.
594 model = PeftModel.from_pretrained(
595 base_model, model_path, adapter_name=model_path
596 )
597 peft_model_cache[base_model_path] = (model, tokenizer)
598 return model, tokenizer
599
600 # In the normal case, load up the base model weights again.
601 base_adapter = get_model_adapter(base_model_path)
602 base_model, tokenizer = base_adapter.load_model(
603 base_model_path, from_pretrained_kwargs
604 )
605 model = PeftModel.from_pretrained(base_model, model_path)
606 return model, tokenizer
607
608 def get_default_conv_template(self, model_path: str) -> Conversation:
609 """Uses the conv template of the base model"""

Callers

nothing calls this directly

Calls 2

get_model_adapterFunction · 0.85
load_modelMethod · 0.45

Tested by

no test coverage detected