MCPcopy Create free account
hub / github.com/Francis-Rings/FlashPortrait / WanT5EncoderModel

Class WanT5EncoderModel

wan/models/wan_text_encoder.py:256–393  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

254 return rel_buckets
255
256class WanT5EncoderModel(ModelMixin, ConfigMixin, FromOriginalModelMixin):
257 def __init__(self,
258 vocab,
259 dim,
260 dim_attn,
261 dim_ffn,
262 num_heads,
263 num_layers,
264 num_buckets,
265 shared_pos=True,
266 dropout=0.1):
267 super(WanT5EncoderModel, self).__init__()
268 self.dim = dim
269 self.dim_attn = dim_attn
270 self.dim_ffn = dim_ffn
271 self.num_heads = num_heads
272 self.num_layers = num_layers
273 self.num_buckets = num_buckets
274 self.shared_pos = shared_pos
275
276 # layers
277 self.token_embedding = vocab if isinstance(vocab, nn.Embedding) \
278 else nn.Embedding(vocab, dim)
279 self.pos_embedding = T5RelativeEmbedding(
280 num_buckets, num_heads, bidirectional=True) if shared_pos else None
281 self.dropout = nn.Dropout(dropout)
282 self.blocks = nn.ModuleList([
283 T5SelfAttention(dim, dim_attn, dim_ffn, num_heads, num_buckets,
284 shared_pos, dropout) for _ in range(num_layers)
285 ])
286 self.norm = T5LayerNorm(dim)
287
288 # initialize weights
289 self.apply(init_weights)
290
291 def forward(
292 self,
293 input_ids: Optional[torch.LongTensor] = None,
294 attention_mask: Optional[torch.FloatTensor] = None,
295 ):
296 x = self.token_embedding(input_ids)
297 x = self.dropout(x)
298 e = self.pos_embedding(x.size(1),
299 x.size(1)) if self.shared_pos else None
300 for block in self.blocks:
301 x = block(x, attention_mask, pos_bias=e)
302 x = self.norm(x)
303 x = self.dropout(x)
304 return (x, )
305
306 @classmethod
307 def from_pretrained(cls, pretrained_model_path, additional_kwargs={}, low_cpu_mem_usage=False, torch_dtype=torch.bfloat16):
308 def filter_kwargs(cls, kwargs):
309 import inspect
310 sig = inspect.signature(cls.__init__)
311 valid_params = set(sig.parameters.keys()) - {'self', 'cls'}
312 filtered_kwargs = {k: v for k, v in kwargs.items() if k in valid_params}
313 return filtered_kwargs

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected