r""" Flax Implementation of VAE Encoder. This model is a Flax Linen [flax.linen.Module](https://flax.readthedocs.io/en/latest/flax.linen.html#module) subclass. Use it as a regular Flax linen Module and refer to the Flax documentation for all matter related to general usage and behav
| 459 | |
| 460 | |
| 461 | class FlaxEncoder(nn.Module): |
| 462 | r""" |
| 463 | Flax Implementation of VAE Encoder. |
| 464 | |
| 465 | This model is a Flax Linen [flax.linen.Module](https://flax.readthedocs.io/en/latest/flax.linen.html#module) |
| 466 | subclass. Use it as a regular Flax linen Module and refer to the Flax documentation for all matter related to |
| 467 | general usage and behavior. |
| 468 | |
| 469 | Finally, this model supports inherent JAX features such as: |
| 470 | - [Just-In-Time (JIT) compilation](https://jax.readthedocs.io/en/latest/jax.html#just-in-time-compilation-jit) |
| 471 | - [Automatic Differentiation](https://jax.readthedocs.io/en/latest/jax.html#automatic-differentiation) |
| 472 | - [Vectorization](https://jax.readthedocs.io/en/latest/jax.html#vectorization-vmap) |
| 473 | - [Parallelization](https://jax.readthedocs.io/en/latest/jax.html#parallelization-pmap) |
| 474 | |
| 475 | Parameters: |
| 476 | in_channels (:obj:`int`, *optional*, defaults to 3): |
| 477 | Input channels |
| 478 | out_channels (:obj:`int`, *optional*, defaults to 3): |
| 479 | Output channels |
| 480 | down_block_types (:obj:`Tuple[str]`, *optional*, defaults to `(DownEncoderBlock2D)`): |
| 481 | DownEncoder block type |
| 482 | block_out_channels (:obj:`Tuple[str]`, *optional*, defaults to `(64,)`): |
| 483 | Tuple containing the number of output channels for each block |
| 484 | layers_per_block (:obj:`int`, *optional*, defaults to `2`): |
| 485 | Number of Resnet layer for each block |
| 486 | norm_num_groups (:obj:`int`, *optional*, defaults to `32`): |
| 487 | norm num group |
| 488 | act_fn (:obj:`str`, *optional*, defaults to `silu`): |
| 489 | Activation function |
| 490 | double_z (:obj:`bool`, *optional*, defaults to `False`): |
| 491 | Whether to double the last output channels |
| 492 | dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32): |
| 493 | Parameters `dtype` |
| 494 | """ |
| 495 | |
| 496 | in_channels: int = 3 |
| 497 | out_channels: int = 3 |
| 498 | down_block_types: Tuple[str] = ("DownEncoderBlock2D",) |
| 499 | block_out_channels: Tuple[int] = (64,) |
| 500 | layers_per_block: int = 2 |
| 501 | norm_num_groups: int = 32 |
| 502 | act_fn: str = "silu" |
| 503 | double_z: bool = False |
| 504 | dtype: jnp.dtype = jnp.float32 |
| 505 | |
| 506 | def setup(self): |
| 507 | block_out_channels = self.block_out_channels |
| 508 | # in |
| 509 | self.conv_in = nn.Conv( |
| 510 | block_out_channels[0], |
| 511 | kernel_size=(3, 3), |
| 512 | strides=(1, 1), |
| 513 | padding=((1, 1), (1, 1)), |
| 514 | dtype=self.dtype, |
| 515 | ) |
| 516 | |
| 517 | # downsampling |
| 518 | down_blocks = [] |