| 78 | return x |
| 79 | |
| 80 | class ConditionalDecoder(nn.Module): |
| 81 | def __init__( |
| 82 | self, |
| 83 | in_channels, |
| 84 | out_channels, |
| 85 | channels=(256, 256), |
| 86 | dropout=0.05, |
| 87 | attention_head_dim=64, |
| 88 | n_blocks=1, |
| 89 | num_mid_blocks=2, |
| 90 | num_heads=4, |
| 91 | act_fn="snake", |
| 92 | ): |
| 93 | """ |
| 94 | This decoder requires an input with the same shape of the target. So, if your text content |
| 95 | is shorter or longer than the outputs, please re-sampling it before feeding to the decoder. |
| 96 | """ |
| 97 | super().__init__() |
| 98 | channels = tuple(channels) |
| 99 | self.in_channels = in_channels |
| 100 | self.out_channels = out_channels |
| 101 | |
| 102 | self.time_embeddings = SinusoidalPosEmb(in_channels) |
| 103 | time_embed_dim = channels[0] * 4 |
| 104 | self.time_mlp = TimestepEmbedding( |
| 105 | in_channels=in_channels, |
| 106 | time_embed_dim=time_embed_dim, |
| 107 | act_fn="silu", |
| 108 | ) |
| 109 | self.down_blocks = nn.ModuleList([]) |
| 110 | self.mid_blocks = nn.ModuleList([]) |
| 111 | self.up_blocks = nn.ModuleList([]) |
| 112 | |
| 113 | output_channel = in_channels |
| 114 | for i in range(len(channels)): # pylint: disable=consider-using-enumerate |
| 115 | input_channel = output_channel |
| 116 | output_channel = channels[i] |
| 117 | is_last = i == len(channels) - 1 |
| 118 | resnet = ResnetBlock1D(dim=input_channel, dim_out=output_channel, time_emb_dim=time_embed_dim) |
| 119 | transformer_blocks = nn.ModuleList( |
| 120 | [ |
| 121 | BasicTransformerBlock( |
| 122 | dim=output_channel, |
| 123 | num_attention_heads=num_heads, |
| 124 | attention_head_dim=attention_head_dim, |
| 125 | dropout=dropout, |
| 126 | activation_fn=act_fn, |
| 127 | ) |
| 128 | for _ in range(n_blocks) |
| 129 | ] |
| 130 | ) |
| 131 | downsample = ( |
| 132 | Downsample1D(output_channel) if not is_last else nn.Conv1d(output_channel, output_channel, 3, padding=1) |
| 133 | ) |
| 134 | self.down_blocks.append(nn.ModuleList([resnet, transformer_blocks, downsample])) |
| 135 | |
| 136 | for _ in range(num_mid_blocks): |
| 137 | input_channel = channels[-1] |
nothing calls this directly
no outgoing calls
no test coverage detected