MCPcopy Create free account
hub / github.com/GeWu-Lab/AnyTouch2 / forward

Method forward

model/layers/decoder_block.py:104–140  ·  view source on GitHub ↗
(self, q, kv)

Source from the content-addressed store, hash-verified

102 self.sample_drop_ratio = drop_path
103
104 def forward(self, q, kv):
105 def self_attn_residual_func(q: Tensor) -> Tensor:
106 return self.ls1(self.self_attn(self.norm1(q)))
107
108 def cross_attn_residual_func(q: Tensor, kv: Tensor) -> Tensor:
109 return self.ls2(self.cross_attn(self.q_norm2(q), self.kv_norm2(kv)))
110
111 def ffn_residual_func(q: Tensor) -> Tensor:
112 return self.ls3(self.mlp(self.norm3(q)))
113
114 if self.training and self.sample_drop_ratio > 0.1:
115 # the overhead is compensated only for a drop path rate larger than 0.1
116 q = drop_add_residual_stochastic_depth(
117 [q],
118 residual_func=self_attn_residual_func,
119 sample_drop_ratio=self.sample_drop_ratio,
120 )
121 q = drop_add_residual_stochastic_depth(
122 [q, kv],
123 residual_func=cross_attn_residual_func,
124 sample_drop_ratio=self.sample_drop_ratio,
125 )
126 q = drop_add_residual_stochastic_depth(
127 [q],
128 residual_func=ffn_residual_func,
129 sample_drop_ratio=self.sample_drop_ratio,
130 )
131 elif self.training and self.sample_drop_ratio > 0.0:
132 q = q + self.drop_path1(self_attn_residual_func(q))
133 q = q + self.drop_path2(cross_attn_residual_func(q, kv))
134 q = q + self.drop_path3(ffn_residual_func(q))
135 else:
136 q = q + self_attn_residual_func(q)
137 q = q + cross_attn_residual_func(q, kv)
138 q = q + ffn_residual_func(q)
139
140 return q
141
142
143def drop_add_residual_stochastic_depth(

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected