MCPcopy Create free account
hub / github.com/THUDM/LongWriter / MLP

Class MLP

train/patch/modeling_chatglm.py:369–412  ·  view source on GitHub ↗

MLP. MLP will take the input with h hidden state, project it to 4*h hidden dimension, perform nonlinear transformation, and project the state back into h hidden dimension.

Source from the content-addressed store, hash-verified

367
368
369class MLP(torch.nn.Module):
370 """MLP.
371
372 MLP will take the input with h hidden state, project it to 4*h
373 hidden dimension, perform nonlinear transformation, and project the
374 state back into h hidden dimension.
375 """
376
377 def __init__(self, config: ChatGLMConfig, device=None):
378 super(MLP, self).__init__()
379
380 self.add_bias = config.add_bias_linear
381
382 # Project to 4h. If using swiglu double the output width, see https://arxiv.org/pdf/2002.05202.pdf
383 self.dense_h_to_4h = nn.Linear(
384 config.hidden_size,
385 config.ffn_hidden_size * 2,
386 bias=self.add_bias,
387 device=device,
388 **_config_to_kwargs(config)
389 )
390
391 def swiglu(x):
392 x = torch.chunk(x, 2, dim=-1)
393 return F.silu(x[0]) * x[1]
394
395 self.activation_func = swiglu
396
397 # Project back to h.
398 self.dense_4h_to_h = nn.Linear(
399 config.ffn_hidden_size,
400 config.hidden_size,
401 bias=self.add_bias,
402 device=device,
403 **_config_to_kwargs(config)
404 )
405
406 def forward(self, hidden_states):
407 # [s, b, 4hp]
408 intermediate_parallel = self.dense_h_to_4h(hidden_states)
409 intermediate_parallel = self.activation_func(intermediate_parallel)
410 # [s, b, h]
411 output = self.dense_4h_to_h(intermediate_parallel)
412 return output
413
414
415class GLMBlock(torch.nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected