Skip to content

Commit be4afa0

Browse files
authored
huggingface#7535 Update FloatTensor type hints to Tensor (huggingface#7883)
* find & replace all FloatTensors to Tensor * apply formatting * Update torch.FloatTensor to torch.Tensor in the remaining files * formatting * Fix the rest of the places where FloatTensor is used as well as in documentation * formatting * Update new file from FloatTensor to Tensor
1 parent 04f4bd5 commit be4afa0

File tree

275 files changed

+3765
-3824
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

275 files changed

+3765
-3824
lines changed

docs/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ Here's an example of a tuple return, comprising several objects:
242242

243243
```
244244
Returns:
245-
`tuple(torch.FloatTensor)` comprising various elements depending on the configuration ([`BertConfig`]) and inputs:
246-
- ** loss** (*optional*, returned when `masked_lm_labels` is provided) `torch.FloatTensor` of shape `(1,)` --
245+
`tuple(torch.Tensor)` comprising various elements depending on the configuration ([`BertConfig`]) and inputs:
246+
- ** loss** (*optional*, returned when `masked_lm_labels` is provided) `torch.Tensor` of shape `(1,)` --
247247
Total loss is the sum of the masked language modeling loss and the next sequence prediction (classification) loss.
248-
- **prediction_scores** (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`) --
248+
- **prediction_scores** (`torch.Tensor` of shape `(batch_size, sequence_length, config.vocab_size)`) --
249249
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
250250
```
251251

docs/source/en/optimization/memory.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ from dataclasses import dataclass
261261

262262
@dataclass
263263
class UNet2DConditionOutput:
264-
sample: torch.FloatTensor
264+
sample: torch.Tensor
265265

266266

267267
pipe = StableDiffusionPipeline.from_pretrained(

docs/source/ko/optimization/fp16.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ from dataclasses import dataclass
339339

340340
@dataclass
341341
class UNet2DConditionOutput:
342-
sample: torch.FloatTensor
342+
sample: torch.Tensor
343343

344344

345345
pipe = StableDiffusionPipeline.from_pretrained(

examples/community/bit_diffusion.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def bits_to_decimal(x, bits=BITS):
4444
# modified scheduler step functions for clamping the predicted x_0 between -bit_scale and +bit_scale
4545
def ddim_bit_scheduler_step(
4646
self,
47-
model_output: torch.FloatTensor,
47+
model_output: torch.Tensor,
4848
timestep: int,
49-
sample: torch.FloatTensor,
49+
sample: torch.Tensor,
5050
eta: float = 0.0,
5151
use_clipped_model_output: bool = True,
5252
generator=None,
@@ -56,9 +56,9 @@ def ddim_bit_scheduler_step(
5656
Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion
5757
process from the learned model outputs (most often the predicted noise).
5858
Args:
59-
model_output (`torch.FloatTensor`): direct output from learned diffusion model.
59+
model_output (`torch.Tensor`): direct output from learned diffusion model.
6060
timestep (`int`): current discrete timestep in the diffusion chain.
61-
sample (`torch.FloatTensor`):
61+
sample (`torch.Tensor`):
6262
current instance of sample being created by diffusion process.
6363
eta (`float`): weight of noise for added noise in diffusion step.
6464
use_clipped_model_output (`bool`): TODO
@@ -134,9 +134,9 @@ def ddim_bit_scheduler_step(
134134

135135
def ddpm_bit_scheduler_step(
136136
self,
137-
model_output: torch.FloatTensor,
137+
model_output: torch.Tensor,
138138
timestep: int,
139-
sample: torch.FloatTensor,
139+
sample: torch.Tensor,
140140
prediction_type="epsilon",
141141
generator=None,
142142
return_dict: bool = True,
@@ -145,9 +145,9 @@ def ddpm_bit_scheduler_step(
145145
Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion
146146
process from the learned model outputs (most often the predicted noise).
147147
Args:
148-
model_output (`torch.FloatTensor`): direct output from learned diffusion model.
148+
model_output (`torch.Tensor`): direct output from learned diffusion model.
149149
timestep (`int`): current discrete timestep in the diffusion chain.
150-
sample (`torch.FloatTensor`):
150+
sample (`torch.Tensor`):
151151
current instance of sample being created by diffusion process.
152152
prediction_type (`str`, default `epsilon`):
153153
indicates whether the model predicts the noise (epsilon), or the samples (`sample`).

examples/community/clip_guided_images_mixing_stable_diffusion.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ def cond_fn(
233233
@torch.no_grad()
234234
def __call__(
235235
self,
236-
style_image: Union[torch.FloatTensor, PIL.Image.Image],
237-
content_image: Union[torch.FloatTensor, PIL.Image.Image],
236+
style_image: Union[torch.Tensor, PIL.Image.Image],
237+
content_image: Union[torch.Tensor, PIL.Image.Image],
238238
style_prompt: Optional[str] = None,
239239
content_prompt: Optional[str] = None,
240240
height: Optional[int] = 512,

examples/community/clip_guided_stable_diffusion.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def __call__(
180180
num_cutouts: Optional[int] = 4,
181181
use_cutouts: Optional[bool] = True,
182182
generator: Optional[torch.Generator] = None,
183-
latents: Optional[torch.FloatTensor] = None,
183+
latents: Optional[torch.Tensor] = None,
184184
output_type: Optional[str] = "pil",
185185
return_dict: bool = True,
186186
):

examples/community/clip_guided_stable_diffusion_img2img.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def __call__(
306306
prompt: Union[str, List[str]],
307307
height: Optional[int] = 512,
308308
width: Optional[int] = 512,
309-
image: Union[torch.FloatTensor, PIL.Image.Image] = None,
309+
image: Union[torch.Tensor, PIL.Image.Image] = None,
310310
strength: float = 0.8,
311311
num_inference_steps: Optional[int] = 50,
312312
guidance_scale: Optional[float] = 7.5,
@@ -317,7 +317,7 @@ def __call__(
317317
num_cutouts: Optional[int] = 4,
318318
use_cutouts: Optional[bool] = True,
319319
generator: Optional[torch.Generator] = None,
320-
latents: Optional[torch.FloatTensor] = None,
320+
latents: Optional[torch.Tensor] = None,
321321
output_type: Optional[str] = "pil",
322322
return_dict: bool = True,
323323
):

examples/community/composable_stable_diffusion.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ def __call__(
354354
num_images_per_prompt: Optional[int] = 1,
355355
eta: float = 0.0,
356356
generator: Optional[torch.Generator] = None,
357-
latents: Optional[torch.FloatTensor] = None,
357+
latents: Optional[torch.Tensor] = None,
358358
output_type: Optional[str] = "pil",
359359
return_dict: bool = True,
360-
callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
360+
callback: Optional[Callable[[int, int, torch.Tensor], None]] = None,
361361
callback_steps: int = 1,
362362
weights: Optional[str] = "",
363363
):
@@ -391,7 +391,7 @@ def __call__(
391391
generator (`torch.Generator`, *optional*):
392392
A [torch generator](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation
393393
deterministic.
394-
latents (`torch.FloatTensor`, *optional*):
394+
latents (`torch.Tensor`, *optional*):
395395
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
396396
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
397397
tensor will ge generated by sampling using the supplied random `generator`.
@@ -403,7 +403,7 @@ def __call__(
403403
plain tuple.
404404
callback (`Callable`, *optional*):
405405
A function that will be called every `callback_steps` steps during inference. The function will be
406-
called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
406+
called with the following arguments: `callback(step: int, timestep: int, latents: torch.Tensor)`.
407407
callback_steps (`int`, *optional*, defaults to 1):
408408
The frequency at which the `callback` function will be called. If not specified, the callback will be
409409
called at every step.

examples/community/ddim_noise_comparative_analysis.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def prepare_latents(self, image, timestep, batch_size, dtype, device, generator=
103103
@torch.no_grad()
104104
def __call__(
105105
self,
106-
image: Union[torch.FloatTensor, PIL.Image.Image] = None,
106+
image: Union[torch.Tensor, PIL.Image.Image] = None,
107107
strength: float = 0.8,
108108
batch_size: int = 1,
109109
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
@@ -115,7 +115,7 @@ def __call__(
115115
) -> Union[ImagePipelineOutput, Tuple]:
116116
r"""
117117
Args:
118-
image (`torch.FloatTensor` or `PIL.Image.Image`):
118+
image (`torch.Tensor` or `PIL.Image.Image`):
119119
`Image`, or tensor representing an image batch, that will be used as the starting point for the
120120
process.
121121
strength (`float`, *optional*, defaults to 0.8):

examples/community/gluegen.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def __init__(
205205
safety_checker: StableDiffusionSafetyChecker,
206206
feature_extractor: CLIPImageProcessor,
207207
language_adapter: TranslatorNoLN = None,
208-
tensor_norm: torch.FloatTensor = None,
208+
tensor_norm: torch.Tensor = None,
209209
requires_safety_checker: bool = True,
210210
):
211211
super().__init__()
@@ -231,7 +231,7 @@ def load_language_adapter(
231231
num_token: int,
232232
dim: int,
233233
dim_out: int,
234-
tensor_norm: torch.FloatTensor,
234+
tensor_norm: torch.Tensor,
235235
mult: int = 2,
236236
depth: int = 5,
237237
):
@@ -242,7 +242,7 @@ def load_language_adapter(
242242
)
243243
self.language_adapter.load_state_dict(torch.load(model_path))
244244

245-
def _adapt_language(self, prompt_embeds: torch.FloatTensor):
245+
def _adapt_language(self, prompt_embeds: torch.Tensor):
246246
prompt_embeds = prompt_embeds / 3
247247
prompt_embeds = self.language_adapter(prompt_embeds) * (self.tensor_norm / 2)
248248
return prompt_embeds
@@ -254,8 +254,8 @@ def encode_prompt(
254254
num_images_per_prompt,
255255
do_classifier_free_guidance,
256256
negative_prompt=None,
257-
prompt_embeds: Optional[torch.FloatTensor] = None,
258-
negative_prompt_embeds: Optional[torch.FloatTensor] = None,
257+
prompt_embeds: Optional[torch.Tensor] = None,
258+
negative_prompt_embeds: Optional[torch.Tensor] = None,
259259
lora_scale: Optional[float] = None,
260260
clip_skip: Optional[int] = None,
261261
):
@@ -275,10 +275,10 @@ def encode_prompt(
275275
The prompt or prompts not to guide the image generation. If not defined, one has to pass
276276
`negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
277277
less than `1`).
278-
prompt_embeds (`torch.FloatTensor`, *optional*):
278+
prompt_embeds (`torch.Tensor`, *optional*):
279279
Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
280280
provided, text embeddings will be generated from `prompt` input argument.
281-
negative_prompt_embeds (`torch.FloatTensor`, *optional*):
281+
negative_prompt_embeds (`torch.Tensor`, *optional*):
282282
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
283283
weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
284284
argument.
@@ -535,7 +535,7 @@ def get_guidance_scale_embedding(self, w, embedding_dim=512, dtype=torch.float32
535535
data type of the generated embeddings
536536
537537
Returns:
538-
`torch.FloatTensor`: Embedding vectors with shape `(len(timesteps), embedding_dim)`
538+
`torch.Tensor`: Embedding vectors with shape `(len(timesteps), embedding_dim)`
539539
"""
540540
assert len(w.shape) == 1
541541
w = w * 1000.0
@@ -594,9 +594,9 @@ def __call__(
594594
num_images_per_prompt: Optional[int] = 1,
595595
eta: float = 0.0,
596596
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
597-
latents: Optional[torch.FloatTensor] = None,
598-
prompt_embeds: Optional[torch.FloatTensor] = None,
599-
negative_prompt_embeds: Optional[torch.FloatTensor] = None,
597+
latents: Optional[torch.Tensor] = None,
598+
prompt_embeds: Optional[torch.Tensor] = None,
599+
negative_prompt_embeds: Optional[torch.Tensor] = None,
600600
output_type: Optional[str] = "pil",
601601
return_dict: bool = True,
602602
cross_attention_kwargs: Optional[Dict[str, Any]] = None,
@@ -635,14 +635,14 @@ def __call__(
635635
generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
636636
A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make
637637
generation deterministic.
638-
latents (`torch.FloatTensor`, *optional*):
638+
latents (`torch.Tensor`, *optional*):
639639
Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image
640640
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
641641
tensor is generated by sampling using the supplied random `generator`.
642-
prompt_embeds (`torch.FloatTensor`, *optional*):
642+
prompt_embeds (`torch.Tensor`, *optional*):
643643
Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
644644
provided, text embeddings are generated from the `prompt` input argument.
645-
negative_prompt_embeds (`torch.FloatTensor`, *optional*):
645+
negative_prompt_embeds (`torch.Tensor`, *optional*):
646646
Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
647647
not provided, `negative_prompt_embeds` are generated from the `negative_prompt` input argument.
648648
ip_adapter_image: (`PipelineImageInput`, *optional*): Optional image input to work with IP Adapters.

examples/community/hd_painter.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def __init__(self, mask, token_idx, scale_factor):
2828
def __call__(
2929
self,
3030
attn: Attention,
31-
hidden_states: torch.FloatTensor,
32-
encoder_hidden_states: Optional[torch.FloatTensor] = None,
33-
attention_mask: Optional[torch.FloatTensor] = None,
34-
temb: Optional[torch.FloatTensor] = None,
31+
hidden_states: torch.Tensor,
32+
encoder_hidden_states: Optional[torch.Tensor] = None,
33+
attention_mask: Optional[torch.Tensor] = None,
34+
temb: Optional[torch.Tensor] = None,
3535
scale: float = 1.0,
3636
) -> torch.Tensor:
3737
# Same as the default AttnProcessor up untill the part where similarity matrix gets saved
@@ -111,10 +111,10 @@ def __init__(self, transformer_block, mask, token_idx, do_classifier_free_guidan
111111
def __call__(
112112
self,
113113
attn: Attention,
114-
hidden_states: torch.FloatTensor,
115-
encoder_hidden_states: Optional[torch.FloatTensor] = None,
116-
attention_mask: Optional[torch.FloatTensor] = None,
117-
temb: Optional[torch.FloatTensor] = None,
114+
hidden_states: torch.Tensor,
115+
encoder_hidden_states: Optional[torch.Tensor] = None,
116+
attention_mask: Optional[torch.Tensor] = None,
117+
temb: Optional[torch.Tensor] = None,
118118
scale: float = 1.0,
119119
) -> torch.Tensor:
120120
# Automatically recognize the resolution of the current attention layer and resize the masks accordingly
@@ -454,7 +454,7 @@ def __call__(
454454
prompt: Union[str, List[str]] = None,
455455
image: PipelineImageInput = None,
456456
mask_image: PipelineImageInput = None,
457-
masked_image_latents: torch.FloatTensor = None,
457+
masked_image_latents: torch.Tensor = None,
458458
height: Optional[int] = None,
459459
width: Optional[int] = None,
460460
padding_mask_crop: Optional[int] = None,
@@ -467,9 +467,9 @@ def __call__(
467467
num_images_per_prompt: Optional[int] = 1,
468468
eta: float = 0.01,
469469
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
470-
latents: Optional[torch.FloatTensor] = None,
471-
prompt_embeds: Optional[torch.FloatTensor] = None,
472-
negative_prompt_embeds: Optional[torch.FloatTensor] = None,
470+
latents: Optional[torch.Tensor] = None,
471+
prompt_embeds: Optional[torch.Tensor] = None,
472+
negative_prompt_embeds: Optional[torch.Tensor] = None,
473473
ip_adapter_image: Optional[PipelineImageInput] = None,
474474
output_type: Optional[str] = "pil",
475475
return_dict: bool = True,

examples/community/iadb.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ class IADBScheduler(SchedulerMixin, ConfigMixin):
1717

1818
def step(
1919
self,
20-
model_output: torch.FloatTensor,
20+
model_output: torch.Tensor,
2121
timestep: int,
22-
x_alpha: torch.FloatTensor,
23-
) -> torch.FloatTensor:
22+
x_alpha: torch.Tensor,
23+
) -> torch.Tensor:
2424
"""
2525
Predict the sample at the previous timestep by reversing the ODE. Core function to propagate the diffusion
2626
process from the learned model outputs (most often the predicted noise).
2727
2828
Args:
29-
model_output (`torch.FloatTensor`): direct output from learned diffusion model. It is the direction from x0 to x1.
29+
model_output (`torch.Tensor`): direct output from learned diffusion model. It is the direction from x0 to x1.
3030
timestep (`float`): current timestep in the diffusion chain.
31-
x_alpha (`torch.FloatTensor`): x_alpha sample for the current timestep
31+
x_alpha (`torch.Tensor`): x_alpha sample for the current timestep
3232
3333
Returns:
34-
`torch.FloatTensor`: the sample at the previous timestep
34+
`torch.Tensor`: the sample at the previous timestep
3535
3636
"""
3737
if self.num_inference_steps is None:
@@ -53,10 +53,10 @@ def set_timesteps(self, num_inference_steps: int):
5353

5454
def add_noise(
5555
self,
56-
original_samples: torch.FloatTensor,
57-
noise: torch.FloatTensor,
58-
alpha: torch.FloatTensor,
59-
) -> torch.FloatTensor:
56+
original_samples: torch.Tensor,
57+
noise: torch.Tensor,
58+
alpha: torch.Tensor,
59+
) -> torch.Tensor:
6060
return original_samples * alpha + noise * (1 - alpha)
6161

6262
def __len__(self):

examples/community/imagic_stable_diffusion.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(
110110
def train(
111111
self,
112112
prompt: Union[str, List[str]],
113-
image: Union[torch.FloatTensor, PIL.Image.Image],
113+
image: Union[torch.Tensor, PIL.Image.Image],
114114
height: Optional[int] = 512,
115115
width: Optional[int] = 512,
116116
generator: Optional[torch.Generator] = None,
@@ -144,7 +144,7 @@ def train(
144144
generator (`torch.Generator`, *optional*):
145145
A [torch generator](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation
146146
deterministic.
147-
latents (`torch.FloatTensor`, *optional*):
147+
latents (`torch.Tensor`, *optional*):
148148
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
149149
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
150150
tensor will ge generated by sampling using the supplied random `generator`.

0 commit comments

Comments
 (0)