Upload jobs/process/BaseExtractProcess.py with huggingface_hub
Browse files
jobs/process/BaseExtractProcess.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 9 |
+
from typing import ForwardRef
|
| 10 |
+
|
| 11 |
+
from toolkit.train_tools import get_torch_dtype
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class BaseExtractProcess(BaseProcess):
|
| 15 |
+
|
| 16 |
+
def __init__(
|
| 17 |
+
self,
|
| 18 |
+
process_id: int,
|
| 19 |
+
job,
|
| 20 |
+
config: OrderedDict
|
| 21 |
+
):
|
| 22 |
+
super().__init__(process_id, job, config)
|
| 23 |
+
self.config: OrderedDict
|
| 24 |
+
self.output_folder: str
|
| 25 |
+
self.output_filename: str
|
| 26 |
+
self.output_path: str
|
| 27 |
+
self.process_id = process_id
|
| 28 |
+
self.job = job
|
| 29 |
+
self.config = config
|
| 30 |
+
self.dtype = self.get_conf('dtype', self.job.dtype)
|
| 31 |
+
self.torch_dtype = get_torch_dtype(self.dtype)
|
| 32 |
+
self.extract_unet = self.get_conf('extract_unet', self.job.extract_unet)
|
| 33 |
+
self.extract_text_encoder = self.get_conf('extract_text_encoder', self.job.extract_text_encoder)
|
| 34 |
+
|
| 35 |
+
def run(self):
|
| 36 |
+
# here instead of init because child init needs to go first
|
| 37 |
+
self.output_path = self.get_output_path()
|
| 38 |
+
# implement in child class
|
| 39 |
+
# be sure to call super().run() first
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
# you can override this in the child class if you want
|
| 43 |
+
# call super().get_output_path(prefix="your_prefix_", suffix="_your_suffix") to extend this
|
| 44 |
+
def get_output_path(self, prefix=None, suffix=None):
|
| 45 |
+
config_output_path = self.get_conf('output_path', None)
|
| 46 |
+
config_filename = self.get_conf('filename', None)
|
| 47 |
+
# replace [name] with name
|
| 48 |
+
|
| 49 |
+
if config_output_path is not None:
|
| 50 |
+
config_output_path = config_output_path.replace('[name]', self.job.name)
|
| 51 |
+
return config_output_path
|
| 52 |
+
|
| 53 |
+
if config_output_path is None and config_filename is not None:
|
| 54 |
+
# build the output path from the output folder and filename
|
| 55 |
+
return os.path.join(self.job.output_folder, config_filename)
|
| 56 |
+
|
| 57 |
+
# build our own
|
| 58 |
+
|
| 59 |
+
if suffix is None:
|
| 60 |
+
# we will just add process it to the end of the filename if there is more than one process
|
| 61 |
+
# and no other suffix was given
|
| 62 |
+
suffix = f"_{self.process_id}" if len(self.config['process']) > 1 else ''
|
| 63 |
+
|
| 64 |
+
if prefix is None:
|
| 65 |
+
prefix = ''
|
| 66 |
+
|
| 67 |
+
output_filename = f"{prefix}{self.output_filename}{suffix}"
|
| 68 |
+
|
| 69 |
+
return os.path.join(self.job.output_folder, output_filename)
|
| 70 |
+
|
| 71 |
+
def save(self, state_dict):
|
| 72 |
+
# prepare meta
|
| 73 |
+
save_meta = get_meta_for_safetensors(self.meta, self.job.name)
|
| 74 |
+
|
| 75 |
+
# save
|
| 76 |
+
os.makedirs(os.path.dirname(self.output_path), exist_ok=True)
|
| 77 |
+
|
| 78 |
+
for key in list(state_dict.keys()):
|
| 79 |
+
v = state_dict[key]
|
| 80 |
+
v = v.detach().clone().to("cpu").to(self.torch_dtype)
|
| 81 |
+
state_dict[key] = v
|
| 82 |
+
|
| 83 |
+
# having issues with meta
|
| 84 |
+
save_file(state_dict, self.output_path, save_meta)
|
| 85 |
+
|
| 86 |
+
print(f"Saved to {self.output_path}")
|