Upload jobs/process/ExtractLoconProcess.py with huggingface_hub
Browse files
jobs/process/ExtractLoconProcess.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import OrderedDict
|
| 2 |
+
from toolkit.lycoris_utils import extract_diff
|
| 3 |
+
from .BaseExtractProcess import BaseExtractProcess
|
| 4 |
+
|
| 5 |
+
mode_dict = {
|
| 6 |
+
'fixed': {
|
| 7 |
+
'linear': 64,
|
| 8 |
+
'conv': 32,
|
| 9 |
+
'type': int
|
| 10 |
+
},
|
| 11 |
+
'threshold': {
|
| 12 |
+
'linear': 0,
|
| 13 |
+
'conv': 0,
|
| 14 |
+
'type': float
|
| 15 |
+
},
|
| 16 |
+
'ratio': {
|
| 17 |
+
'linear': 0.5,
|
| 18 |
+
'conv': 0.5,
|
| 19 |
+
'type': float
|
| 20 |
+
},
|
| 21 |
+
'quantile': {
|
| 22 |
+
'linear': 0.5,
|
| 23 |
+
'conv': 0.5,
|
| 24 |
+
'type': float
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class ExtractLoconProcess(BaseExtractProcess):
|
| 30 |
+
def __init__(self, process_id: int, job, config: OrderedDict):
|
| 31 |
+
super().__init__(process_id, job, config)
|
| 32 |
+
self.mode = self.get_conf('mode', 'fixed')
|
| 33 |
+
self.use_sparse_bias = self.get_conf('use_sparse_bias', False)
|
| 34 |
+
self.sparsity = self.get_conf('sparsity', 0.98)
|
| 35 |
+
self.disable_cp = self.get_conf('disable_cp', False)
|
| 36 |
+
|
| 37 |
+
# set modes
|
| 38 |
+
if self.mode not in list(mode_dict.keys()):
|
| 39 |
+
raise ValueError(f"Unknown mode: {self.mode}")
|
| 40 |
+
self.linear_param = self.get_conf('linear', mode_dict[self.mode]['linear'], as_type=mode_dict[self.mode]['type'])
|
| 41 |
+
self.conv_param = self.get_conf('conv', mode_dict[self.mode]['conv'], as_type=mode_dict[self.mode]['type'])
|
| 42 |
+
|
| 43 |
+
def run(self):
|
| 44 |
+
super().run()
|
| 45 |
+
print(f"Running process: {self.mode}, lin: {self.linear_param}, conv: {self.conv_param}")
|
| 46 |
+
|
| 47 |
+
state_dict, extract_diff_meta = extract_diff(
|
| 48 |
+
self.job.model_base,
|
| 49 |
+
self.job.model_extract,
|
| 50 |
+
self.mode,
|
| 51 |
+
self.linear_param,
|
| 52 |
+
self.conv_param,
|
| 53 |
+
self.job.device,
|
| 54 |
+
self.use_sparse_bias,
|
| 55 |
+
self.sparsity,
|
| 56 |
+
not self.disable_cp,
|
| 57 |
+
extract_unet=self.extract_unet,
|
| 58 |
+
extract_text_encoder=self.extract_text_encoder
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
self.add_meta(extract_diff_meta)
|
| 62 |
+
self.save(state_dict)
|
| 63 |
+
|
| 64 |
+
def get_output_path(self, prefix=None, suffix=None):
|
| 65 |
+
if suffix is None:
|
| 66 |
+
suffix = f"_{self.mode}_{self.linear_param}_{self.conv_param}"
|
| 67 |
+
return super().get_output_path(prefix, suffix)
|
| 68 |
+
|