r""" An AdapterBlock is a helper model that contains multiple ResNet-like blocks. It is used in the `FullAdapter` and `FullAdapterXL` models. Parameters: in_channels (`int`): Number of channels of AdapterBlock's input. out_channels (`int`): Number
| 389 | |
| 390 | |
| 391 | class AdapterBlock(nn.Module): |
| 392 | r""" |
| 393 | An AdapterBlock is a helper model that contains multiple ResNet-like blocks. It is used in the `FullAdapter` and |
| 394 | `FullAdapterXL` models. |
| 395 | |
| 396 | Parameters: |
| 397 | in_channels (`int`): |
| 398 | Number of channels of AdapterBlock's input. |
| 399 | out_channels (`int`): |
| 400 | Number of channels of AdapterBlock's output. |
| 401 | num_res_blocks (`int`): |
| 402 | Number of ResNet blocks in the AdapterBlock. |
| 403 | down (`bool`, *optional*, defaults to `False`): |
| 404 | Whether to perform downsampling on AdapterBlock's input. |
| 405 | """ |
| 406 | |
| 407 | def __init__(self, in_channels: int, out_channels: int, num_res_blocks: int, down: bool = False): |
| 408 | super().__init__() |
| 409 | |
| 410 | self.downsample = None |
| 411 | if down: |
| 412 | self.downsample = nn.AvgPool2d(kernel_size=2, stride=2, ceil_mode=True) |
| 413 | |
| 414 | self.in_conv = None |
| 415 | if in_channels != out_channels: |
| 416 | self.in_conv = nn.Conv2d(in_channels, out_channels, kernel_size=1) |
| 417 | |
| 418 | self.resnets = nn.Sequential( |
| 419 | *[AdapterResnetBlock(out_channels) for _ in range(num_res_blocks)], |
| 420 | ) |
| 421 | |
| 422 | def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 423 | r""" |
| 424 | This method takes tensor x as input and performs operations downsampling and convolutional layers if the |
| 425 | self.downsample and self.in_conv properties of AdapterBlock model are specified. Then it applies a series of |
| 426 | residual blocks to the input tensor. |
| 427 | """ |
| 428 | if self.downsample is not None: |
| 429 | x = self.downsample(x) |
| 430 | |
| 431 | if self.in_conv is not None: |
| 432 | x = self.in_conv(x) |
| 433 | |
| 434 | x = self.resnets(x) |
| 435 | |
| 436 | return x |
| 437 | |
| 438 | |
| 439 | class AdapterResnetBlock(nn.Module): |