r""" Flax Resnet blocks-based Decoder block for diffusion-based VAE. Parameters: in_channels (:obj:`int`): Input channels out_channels (:obj:`int`): Output channels dropout (:obj:`float`, *optional*, defaults to 0.0): Dropout rate
| 330 | |
| 331 | |
| 332 | class FlaxUpDecoderBlock2D(nn.Module): |
| 333 | r""" |
| 334 | Flax Resnet blocks-based Decoder block for diffusion-based VAE. |
| 335 | |
| 336 | Parameters: |
| 337 | in_channels (:obj:`int`): |
| 338 | Input channels |
| 339 | out_channels (:obj:`int`): |
| 340 | Output channels |
| 341 | dropout (:obj:`float`, *optional*, defaults to 0.0): |
| 342 | Dropout rate |
| 343 | num_layers (:obj:`int`, *optional*, defaults to 1): |
| 344 | Number of Resnet layer block |
| 345 | resnet_groups (:obj:`int`, *optional*, defaults to `32`): |
| 346 | The number of groups to use for the Resnet block group norm |
| 347 | add_upsample (:obj:`bool`, *optional*, defaults to `True`): |
| 348 | Whether to add upsample layer |
| 349 | dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32): |
| 350 | Parameters `dtype` |
| 351 | """ |
| 352 | |
| 353 | in_channels: int |
| 354 | out_channels: int |
| 355 | dropout: float = 0.0 |
| 356 | num_layers: int = 1 |
| 357 | resnet_groups: int = 32 |
| 358 | add_upsample: bool = True |
| 359 | dtype: jnp.dtype = jnp.float32 |
| 360 | |
| 361 | def setup(self): |
| 362 | resnets = [] |
| 363 | for i in range(self.num_layers): |
| 364 | in_channels = self.in_channels if i == 0 else self.out_channels |
| 365 | res_block = FlaxResnetBlock2D( |
| 366 | in_channels=in_channels, |
| 367 | out_channels=self.out_channels, |
| 368 | dropout=self.dropout, |
| 369 | groups=self.resnet_groups, |
| 370 | dtype=self.dtype, |
| 371 | ) |
| 372 | resnets.append(res_block) |
| 373 | |
| 374 | self.resnets = resnets |
| 375 | |
| 376 | if self.add_upsample: |
| 377 | self.upsamplers_0 = FlaxUpsample2D(self.out_channels, dtype=self.dtype) |
| 378 | |
| 379 | def __call__(self, hidden_states, deterministic=True): |
| 380 | for resnet in self.resnets: |
| 381 | hidden_states = resnet(hidden_states, deterministic=deterministic) |
| 382 | |
| 383 | if self.add_upsample: |
| 384 | hidden_states = self.upsamplers_0(hidden_states) |
| 385 | |
| 386 | return hidden_states |
| 387 | |
| 388 | |
| 389 | class FlaxUNetMidBlock2D(nn.Module): |