Compare commits
4 Commits
optimizers
...
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)
|
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(
|
def train(
|
||||||
config: Path = Path("configs/"),
|
config: Path = Path("configs/"),
|
||||||
prepare_ds_only: bool = False,
|
prepare_ds_only: bool = False,
|
||||||
@@ -214,17 +231,7 @@ def train(
|
|||||||
safe_serialization = cfg.save_safetensors is True
|
safe_serialization = cfg.save_safetensors is True
|
||||||
|
|
||||||
if "merge_lora" in kwargs and cfg.adapter is not None:
|
if "merge_lora" in kwargs and cfg.adapter is not None:
|
||||||
LOG.info("running merge of LoRA with base model")
|
merge_lora(model, tokenizer, cfg)
|
||||||
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"))
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if cfg.inference:
|
if cfg.inference:
|
||||||
@@ -310,6 +317,9 @@ def train(
|
|||||||
model = BetterTransformer.reverse(model)
|
model = BetterTransformer.reverse(model)
|
||||||
model.save_pretrained(cfg.output_dir, safe_serialization=safe_serialization)
|
model.save_pretrained(cfg.output_dir, safe_serialization=safe_serialization)
|
||||||
|
|
||||||
|
if cfg.adapter is not None:
|
||||||
|
merge_lora(model, tokenizer, cfg)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
fire.Fire(train)
|
fire.Fire(train)
|
||||||
|
|||||||
Reference in New Issue
Block a user