Upload jobs/process/BaseMergeProcess.py with huggingface_hub
Browse files
jobs/process/BaseMergeProcess.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from collections import OrderedDict
|
| 3 |
+
|
| 4 |
+
from safetensors.torch import save_file
|
| 5 |
+
|
| 6 |
+
from jobs.process.BaseProcess import BaseProcess
|
| 7 |
+
from toolkit.metadata import get_meta_for_safetensors
|
| 8 |
+
from toolkit.train_tools import get_torch_dtype
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class BaseMergeProcess(BaseProcess):
|
| 12 |
+
|
| 13 |
+
def __init__(
|
| 14 |
+
self,
|
| 15 |
+
process_id: int,
|
| 16 |
+
job,
|
| 17 |
+
config: OrderedDict
|
| 18 |
+
):
|
| 19 |
+
super().__init__(process_id, job, config)
|
| 20 |
+
self.process_id: int
|
| 21 |
+
self.config: OrderedDict
|
| 22 |
+
self.output_path = self.get_conf('output_path', required=True)
|
| 23 |
+
self.dtype = self.get_conf('dtype', self.job.dtype)
|
| 24 |
+
self.torch_dtype = get_torch_dtype(self.dtype)
|
| 25 |
+
|
| 26 |
+
def run(self):
|
| 27 |
+
# implement in child class
|
| 28 |
+
# be sure to call super().run() first
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
def save(self, state_dict):
|
| 32 |
+
# prepare meta
|
| 33 |
+
save_meta = get_meta_for_safetensors(self.meta, self.job.name)
|
| 34 |
+
|
| 35 |
+
# save
|
| 36 |
+
os.makedirs(os.path.dirname(self.output_path), exist_ok=True)
|
| 37 |
+
|
| 38 |
+
for key in list(state_dict.keys()):
|
| 39 |
+
v = state_dict[key]
|
| 40 |
+
v = v.detach().clone().to("cpu").to(self.torch_dtype)
|
| 41 |
+
state_dict[key] = v
|
| 42 |
+
|
| 43 |
+
# having issues with meta
|
| 44 |
+
save_file(state_dict, self.output_path, save_meta)
|
| 45 |
+
|
| 46 |
+
print(f"Saved to {self.output_path}")
|