-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmusubi_zimage_base_lora_trainer.py
More file actions
738 lines (622 loc) · 30.5 KB
/
musubi_zimage_base_lora_trainer.py
File metadata and controls
738 lines (622 loc) · 30.5 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
"""
Musubi Tuner Z-Image Base LoRA Trainer Node for ComfyUI
Trains Z-Image Base (non-distilled) LoRAs using kohya-ss/musubi-tuner.
Key differences from Z-Image Turbo:
- Default learning rate: 0.0001 (vs 0.0002)
- Default LoRA rank: 32 (vs 16)
"""
import os
import sys
import json
import hashlib
import tempfile
import shutil
import subprocess
from datetime import datetime
import numpy as np
from PIL import Image
import folder_paths
from .musubi_zimage_base_config_template import (
MUSUBI_ZIMAGE_BASE_VRAM_PRESETS,
)
from .musubi_zimage_config_template import (
generate_dataset_config,
save_config,
)
# Global config for Musubi Z-Image Base trainer
_musubi_zimage_base_config = {}
_musubi_zimage_base_config_file = os.path.join(os.path.dirname(__file__), ".musubi_zimage_base_config.json")
# Global cache for trained LoRAs
_musubi_zimage_base_lora_cache = {}
_musubi_zimage_base_cache_file = os.path.join(os.path.dirname(__file__), ".musubi_zimage_base_lora_cache.json")
def _load_musubi_config():
"""Load Musubi config from disk."""
global _musubi_zimage_base_config
if os.path.exists(_musubi_zimage_base_config_file):
try:
with open(_musubi_zimage_base_config_file, 'r', encoding='utf-8') as f:
_musubi_zimage_base_config = json.load(f)
except:
_musubi_zimage_base_config = {}
def _save_musubi_config():
"""Save Musubi config to disk."""
try:
with open(_musubi_zimage_base_config_file, 'w', encoding='utf-8') as f:
json.dump(_musubi_zimage_base_config, f, indent=2)
except:
pass
def _load_musubi_cache():
"""Load Musubi LoRA cache from disk."""
global _musubi_zimage_base_lora_cache
if os.path.exists(_musubi_zimage_base_cache_file):
try:
with open(_musubi_zimage_base_cache_file, 'r', encoding='utf-8') as f:
_musubi_zimage_base_lora_cache = json.load(f)
except:
_musubi_zimage_base_lora_cache = {}
def _save_musubi_cache():
"""Save Musubi LoRA cache to disk."""
try:
with open(_musubi_zimage_base_cache_file, 'w', encoding='utf-8') as f:
json.dump(_musubi_zimage_base_lora_cache, f)
except:
pass
def _compute_image_hash(images, captions, training_steps, learning_rate, lora_rank, vram_mode, output_name, dit_model, vae_model, text_encoder, use_folder_path=False):
"""Compute a hash of all images, captions, and training parameters."""
hasher = hashlib.sha256()
if use_folder_path:
# For folder paths, hash the file paths and modification times
for img_path in images:
hasher.update(img_path.encode('utf-8'))
if os.path.exists(img_path):
hasher.update(str(os.path.getmtime(img_path)).encode('utf-8'))
else:
# For tensor inputs, hash the image data
for img_tensor in images:
img_np = (img_tensor[0].cpu().numpy() * 255).astype(np.uint8)
img_bytes = img_np.tobytes()
hasher.update(img_bytes)
# Include all captions and model paths in hash
captions_str = "|".join(captions)
params_str = f"musubi_zimage_base|{captions_str}|{training_steps}|{learning_rate}|{lora_rank}|{vram_mode}|{output_name}|{len(images)}|{dit_model}|{vae_model}|{text_encoder}"
hasher.update(params_str.encode('utf-8'))
return hasher.hexdigest()[:16]
def _get_venv_python_path(musubi_path):
"""Get the Python path for musubi-tuner venv based on platform.
Checks both .venv (uv default) and venv (traditional) folders."""
venv_folders = [".venv", "venv"]
for venv_folder in venv_folders:
if sys.platform == 'win32':
python_path = os.path.join(musubi_path, venv_folder, "Scripts", "python.exe")
else:
python_path = os.path.join(musubi_path, venv_folder, "bin", "python")
if os.path.exists(python_path):
return python_path
# Return traditional path for error messaging
if sys.platform == 'win32':
return os.path.join(musubi_path, "venv", "Scripts", "python.exe")
else:
return os.path.join(musubi_path, "venv", "bin", "python")
def _get_accelerate_path(musubi_path):
"""Get the accelerate path for musubi-tuner venv based on platform.
Checks both .venv (uv default) and venv (traditional) folders."""
venv_folders = [".venv", "venv"]
for venv_folder in venv_folders:
if sys.platform == 'win32':
accel_path = os.path.join(musubi_path, venv_folder, "Scripts", "accelerate.exe")
else:
accel_path = os.path.join(musubi_path, venv_folder, "bin", "accelerate")
if os.path.exists(accel_path):
return accel_path
# Return traditional path for error messaging
if sys.platform == 'win32':
return os.path.join(musubi_path, "venv", "Scripts", "accelerate.exe")
else:
return os.path.join(musubi_path, "venv", "bin", "accelerate")
def _get_model_path(name, folder_type):
"""Get full path to a model file from ComfyUI folders.
Returns the name as-is if it's already an absolute path that exists."""
if not name:
return ""
# If it's already an absolute path that exists, use it
if os.path.isabs(name) and os.path.exists(name):
return name
# Try to get from ComfyUI folder
try:
return folder_paths.get_full_path(folder_type, name)
except:
return name
# Load config and cache on module import
_load_musubi_config()
_load_musubi_cache()
class MusubiZImageBaseLoraTrainer:
"""
Trains a Z-Image Base (non-distilled) LoRA from one or more images using Musubi Tuner.
"""
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
# Get saved settings or use defaults
if sys.platform == 'win32':
musubi_fallback = 'C:\\musubi-tuner'
else:
musubi_fallback = '~/musubi-tuner'
saved = _musubi_zimage_base_config.get('trainer_settings', {})
# Get available models from ComfyUI folders
diffusion_models = folder_paths.get_filename_list("diffusion_models")
vae_models = folder_paths.get_filename_list("vae")
# Text encoders can be in clip or text_encoders folder
try:
text_encoders = folder_paths.get_filename_list("text_encoders")
except:
text_encoders = []
try:
clip_models = folder_paths.get_filename_list("clip")
except:
clip_models = []
text_encoder_list = sorted(set(text_encoders + clip_models)) if (text_encoders or clip_models) else ["(no text encoders found)"]
# Get saved model selections (for default)
saved_dit = saved.get('dit_model', '')
saved_vae = saved.get('vae_model', '')
saved_te = saved.get('text_encoder', '')
# Build dropdown configs with saved defaults if available
dit_config = {"tooltip": "Z-Image Base DiT model (transformer) from diffusion_models folder."}
if saved_dit and saved_dit in diffusion_models:
dit_config["default"] = saved_dit
vae_config = {"tooltip": "Z-Image VAE model from vae folder."}
if saved_vae and saved_vae in vae_models:
vae_config["default"] = saved_vae
te_config = {"tooltip": "Qwen3 text encoder from text_encoders or clip folder."}
if saved_te and saved_te in text_encoder_list:
te_config["default"] = saved_te
return {
"required": {
"inputcount": ("INT", {"default": 4, "min": 1, "max": 100, "step": 1,
"tooltip": "Number of image inputs. Click 'Update inputs' button after changing."}),
"images_path": ("STRING", {
"default": "",
"tooltip": "Optional: Path to folder containing training images. If provided, images from this folder are used instead of image inputs. Caption .txt files with matching names are used if present."
}),
"musubi_path": ("STRING", {
"default": _musubi_zimage_base_config.get('musubi_path', musubi_fallback),
"tooltip": "Path to musubi-tuner installation."
}),
"dit_model": (diffusion_models, dit_config),
"vae_model": (vae_models, vae_config),
"text_encoder": (text_encoder_list, te_config),
"caption": ("STRING", {
"default": saved.get('caption', "photo of subject"),
"multiline": True,
"tooltip": "Default caption for all images. Per-image caption inputs override this."
}),
"training_steps": ("INT", {
"default": saved.get('training_steps', 400),
"min": 10,
"max": 5000,
"step": 10,
"tooltip": "Number of training steps. 400 is a good starting point."
}),
"learning_rate": ("FLOAT", {
"default": saved.get('learning_rate', 0.0001),
"min": 0.00001,
"max": 0.1,
"step": 0.00001,
"tooltip": "Learning rate. 0.0001 is recommended for Z-Image Base training."
}),
"lora_rank": ("INT", {
"default": saved.get('lora_rank', 32),
"min": 4,
"max": 128,
"step": 4,
"tooltip": "LoRA rank/dimension. 32 is recommended for Z-Image Base."
}),
"vram_mode": (["Max (1256px)", "Max (1256px) fp8", "Max (1256px) fp8 offload", "Medium (1024px)", "Medium (1024px) fp8", "Medium (1024px) fp8 offload", "Low (768px)", "Min (512px)"], {
"default": saved.get('vram_mode', "Low (768px)"),
"tooltip": "VRAM optimization preset. Low/Min always use fp8. Min adds pre-caching for lowest VRAM."
}),
"keep_lora": ("BOOLEAN", {
"default": saved.get('keep_lora', True),
"tooltip": "If True, keeps the trained LoRA file."
}),
"output_name": ("STRING", {
"default": saved.get('output_name', "MyLora"),
"tooltip": "Custom name for the output LoRA. Timestamp will be appended."
}),
"custom_python_exe": ("STRING", {
"default": saved.get('custom_python_exe', ""),
"tooltip": "Advanced: Optionally enter the full path to a custom python.exe (e.g. C:\\my-venv\\Scripts\\python.exe). If empty, uses the venv inside musubi_path. The musubi_path field is still required for locating training scripts."
}),
},
"optional": {
"image_1": ("IMAGE", {"tooltip": "Training image (not needed if images_path is set)."}),
"caption_1": ("STRING", {"forceInput": True, "tooltip": "Caption for image_1. Overrides default caption."}),
"image_2": ("IMAGE", {"tooltip": "Training image."}),
"caption_2": ("STRING", {"forceInput": True, "tooltip": "Caption for image_2. Overrides default caption."}),
"image_3": ("IMAGE", {"tooltip": "Training image."}),
"caption_3": ("STRING", {"forceInput": True, "tooltip": "Caption for image_3. Overrides default caption."}),
"image_4": ("IMAGE", {"tooltip": "Training image."}),
"caption_4": ("STRING", {"forceInput": True, "tooltip": "Caption for image_4. Overrides default caption."}),
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("lora_path",)
OUTPUT_TOOLTIPS = ("Path to the trained Z-Image Base LoRA file (ComfyUI format).",)
FUNCTION = "train_zimage_base_lora"
CATEGORY = "loaders"
DESCRIPTION = "Trains a Z-Image Base (non-distilled) LoRA from images using Musubi Tuner."
def train_zimage_base_lora(
self,
inputcount,
images_path,
musubi_path,
dit_model,
vae_model,
text_encoder,
caption,
training_steps,
learning_rate,
lora_rank,
vram_mode,
keep_lora=True,
output_name="MyLora",
custom_python_exe="",
image_1=None,
**kwargs
):
global _musubi_zimage_base_lora_cache
# Expand paths
musubi_path = os.path.expanduser(musubi_path.strip())
# Get full paths from ComfyUI folders
dit_path = _get_model_path(dit_model, "diffusion_models")
vae_path = _get_model_path(vae_model, "vae")
# Try text_encoders first, then clip
text_encoder_path = _get_model_path(text_encoder, "text_encoders")
if not text_encoder_path or not os.path.exists(text_encoder_path):
text_encoder_path = _get_model_path(text_encoder, "clip")
# Check if using folder path for images
use_folder_path = False
folder_images = []
folder_captions = []
if images_path and images_path.strip():
images_path = os.path.expanduser(images_path.strip())
if os.path.isdir(images_path):
# Find all image files in the folder
image_extensions = ('.png', '.jpg', '.jpeg', '.webp', '.bmp')
for filename in sorted(os.listdir(images_path)):
if filename.lower().endswith(image_extensions):
img_path = os.path.join(images_path, filename)
folder_images.append(img_path)
# Look for matching caption file
base_name = os.path.splitext(filename)[0]
caption_file = os.path.join(images_path, f"{base_name}.txt")
if os.path.exists(caption_file):
with open(caption_file, 'r', encoding='utf-8') as f:
folder_captions.append(f.read().strip())
else:
folder_captions.append(caption) # Use default caption
if folder_images:
use_folder_path = True
print(f"[Musubi Z-Image Base] Using {len(folder_images)} images from folder: {images_path}")
else:
print(f"[Musubi Z-Image Base] No images found in folder: {images_path}, falling back to inputs")
else:
print(f"[Musubi Z-Image Base] Invalid folder path: {images_path}, falling back to inputs")
if not use_folder_path:
# Collect all images and captions from inputs
all_images = []
all_captions = []
if image_1 is not None:
all_images.append(image_1)
cap_1 = kwargs.get("caption_1", "")
all_captions.append(cap_1 if cap_1 else caption)
for i in range(2, inputcount + 1):
img = kwargs.get(f"image_{i}")
if img is not None:
all_images.append(img)
cap = kwargs.get(f"caption_{i}", "")
all_captions.append(cap if cap else caption)
if not all_images:
raise ValueError("No images provided. Either set images_path to a folder containing images, or connect at least one image input.")
num_images = len(folder_images) if use_folder_path else len(all_images)
print(f"[Musubi Z-Image Base] Training with {num_images} image(s)")
print(f"[Musubi Z-Image Base] DiT: {dit_model}")
print(f"[Musubi Z-Image Base] VAE: {vae_model}")
print(f"[Musubi Z-Image Base] Text Encoder: {text_encoder}")
# Get VRAM preset settings
preset = MUSUBI_ZIMAGE_BASE_VRAM_PRESETS.get(vram_mode, MUSUBI_ZIMAGE_BASE_VRAM_PRESETS["Low (768px)"])
print(f"[Musubi Z-Image Base] Using VRAM mode: {vram_mode}")
# Validate paths
accelerate_path = _get_accelerate_path(musubi_path)
train_script = os.path.join(musubi_path, "src", "musubi_tuner", "zimage_train_network.py")
convert_script = os.path.join(musubi_path, "src", "musubi_tuner", "convert_lora.py")
if not os.path.exists(accelerate_path):
raise FileNotFoundError(f"Musubi Tuner accelerate not found at: {accelerate_path}")
if not os.path.exists(train_script):
raise FileNotFoundError(f"zimage_train_network.py not found at: {train_script}")
if not os.path.exists(convert_script):
raise FileNotFoundError(f"convert_lora.py not found at: {convert_script}")
if not dit_path or not os.path.exists(dit_path):
raise FileNotFoundError(f"DiT model not found at: {dit_path}")
if not vae_path or not os.path.exists(vae_path):
raise FileNotFoundError(f"VAE model not found at: {vae_path}")
if not text_encoder_path or not os.path.exists(text_encoder_path):
raise FileNotFoundError(f"Text encoder not found at: {text_encoder_path}")
# Save settings
global _musubi_zimage_base_config
_musubi_zimage_base_config['musubi_path'] = musubi_path
_musubi_zimage_base_config['trainer_settings'] = {
'dit_model': dit_model,
'vae_model': vae_model,
'text_encoder': text_encoder,
'caption': caption,
'training_steps': training_steps,
'learning_rate': learning_rate,
'lora_rank': lora_rank,
'vram_mode': vram_mode,
'keep_lora': keep_lora,
'output_name': output_name,
'custom_python_exe': custom_python_exe,
}
_save_musubi_config()
# Compute hash for caching
if use_folder_path:
image_hash = _compute_image_hash(folder_images, folder_captions, training_steps, learning_rate, lora_rank, vram_mode, output_name, dit_model, vae_model, text_encoder, use_folder_path=True)
else:
image_hash = _compute_image_hash(all_images, all_captions, training_steps, learning_rate, lora_rank, vram_mode, output_name, dit_model, vae_model, text_encoder, use_folder_path=False)
# Check cache
if keep_lora and image_hash in _musubi_zimage_base_lora_cache:
cached_path = _musubi_zimage_base_lora_cache[image_hash]
if os.path.exists(cached_path):
print(f"[Musubi Z-Image Base] Cache hit! Reusing: {cached_path}")
return (cached_path,)
else:
del _musubi_zimage_base_lora_cache[image_hash]
_save_musubi_cache()
# Generate run name with timestamp
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
run_name = f"{output_name}_{timestamp}" if output_name else f"zimage_base_lora_{image_hash}"
# Output folder
output_folder = os.path.join(musubi_path, "output")
os.makedirs(output_folder, exist_ok=True)
lora_output_path = os.path.join(output_folder, f"{run_name}.safetensors")
lora_comfy_path = os.path.join(output_folder, f"{run_name}_comfy.safetensors")
# Auto-increment if file somehow still exists (same second)
if os.path.exists(lora_output_path):
counter = 1
while os.path.exists(os.path.join(output_folder, f"{run_name}_{counter}.safetensors")):
counter += 1
run_name = f"{run_name}_{counter}"
lora_output_path = os.path.join(output_folder, f"{run_name}.safetensors")
lora_comfy_path = os.path.join(output_folder, f"{run_name}_comfy.safetensors")
print(f"[Musubi Z-Image Base] Name exists, using: {run_name}")
# Create temp directory for images
temp_dir = tempfile.mkdtemp(prefix="comfy_musubi_zimage_base_")
image_folder = temp_dir # Musubi uses image_directory directly
try:
# Save images with captions
if use_folder_path:
# Copy images from folder and create caption files
for idx, (src_path, cap) in enumerate(zip(folder_images, folder_captions)):
ext = os.path.splitext(src_path)[1]
dest_path = os.path.join(image_folder, f"image_{idx+1:03d}{ext}")
shutil.copy2(src_path, dest_path)
caption_path = os.path.join(image_folder, f"image_{idx+1:03d}.txt")
with open(caption_path, 'w', encoding='utf-8') as f:
f.write(cap)
else:
# Save tensor images
for idx, img_tensor in enumerate(all_images):
img_data = img_tensor[0]
img_np = (img_data.cpu().numpy() * 255).astype(np.uint8)
img_pil = Image.fromarray(img_np)
image_path = os.path.join(image_folder, f"image_{idx+1:03d}.png")
img_pil.save(image_path, "PNG")
caption_path = os.path.join(image_folder, f"image_{idx+1:03d}.txt")
with open(caption_path, 'w', encoding='utf-8') as f:
f.write(all_captions[idx])
print(f"[Musubi Z-Image Base] Saved {num_images} images to {image_folder}")
# Generate dataset config
config_content = generate_dataset_config(
image_folder=image_folder,
resolution=preset['resolution'],
batch_size=preset['batch_size'],
enable_bucket=True,
)
config_path = os.path.join(temp_dir, "dataset_config.toml")
save_config(config_content, config_path)
print(f"[Musubi Z-Image Base] Dataset config saved to {config_path}")
# Set up subprocess environment
startupinfo = None
if sys.platform == 'win32':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
env = os.environ.copy()
env['PYTHONIOENCODING'] = 'utf-8'
# Use custom python exe if provided, otherwise detect from musubi_path
if custom_python_exe and custom_python_exe.strip():
python_path = custom_python_exe.strip()
if not os.path.exists(python_path):
raise FileNotFoundError(f"Custom python.exe not found at: {python_path}")
else:
python_path = _get_venv_python_path(musubi_path)
# Pre-cache latents and text encoder outputs (REQUIRED for Musubi Z-Image training)
print(f"[Musubi Z-Image Base] Pre-caching latents and text encoder outputs...")
# Cache latents
cache_latents_script = os.path.join(musubi_path, "src", "musubi_tuner", "zimage_cache_latents.py")
if not os.path.exists(cache_latents_script):
raise FileNotFoundError(f"zimage_cache_latents.py not found at: {cache_latents_script}")
print(f"[Musubi Z-Image Base] Caching VAE latents...")
cache_latents_cmd = [
python_path,
cache_latents_script,
f"--dataset_config={config_path}",
f"--vae={vae_path}",
]
cache_latents_process = subprocess.Popen(
cache_latents_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding='utf-8',
errors='replace',
cwd=musubi_path,
startupinfo=startupinfo,
env=env,
)
for line in cache_latents_process.stdout:
line = line.rstrip()
if line:
print(f"[musubi-tuner] {line}")
cache_latents_process.wait()
if cache_latents_process.returncode != 0:
raise RuntimeError(f"Latent caching failed with code {cache_latents_process.returncode}")
print(f"[Musubi Z-Image Base] VAE latents cached.")
# Cache text encoder outputs
cache_te_script = os.path.join(musubi_path, "src", "musubi_tuner", "zimage_cache_text_encoder_outputs.py")
if not os.path.exists(cache_te_script):
raise FileNotFoundError(f"zimage_cache_text_encoder_outputs.py not found at: {cache_te_script}")
print(f"[Musubi Z-Image Base] Caching text encoder outputs...")
cache_te_cmd = [
python_path,
cache_te_script,
f"--dataset_config={config_path}",
f"--text_encoder={text_encoder_path}",
"--batch_size=1",
]
# Use fp8 for text encoder caching if enabled
if preset.get('fp8_llm', False):
cache_te_cmd.append("--fp8_llm")
cache_te_process = subprocess.Popen(
cache_te_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding='utf-8',
errors='replace',
cwd=musubi_path,
startupinfo=startupinfo,
env=env,
)
for line in cache_te_process.stdout:
line = line.rstrip()
if line:
print(f"[musubi-tuner] {line}")
cache_te_process.wait()
if cache_te_process.returncode != 0:
raise RuntimeError(f"Text encoder caching failed with code {cache_te_process.returncode}")
print(f"[Musubi Z-Image Base] Text encoder outputs cached.")
# Build training command
cmd = [
accelerate_path,
"launch",
"--num_cpu_threads_per_process=1",
f"--mixed_precision={preset['mixed_precision']}",
train_script,
f"--dit={dit_path}",
f"--vae={vae_path}",
f"--text_encoder={text_encoder_path}",
f"--dataset_config={config_path}",
"--sdpa",
f"--mixed_precision={preset['mixed_precision']}",
"--timestep_sampling=shift",
"--weighting_scheme=none",
"--discrete_flow_shift=2.0",
f"--optimizer_type={preset['optimizer']}",
f"--learning_rate={learning_rate}",
f"--network_module=networks.lora_zimage",
f"--network_dim={lora_rank}",
f"--network_alpha={lora_rank}",
f"--max_train_steps={training_steps}",
"--max_data_loader_n_workers=2",
"--persistent_data_loader_workers",
f"--output_dir={output_folder}",
f"--output_name={run_name}",
"--seed=42",
]
# Add memory optimization flags
if preset['gradient_checkpointing']:
cmd.append("--gradient_checkpointing")
if preset['fp8_scaled']:
cmd.append("--fp8_base")
cmd.append("--fp8_scaled")
if preset['fp8_llm']:
cmd.append("--fp8_llm")
if preset.get('blocks_to_swap', 0) > 0:
cmd.append(f"--blocks_to_swap={preset['blocks_to_swap']}")
print(f"[Musubi Z-Image Base] Starting training: {run_name}")
print(f"[Musubi Z-Image Base] Images: {num_images}, Steps: {training_steps}, LR: {learning_rate}, Rank: {lora_rank}")
# Run training
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding='utf-8',
errors='replace',
cwd=musubi_path,
startupinfo=startupinfo,
env=env,
)
# Stream output
for line in process.stdout:
line = line.rstrip()
if line:
print(f"[musubi-tuner] {line}")
process.wait()
if process.returncode != 0:
raise RuntimeError(f"Musubi Tuner training failed with code {process.returncode}")
print(f"[Musubi Z-Image Base] Training completed!")
# Find the trained LoRA
if not os.path.exists(lora_output_path):
# Check for alternative naming
possible_files = [f for f in os.listdir(output_folder) if f.startswith(run_name) and f.endswith('.safetensors') and '_comfy' not in f]
if possible_files:
lora_output_path = os.path.join(output_folder, possible_files[-1])
else:
raise FileNotFoundError(f"No LoRA file found in {output_folder}")
print(f"[Musubi Z-Image Base] Found trained LoRA: {lora_output_path}")
# Convert LoRA to ComfyUI format
print(f"[Musubi Z-Image Base] Converting LoRA to ComfyUI format...")
convert_cmd = [
python_path,
convert_script,
"--input", lora_output_path,
"--output", lora_comfy_path,
"--target", "other",
]
convert_process = subprocess.Popen(
convert_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding='utf-8',
errors='replace',
cwd=musubi_path,
startupinfo=startupinfo,
env=env,
)
for line in convert_process.stdout:
line = line.rstrip()
if line:
print(f"[musubi-tuner] {line}")
convert_process.wait()
if convert_process.returncode != 0:
raise RuntimeError(f"LoRA conversion failed with code {convert_process.returncode}")
if not os.path.exists(lora_comfy_path):
raise FileNotFoundError(f"Converted LoRA not found at: {lora_comfy_path}")
print(f"[Musubi Z-Image Base] Converted LoRA: {lora_comfy_path}")
# Handle caching - cache the ComfyUI format LoRA
if keep_lora:
_musubi_zimage_base_lora_cache[image_hash] = lora_comfy_path
_save_musubi_cache()
print(f"[Musubi Z-Image Base] LoRA saved and cached at: {lora_comfy_path}")
else:
print(f"[Musubi Z-Image Base] LoRA available at: {lora_comfy_path}")
return (lora_comfy_path,)
finally:
# Cleanup temp directory
try:
shutil.rmtree(temp_dir)
except Exception as e:
print(f"[Musubi Z-Image Base] Warning: Could not clean up temp dir: {e}")