Skip to content

Commit 27a49ec

Browse files
authored
Merge branch 'main' into readme-spelling-sana
2 parents e282ce0 + 0ac52d6 commit 27a49ec

File tree

8 files changed

+71
-7
lines changed

8 files changed

+71
-7
lines changed

examples/community/pipeline_hunyuandit_differential_img2img.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,8 @@ def __call__(
10081008
self.transformer.inner_dim // self.transformer.num_heads,
10091009
grid_crops_coords,
10101010
(grid_height, grid_width),
1011+
device=device,
1012+
output_type="pt",
10111013
)
10121014

10131015
style = torch.tensor([0], device=device)

src/diffusers/models/embeddings.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,57 @@ def get_3d_rotary_pos_embed_allegro(
957957
return freqs_t, freqs_h, freqs_w, grid_t, grid_h, grid_w
958958

959959

960-
def get_2d_rotary_pos_embed(embed_dim, crops_coords, grid_size, use_real=True):
960+
def get_2d_rotary_pos_embed(
961+
embed_dim, crops_coords, grid_size, use_real=True, device: Optional[torch.device] = None, output_type: str = "np"
962+
):
963+
"""
964+
RoPE for image tokens with 2d structure.
965+
966+
Args:
967+
embed_dim: (`int`):
968+
The embedding dimension size
969+
crops_coords (`Tuple[int]`)
970+
The top-left and bottom-right coordinates of the crop.
971+
grid_size (`Tuple[int]`):
972+
The grid size of the positional embedding.
973+
use_real (`bool`):
974+
If True, return real part and imaginary part separately. Otherwise, return complex numbers.
975+
device: (`torch.device`, **optional**):
976+
The device used to create tensors.
977+
978+
Returns:
979+
`torch.Tensor`: positional embedding with shape `( grid_size * grid_size, embed_dim/2)`.
980+
"""
981+
if output_type == "np":
982+
deprecation_message = (
983+
"`get_2d_sincos_pos_embed` uses `torch` and supports `device`."
984+
" `from_numpy` is no longer required."
985+
" Pass `output_type='pt' to use the new version now."
986+
)
987+
deprecate("output_type=='np'", "0.33.0", deprecation_message, standard_warn=False)
988+
return _get_2d_rotary_pos_embed_np(
989+
embed_dim=embed_dim,
990+
crops_coords=crops_coords,
991+
grid_size=grid_size,
992+
use_real=use_real,
993+
)
994+
start, stop = crops_coords
995+
# scale end by (steps−1)/steps matches np.linspace(..., endpoint=False)
996+
grid_h = torch.linspace(
997+
start[0], stop[0] * (grid_size[0] - 1) / grid_size[0], grid_size[0], device=device, dtype=torch.float32
998+
)
999+
grid_w = torch.linspace(
1000+
start[1], stop[1] * (grid_size[1] - 1) / grid_size[1], grid_size[1], device=device, dtype=torch.float32
1001+
)
1002+
grid = torch.meshgrid(grid_w, grid_h, indexing="xy")
1003+
grid = torch.stack(grid, dim=0) # [2, W, H]
1004+
1005+
grid = grid.reshape([2, 1, *grid.shape[1:]])
1006+
pos_embed = get_2d_rotary_pos_embed_from_grid(embed_dim, grid, use_real=use_real)
1007+
return pos_embed
1008+
1009+
1010+
def _get_2d_rotary_pos_embed_np(embed_dim, crops_coords, grid_size, use_real=True):
9611011
"""
9621012
RoPE for image tokens with 2d structure.
9631013

src/diffusers/pipelines/controlnet_hunyuandit/pipeline_hunyuandit_controlnet.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,11 @@ def __call__(
925925
base_size = 512 // 8 // self.transformer.config.patch_size
926926
grid_crops_coords = get_resize_crop_region_for_grid((grid_height, grid_width), base_size)
927927
image_rotary_emb = get_2d_rotary_pos_embed(
928-
self.transformer.inner_dim // self.transformer.num_heads, grid_crops_coords, (grid_height, grid_width)
928+
self.transformer.inner_dim // self.transformer.num_heads,
929+
grid_crops_coords,
930+
(grid_height, grid_width),
931+
device=device,
932+
output_type="pt",
929933
)
930934

931935
style = torch.tensor([0], device=device)

src/diffusers/pipelines/hunyuandit/pipeline_hunyuandit.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,11 @@ def __call__(
798798
base_size = 512 // 8 // self.transformer.config.patch_size
799799
grid_crops_coords = get_resize_crop_region_for_grid((grid_height, grid_width), base_size)
800800
image_rotary_emb = get_2d_rotary_pos_embed(
801-
self.transformer.inner_dim // self.transformer.num_heads, grid_crops_coords, (grid_height, grid_width)
801+
self.transformer.inner_dim // self.transformer.num_heads,
802+
grid_crops_coords,
803+
(grid_height, grid_width),
804+
device=device,
805+
output_type="pt",
802806
)
803807

804808
style = torch.tensor([0], device=device)

src/diffusers/pipelines/ltx/pipeline_ltx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 Black Forest Labs and The HuggingFace Team. All rights reserved.
1+
# Copyright 2024 Lightricks and The HuggingFace Team. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

src/diffusers/pipelines/ltx/pipeline_ltx_image2video.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 Black Forest Labs and The HuggingFace Team. All rights reserved.
1+
# Copyright 2024 Lightricks and The HuggingFace Team. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

src/diffusers/pipelines/mochi/pipeline_mochi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 Black Forest Labs and The HuggingFace Team. All rights reserved.
1+
# Copyright 2024 Genmo and The HuggingFace Team. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

src/diffusers/pipelines/pag/pipeline_pag_hunyuandit.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,11 @@ def __call__(
818818
base_size = 512 // 8 // self.transformer.config.patch_size
819819
grid_crops_coords = get_resize_crop_region_for_grid((grid_height, grid_width), base_size)
820820
image_rotary_emb = get_2d_rotary_pos_embed(
821-
self.transformer.inner_dim // self.transformer.num_heads, grid_crops_coords, (grid_height, grid_width)
821+
self.transformer.inner_dim // self.transformer.num_heads,
822+
grid_crops_coords,
823+
(grid_height, grid_width),
824+
device=device,
825+
output_type="pt",
822826
)
823827

824828
style = torch.tensor([0], device=device)

0 commit comments

Comments
 (0)