MCPcopy Create free account
hub / github.com/kyegomez/BitNet / FeedForward

Class FeedForward

bitnet/bit_llama.py:313–354  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

311
312
313class FeedForward(nn.Module):
314 def __init__(
315 self,
316 dim: int,
317 hidden_dim: int,
318 multiple_of: int,
319 ffn_dim_multiplier: Optional[float],
320 ):
321 """
322 Initialize the FeedForward module.
323
324 Args:
325 dim (int): Input dimension.
326 hidden_dim (int): Hidden dimension of the feedforward layer.
327 multiple_of (int): Value to ensure hidden dimension is a multiple of this value.
328 ffn_dim_multiplier (float, optional): Custom multiplier for hidden dimension. Defaults to None.
329
330 Attributes:
331 w1 (ColumnParallelLinear): Linear transformation for the first layer.
332 w2 (RowParallelLinear): Linear transformation for the second layer.
333 w3 (ColumnParallelLinear): Linear transformation for the third layer.
334
335 """
336 super().__init__()
337 hidden_dim = int(2 * hidden_dim / 3)
338 # custom dim factor multiplier
339 if ffn_dim_multiplier is not None:
340 hidden_dim = int(ffn_dim_multiplier * hidden_dim)
341 hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of)
342
343 self.w1 = ColumnParallelLinear(
344 dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x
345 )
346 self.w2 = RowParallelLinear(
347 hidden_dim, dim, bias=False, input_is_parallel=True, init_method=lambda x: x
348 )
349 self.w3 = ColumnParallelLinear(
350 dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x
351 )
352
353 def forward(self, x):
354 return self.w2(F.silu(self.w1(x)) * self.w3(x))
355
356
357class TransformerBlock(nn.Module):

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected