-
Notifications
You must be signed in to change notification settings - Fork 211
/
flow_parser.py
384 lines (339 loc) · 13.4 KB
/
flow_parser.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
'''
A simple python script that parses the flow.yml and removes any undesired
executors based on environmental variables that are present, then creates
flow*.tmp.yml.
Environmental flags available:
DISABLE_CLIP
DISABLE_DALLE_MEGA
DISABLE_GLID3XL
DISABLE_SWINIR
ENABLE_STABLE_DIFFUSION
ENABLE_CLIPSEG
TODO Support jcloud and k8s configurations?
'''
import argparse
import os
import sys
import yaml
from collections import OrderedDict
ENV_DISABLE_CLIP = 'DISABLE_CLIP'
ENV_DISABLE_DALLE_MEGA = 'DISABLE_DALLE_MEGA'
ENV_DISABLE_GLID3XL = 'DISABLE_GLID3XL'
ENV_DISABLE_SWINIR = 'DISABLE_SWINIR'
ENV_ENABLE_CLIPSEG = 'ENABLE_CLIPSEG'
ENV_ENABLE_REALESRGAN = 'ENABLE_REALESRGAN'
ENV_ENABLE_STABLE_DIFFUSION = 'ENABLE_STABLE_DIFFUSION'
ENV_GPUS_DALLE_MEGA = 'GPUS_DALLE_MEGA'
ENV_GPUS_GLID3XL = 'GPUS_GLID3XL'
ENV_GPUS_REALESRGAN = 'GPUS_REALESRGAN'
ENV_GPUS_SWINIR = 'GPUS_SWINIR'
ENV_GPUS_STABLE_DIFFUSION = 'GPUS_STABLE_DIFFUSION'
ENV_CAS_TOKEN = 'CAS_TOKEN'
ENV_REPLICAS_DALLE_MEGA = 'REPLICAS_DALLE_MEGA'
ENV_REPLICAS_GLID3XL = 'REPLICAS_GLID3XL'
ENV_REPLICAS_REALESRGAN = 'REPLICAS_REALESRGAN'
ENV_REPLICAS_SWINIR = 'REPLICAS_SWINIR'
ENV_REPLICAS_STABLE_DIFFUSION = 'REPLICAS_STABLE_DIFFUSION'
FLOW_KEY_ENV = 'env'
FLOW_KEY_ENV_CUDA_DEV = 'CUDA_VISIBLE_DEVICES'
FLOW_KEY_REPLICAS = 'replicas'
CAS_FLOW_NAME = 'clip_encoder'
CLIPSEG_FLOW_NAME = 'clipseg'
DALLE_MEGA_FLOW_NAME = 'dalle'
GLID3XL_FLOW_NAME = 'diffusion'
REALESRGAN_FLOW_NAME = 'realesrgan'
RERANK_FLOW_NAME = 'rerank'
SWINIR_FLOW_NAME = 'upscaler'
STABLE_DIFFUSION_FLOW_NAME = 'stable'
CLIP_AS_SERVICE_HOST = os.environ.get('CLIP_AS_SERVICE_HOST', 'api.clip.jina.ai')
CLIP_AS_SERVICE_PORT = os.environ.get('CLIP_AS_SERVICE_PORT', '2096')
def represent_ordereddict(dumper, data):
'''
Used to edit the YAML filters in place so that jina doesn't freak out when
we use the newly parsed file. Otherwise the new YAML is sorted by keys and
that breaks jina.
'''
value = []
for item_key, item_value in data.items():
node_key = dumper.represent_data(item_key)
node_value = dumper.represent_data(item_value)
value.append((node_key, node_value))
return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', value)
yaml.add_representer(OrderedDict, represent_ordereddict)
parser = argparse.ArgumentParser()
parser.add_argument('-fn','--filename',
dest='filename',
help='YAML file to use (default is flow.yaml)',
required=False)
parser.add_argument('-o','--output',
dest='output',
help='YAML file to output (default is flow.tmp.yaml)',
required=False)
parser.add_argument('--disable-clip',
dest='no_clip',
action='store_true',
help="Disable clip-as-a-service executor (default false)",
required=False)
parser.add_argument('--disable-dalle-mega',
dest='no_dalle_mega',
action='store_true',
help="Disable DALLE-MEGA executor (default false)",
required=False)
parser.add_argument('--disable-glid3xl',
dest='no_glid3xl',
action='store_true',
help="Disable GLID3XL executor (default false)",
required=False)
parser.add_argument('--disable-swinir',
dest='no_swinir',
action='store_true',
help="Disable SWINIR upscaler executor (default false)",
required=False)
parser.add_argument('--enable-clipseg',
dest='yes_clipseg',
action='store_true',
help="Enable CLIP segmentation executor (default false)",
required=False)
parser.add_argument('--enable-realesrgan',
dest='yes_realesrgan',
action='store_true',
help="Enable RealESRGAN upscaler (default false)",
required=False)
parser.add_argument('--enable-stable-diffusion',
dest='yes_stable_diffusion',
action='store_true',
help="Enable Stable Diffusion executor (default false)",
required=False)
parser.add_argument('--cas-token',
dest='cas_token',
help="Token to authenticate with the CAS service (default ''). If not set, the CAS service will not be used.",
default='',
required=False)
parser.add_argument('--gpus-dalle-mega',
dest='gpus_dalle_mega',
help="GPU device ID(s) for DALLE-MEGA (default 0)",
default=0,
required=False)
parser.add_argument('--gpus-glid3xl',
dest='gpus_glid3xl',
help="GPU device ID(s) for GLID3XL (default 0)",
default=0,
required=False)
parser.add_argument('--gpus-realesrgan',
dest='gpus_realesrgan',
help="GPU device ID(s) for RealESRGAN (default 0)",
default=0,
required=False)
parser.add_argument('--gpus-stable-diffusion',
dest='gpus_stable_diffusion',
help="GPU device ID(s) for Stable Diffusion (default 0)",
default=0,
required=False)
parser.add_argument('--gpus-swinir',
dest='gpus_swinir',
help="GPU device ID(s) for SWINIR (default 0)",
default=0,
required=False)
parser.add_argument('--replicas-dalle-mega',
dest='replicas_dalle_mega',
help="Replica number for DALLE-MEGA (default 1)",
default=1,
required=False)
parser.add_argument('--replicas-glid3xl',
dest='replicas_glid3xl',
help="Replica number for GLID3XL (default 1)",
default=1,
required=False)
parser.add_argument('--replicas-realesrgan',
dest='replicas_realesrgan',
help="Replica number for RealESRGAN (default 1)",
default=1,
required=False)
parser.add_argument('--replicas-stable-diffusion',
dest='replicas_stable_diffusion',
help="Replica number for Stable Diffusion (default 1)",
default=1,
required=False)
parser.add_argument('--replicas-swinir',
dest='replicas_swinir',
help="Replica number for SWINIR (default 1)",
default=1,
required=False)
args = vars(parser.parse_args())
flow_to_use = 'flow.yml'
if args.get('filename', None) is not None:
flow_to_use = args['filename']
output_flow = 'flow.tmp.yml'
if args.get('output', None) is not None:
output_flow = args['output']
no_clip = args.get('no_clip') or \
os.environ.get(ENV_DISABLE_CLIP, False)
no_dalle_mega = args.get('no_dalle_mega') or \
os.environ.get(ENV_DISABLE_DALLE_MEGA, False)
no_glid3xl = args.get('no_glid3xl') or os.environ.get(ENV_DISABLE_GLID3XL, False)
no_swinir = args.get('no_swinir') or os.environ.get(ENV_DISABLE_SWINIR, False)
yes_clipseg = args.get('yes_clipseg') or \
os.environ.get(ENV_ENABLE_CLIPSEG, False)
yes_realesrgan = args.get('yes_realesrgan') or \
os.environ.get(ENV_ENABLE_REALESRGAN, False)
yes_stable_diffusion = args.get('yes_stable_diffusion') or \
os.environ.get(ENV_ENABLE_STABLE_DIFFUSION, False)
gpus_dalle_mega = os.environ.get(ENV_GPUS_DALLE_MEGA, False) or \
args.get('gpus_dalle_mega')
gpus_glid3xl = os.environ.get(ENV_GPUS_GLID3XL, False) or \
args.get('gpus_glid3xl')
gpus_realesrgan = os.environ.get(ENV_GPUS_REALESRGAN, False) or \
args.get('gpus_realesrgan')
gpus_stable_diffusion = os.environ.get(ENV_GPUS_STABLE_DIFFUSION, False) or \
args.get('gpus_stable_diffusion')
gpus_swinir = os.environ.get(ENV_GPUS_SWINIR, False) or \
args.get('gpus_swinir')
replicas_dalle_mega = os.environ.get(ENV_REPLICAS_DALLE_MEGA, False) or \
args.get('replicas_dalle_mega')
replicas_glid3xl = os.environ.get(ENV_REPLICAS_GLID3XL, False) or \
args.get('replicas_glid3xl')
replicas_realesrgan = os.environ.get(ENV_REPLICAS_REALESRGAN, False) or \
args.get('replicas_realesrgan')
replicas_stable_diffusion = os.environ.get(ENV_REPLICAS_STABLE_DIFFUSION, False) or \
args.get('replicas_stable_diffusion')
replicas_swinir = os.environ.get(ENV_REPLICAS_SWINIR, False) or \
args.get('replicas_swinir')
cas_token = os.environ.get(ENV_CAS_TOKEN, '') or args.get('cas_token')
if no_clip and not no_glid3xl:
raise ValueError('GLID3XL requires a CLIP encoder executor to work')
CLIPSEG_DICT = OrderedDict({
'env': {
'CUDA_VISIBLE_DEVICES': 0,
'XLA_PYTHON_CLIENT_ALLOCATOR': 'platform',
},
'name': CLIPSEG_FLOW_NAME,
'replicas': 1,
'timeout_ready': -1,
'uses': f'executors/{CLIPSEG_FLOW_NAME}/config.yml',
})
REALESRGAN_DICT = OrderedDict({
'env': {
'CUDA_VISIBLE_DEVICES': gpus_realesrgan,
'XLA_PYTHON_CLIENT_ALLOCATOR': 'platform',
},
'name': REALESRGAN_FLOW_NAME,
'replicas': int(replicas_realesrgan),
'timeout_ready': -1,
'uses': f'executors/{REALESRGAN_FLOW_NAME}/config.yml',
})
STABLE_YAML_DICT = OrderedDict({
'env': {
'MEMORY_EFFICIENT_CROSS_ATTENTION': 1,
'CUDA_VISIBLE_DEVICES': gpus_stable_diffusion,
'XLA_PYTHON_CLIENT_ALLOCATOR': 'platform',
},
'name': STABLE_DIFFUSION_FLOW_NAME,
'replicas': int(replicas_stable_diffusion),
'timeout_ready': -1,
'uses': f'executors/{STABLE_DIFFUSION_FLOW_NAME}/config.yml',
})
def _filter_out(flow_exec_list, name):
return list(filter(lambda exc: exc['name'] != name, flow_exec_list))
with open(flow_to_use, 'r') as f_in:
flow_as_dict = None
try:
flow_as_dict = OrderedDict(yaml.safe_load(f_in))
except yaml.YAMLError as exc:
print(exc)
sys.exit(1)
# If the cas_token is not empty, we will use the clip-as-a-service as external executor
if cas_token:
for ext in flow_as_dict['executors']:
if ext['name'] in [CAS_FLOW_NAME, RERANK_FLOW_NAME]:
ext['host'] = CLIP_AS_SERVICE_HOST
ext['port'] = int(CLIP_AS_SERVICE_PORT)
ext['external'] = True
ext['tls'] = True
ext['grpc_metadata'] = {'authorization': cas_token}
# For backwards compatibility, we inject the stable diffusion configuration
# into the flow yml and then remove it if needed.
#
# Find the index of latent diffusion and inject stable diffusion and
# clipseg after it.
glid3xl_idx = next(i for i, exc in enumerate(flow_as_dict['executors'])
if exc['name'] == GLID3XL_FLOW_NAME)
flow_as_dict['executors'].insert(glid3xl_idx + 1, CLIPSEG_DICT)
flow_as_dict['executors'].insert(glid3xl_idx + 1, REALESRGAN_DICT)
flow_as_dict['executors'].insert(glid3xl_idx + 1, STABLE_YAML_DICT)
# Find the rerank executor, jam stable into its needs.
rerank_idx = next(i for i, exc in enumerate(flow_as_dict['executors'])
if exc['name'] == RERANK_FLOW_NAME)
flow_as_dict['executors'][rerank_idx]['needs'].append(
STABLE_DIFFUSION_FLOW_NAME)
if flow_as_dict is None:
print('Input yaml was empty')
sys.exit(1)
if flow_as_dict.get('executors', None) is None:
print('No executors found in yaml file')
sys.exit(1)
if no_dalle_mega:
flow_as_dict['executors'] = _filter_out(flow_as_dict['executors'],
DALLE_MEGA_FLOW_NAME)
else:
dalle_mega_idx = next(i for i, exc in enumerate(flow_as_dict['executors'])
if exc['name'] == DALLE_MEGA_FLOW_NAME)
flow_as_dict['executors'][dalle_mega_idx][FLOW_KEY_ENV][FLOW_KEY_ENV_CUDA_DEV] = gpus_dalle_mega
flow_as_dict['executors'][dalle_mega_idx][FLOW_KEY_REPLICAS] = int(replicas_dalle_mega)
if no_glid3xl:
flow_as_dict['executors'] = _filter_out(flow_as_dict['executors'],
GLID3XL_FLOW_NAME)
else:
glid3xl_idx = next(i for i, exc in enumerate(flow_as_dict['executors'])
if exc['name'] == GLID3XL_FLOW_NAME)
flow_as_dict['executors'][glid3xl_idx][FLOW_KEY_ENV][FLOW_KEY_ENV_CUDA_DEV] = gpus_glid3xl
flow_as_dict['executors'][glid3xl_idx][FLOW_KEY_REPLICAS] = int(replicas_glid3xl)
if no_swinir:
flow_as_dict['executors'] = _filter_out(flow_as_dict['executors'],
SWINIR_FLOW_NAME)
else:
swinir_idx = next(i for i, exc in enumerate(flow_as_dict['executors'])
if exc['name'] == SWINIR_FLOW_NAME)
flow_as_dict['executors'][swinir_idx][FLOW_KEY_ENV][FLOW_KEY_ENV_CUDA_DEV] = gpus_swinir
flow_as_dict['executors'][swinir_idx][FLOW_KEY_REPLICAS] = int(replicas_swinir)
if not yes_clipseg:
flow_as_dict['executors'] = _filter_out(flow_as_dict['executors'],
CLIPSEG_FLOW_NAME)
if not yes_realesrgan:
flow_as_dict['executors'] = _filter_out(flow_as_dict['executors'],
REALESRGAN_FLOW_NAME)
if not yes_stable_diffusion:
flow_as_dict['executors'] = _filter_out(flow_as_dict['executors'],
STABLE_DIFFUSION_FLOW_NAME)
for exc in flow_as_dict['executors']:
if type(exc.get('needs', None)) == list:
if no_dalle_mega:
exc['needs'] = list(filter(
lambda _n: _n != DALLE_MEGA_FLOW_NAME,
exc['needs']))
if no_glid3xl:
exc['needs'] = list(filter(
lambda _n: _n != GLID3XL_FLOW_NAME,
exc['needs']))
if no_swinir:
exc['needs'] = list(filter(
lambda _n: _n != SWINIR_FLOW_NAME,
exc['needs']))
if not yes_clipseg:
exc['needs'] = list(filter(
lambda _n: _n != CLIPSEG_FLOW_NAME,
exc['needs']))
if not yes_realesrgan:
exc['needs'] = list(filter(
lambda _n: _n != REALESRGAN_FLOW_NAME,
exc['needs']))
if not yes_stable_diffusion:
exc['needs'] = list(filter(
lambda _n: _n != STABLE_DIFFUSION_FLOW_NAME,
exc['needs']))
if no_clip:
flow_as_dict['executors'] = _filter_out(flow_as_dict['executors'],
CAS_FLOW_NAME)
flow_as_dict['executors'] = _filter_out(flow_as_dict['executors'],
RERANK_FLOW_NAME)
with open(output_flow, 'w') as f_out:
f_out.write(yaml.dump(flow_as_dict))