3636 UNet2DConditionModel ,
3737 UniPCMultistepScheduler ,
3838)
39- from diffusers .utils .import_utils import is_torch_neuronx_available
4039
4140from ...testing_utils import (
4241 backend_empty_cache ,
42+ backend_synchronize ,
4343 enable_full_determinism ,
4444 load_image ,
4545 numpy_cosine_similarity_distance ,
@@ -987,10 +987,8 @@ class StableDiffusionXLTurboPipelineIntegrationTests(unittest.TestCase):
987987
988988 def setUp (self ):
989989 super ().setUp ()
990- self ._saved_env = {}
991- if is_torch_neuronx_available ():
992- self ._saved_env ["TORCH_NEURONX_ENABLE_NKI_SDPA" ] = os .environ .get ("TORCH_NEURONX_ENABLE_NKI_SDPA" )
993- os .environ .setdefault ("TORCH_NEURONX_ENABLE_NKI_SDPA" , "0" )
990+ self ._saved_env = {"TORCH_NEURONX_ENABLE_NKI_SDPA" : os .environ .get ("TORCH_NEURONX_ENABLE_NKI_SDPA" )}
991+ os .environ .setdefault ("TORCH_NEURONX_ENABLE_NKI_SDPA" , "0" )
994992 gc .collect ()
995993 backend_empty_cache (torch_device )
996994
@@ -1009,8 +1007,7 @@ def test_sdxl_turbo_512(self):
10091007
10101008 pipe = AutoPipelineForText2Image .from_pretrained (self .ckpt_id , torch_dtype = torch .float16 , variant = "fp16" )
10111009 pipe .to (torch_device )
1012- if is_torch_neuronx_available ():
1013- torch .neuron .synchronize ()
1010+ backend_synchronize (torch_device )
10141011 pipe .set_progress_bar_config (disable = None )
10151012
10161013 image = pipe (
@@ -1026,3 +1023,59 @@ def test_sdxl_turbo_512(self):
10261023 self .assertTrue (np .all ((image >= 0.0 ) & (image <= 1.0 )), "Pixel values must be in [0, 1]" )
10271024 expected_slice = np .array ([0.3524 , 0.3160 , 0.3652 , 0.3316 , 0.3376 , 0.3315 , 0.3042 , 0.3102 , 0.3449 ])
10281025 self .assertLess (np .abs (image_slice .flatten () - expected_slice ).max (), 5e-2 )
1026+
1027+ @require_torch_neuron
1028+ def test_sdxl_turbo_neuron_compile_256 (self ):
1029+ from torch_neuronx .neuron_dynamo_backend import set_model_name
1030+ from transformers .utils .output_capturing import install_all_output_capturing_hooks
1031+
1032+ device = torch .neuron .current_device ()
1033+ generator = torch .Generator ("cpu" ).manual_seed (0 )
1034+
1035+ pipe = AutoPipelineForText2Image .from_pretrained (self .ckpt_id , torch_dtype = torch .bfloat16 , variant = "fp16" )
1036+ pipe = pipe .to (device )
1037+ backend_synchronize (torch_device )
1038+
1039+ pipe .unet .eval ()
1040+ pipe .vae .eval ()
1041+ pipe .text_encoder .eval ()
1042+ pipe .text_encoder_2 .eval ()
1043+
1044+ install_all_output_capturing_hooks (pipe .text_encoder )
1045+ set_model_name ("sdxl_turbo_text_encoder" )
1046+ pipe .text_encoder = torch .compile (pipe .text_encoder , backend = "neuron" , fullgraph = True )
1047+
1048+ install_all_output_capturing_hooks (pipe .text_encoder_2 )
1049+ set_model_name ("sdxl_turbo_text_encoder_2" )
1050+ pipe .text_encoder_2 = torch .compile (pipe .text_encoder_2 , backend = "neuron" , fullgraph = True )
1051+
1052+ set_model_name ("sdxl_turbo_unet" )
1053+ pipe .unet = torch .compile (pipe .unet , backend = "neuron" , fullgraph = True )
1054+
1055+ # Pre-warm text encoders and copy ops for 256×256 (latent: 32×32).
1056+ tok_kwargs = {"padding" : "max_length" , "max_length" : 77 , "truncation" : True , "return_tensors" : "pt" }
1057+ with torch .no_grad ():
1058+ _ids = pipe .tokenizer ("warmup" , ** tok_kwargs ).input_ids .to (device )
1059+ _ = pipe .text_encoder (_ids , output_hidden_states = True )
1060+ _ids2 = pipe .tokenizer_2 ("warmup" , ** tok_kwargs ).input_ids .to (device )
1061+ _ = pipe .text_encoder_2 (_ids2 , output_hidden_states = True )
1062+ for _shape , _dtype in [((1 , 4 , 32 , 32 ), torch .bfloat16 ), ((1 , 6 ), torch .bfloat16 )]:
1063+ _ = torch .zeros (_shape , dtype = _dtype ).to (device )
1064+ backend_synchronize (torch_device )
1065+
1066+ image = pipe (
1067+ self .prompt ,
1068+ height = 256 ,
1069+ width = 256 ,
1070+ num_inference_steps = 1 ,
1071+ guidance_scale = 0.0 ,
1072+ generator = generator ,
1073+ output_type = "np" ,
1074+ ).images
1075+
1076+ self .assertEqual (image .shape , (1 , 256 , 256 , 3 ))
1077+ self .assertFalse (np .isnan (image ).any (), "Output contains NaN values" )
1078+ self .assertTrue (
1079+ (image >= 0.0 ).all () and (image <= 1.0 ).all (),
1080+ "Output pixel values outside [0, 1]" ,
1081+ )
0 commit comments