Improve docs and type hints (#5759) · Aivean/diffusers@038b42d · GitHub
Skip to content

Commit 038b42d

Browse files
a-r-r-o-wDN6
andauthored
Improve docs and type hints (huggingface#5759)
* improvement: docs and type hints * improvement: docs and type hints minor refactor * improvement: docs and type hints * update with suggestions from review Co-Authored-By: Dhruv Nair <dhruv.nair@gmail.com> --------- Co-authored-by: Dhruv Nair <dhruv.nair@gmail.com>
1 parent ecbe27a commit 038b42d

19 files changed

Lines changed: 533 additions & 415 deletions

‎src/diffusers/image_processor.py‎

Lines changed: 65 additions & 13 deletions

‎src/diffusers/models/autoencoder_asym_kl.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ def __init__(
6565
self,
6666
in_channels: int = 3,
6767
out_channels: int = 3,
68-
down_block_types: Tuple[str] = ("DownEncoderBlock2D",),
69-
down_block_out_channels: Tuple[int] = (64,),
68+
down_block_types: Tuple[str, ...] = ("DownEncoderBlock2D",),
69+
down_block_out_channels: Tuple[int, ...] = (64,),
7070
layers_per_down_block: int = 1,
71-
up_block_types: Tuple[str] = ("UpDecoderBlock2D",),
72-
up_block_out_channels: Tuple[int] = (64,),
71+
up_block_types: Tuple[str, ...] = ("UpDecoderBlock2D",),
72+
up_block_out_channels: Tuple[int, ...] = (64,),
7373
layers_per_up_block: int = 1,
7474
act_fn: str = "silu",
7575
latent_channels: int = 4,
@@ -109,7 +109,9 @@ def __init__(
109109
self.use_tiling = False
110110

111111
@apply_forward_hook
112-
def encode(self, x: torch.FloatTensor, return_dict: bool = True) -> AutoencoderKLOutput:
112+
def encode(
113+
self, x: torch.FloatTensor, return_dict: bool = True
114+
) -> Union[AutoencoderKLOutput, Tuple[torch.FloatTensor]]:
113115
h = self.encoder(x)
114116
moments = self.quant_conv(h)
115117
posterior = DiagonalGaussianDistribution(moments)
@@ -125,7 +127,7 @@ def _decode(
125127
image: Optional[torch.FloatTensor] = None,
126128
mask: Optional[torch.FloatTensor] = None,
127129
return_dict: bool = True,
128-
) -> Union[DecoderOutput, torch.FloatTensor]:
130+
) -> Union[DecoderOutput, Tuple[torch.FloatTensor]]:
129131
z = self.post_quant_conv(z)
130132
dec = self.decoder(z, image, mask)
131133

@@ -142,7 +144,7 @@ def decode(
142144
image: Optional[torch.FloatTensor] = None,
143145
mask: Optional[torch.FloatTensor] = None,
144146
return_dict: bool = True,
145-
) -> Union[DecoderOutput, torch.FloatTensor]:
147+
) -> Union[DecoderOutput, Tuple[torch.FloatTensor]]:
146148
decoded = self._decode(z, image, mask).sample
147149

148150
if not return_dict:
@@ -157,7 +159,7 @@ def forward(
157159
sample_posterior: bool = False,
158160
return_dict: bool = True,
159161
generator: Optional[torch.Generator] = None,
160-
) -> Union[DecoderOutput, torch.FloatTensor]:
162+
) -> Union[DecoderOutput, Tuple[torch.FloatTensor]]:
161163
r"""
162164
Args:
163165
sample (`torch.FloatTensor`): Input sample.

‎src/diffusers/models/autoencoder_kl.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,13 @@ def decode(
322322

323323
return DecoderOutput(sample=decoded)
324324

325-
def blend_v(self, a, b, blend_extent):
325+
def blend_v(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -> torch.Tensor:
326326
blend_extent = min(a.shape[2], b.shape[2], blend_extent)
327327
for y in range(blend_extent):
328328
b[:, :, y, :] = a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
329329
return b
330330

331-
def blend_h(self, a, b, blend_extent):
331+
def blend_h(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -> torch.Tensor:
332332
blend_extent = min(a.shape[3], b.shape[3], blend_extent)
333333
for x in range(blend_extent):
334334
b[:, :, :, x] = a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)

‎src/diffusers/models/autoencoder_tiny.py‎

Lines changed: 16 additions & 24 deletions

0 commit comments

Comments
 (0)