diffusers 0.24.0__py3-none-any.whl → 0.25.1__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- diffusers/__init__.py +11 -1
- diffusers/commands/fp16_safetensors.py +10 -11
- diffusers/configuration_utils.py +12 -8
- diffusers/dependency_versions_table.py +3 -2
- diffusers/experimental/rl/value_guided_sampling.py +1 -1
- diffusers/image_processor.py +286 -46
- diffusers/loaders/ip_adapter.py +11 -9
- diffusers/loaders/lora.py +198 -60
- diffusers/loaders/single_file.py +24 -18
- diffusers/loaders/textual_inversion.py +10 -14
- diffusers/loaders/unet.py +130 -37
- diffusers/models/__init__.py +18 -12
- diffusers/models/activations.py +9 -6
- diffusers/models/attention.py +137 -16
- diffusers/models/attention_processor.py +133 -46
- diffusers/models/autoencoders/__init__.py +5 -0
- diffusers/models/{autoencoder_asym_kl.py → autoencoders/autoencoder_asym_kl.py} +4 -4
- diffusers/models/{autoencoder_kl.py → autoencoders/autoencoder_kl.py} +45 -6
- diffusers/models/{autoencoder_kl_temporal_decoder.py → autoencoders/autoencoder_kl_temporal_decoder.py} +8 -8
- diffusers/models/{autoencoder_tiny.py → autoencoders/autoencoder_tiny.py} +4 -4
- diffusers/models/{consistency_decoder_vae.py → autoencoders/consistency_decoder_vae.py} +14 -14
- diffusers/models/{vae.py → autoencoders/vae.py} +9 -5
- diffusers/models/downsampling.py +338 -0
- diffusers/models/embeddings.py +112 -29
- diffusers/models/modeling_flax_utils.py +12 -7
- diffusers/models/modeling_utils.py +10 -10
- diffusers/models/normalization.py +108 -2
- diffusers/models/resnet.py +15 -699
- diffusers/models/transformer_2d.py +2 -2
- diffusers/models/unet_2d_condition.py +37 -0
- diffusers/models/{unet_kandi3.py → unet_kandinsky3.py} +105 -159
- diffusers/models/upsampling.py +454 -0
- diffusers/models/uvit_2d.py +471 -0
- diffusers/models/vq_model.py +9 -2
- diffusers/pipelines/__init__.py +81 -73
- diffusers/pipelines/amused/__init__.py +62 -0
- diffusers/pipelines/amused/pipeline_amused.py +328 -0
- diffusers/pipelines/amused/pipeline_amused_img2img.py +347 -0
- diffusers/pipelines/amused/pipeline_amused_inpaint.py +378 -0
- diffusers/pipelines/animatediff/pipeline_animatediff.py +38 -10
- diffusers/pipelines/auto_pipeline.py +17 -13
- diffusers/pipelines/controlnet/pipeline_controlnet.py +27 -10
- diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +47 -5
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +25 -8
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +4 -6
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +26 -10
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +4 -3
- diffusers/pipelines/deprecated/__init__.py +153 -0
- diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/__init__.py +3 -3
- diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/pipeline_alt_diffusion.py +91 -18
- diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/pipeline_alt_diffusion_img2img.py +91 -18
- diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/pipeline_output.py +1 -1
- diffusers/pipelines/{audio_diffusion → deprecated/audio_diffusion}/__init__.py +1 -1
- diffusers/pipelines/{audio_diffusion → deprecated/audio_diffusion}/mel.py +2 -2
- diffusers/pipelines/{audio_diffusion → deprecated/audio_diffusion}/pipeline_audio_diffusion.py +4 -4
- diffusers/pipelines/{latent_diffusion_uncond → deprecated/latent_diffusion_uncond}/__init__.py +1 -1
- diffusers/pipelines/{latent_diffusion_uncond → deprecated/latent_diffusion_uncond}/pipeline_latent_diffusion_uncond.py +4 -4
- diffusers/pipelines/{pndm → deprecated/pndm}/__init__.py +1 -1
- diffusers/pipelines/{pndm → deprecated/pndm}/pipeline_pndm.py +4 -4
- diffusers/pipelines/{repaint → deprecated/repaint}/__init__.py +1 -1
- diffusers/pipelines/{repaint → deprecated/repaint}/pipeline_repaint.py +5 -5
- diffusers/pipelines/{score_sde_ve → deprecated/score_sde_ve}/__init__.py +1 -1
- diffusers/pipelines/{score_sde_ve → deprecated/score_sde_ve}/pipeline_score_sde_ve.py +4 -4
- diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/__init__.py +6 -6
- diffusers/pipelines/{spectrogram_diffusion/continous_encoder.py → deprecated/spectrogram_diffusion/continuous_encoder.py} +2 -2
- diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/midi_utils.py +1 -1
- diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/notes_encoder.py +2 -2
- diffusers/pipelines/{spectrogram_diffusion → deprecated/spectrogram_diffusion}/pipeline_spectrogram_diffusion.py +7 -7
- diffusers/pipelines/deprecated/stable_diffusion_variants/__init__.py +55 -0
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_cycle_diffusion.py +16 -11
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_onnx_stable_diffusion_inpaint_legacy.py +6 -6
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_inpaint_legacy.py +11 -11
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_model_editing.py +16 -11
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_paradigms.py +10 -10
- diffusers/pipelines/{stable_diffusion → deprecated/stable_diffusion_variants}/pipeline_stable_diffusion_pix2pix_zero.py +13 -13
- diffusers/pipelines/{stochastic_karras_ve → deprecated/stochastic_karras_ve}/__init__.py +1 -1
- diffusers/pipelines/{stochastic_karras_ve → deprecated/stochastic_karras_ve}/pipeline_stochastic_karras_ve.py +4 -4
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/__init__.py +3 -3
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/modeling_text_unet.py +54 -11
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion.py +4 -4
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion_dual_guided.py +6 -6
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion_image_variation.py +6 -6
- diffusers/pipelines/{versatile_diffusion → deprecated/versatile_diffusion}/pipeline_versatile_diffusion_text_to_image.py +6 -6
- diffusers/pipelines/{vq_diffusion → deprecated/vq_diffusion}/__init__.py +3 -3
- diffusers/pipelines/{vq_diffusion → deprecated/vq_diffusion}/pipeline_vq_diffusion.py +5 -5
- diffusers/pipelines/kandinsky3/__init__.py +4 -4
- diffusers/pipelines/kandinsky3/convert_kandinsky3_unet.py +98 -0
- diffusers/pipelines/kandinsky3/{kandinsky3_pipeline.py → pipeline_kandinsky3.py} +172 -35
- diffusers/pipelines/kandinsky3/{kandinsky3img2img_pipeline.py → pipeline_kandinsky3_img2img.py} +228 -34
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +46 -5
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +47 -6
- diffusers/pipelines/onnx_utils.py +8 -5
- diffusers/pipelines/pipeline_flax_utils.py +7 -6
- diffusers/pipelines/pipeline_utils.py +32 -31
- diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +51 -2
- diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py +3 -3
- diffusers/pipelines/stable_diffusion/__init__.py +1 -72
- diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +67 -75
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +92 -8
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +92 -8
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +138 -10
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +57 -7
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_latent_upscale.py +3 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +6 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip.py +5 -0
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip_img2img.py +5 -0
- diffusers/pipelines/stable_diffusion_attend_and_excite/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_attend_and_excite}/pipeline_stable_diffusion_attend_and_excite.py +5 -2
- diffusers/pipelines/stable_diffusion_diffedit/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_diffedit}/pipeline_stable_diffusion_diffedit.py +2 -3
- diffusers/pipelines/stable_diffusion_gligen/__init__.py +50 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_gligen}/pipeline_stable_diffusion_gligen.py +2 -2
- diffusers/pipelines/{stable_diffusion → stable_diffusion_gligen}/pipeline_stable_diffusion_gligen_text_image.py +3 -3
- diffusers/pipelines/stable_diffusion_k_diffusion/__init__.py +60 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_k_diffusion}/pipeline_stable_diffusion_k_diffusion.py +6 -1
- diffusers/pipelines/stable_diffusion_ldm3d/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_ldm3d}/pipeline_stable_diffusion_ldm3d.py +50 -7
- diffusers/pipelines/stable_diffusion_panorama/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_panorama}/pipeline_stable_diffusion_panorama.py +56 -8
- diffusers/pipelines/stable_diffusion_safe/pipeline_stable_diffusion_safe.py +58 -6
- diffusers/pipelines/stable_diffusion_sag/__init__.py +48 -0
- diffusers/pipelines/{stable_diffusion → stable_diffusion_sag}/pipeline_stable_diffusion_sag.py +67 -10
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +97 -15
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +98 -14
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +97 -14
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +7 -5
- diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +12 -9
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +6 -0
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +5 -0
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +5 -0
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +331 -9
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +468 -9
- diffusers/pipelines/unclip/pipeline_unclip.py +2 -1
- diffusers/pipelines/unclip/pipeline_unclip_image_variation.py +1 -0
- diffusers/pipelines/wuerstchen/modeling_paella_vq_model.py +1 -1
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +4 -0
- diffusers/schedulers/__init__.py +2 -0
- diffusers/schedulers/scheduling_amused.py +162 -0
- diffusers/schedulers/scheduling_consistency_models.py +2 -0
- diffusers/schedulers/scheduling_ddim_inverse.py +1 -4
- diffusers/schedulers/scheduling_ddpm.py +46 -0
- diffusers/schedulers/scheduling_ddpm_parallel.py +46 -0
- diffusers/schedulers/scheduling_deis_multistep.py +13 -1
- diffusers/schedulers/scheduling_dpmsolver_multistep.py +13 -1
- diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +13 -1
- diffusers/schedulers/scheduling_dpmsolver_sde.py +2 -0
- diffusers/schedulers/scheduling_dpmsolver_singlestep.py +13 -1
- diffusers/schedulers/scheduling_euler_ancestral_discrete.py +58 -0
- diffusers/schedulers/scheduling_euler_discrete.py +62 -3
- diffusers/schedulers/scheduling_heun_discrete.py +2 -0
- diffusers/schedulers/scheduling_k_dpm_2_ancestral_discrete.py +2 -0
- diffusers/schedulers/scheduling_k_dpm_2_discrete.py +2 -0
- diffusers/schedulers/scheduling_lms_discrete.py +2 -0
- diffusers/schedulers/scheduling_unipc_multistep.py +13 -1
- diffusers/schedulers/scheduling_utils.py +3 -1
- diffusers/schedulers/scheduling_utils_flax.py +3 -1
- diffusers/training_utils.py +1 -1
- diffusers/utils/__init__.py +0 -2
- diffusers/utils/constants.py +2 -5
- diffusers/utils/dummy_pt_objects.py +30 -0
- diffusers/utils/dummy_torch_and_transformers_objects.py +45 -0
- diffusers/utils/dynamic_modules_utils.py +14 -18
- diffusers/utils/hub_utils.py +24 -36
- diffusers/utils/logging.py +1 -1
- diffusers/utils/state_dict_utils.py +8 -0
- diffusers/utils/testing_utils.py +199 -1
- diffusers/utils/torch_utils.py +3 -3
- {diffusers-0.24.0.dist-info → diffusers-0.25.1.dist-info}/METADATA +55 -53
- {diffusers-0.24.0.dist-info → diffusers-0.25.1.dist-info}/RECORD +174 -155
- {diffusers-0.24.0.dist-info → diffusers-0.25.1.dist-info}/WHEEL +1 -1
- {diffusers-0.24.0.dist-info → diffusers-0.25.1.dist-info}/entry_points.txt +0 -1
- /diffusers/pipelines/{alt_diffusion → deprecated/alt_diffusion}/modeling_roberta_series.py +0 -0
- {diffusers-0.24.0.dist-info → diffusers-0.25.1.dist-info}/LICENSE +0 -0
- {diffusers-0.24.0.dist-info → diffusers-0.25.1.dist-info}/top_level.txt +0 -0
diffusers/models/resnet.py
CHANGED
@@ -23,562 +23,23 @@ import torch.nn.functional as F
|
|
23
23
|
from ..utils import USE_PEFT_BACKEND
|
24
24
|
from .activations import get_activation
|
25
25
|
from .attention_processor import SpatialNorm
|
26
|
+
from .downsampling import ( # noqa
|
27
|
+
Downsample1D,
|
28
|
+
Downsample2D,
|
29
|
+
FirDownsample2D,
|
30
|
+
KDownsample2D,
|
31
|
+
downsample_2d,
|
32
|
+
)
|
26
33
|
from .lora import LoRACompatibleConv, LoRACompatibleLinear
|
27
34
|
from .normalization import AdaGroupNorm
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
use_conv (`bool`, default `False`):
|
37
|
-
option to use a convolution.
|
38
|
-
use_conv_transpose (`bool`, default `False`):
|
39
|
-
option to use a convolution transpose.
|
40
|
-
out_channels (`int`, optional):
|
41
|
-
number of output channels. Defaults to `channels`.
|
42
|
-
name (`str`, default `conv`):
|
43
|
-
name of the upsampling 1D layer.
|
44
|
-
"""
|
45
|
-
|
46
|
-
def __init__(
|
47
|
-
self,
|
48
|
-
channels: int,
|
49
|
-
use_conv: bool = False,
|
50
|
-
use_conv_transpose: bool = False,
|
51
|
-
out_channels: Optional[int] = None,
|
52
|
-
name: str = "conv",
|
53
|
-
):
|
54
|
-
super().__init__()
|
55
|
-
self.channels = channels
|
56
|
-
self.out_channels = out_channels or channels
|
57
|
-
self.use_conv = use_conv
|
58
|
-
self.use_conv_transpose = use_conv_transpose
|
59
|
-
self.name = name
|
60
|
-
|
61
|
-
self.conv = None
|
62
|
-
if use_conv_transpose:
|
63
|
-
self.conv = nn.ConvTranspose1d(channels, self.out_channels, 4, 2, 1)
|
64
|
-
elif use_conv:
|
65
|
-
self.conv = nn.Conv1d(self.channels, self.out_channels, 3, padding=1)
|
66
|
-
|
67
|
-
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
|
68
|
-
assert inputs.shape[1] == self.channels
|
69
|
-
if self.use_conv_transpose:
|
70
|
-
return self.conv(inputs)
|
71
|
-
|
72
|
-
outputs = F.interpolate(inputs, scale_factor=2.0, mode="nearest")
|
73
|
-
|
74
|
-
if self.use_conv:
|
75
|
-
outputs = self.conv(outputs)
|
76
|
-
|
77
|
-
return outputs
|
78
|
-
|
79
|
-
|
80
|
-
class Downsample1D(nn.Module):
|
81
|
-
"""A 1D downsampling layer with an optional convolution.
|
82
|
-
|
83
|
-
Parameters:
|
84
|
-
channels (`int`):
|
85
|
-
number of channels in the inputs and outputs.
|
86
|
-
use_conv (`bool`, default `False`):
|
87
|
-
option to use a convolution.
|
88
|
-
out_channels (`int`, optional):
|
89
|
-
number of output channels. Defaults to `channels`.
|
90
|
-
padding (`int`, default `1`):
|
91
|
-
padding for the convolution.
|
92
|
-
name (`str`, default `conv`):
|
93
|
-
name of the downsampling 1D layer.
|
94
|
-
"""
|
95
|
-
|
96
|
-
def __init__(
|
97
|
-
self,
|
98
|
-
channels: int,
|
99
|
-
use_conv: bool = False,
|
100
|
-
out_channels: Optional[int] = None,
|
101
|
-
padding: int = 1,
|
102
|
-
name: str = "conv",
|
103
|
-
):
|
104
|
-
super().__init__()
|
105
|
-
self.channels = channels
|
106
|
-
self.out_channels = out_channels or channels
|
107
|
-
self.use_conv = use_conv
|
108
|
-
self.padding = padding
|
109
|
-
stride = 2
|
110
|
-
self.name = name
|
111
|
-
|
112
|
-
if use_conv:
|
113
|
-
self.conv = nn.Conv1d(self.channels, self.out_channels, 3, stride=stride, padding=padding)
|
114
|
-
else:
|
115
|
-
assert self.channels == self.out_channels
|
116
|
-
self.conv = nn.AvgPool1d(kernel_size=stride, stride=stride)
|
117
|
-
|
118
|
-
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
|
119
|
-
assert inputs.shape[1] == self.channels
|
120
|
-
return self.conv(inputs)
|
121
|
-
|
122
|
-
|
123
|
-
class Upsample2D(nn.Module):
|
124
|
-
"""A 2D upsampling layer with an optional convolution.
|
125
|
-
|
126
|
-
Parameters:
|
127
|
-
channels (`int`):
|
128
|
-
number of channels in the inputs and outputs.
|
129
|
-
use_conv (`bool`, default `False`):
|
130
|
-
option to use a convolution.
|
131
|
-
use_conv_transpose (`bool`, default `False`):
|
132
|
-
option to use a convolution transpose.
|
133
|
-
out_channels (`int`, optional):
|
134
|
-
number of output channels. Defaults to `channels`.
|
135
|
-
name (`str`, default `conv`):
|
136
|
-
name of the upsampling 2D layer.
|
137
|
-
"""
|
138
|
-
|
139
|
-
def __init__(
|
140
|
-
self,
|
141
|
-
channels: int,
|
142
|
-
use_conv: bool = False,
|
143
|
-
use_conv_transpose: bool = False,
|
144
|
-
out_channels: Optional[int] = None,
|
145
|
-
name: str = "conv",
|
146
|
-
):
|
147
|
-
super().__init__()
|
148
|
-
self.channels = channels
|
149
|
-
self.out_channels = out_channels or channels
|
150
|
-
self.use_conv = use_conv
|
151
|
-
self.use_conv_transpose = use_conv_transpose
|
152
|
-
self.name = name
|
153
|
-
conv_cls = nn.Conv2d if USE_PEFT_BACKEND else LoRACompatibleConv
|
154
|
-
|
155
|
-
conv = None
|
156
|
-
if use_conv_transpose:
|
157
|
-
conv = nn.ConvTranspose2d(channels, self.out_channels, 4, 2, 1)
|
158
|
-
elif use_conv:
|
159
|
-
conv = conv_cls(self.channels, self.out_channels, 3, padding=1)
|
160
|
-
|
161
|
-
# TODO(Suraj, Patrick) - clean up after weight dicts are correctly renamed
|
162
|
-
if name == "conv":
|
163
|
-
self.conv = conv
|
164
|
-
else:
|
165
|
-
self.Conv2d_0 = conv
|
166
|
-
|
167
|
-
def forward(
|
168
|
-
self,
|
169
|
-
hidden_states: torch.FloatTensor,
|
170
|
-
output_size: Optional[int] = None,
|
171
|
-
scale: float = 1.0,
|
172
|
-
) -> torch.FloatTensor:
|
173
|
-
assert hidden_states.shape[1] == self.channels
|
174
|
-
|
175
|
-
if self.use_conv_transpose:
|
176
|
-
return self.conv(hidden_states)
|
177
|
-
|
178
|
-
# Cast to float32 to as 'upsample_nearest2d_out_frame' op does not support bfloat16
|
179
|
-
# TODO(Suraj): Remove this cast once the issue is fixed in PyTorch
|
180
|
-
# https://github.com/pytorch/pytorch/issues/86679
|
181
|
-
dtype = hidden_states.dtype
|
182
|
-
if dtype == torch.bfloat16:
|
183
|
-
hidden_states = hidden_states.to(torch.float32)
|
184
|
-
|
185
|
-
# upsample_nearest_nhwc fails with large batch sizes. see https://github.com/huggingface/diffusers/issues/984
|
186
|
-
if hidden_states.shape[0] >= 64:
|
187
|
-
hidden_states = hidden_states.contiguous()
|
188
|
-
|
189
|
-
# if `output_size` is passed we force the interpolation output
|
190
|
-
# size and do not make use of `scale_factor=2`
|
191
|
-
if output_size is None:
|
192
|
-
hidden_states = F.interpolate(hidden_states, scale_factor=2.0, mode="nearest")
|
193
|
-
else:
|
194
|
-
hidden_states = F.interpolate(hidden_states, size=output_size, mode="nearest")
|
195
|
-
|
196
|
-
# If the input is bfloat16, we cast back to bfloat16
|
197
|
-
if dtype == torch.bfloat16:
|
198
|
-
hidden_states = hidden_states.to(dtype)
|
199
|
-
|
200
|
-
# TODO(Suraj, Patrick) - clean up after weight dicts are correctly renamed
|
201
|
-
if self.use_conv:
|
202
|
-
if self.name == "conv":
|
203
|
-
if isinstance(self.conv, LoRACompatibleConv) and not USE_PEFT_BACKEND:
|
204
|
-
hidden_states = self.conv(hidden_states, scale)
|
205
|
-
else:
|
206
|
-
hidden_states = self.conv(hidden_states)
|
207
|
-
else:
|
208
|
-
if isinstance(self.Conv2d_0, LoRACompatibleConv) and not USE_PEFT_BACKEND:
|
209
|
-
hidden_states = self.Conv2d_0(hidden_states, scale)
|
210
|
-
else:
|
211
|
-
hidden_states = self.Conv2d_0(hidden_states)
|
212
|
-
|
213
|
-
return hidden_states
|
214
|
-
|
215
|
-
|
216
|
-
class Downsample2D(nn.Module):
|
217
|
-
"""A 2D downsampling layer with an optional convolution.
|
218
|
-
|
219
|
-
Parameters:
|
220
|
-
channels (`int`):
|
221
|
-
number of channels in the inputs and outputs.
|
222
|
-
use_conv (`bool`, default `False`):
|
223
|
-
option to use a convolution.
|
224
|
-
out_channels (`int`, optional):
|
225
|
-
number of output channels. Defaults to `channels`.
|
226
|
-
padding (`int`, default `1`):
|
227
|
-
padding for the convolution.
|
228
|
-
name (`str`, default `conv`):
|
229
|
-
name of the downsampling 2D layer.
|
230
|
-
"""
|
231
|
-
|
232
|
-
def __init__(
|
233
|
-
self,
|
234
|
-
channels: int,
|
235
|
-
use_conv: bool = False,
|
236
|
-
out_channels: Optional[int] = None,
|
237
|
-
padding: int = 1,
|
238
|
-
name: str = "conv",
|
239
|
-
):
|
240
|
-
super().__init__()
|
241
|
-
self.channels = channels
|
242
|
-
self.out_channels = out_channels or channels
|
243
|
-
self.use_conv = use_conv
|
244
|
-
self.padding = padding
|
245
|
-
stride = 2
|
246
|
-
self.name = name
|
247
|
-
conv_cls = nn.Conv2d if USE_PEFT_BACKEND else LoRACompatibleConv
|
248
|
-
|
249
|
-
if use_conv:
|
250
|
-
conv = conv_cls(self.channels, self.out_channels, 3, stride=stride, padding=padding)
|
251
|
-
else:
|
252
|
-
assert self.channels == self.out_channels
|
253
|
-
conv = nn.AvgPool2d(kernel_size=stride, stride=stride)
|
254
|
-
|
255
|
-
# TODO(Suraj, Patrick) - clean up after weight dicts are correctly renamed
|
256
|
-
if name == "conv":
|
257
|
-
self.Conv2d_0 = conv
|
258
|
-
self.conv = conv
|
259
|
-
elif name == "Conv2d_0":
|
260
|
-
self.conv = conv
|
261
|
-
else:
|
262
|
-
self.conv = conv
|
263
|
-
|
264
|
-
def forward(self, hidden_states: torch.FloatTensor, scale: float = 1.0) -> torch.FloatTensor:
|
265
|
-
assert hidden_states.shape[1] == self.channels
|
266
|
-
|
267
|
-
if self.use_conv and self.padding == 0:
|
268
|
-
pad = (0, 1, 0, 1)
|
269
|
-
hidden_states = F.pad(hidden_states, pad, mode="constant", value=0)
|
270
|
-
|
271
|
-
assert hidden_states.shape[1] == self.channels
|
272
|
-
|
273
|
-
if not USE_PEFT_BACKEND:
|
274
|
-
if isinstance(self.conv, LoRACompatibleConv):
|
275
|
-
hidden_states = self.conv(hidden_states, scale)
|
276
|
-
else:
|
277
|
-
hidden_states = self.conv(hidden_states)
|
278
|
-
else:
|
279
|
-
hidden_states = self.conv(hidden_states)
|
280
|
-
|
281
|
-
return hidden_states
|
282
|
-
|
283
|
-
|
284
|
-
class FirUpsample2D(nn.Module):
|
285
|
-
"""A 2D FIR upsampling layer with an optional convolution.
|
286
|
-
|
287
|
-
Parameters:
|
288
|
-
channels (`int`, optional):
|
289
|
-
number of channels in the inputs and outputs.
|
290
|
-
use_conv (`bool`, default `False`):
|
291
|
-
option to use a convolution.
|
292
|
-
out_channels (`int`, optional):
|
293
|
-
number of output channels. Defaults to `channels`.
|
294
|
-
fir_kernel (`tuple`, default `(1, 3, 3, 1)`):
|
295
|
-
kernel for the FIR filter.
|
296
|
-
"""
|
297
|
-
|
298
|
-
def __init__(
|
299
|
-
self,
|
300
|
-
channels: Optional[int] = None,
|
301
|
-
out_channels: Optional[int] = None,
|
302
|
-
use_conv: bool = False,
|
303
|
-
fir_kernel: Tuple[int, int, int, int] = (1, 3, 3, 1),
|
304
|
-
):
|
305
|
-
super().__init__()
|
306
|
-
out_channels = out_channels if out_channels else channels
|
307
|
-
if use_conv:
|
308
|
-
self.Conv2d_0 = nn.Conv2d(channels, out_channels, kernel_size=3, stride=1, padding=1)
|
309
|
-
self.use_conv = use_conv
|
310
|
-
self.fir_kernel = fir_kernel
|
311
|
-
self.out_channels = out_channels
|
312
|
-
|
313
|
-
def _upsample_2d(
|
314
|
-
self,
|
315
|
-
hidden_states: torch.FloatTensor,
|
316
|
-
weight: Optional[torch.FloatTensor] = None,
|
317
|
-
kernel: Optional[torch.FloatTensor] = None,
|
318
|
-
factor: int = 2,
|
319
|
-
gain: float = 1,
|
320
|
-
) -> torch.FloatTensor:
|
321
|
-
"""Fused `upsample_2d()` followed by `Conv2d()`.
|
322
|
-
|
323
|
-
Padding is performed only once at the beginning, not between the operations. The fused op is considerably more
|
324
|
-
efficient than performing the same calculation using standard TensorFlow ops. It supports gradients of
|
325
|
-
arbitrary order.
|
326
|
-
|
327
|
-
Args:
|
328
|
-
hidden_states (`torch.FloatTensor`):
|
329
|
-
Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`.
|
330
|
-
weight (`torch.FloatTensor`, *optional*):
|
331
|
-
Weight tensor of the shape `[filterH, filterW, inChannels, outChannels]`. Grouped convolution can be
|
332
|
-
performed by `inChannels = x.shape[0] // numGroups`.
|
333
|
-
kernel (`torch.FloatTensor`, *optional*):
|
334
|
-
FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which
|
335
|
-
corresponds to nearest-neighbor upsampling.
|
336
|
-
factor (`int`, *optional*): Integer upsampling factor (default: 2).
|
337
|
-
gain (`float`, *optional*): Scaling factor for signal magnitude (default: 1.0).
|
338
|
-
|
339
|
-
Returns:
|
340
|
-
output (`torch.FloatTensor`):
|
341
|
-
Tensor of the shape `[N, C, H * factor, W * factor]` or `[N, H * factor, W * factor, C]`, and same
|
342
|
-
datatype as `hidden_states`.
|
343
|
-
"""
|
344
|
-
|
345
|
-
assert isinstance(factor, int) and factor >= 1
|
346
|
-
|
347
|
-
# Setup filter kernel.
|
348
|
-
if kernel is None:
|
349
|
-
kernel = [1] * factor
|
350
|
-
|
351
|
-
# setup kernel
|
352
|
-
kernel = torch.tensor(kernel, dtype=torch.float32)
|
353
|
-
if kernel.ndim == 1:
|
354
|
-
kernel = torch.outer(kernel, kernel)
|
355
|
-
kernel /= torch.sum(kernel)
|
356
|
-
|
357
|
-
kernel = kernel * (gain * (factor**2))
|
358
|
-
|
359
|
-
if self.use_conv:
|
360
|
-
convH = weight.shape[2]
|
361
|
-
convW = weight.shape[3]
|
362
|
-
inC = weight.shape[1]
|
363
|
-
|
364
|
-
pad_value = (kernel.shape[0] - factor) - (convW - 1)
|
365
|
-
|
366
|
-
stride = (factor, factor)
|
367
|
-
# Determine data dimensions.
|
368
|
-
output_shape = (
|
369
|
-
(hidden_states.shape[2] - 1) * factor + convH,
|
370
|
-
(hidden_states.shape[3] - 1) * factor + convW,
|
371
|
-
)
|
372
|
-
output_padding = (
|
373
|
-
output_shape[0] - (hidden_states.shape[2] - 1) * stride[0] - convH,
|
374
|
-
output_shape[1] - (hidden_states.shape[3] - 1) * stride[1] - convW,
|
375
|
-
)
|
376
|
-
assert output_padding[0] >= 0 and output_padding[1] >= 0
|
377
|
-
num_groups = hidden_states.shape[1] // inC
|
378
|
-
|
379
|
-
# Transpose weights.
|
380
|
-
weight = torch.reshape(weight, (num_groups, -1, inC, convH, convW))
|
381
|
-
weight = torch.flip(weight, dims=[3, 4]).permute(0, 2, 1, 3, 4)
|
382
|
-
weight = torch.reshape(weight, (num_groups * inC, -1, convH, convW))
|
383
|
-
|
384
|
-
inverse_conv = F.conv_transpose2d(
|
385
|
-
hidden_states,
|
386
|
-
weight,
|
387
|
-
stride=stride,
|
388
|
-
output_padding=output_padding,
|
389
|
-
padding=0,
|
390
|
-
)
|
391
|
-
|
392
|
-
output = upfirdn2d_native(
|
393
|
-
inverse_conv,
|
394
|
-
torch.tensor(kernel, device=inverse_conv.device),
|
395
|
-
pad=((pad_value + 1) // 2 + factor - 1, pad_value // 2 + 1),
|
396
|
-
)
|
397
|
-
else:
|
398
|
-
pad_value = kernel.shape[0] - factor
|
399
|
-
output = upfirdn2d_native(
|
400
|
-
hidden_states,
|
401
|
-
torch.tensor(kernel, device=hidden_states.device),
|
402
|
-
up=factor,
|
403
|
-
pad=((pad_value + 1) // 2 + factor - 1, pad_value // 2),
|
404
|
-
)
|
405
|
-
|
406
|
-
return output
|
407
|
-
|
408
|
-
def forward(self, hidden_states: torch.FloatTensor) -> torch.FloatTensor:
|
409
|
-
if self.use_conv:
|
410
|
-
height = self._upsample_2d(hidden_states, self.Conv2d_0.weight, kernel=self.fir_kernel)
|
411
|
-
height = height + self.Conv2d_0.bias.reshape(1, -1, 1, 1)
|
412
|
-
else:
|
413
|
-
height = self._upsample_2d(hidden_states, kernel=self.fir_kernel, factor=2)
|
414
|
-
|
415
|
-
return height
|
416
|
-
|
417
|
-
|
418
|
-
class FirDownsample2D(nn.Module):
|
419
|
-
"""A 2D FIR downsampling layer with an optional convolution.
|
420
|
-
|
421
|
-
Parameters:
|
422
|
-
channels (`int`):
|
423
|
-
number of channels in the inputs and outputs.
|
424
|
-
use_conv (`bool`, default `False`):
|
425
|
-
option to use a convolution.
|
426
|
-
out_channels (`int`, optional):
|
427
|
-
number of output channels. Defaults to `channels`.
|
428
|
-
fir_kernel (`tuple`, default `(1, 3, 3, 1)`):
|
429
|
-
kernel for the FIR filter.
|
430
|
-
"""
|
431
|
-
|
432
|
-
def __init__(
|
433
|
-
self,
|
434
|
-
channels: Optional[int] = None,
|
435
|
-
out_channels: Optional[int] = None,
|
436
|
-
use_conv: bool = False,
|
437
|
-
fir_kernel: Tuple[int, int, int, int] = (1, 3, 3, 1),
|
438
|
-
):
|
439
|
-
super().__init__()
|
440
|
-
out_channels = out_channels if out_channels else channels
|
441
|
-
if use_conv:
|
442
|
-
self.Conv2d_0 = nn.Conv2d(channels, out_channels, kernel_size=3, stride=1, padding=1)
|
443
|
-
self.fir_kernel = fir_kernel
|
444
|
-
self.use_conv = use_conv
|
445
|
-
self.out_channels = out_channels
|
446
|
-
|
447
|
-
def _downsample_2d(
|
448
|
-
self,
|
449
|
-
hidden_states: torch.FloatTensor,
|
450
|
-
weight: Optional[torch.FloatTensor] = None,
|
451
|
-
kernel: Optional[torch.FloatTensor] = None,
|
452
|
-
factor: int = 2,
|
453
|
-
gain: float = 1,
|
454
|
-
) -> torch.FloatTensor:
|
455
|
-
"""Fused `Conv2d()` followed by `downsample_2d()`.
|
456
|
-
Padding is performed only once at the beginning, not between the operations. The fused op is considerably more
|
457
|
-
efficient than performing the same calculation using standard TensorFlow ops. It supports gradients of
|
458
|
-
arbitrary order.
|
459
|
-
|
460
|
-
Args:
|
461
|
-
hidden_states (`torch.FloatTensor`):
|
462
|
-
Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`.
|
463
|
-
weight (`torch.FloatTensor`, *optional*):
|
464
|
-
Weight tensor of the shape `[filterH, filterW, inChannels, outChannels]`. Grouped convolution can be
|
465
|
-
performed by `inChannels = x.shape[0] // numGroups`.
|
466
|
-
kernel (`torch.FloatTensor`, *optional*):
|
467
|
-
FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which
|
468
|
-
corresponds to average pooling.
|
469
|
-
factor (`int`, *optional*, default to `2`):
|
470
|
-
Integer downsampling factor.
|
471
|
-
gain (`float`, *optional*, default to `1.0`):
|
472
|
-
Scaling factor for signal magnitude.
|
473
|
-
|
474
|
-
Returns:
|
475
|
-
output (`torch.FloatTensor`):
|
476
|
-
Tensor of the shape `[N, C, H // factor, W // factor]` or `[N, H // factor, W // factor, C]`, and same
|
477
|
-
datatype as `x`.
|
478
|
-
"""
|
479
|
-
|
480
|
-
assert isinstance(factor, int) and factor >= 1
|
481
|
-
if kernel is None:
|
482
|
-
kernel = [1] * factor
|
483
|
-
|
484
|
-
# setup kernel
|
485
|
-
kernel = torch.tensor(kernel, dtype=torch.float32)
|
486
|
-
if kernel.ndim == 1:
|
487
|
-
kernel = torch.outer(kernel, kernel)
|
488
|
-
kernel /= torch.sum(kernel)
|
489
|
-
|
490
|
-
kernel = kernel * gain
|
491
|
-
|
492
|
-
if self.use_conv:
|
493
|
-
_, _, convH, convW = weight.shape
|
494
|
-
pad_value = (kernel.shape[0] - factor) + (convW - 1)
|
495
|
-
stride_value = [factor, factor]
|
496
|
-
upfirdn_input = upfirdn2d_native(
|
497
|
-
hidden_states,
|
498
|
-
torch.tensor(kernel, device=hidden_states.device),
|
499
|
-
pad=((pad_value + 1) // 2, pad_value // 2),
|
500
|
-
)
|
501
|
-
output = F.conv2d(upfirdn_input, weight, stride=stride_value, padding=0)
|
502
|
-
else:
|
503
|
-
pad_value = kernel.shape[0] - factor
|
504
|
-
output = upfirdn2d_native(
|
505
|
-
hidden_states,
|
506
|
-
torch.tensor(kernel, device=hidden_states.device),
|
507
|
-
down=factor,
|
508
|
-
pad=((pad_value + 1) // 2, pad_value // 2),
|
509
|
-
)
|
510
|
-
|
511
|
-
return output
|
512
|
-
|
513
|
-
def forward(self, hidden_states: torch.FloatTensor) -> torch.FloatTensor:
|
514
|
-
if self.use_conv:
|
515
|
-
downsample_input = self._downsample_2d(hidden_states, weight=self.Conv2d_0.weight, kernel=self.fir_kernel)
|
516
|
-
hidden_states = downsample_input + self.Conv2d_0.bias.reshape(1, -1, 1, 1)
|
517
|
-
else:
|
518
|
-
hidden_states = self._downsample_2d(hidden_states, kernel=self.fir_kernel, factor=2)
|
519
|
-
|
520
|
-
return hidden_states
|
521
|
-
|
522
|
-
|
523
|
-
# downsample/upsample layer used in k-upscaler, might be able to use FirDownsample2D/DirUpsample2D instead
|
524
|
-
class KDownsample2D(nn.Module):
|
525
|
-
r"""A 2D K-downsampling layer.
|
526
|
-
|
527
|
-
Parameters:
|
528
|
-
pad_mode (`str`, *optional*, default to `"reflect"`): the padding mode to use.
|
529
|
-
"""
|
530
|
-
|
531
|
-
def __init__(self, pad_mode: str = "reflect"):
|
532
|
-
super().__init__()
|
533
|
-
self.pad_mode = pad_mode
|
534
|
-
kernel_1d = torch.tensor([[1 / 8, 3 / 8, 3 / 8, 1 / 8]])
|
535
|
-
self.pad = kernel_1d.shape[1] // 2 - 1
|
536
|
-
self.register_buffer("kernel", kernel_1d.T @ kernel_1d, persistent=False)
|
537
|
-
|
538
|
-
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
|
539
|
-
inputs = F.pad(inputs, (self.pad,) * 4, self.pad_mode)
|
540
|
-
weight = inputs.new_zeros(
|
541
|
-
[
|
542
|
-
inputs.shape[1],
|
543
|
-
inputs.shape[1],
|
544
|
-
self.kernel.shape[0],
|
545
|
-
self.kernel.shape[1],
|
546
|
-
]
|
547
|
-
)
|
548
|
-
indices = torch.arange(inputs.shape[1], device=inputs.device)
|
549
|
-
kernel = self.kernel.to(weight)[None, :].expand(inputs.shape[1], -1, -1)
|
550
|
-
weight[indices, indices] = kernel
|
551
|
-
return F.conv2d(inputs, weight, stride=2)
|
552
|
-
|
553
|
-
|
554
|
-
class KUpsample2D(nn.Module):
|
555
|
-
r"""A 2D K-upsampling layer.
|
556
|
-
|
557
|
-
Parameters:
|
558
|
-
pad_mode (`str`, *optional*, default to `"reflect"`): the padding mode to use.
|
559
|
-
"""
|
560
|
-
|
561
|
-
def __init__(self, pad_mode: str = "reflect"):
|
562
|
-
super().__init__()
|
563
|
-
self.pad_mode = pad_mode
|
564
|
-
kernel_1d = torch.tensor([[1 / 8, 3 / 8, 3 / 8, 1 / 8]]) * 2
|
565
|
-
self.pad = kernel_1d.shape[1] // 2 - 1
|
566
|
-
self.register_buffer("kernel", kernel_1d.T @ kernel_1d, persistent=False)
|
567
|
-
|
568
|
-
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
|
569
|
-
inputs = F.pad(inputs, ((self.pad + 1) // 2,) * 4, self.pad_mode)
|
570
|
-
weight = inputs.new_zeros(
|
571
|
-
[
|
572
|
-
inputs.shape[1],
|
573
|
-
inputs.shape[1],
|
574
|
-
self.kernel.shape[0],
|
575
|
-
self.kernel.shape[1],
|
576
|
-
]
|
577
|
-
)
|
578
|
-
indices = torch.arange(inputs.shape[1], device=inputs.device)
|
579
|
-
kernel = self.kernel.to(weight)[None, :].expand(inputs.shape[1], -1, -1)
|
580
|
-
weight[indices, indices] = kernel
|
581
|
-
return F.conv_transpose2d(inputs, weight, stride=2, padding=self.pad * 2 + 1)
|
35
|
+
from .upsampling import ( # noqa
|
36
|
+
FirUpsample2D,
|
37
|
+
KUpsample2D,
|
38
|
+
Upsample1D,
|
39
|
+
Upsample2D,
|
40
|
+
upfirdn2d_native,
|
41
|
+
upsample_2d,
|
42
|
+
)
|
582
43
|
|
583
44
|
|
584
45
|
class ResnetBlock2D(nn.Module):
|
@@ -894,151 +355,6 @@ class ResidualTemporalBlock1D(nn.Module):
|
|
894
355
|
return out + self.residual_conv(inputs)
|
895
356
|
|
896
357
|
|
897
|
-
def upsample_2d(
|
898
|
-
hidden_states: torch.FloatTensor,
|
899
|
-
kernel: Optional[torch.FloatTensor] = None,
|
900
|
-
factor: int = 2,
|
901
|
-
gain: float = 1,
|
902
|
-
) -> torch.FloatTensor:
|
903
|
-
r"""Upsample2D a batch of 2D images with the given filter.
|
904
|
-
Accepts a batch of 2D images of the shape `[N, C, H, W]` or `[N, H, W, C]` and upsamples each image with the given
|
905
|
-
filter. The filter is normalized so that if the input pixels are constant, they will be scaled by the specified
|
906
|
-
`gain`. Pixels outside the image are assumed to be zero, and the filter is padded with zeros so that its shape is
|
907
|
-
a: multiple of the upsampling factor.
|
908
|
-
|
909
|
-
Args:
|
910
|
-
hidden_states (`torch.FloatTensor`):
|
911
|
-
Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`.
|
912
|
-
kernel (`torch.FloatTensor`, *optional*):
|
913
|
-
FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which
|
914
|
-
corresponds to nearest-neighbor upsampling.
|
915
|
-
factor (`int`, *optional*, default to `2`):
|
916
|
-
Integer upsampling factor.
|
917
|
-
gain (`float`, *optional*, default to `1.0`):
|
918
|
-
Scaling factor for signal magnitude (default: 1.0).
|
919
|
-
|
920
|
-
Returns:
|
921
|
-
output (`torch.FloatTensor`):
|
922
|
-
Tensor of the shape `[N, C, H * factor, W * factor]`
|
923
|
-
"""
|
924
|
-
assert isinstance(factor, int) and factor >= 1
|
925
|
-
if kernel is None:
|
926
|
-
kernel = [1] * factor
|
927
|
-
|
928
|
-
kernel = torch.tensor(kernel, dtype=torch.float32)
|
929
|
-
if kernel.ndim == 1:
|
930
|
-
kernel = torch.outer(kernel, kernel)
|
931
|
-
kernel /= torch.sum(kernel)
|
932
|
-
|
933
|
-
kernel = kernel * (gain * (factor**2))
|
934
|
-
pad_value = kernel.shape[0] - factor
|
935
|
-
output = upfirdn2d_native(
|
936
|
-
hidden_states,
|
937
|
-
kernel.to(device=hidden_states.device),
|
938
|
-
up=factor,
|
939
|
-
pad=((pad_value + 1) // 2 + factor - 1, pad_value // 2),
|
940
|
-
)
|
941
|
-
return output
|
942
|
-
|
943
|
-
|
944
|
-
def downsample_2d(
|
945
|
-
hidden_states: torch.FloatTensor,
|
946
|
-
kernel: Optional[torch.FloatTensor] = None,
|
947
|
-
factor: int = 2,
|
948
|
-
gain: float = 1,
|
949
|
-
) -> torch.FloatTensor:
|
950
|
-
r"""Downsample2D a batch of 2D images with the given filter.
|
951
|
-
Accepts a batch of 2D images of the shape `[N, C, H, W]` or `[N, H, W, C]` and downsamples each image with the
|
952
|
-
given filter. The filter is normalized so that if the input pixels are constant, they will be scaled by the
|
953
|
-
specified `gain`. Pixels outside the image are assumed to be zero, and the filter is padded with zeros so that its
|
954
|
-
shape is a multiple of the downsampling factor.
|
955
|
-
|
956
|
-
Args:
|
957
|
-
hidden_states (`torch.FloatTensor`)
|
958
|
-
Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`.
|
959
|
-
kernel (`torch.FloatTensor`, *optional*):
|
960
|
-
FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which
|
961
|
-
corresponds to average pooling.
|
962
|
-
factor (`int`, *optional*, default to `2`):
|
963
|
-
Integer downsampling factor.
|
964
|
-
gain (`float`, *optional*, default to `1.0`):
|
965
|
-
Scaling factor for signal magnitude.
|
966
|
-
|
967
|
-
Returns:
|
968
|
-
output (`torch.FloatTensor`):
|
969
|
-
Tensor of the shape `[N, C, H // factor, W // factor]`
|
970
|
-
"""
|
971
|
-
|
972
|
-
assert isinstance(factor, int) and factor >= 1
|
973
|
-
if kernel is None:
|
974
|
-
kernel = [1] * factor
|
975
|
-
|
976
|
-
kernel = torch.tensor(kernel, dtype=torch.float32)
|
977
|
-
if kernel.ndim == 1:
|
978
|
-
kernel = torch.outer(kernel, kernel)
|
979
|
-
kernel /= torch.sum(kernel)
|
980
|
-
|
981
|
-
kernel = kernel * gain
|
982
|
-
pad_value = kernel.shape[0] - factor
|
983
|
-
output = upfirdn2d_native(
|
984
|
-
hidden_states,
|
985
|
-
kernel.to(device=hidden_states.device),
|
986
|
-
down=factor,
|
987
|
-
pad=((pad_value + 1) // 2, pad_value // 2),
|
988
|
-
)
|
989
|
-
return output
|
990
|
-
|
991
|
-
|
992
|
-
def upfirdn2d_native(
|
993
|
-
tensor: torch.Tensor,
|
994
|
-
kernel: torch.Tensor,
|
995
|
-
up: int = 1,
|
996
|
-
down: int = 1,
|
997
|
-
pad: Tuple[int, int] = (0, 0),
|
998
|
-
) -> torch.Tensor:
|
999
|
-
up_x = up_y = up
|
1000
|
-
down_x = down_y = down
|
1001
|
-
pad_x0 = pad_y0 = pad[0]
|
1002
|
-
pad_x1 = pad_y1 = pad[1]
|
1003
|
-
|
1004
|
-
_, channel, in_h, in_w = tensor.shape
|
1005
|
-
tensor = tensor.reshape(-1, in_h, in_w, 1)
|
1006
|
-
|
1007
|
-
_, in_h, in_w, minor = tensor.shape
|
1008
|
-
kernel_h, kernel_w = kernel.shape
|
1009
|
-
|
1010
|
-
out = tensor.view(-1, in_h, 1, in_w, 1, minor)
|
1011
|
-
out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
|
1012
|
-
out = out.view(-1, in_h * up_y, in_w * up_x, minor)
|
1013
|
-
|
1014
|
-
out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0), max(pad_y1, 0)])
|
1015
|
-
out = out.to(tensor.device) # Move back to mps if necessary
|
1016
|
-
out = out[
|
1017
|
-
:,
|
1018
|
-
max(-pad_y0, 0) : out.shape[1] - max(-pad_y1, 0),
|
1019
|
-
max(-pad_x0, 0) : out.shape[2] - max(-pad_x1, 0),
|
1020
|
-
:,
|
1021
|
-
]
|
1022
|
-
|
1023
|
-
out = out.permute(0, 3, 1, 2)
|
1024
|
-
out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x + pad_x0 + pad_x1])
|
1025
|
-
w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
|
1026
|
-
out = F.conv2d(out, w)
|
1027
|
-
out = out.reshape(
|
1028
|
-
-1,
|
1029
|
-
minor,
|
1030
|
-
in_h * up_y + pad_y0 + pad_y1 - kernel_h + 1,
|
1031
|
-
in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1,
|
1032
|
-
)
|
1033
|
-
out = out.permute(0, 2, 3, 1)
|
1034
|
-
out = out[:, ::down_y, ::down_x, :]
|
1035
|
-
|
1036
|
-
out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
|
1037
|
-
out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
|
1038
|
-
|
1039
|
-
return out.view(-1, channel, out_h, out_w)
|
1040
|
-
|
1041
|
-
|
1042
358
|
class TemporalConvLayer(nn.Module):
|
1043
359
|
"""
|
1044
360
|
Temporal convolutional layer that can be used for video (sequence of images) input Code mostly copied from:
|