r""" Flax Unet Mid-Block module. Parameters: in_channels (:obj:`int`): Input channels dropout (:obj:`float`, *optional*, defaults to 0.0): Dropout rate num_layers (:obj:`int`, *optional*, defaults to 1): Number of Resnet layer bloc
| 387 | |
| 388 | |
| 389 | class FlaxUNetMidBlock2D(nn.Module): |
| 390 | r""" |
| 391 | Flax Unet Mid-Block module. |
| 392 | |
| 393 | Parameters: |
| 394 | in_channels (:obj:`int`): |
| 395 | Input channels |
| 396 | dropout (:obj:`float`, *optional*, defaults to 0.0): |
| 397 | Dropout rate |
| 398 | num_layers (:obj:`int`, *optional*, defaults to 1): |
| 399 | Number of Resnet layer block |
| 400 | resnet_groups (:obj:`int`, *optional*, defaults to `32`): |
| 401 | The number of groups to use for the Resnet and Attention block group norm |
| 402 | num_attention_heads (:obj:`int`, *optional*, defaults to `1`): |
| 403 | Number of attention heads for each attention block |
| 404 | dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32): |
| 405 | Parameters `dtype` |
| 406 | """ |
| 407 | |
| 408 | in_channels: int |
| 409 | dropout: float = 0.0 |
| 410 | num_layers: int = 1 |
| 411 | resnet_groups: int = 32 |
| 412 | num_attention_heads: int = 1 |
| 413 | dtype: jnp.dtype = jnp.float32 |
| 414 | |
| 415 | def setup(self): |
| 416 | resnet_groups = self.resnet_groups if self.resnet_groups is not None else min(self.in_channels // 4, 32) |
| 417 | |
| 418 | # there is always at least one resnet |
| 419 | resnets = [ |
| 420 | FlaxResnetBlock2D( |
| 421 | in_channels=self.in_channels, |
| 422 | out_channels=self.in_channels, |
| 423 | dropout=self.dropout, |
| 424 | groups=resnet_groups, |
| 425 | dtype=self.dtype, |
| 426 | ) |
| 427 | ] |
| 428 | |
| 429 | attentions = [] |
| 430 | |
| 431 | for _ in range(self.num_layers): |
| 432 | attn_block = FlaxAttentionBlock( |
| 433 | channels=self.in_channels, |
| 434 | num_head_channels=self.num_attention_heads, |
| 435 | num_groups=resnet_groups, |
| 436 | dtype=self.dtype, |
| 437 | ) |
| 438 | attentions.append(attn_block) |
| 439 | |
| 440 | res_block = FlaxResnetBlock2D( |
| 441 | in_channels=self.in_channels, |
| 442 | out_channels=self.in_channels, |
| 443 | dropout=self.dropout, |
| 444 | groups=resnet_groups, |
| 445 | dtype=self.dtype, |
| 446 | ) |