11import inspect
2+ import warnings
23from typing import List , Optional , Union
34
45import torch
@@ -45,11 +46,20 @@ def __call__(
4546 guidance_scale : Optional [float ] = 7.5 ,
4647 eta : Optional [float ] = 0.0 ,
4748 generator : Optional [torch .Generator ] = None ,
48- torch_device : Optional [Union [str , torch .device ]] = None ,
4949 output_type : Optional [str ] = "pil" ,
50+ ** kwargs ,
5051 ):
51- if torch_device is None :
52- torch_device = "cuda" if torch .cuda .is_available () else "cpu"
52+ if "torch_device" in kwargs :
53+ device = kwargs .pop ("torch_device" )
54+ warnings .warn (
55+ "`torch_device` is deprecated as an input argument to `__call__` and will be removed in v0.3.0."
56+ " Consider using `pipe.to(torch_device)` instead."
57+ )
58+
59+ # Set device as before (to be removed in 0.3.0)
60+ if device is None :
61+ device = "cuda" if torch .cuda .is_available () else "cpu"
62+ self .to (device )
5363
5464 if isinstance (prompt , str ):
5565 batch_size = 1
@@ -61,11 +71,6 @@ def __call__(
6171 if height % 8 != 0 or width % 8 != 0 :
6272 raise ValueError (f"`height` and `width` have to be divisible by 8 but are { height } and { width } ." )
6373
64- self .unet .to (torch_device )
65- self .vae .to (torch_device )
66- self .text_encoder .to (torch_device )
67- self .safety_checker .to (torch_device )
68-
6974 # get prompt text embeddings
7075 text_input = self .tokenizer (
7176 prompt ,
@@ -74,7 +79,7 @@ def __call__(
7479 truncation = True ,
7580 return_tensors = "pt" ,
7681 )
77- text_embeddings = self .text_encoder (text_input .input_ids .to (torch_device ))[0 ]
82+ text_embeddings = self .text_encoder (text_input .input_ids .to (self . device ))[0 ]
7883
7984 # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
8085 # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
@@ -86,7 +91,7 @@ def __call__(
8691 uncond_input = self .tokenizer (
8792 ["" ] * batch_size , padding = "max_length" , max_length = max_length , return_tensors = "pt"
8893 )
89- uncond_embeddings = self .text_encoder (uncond_input .input_ids .to (torch_device ))[0 ]
94+ uncond_embeddings = self .text_encoder (uncond_input .input_ids .to (self . device ))[0 ]
9095
9196 # For classifier free guidance, we need to do two forward passes.
9297 # Here we concatenate the unconditional and text embeddings into a single batch
@@ -97,7 +102,7 @@ def __call__(
97102 latents = torch .randn (
98103 (batch_size , self .unet .in_channels , height // 8 , width // 8 ),
99104 generator = generator ,
100- device = torch_device ,
105+ device = self . device ,
101106 )
102107
103108 # set timesteps
@@ -150,7 +155,7 @@ def __call__(
150155 image = image .cpu ().permute (0 , 2 , 3 , 1 ).numpy ()
151156
152157 # run safety checker
153- safety_cheker_input = self .feature_extractor (self .numpy_to_pil (image ), return_tensors = "pt" ).to (torch_device )
158+ safety_cheker_input = self .feature_extractor (self .numpy_to_pil (image ), return_tensors = "pt" ).to (self . device )
154159 image , has_nsfw_concept = self .safety_checker (images = image , clip_input = safety_cheker_input .pixel_values )
155160
156161 if output_type == "pil" :
0 commit comments