MCPcopy Create free account
hub / github.com/MotrixLab/ViMoGen / T5Decoder

Class T5Decoder

models/transformer/wan/modules/t5.py:315–369  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

313
314
315class T5Decoder(nn.Module):
316
317 def __init__(self,
318 vocab,
319 dim,
320 dim_attn,
321 dim_ffn,
322 num_heads,
323 num_layers,
324 num_buckets,
325 shared_pos=True,
326 dropout=0.1):
327 super(T5Decoder, self).__init__()
328 self.dim = dim
329 self.dim_attn = dim_attn
330 self.dim_ffn = dim_ffn
331 self.num_heads = num_heads
332 self.num_layers = num_layers
333 self.num_buckets = num_buckets
334 self.shared_pos = shared_pos
335
336 # layers
337 self.token_embedding = vocab if isinstance(vocab, nn.Embedding) \
338 else nn.Embedding(vocab, dim)
339 self.pos_embedding = T5RelativeEmbedding(
340 num_buckets, num_heads, bidirectional=False) if shared_pos else None
341 self.dropout = nn.Dropout(dropout)
342 self.blocks = nn.ModuleList([
343 T5CrossAttention(dim, dim_attn, dim_ffn, num_heads, num_buckets,
344 shared_pos, dropout) for _ in range(num_layers)
345 ])
346 self.norm = T5LayerNorm(dim)
347
348 # initialize weights
349 self.apply(init_weights)
350
351 def forward(self, ids, mask=None, encoder_states=None, encoder_mask=None):
352 b, s = ids.size()
353
354 # causal mask
355 if mask is None:
356 mask = torch.tril(torch.ones(1, s, s).to(ids.device))
357 elif mask.ndim == 2:
358 mask = torch.tril(mask.unsqueeze(1).expand(-1, s, -1))
359
360 # layers
361 x = self.token_embedding(ids)
362 x = self.dropout(x)
363 e = self.pos_embedding(x.size(1),
364 x.size(1)) if self.shared_pos else None
365 for block in self.blocks:
366 x = block(x, mask, encoder_states, encoder_mask, pos_bias=e)
367 x = self.norm(x)
368 x = self.dropout(x)
369 return x
370
371
372class T5Model(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected