MCPcopy Create free account
hub / github.com/OpenImagingLab/FlashVSR / CLIPEncoderLayer

Class CLIPEncoderLayer

diffsynth/models/sd_text_encoder.py:5–36  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3
4
5class CLIPEncoderLayer(torch.nn.Module):
6 def __init__(self, embed_dim, intermediate_size, num_heads=12, head_dim=64, use_quick_gelu=True):
7 super().__init__()
8 self.attn = Attention(q_dim=embed_dim, num_heads=num_heads, head_dim=head_dim, bias_q=True, bias_kv=True, bias_out=True)
9 self.layer_norm1 = torch.nn.LayerNorm(embed_dim)
10 self.layer_norm2 = torch.nn.LayerNorm(embed_dim)
11 self.fc1 = torch.nn.Linear(embed_dim, intermediate_size)
12 self.fc2 = torch.nn.Linear(intermediate_size, embed_dim)
13
14 self.use_quick_gelu = use_quick_gelu
15
16 def quickGELU(self, x):
17 return x * torch.sigmoid(1.702 * x)
18
19 def forward(self, hidden_states, attn_mask=None):
20 residual = hidden_states
21
22 hidden_states = self.layer_norm1(hidden_states)
23 hidden_states = self.attn(hidden_states, attn_mask=attn_mask)
24 hidden_states = residual + hidden_states
25
26 residual = hidden_states
27 hidden_states = self.layer_norm2(hidden_states)
28 hidden_states = self.fc1(hidden_states)
29 if self.use_quick_gelu:
30 hidden_states = self.quickGELU(hidden_states)
31 else:
32 hidden_states = torch.nn.functional.gelu(hidden_states)
33 hidden_states = self.fc2(hidden_states)
34 hidden_states = residual + hidden_states
35
36 return hidden_states
37
38
39class SDTextEncoder(torch.nn.Module):

Callers 4

__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected