@@ -324,6 +324,7 @@ def train_epoch(
324324 uncond_text_emb = None , use_wandb = False , use_min_snr = True ,
325325 min_snr_gamma = 5.0 , cfg_dropout = 0.0 ,
326326 save_steps = 0 , ckpt_dir = "checkpoints" , best_loss = float ("inf" ),
327+ memory_format : str = "channels_last" ,
327328) -> tuple [float , int ]:
328329 ddp_unet .train ()
329330 model .text_encoder .eval ()
@@ -338,7 +339,8 @@ def train_epoch(
338339 continue
339340 try :
340341 latents = batch ["pixel_values" ].to (device , dtype = torch .bfloat16 , non_blocking = True )
341- latents = latents .contiguous (memory_format = torch .channels_last )
342+ if memory_format == "channels_last" :
343+ latents = latents .contiguous (memory_format = torch .channels_last )
342344 ids = batch ["input_ids" ].to (device , non_blocking = True )
343345 mask = batch ["attention_mask" ].to (device , non_blocking = True )
344346
@@ -603,7 +605,8 @@ def main(rank, world_size, args):
603605
604606 noise_scheduler = DDPMScheduler (steps = 1000 , beta_start = 0.00085 , beta_end = 0.012 , schedule = "scaled_linear" )
605607 model = StableDiffusionModel (vae , text_enc , unet , noise_scheduler ).to (device )
606- model .unet = model .unet .to (memory_format = torch .channels_last )
608+ if args .memory_format == "channels_last" :
609+ model .unet = model .unet .to (memory_format = torch .channels_last )
607610 noise_scheduler .to (device )
608611
609612 # ── Dataset + unconditional embedding (precomputed once) ──────────────────
@@ -681,6 +684,7 @@ def main(rank, world_size, args):
681684 use_wandb = args .use_wandb , use_min_snr = args .min_snr , min_snr_gamma = args .min_snr_gamma ,
682685 cfg_dropout = args .cfg_dropout ,
683686 save_steps = args .save_steps , ckpt_dir = args .ckpt_dir , best_loss = best_loss ,
687+ memory_format = args .memory_format ,
684688 )
685689
686690 if avg_loss < best_loss :
@@ -720,7 +724,7 @@ def main(rank, world_size, args):
720724
721725 # Data
722726 parser .add_argument ("--cache_path" , type = str , default = "laion_hf_dataset/train" )
723- parser .add_argument ("--latent_dir" , type = str , default = "laion_latents/laion_latents " )
727+ parser .add_argument ("--latent_dir" , type = str , default = "laion_latents" )
724728 parser .add_argument ("--val_size" , type = int , default = 500 )
725729
726730 # Model
@@ -743,6 +747,10 @@ def main(rank, world_size, args):
743747 parser .add_argument ("--no-min-snr" , dest = "min_snr" , action = "store_false" )
744748 parser .add_argument ("--min_snr_gamma" , type = float , default = 5.0 )
745749 parser .add_argument ("--cfg_dropout" , type = float , default = 0.05 , help = "CFG dropout probability." )
750+ parser .add_argument ("--memory_format" , type = str , default = "channels_last" ,
751+ choices = ("channels_last" , "contiguous" ),
752+ help = "Memory format for UNet and latents. channels_last speeds up convs on NVIDIA GPUs. "
753+ "Use 'contiguous' for AMD or Apple Silicon." )
746754
747755 # Checkpointing
748756 parser .add_argument ("--save_every" , type = int , default = 1 , help = "Save checkpoint every N epochs." )
0 commit comments