Compare commits

...

4 Commits

Author SHA1 Message Date
Wing Lian
9aaa4b8ced set model merge dtype based on cfg
Some checks failed
pre-commit / pre-commit (push) Has been cancelled
PyTest / test (3.10) (push) Has been cancelled
PyTest / test (3.9) (push) Has been cancelled
2023-08-23 04:07:44 -04:00
Wing Lian
8be7da8999 push merged lora to hf 2023-08-23 04:07:44 -04:00
Wing Lian
53e739f11e deduplicate code 2023-08-23 04:07:44 -04:00
Wing Lian
f20c8deff1 merge lora on train completion 2023-08-23 04:07:44 -04:00

View File

@@ -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)