44import torch
55
66from diffusers import (
7+ AutoencoderKL ,
78 AutoPipelineForImage2Image ,
89 AutoPipelineForInpainting ,
910 AutoPipelineForText2Image ,
1516 StableDiffusionXLControlNetPipeline ,
1617 T2IAdapter ,
1718 WuerstchenCombinedPipeline ,
18- AutoencoderKL ,
1919)
2020from diffusers .utils import load_image
2121
3131 flush ,
3232 generate_csv_dict ,
3333 generate_csv_dict_model ,
34- write_to_csv ,
3534 write_list_to_csv ,
35+ write_to_csv ,
3636)
3737
3838
@@ -359,11 +359,7 @@ def __init__(self):
359359
360360 def get_result_filepath (self , suffix ):
361361 name = (
362- self .model_class_name
363- + "_"
364- + self .pretrained_model_name_or_path .replace ("/" , "_" )
365- + "_"
366- + f"{ suffix } .csv"
362+ self .model_class_name + "_" + self .pretrained_model_name_or_path .replace ("/" , "_" ) + "_" + f"{ suffix } .csv"
367363 )
368364 filepath = os .path .join (BASE_PATH , name )
369365 return filepath
@@ -375,7 +371,9 @@ class AutoencoderKLBenchmark(BaseBenchmarkTestCase):
375371 def __init__ (self , pretrained_model_name_or_path , dtype , tiling , ** kwargs ):
376372 super ().__init__ ()
377373 self .dtype = getattr (torch , dtype )
378- model = self .model_class .from_pretrained (pretrained_model_name_or_path , torch_dtype = self .dtype , ** kwargs ).eval ()
374+ model = self .model_class .from_pretrained (
375+ pretrained_model_name_or_path , torch_dtype = self .dtype , ** kwargs
376+ ).eval ()
379377 model = model .to ("cuda" )
380378 self .tiling = False
381379 if tiling :
@@ -389,13 +387,15 @@ def __init__(self, pretrained_model_name_or_path, dtype, tiling, **kwargs):
389387 def run_decode (self , model , tensor ):
390388 _ = model .decode (tensor )
391389
392- @torch .no_grad
390+ @torch .no_grad ()
393391 def _test_decode (self , ** kwargs ):
394392 batch = kwargs .get ("batch" )
395393 height = kwargs .get ("height" )
396394 width = kwargs .get ("width" )
397395
398- tensor = torch .randn ((batch , self .model .config .latent_channels , height , width ), dtype = self .dtype , device = "cuda" )
396+ tensor = torch .randn (
397+ (batch , self .model .config .latent_channels , height , width ), dtype = self .dtype , device = "cuda"
398+ )
399399
400400 try :
401401 time = benchmark_fn (self .run_decode , self .model , tensor )
@@ -406,7 +406,10 @@ def _test_decode(self, **kwargs):
406406
407407 benchmark_info = BenchmarkInfo (time = time , memory = memory )
408408 csv_dict = generate_csv_dict_model (
409- model_cls = self .model_class_name , ckpt = self .pretrained_model_name_or_path , benchmark_info = benchmark_info , ** kwargs ,
409+ model_cls = self .model_class_name ,
410+ ckpt = self .pretrained_model_name_or_path ,
411+ benchmark_info = benchmark_info ,
412+ ** kwargs ,
410413 )
411414 print (f"{ self .model_class_name } decode - shape: { list (tensor .shape )} , time: { time } , memory: { memory } " )
412415 return csv_dict
@@ -416,15 +419,92 @@ def test_decode(self):
416419
417420 batches = (1 ,)
418421 # heights = (32, 64, 128, 256,)
419- widths = (32 , 64 , 128 , 256 ,)
422+ widths = (
423+ 32 ,
424+ 64 ,
425+ 128 ,
426+ 256 ,
427+ )
420428 for batch in batches :
421429 # for height in heights:
422- for width in widths :
423- benchmark_info = self ._test_decode (batch = batch , height = width , width = width )
424- benchmark_infos .append (benchmark_info )
430+ for width in widths :
431+ benchmark_info = self ._test_decode (batch = batch , height = width , width = width )
432+ benchmark_infos .append (benchmark_info )
425433
426434 suffix = "decode"
427435 if self .tiling :
428436 suffix = "tiled_decode"
429437 filepath = self .get_result_filepath (suffix )
430438 write_list_to_csv (filepath , benchmark_infos )
439+
440+
441+ class AutoencoderKLEncodeBenchmark (BaseBenchmarkTestCase ):
442+ model_class = AutoencoderKL
443+
444+ def __init__ (self , pretrained_model_name_or_path , dtype , tiling , ** kwargs ):
445+ super ().__init__ ()
446+ self .dtype = getattr (torch , dtype )
447+ model = self .model_class .from_pretrained (
448+ pretrained_model_name_or_path , torch_dtype = self .dtype , ** kwargs
449+ ).eval ()
450+ model = model .to ("cuda" )
451+ self .tiling = False
452+ if tiling :
453+ model .enable_tiling ()
454+ self .tiling = True
455+ self .model = model
456+ self .model_class_name = str (self .model .__class__ .__name__ )
457+ self .pretrained_model_name_or_path = pretrained_model_name_or_path
458+
459+ @torch .no_grad ()
460+ def run_encode (self , model , tensor ):
461+ _ = model .encode (tensor )
462+
463+ @torch .no_grad ()
464+ def _test_encode (self , ** kwargs ):
465+ batch = kwargs .get ("batch" )
466+ height = kwargs .get ("height" )
467+ width = kwargs .get ("width" )
468+
469+ tensor = torch .randn (
470+ (batch , self .model .config .latent_channels , height , width ), dtype = self .dtype , device = "cuda"
471+ )
472+
473+ try :
474+ time = benchmark_fn (self .run_encode , self .model , tensor )
475+ memory = bytes_to_giga_bytes (torch .cuda .max_memory_reserved ())
476+ except torch .OutOfMemoryError :
477+ time = "OOM"
478+ memory = "OOM"
479+
480+ benchmark_info = BenchmarkInfo (time = time , memory = memory )
481+ csv_dict = generate_csv_dict_model (
482+ model_cls = self .model_class_name ,
483+ ckpt = self .pretrained_model_name_or_path ,
484+ benchmark_info = benchmark_info ,
485+ ** kwargs ,
486+ )
487+ print (f"{ self .model_class_name } encode - shape: { list (tensor .shape )} , time: { time } , memory: { memory } " )
488+ return csv_dict
489+
490+ def test_encode (self ):
491+ benchmark_infos = []
492+
493+ batches = (1 ,)
494+ widths = (
495+ 256 ,
496+ 512 ,
497+ 1024 ,
498+ 2048 ,
499+ )
500+ for batch in batches :
501+ # for height in heights:
502+ for width in widths :
503+ benchmark_info = self ._test_encode (batch = batch , height = width , width = width )
504+ benchmark_infos .append (benchmark_info )
505+
506+ suffix = "encode"
507+ if self .tiling :
508+ suffix = "tiled_encode"
509+ filepath = self .get_result_filepath (suffix )
510+ write_list_to_csv (filepath , benchmark_infos )
0 commit comments