/
lora.py
182 lines (166 loc) · 6.27 KB
/
lora.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import yaml
import textwrap
IMAGE = "quay.io/lukemarsden/lora:v0.0.4"
MODEL_NAME = "runwayml/stable-diffusion-v1-5"
# Don't want to make this variable since the job is fixed price
NUM_IMAGES = 10
def _lora_training(params: str):
if params.startswith("{"):
params = yaml.safe_load(params)
else:
raise Exception("Please set params to yaml like {seed: 42, 'images_cid': 'Qm...'} where images_cid contains an images.zip with training images in")
seed = params.get("seed", 42)
images_cid = params["images_cid"]
return {
"APIVersion": "V1beta1",
"Metadata": {
"CreatedAt": "0001-01-01T00:00:00Z",
"Requester": {}
},
"Spec": {
"Deal": {
"Concurrency": 1
},
"Docker": {
"EnvironmentVariables": [
f"RANDOM_SEED={seed}",
],
"Entrypoint": [
"bash", "-c", f'(cd /input && unzip images.zip && rm images.zip) && lora_pti --pretrained_model_name_or_path={MODEL_NAME} --instance_data_dir=/input --output_dir=/output --train_text_encoder --resolution=512 --train_batch_size=1 --gradient_accumulation_steps=4 --scale_lr --learning_rate_unet=1e-4 --learning_rate_text=1e-5 --learning_rate_ti=5e-4 --color_jitter --lr_scheduler="linear" --lr_warmup_steps=0 --placeholder_tokens="<s1>|<s2>" --use_template="style" --save_steps=100 --max_train_steps_ti=1000 --max_train_steps_tuning=1000 --perform_inversion=True --clip_ti_decay --weight_decay_ti=0.000 --weight_decay_lora=0.001 --continue_inversion --continue_inversion_lr=1e-4 --device="cuda:0" --lora_rank=1'
],
"Image": IMAGE,
},
"Engine": "Docker",
"Language": {
"JobContext": {}
},
"Network": {
"Type": "None"
},
"PublisherSpec": {
"Type": "Estuary"
},
"Resources": {
"GPU": "1"
},
"Timeout": 1800,
"Verifier": "Noop",
"Wasm": {
"EntryModule": {}
},
"inputs": [
{
"CID": images_cid,
"Name": "lora_input",
"StorageSource": "IPFS",
"path": "/input",
},
],
"outputs": [
{
"Name": "output",
"StorageSource": "IPFS",
"path": "/output"
}
]
}
}
def _lora_inference(params: str):
if params.startswith("{"):
params = yaml.safe_load(params)
else:
raise Exception("Please set params to yaml like {seed: 42, 'lora_cid': 'Qm...', "+
"prompt: 'an astronaut in the style of <s1><s2>'} "+
"where lora_cid is the output cid of the above step")
# TODO add a default we pin
lora_cid = params["lora_cid"]
seed = params.get("seed", 42)
prompt = params.get("prompt", "question mark floating in space")
finetune_weighting = params.get("finetune_weighting", 0.5)
return {
"APIVersion": "V1beta1",
"Metadata": {
"CreatedAt": "0001-01-01T00:00:00Z",
"Requester": {}
},
"Spec": {
"Deal": {
"Concurrency": 1
},
"Docker": {
"EnvironmentVariables": [
f"PROMPT={prompt}",
f"RANDOM_SEED={seed}",
f"FINETUNE_WEIGHTING={finetune_weighting}",
"HF_HUB_OFFLINE=1",
],
"Entrypoint": [
'python3',
'-c',
# dedent
textwrap.dedent(f"""
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
import os
import torch
from lora_diffusion import tune_lora_scale, patch_pipe
model_id = "{MODEL_NAME}"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to(
"cuda"
)
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
prompt = os.getenv("PROMPT")
seed = int(os.getenv("RANDOM_SEED"))
torch.manual_seed(seed)
os.system("find /input")
patch_pipe(
pipe,
"/input/output/final_lora.safetensors",
patch_text=True,
patch_ti=True,
patch_unet=True,
)
coeff = float(os.getenv("FINETUNE_WEIGHTING", 0.5))
tune_lora_scale(pipe.unet, coeff)
tune_lora_scale(pipe.text_encoder, coeff)
image = pipe(prompt, num_inference_steps=50, guidance_scale=7).images[0]
image.save(f"/output/image-{{seed}}.jpg")
image
""")
],
"Image": IMAGE,
},
"Engine": "Docker",
"Language": {
"JobContext": {}
},
"Network": {
"Type": "None"
},
"PublisherSpec": {
"Type": "Estuary"
},
"Resources": {
"GPU": "1"
},
"Timeout": 1800,
"Verifier": "Noop",
"Wasm": {
"EntryModule": {}
},
"inputs": [
{
"CID": lora_cid,
"Name": "lora_input",
"StorageSource": "IPFS",
"path": "/input",
},
],
"outputs": [
{
"Name": "output",
"StorageSource": "IPFS",
"path": "/output"
},
]
}
}