Compare commits
4 Commits
v0.13.2
...
merge-lora
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9aaa4b8ced | ||
|
|
8be7da8999 | ||
|
|
53e739f11e | ||
|
|
f20c8deff1 |
@@ -155,6 +155,23 @@ def check_not_in(list1: List[str], list2: Union[Dict[str, Any], List[str]]) -> b
|
||||
return not any(el in list2 for el in list1)
|
||||
|
||||
|
||||
def merge_lora(model, tokenizer, cfg):
|
||||
LOG.info("running merge of LoRA with base model")
|
||||
model = model.merge_and_unload()
|
||||
model_dtype = torch.bfloat16 if cfg.bf16 or cfg.bfloat16 else torch.float16
|
||||
model.to(dtype=model_dtype)
|
||||
if cfg.hub_model_id:
|
||||
model.push_to_hub("hub_model_id")
|
||||
|
||||
if cfg.local_rank == 0:
|
||||
LOG.info("saving merged model")
|
||||
model.save_pretrained(
|
||||
str(Path(cfg.output_dir) / "merged"),
|
||||
safe_serialization=cfg.save_safetensors is True,
|
||||
)
|
||||
tokenizer.save_pretrained(str(Path(cfg.output_dir) / "merged"))
|
||||
|
||||
|
||||
def train(
|
||||
config: Path = Path("configs/"),
|
||||
prepare_ds_only: bool = False,
|
||||
@@ -214,17 +231,7 @@ def train(
|
||||
safe_serialization = cfg.save_safetensors is True
|
||||
|
||||
if "merge_lora" in kwargs and cfg.adapter is not None:
|
||||
LOG.info("running merge of LoRA with base model")
|
||||
model = model.merge_and_unload()
|
||||
model.to(dtype=torch.float16)
|
||||
|
||||
if cfg.local_rank == 0:
|
||||
LOG.info("saving merged model")
|
||||
model.save_pretrained(
|
||||
str(Path(cfg.output_dir) / "merged"),
|
||||
safe_serialization=safe_serialization,
|
||||
)
|
||||
tokenizer.save_pretrained(str(Path(cfg.output_dir) / "merged"))
|
||||
merge_lora(model, tokenizer, cfg)
|
||||
return
|
||||
|
||||
if cfg.inference:
|
||||
@@ -310,6 +317,9 @@ def train(
|
||||
model = BetterTransformer.reverse(model)
|
||||
model.save_pretrained(cfg.output_dir, safe_serialization=safe_serialization)
|
||||
|
||||
if cfg.adapter is not None:
|
||||
merge_lora(model, tokenizer, cfg)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
fire.Fire(train)
|
||||
|
||||
Reference in New Issue
Block a user