Don't reload model for the merge step;
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import configparser
|
||||
import torch
|
||||
|
||||
from peft import PeftModel
|
||||
from sympy.printing import str
|
||||
|
||||
class Context:
|
||||
model: PeftModel
|
||||
|
||||
accel: bool
|
||||
device: str
|
||||
purgeTargetDirectories: bool
|
||||
|
||||
14
Merger.py
14
Merger.py
@@ -7,15 +7,17 @@ class Merger:
|
||||
def mergeAndStore(self, context: Context):
|
||||
if not context.mergeFull:
|
||||
return
|
||||
if not context.storeAdapter:
|
||||
raise Exception("Configuration problem: mergeFull requires storeAdapter")
|
||||
|
||||
print("Reloading model")
|
||||
baseModel = ModelLoader().load(context.locBaseModel, False, True, context)
|
||||
peftModel = PeftModel.from_pretrained(baseModel, context.locAdapter)
|
||||
if not context.model:
|
||||
if not context.storeAdapter:
|
||||
raise Exception("Configuration problem: mergeFull requires storeAdapter")
|
||||
|
||||
print("Loading model")
|
||||
baseModel = ModelLoader().load(context.locBaseModel, False, True, context)
|
||||
context.model = PeftModel.from_pretrained(baseModel, context.locAdapter)
|
||||
|
||||
print("Merging")
|
||||
mergedModel = peftModel.merge_and_unload()
|
||||
mergedModel = context.model.merge_and_unload()
|
||||
|
||||
print("Dequantize")
|
||||
try:
|
||||
|
||||
52
Trainer.py
52
Trainer.py
@@ -16,42 +16,36 @@ class Trainer:
|
||||
|
||||
baseModel = ModelLoader().load(context.locBaseModel, False, True, context)
|
||||
sftTrainer = self.__createTrainer(baseModel, context)
|
||||
context.model = sftTrainer.model
|
||||
|
||||
vInplace = context.validate and context.vInplace
|
||||
validator = None
|
||||
if vInplace:
|
||||
validator = Validator()
|
||||
|
||||
try:
|
||||
vInplace = context.validate and context.vInplace
|
||||
validator = None
|
||||
if vInplace:
|
||||
validator = Validator()
|
||||
cnt = 0
|
||||
continueTraining = True
|
||||
while continueTraining:
|
||||
cnt += 1
|
||||
print(f"Training run {cnt}")
|
||||
|
||||
try:
|
||||
cnt = 0
|
||||
continueTraining = True
|
||||
while continueTraining:
|
||||
cnt += 1
|
||||
print(f"Training run {cnt}")
|
||||
self.__train(sftTrainer)
|
||||
|
||||
self.__train(sftTrainer)
|
||||
|
||||
if vInplace:
|
||||
continueTraining = not validator.validateInPlace(sftTrainer.tokenizer, sftTrainer.model, context)
|
||||
else:
|
||||
continueTraining = False
|
||||
|
||||
print(f"Total training runs: {cnt} epochs {cnt * (context.trEpochs or 1)}")
|
||||
|
||||
finally:
|
||||
if vInplace:
|
||||
print(f"Validation statistics:\n{validator.statistics}")
|
||||
validator.unload(context)
|
||||
continueTraining = not validator.validateInPlace(sftTrainer.tokenizer, sftTrainer.model, context)
|
||||
else:
|
||||
continueTraining = False
|
||||
|
||||
if context.storeAdapter:
|
||||
self.__storeAdapter(sftTrainer, context)
|
||||
print(f"Total training runs: {cnt} epochs {cnt * (context.trEpochs or 1)}")
|
||||
|
||||
finally:
|
||||
del sftTrainer
|
||||
del baseModel
|
||||
gc.collect()
|
||||
if context.accel:
|
||||
torch.cuda.empty_cache()
|
||||
if vInplace:
|
||||
print(f"Validation statistics:\n{validator.statistics}")
|
||||
validator.unload(context)
|
||||
|
||||
if context.storeAdapter:
|
||||
self.__storeAdapter(sftTrainer, context)
|
||||
|
||||
def __createTrainer(self, baseModel, context: Context):
|
||||
tokenizer = AutoTokenizer.from_pretrained(
|
||||
|
||||
Reference in New Issue
Block a user