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

Method forward

train/patch/modeling_chatglm.py:446–486  ·  view source on GitHub ↗
(
            self, hidden_states, attention_mask, rotary_pos_emb, kv_cache=None, use_cache=True,
    )

Source from the content-addressed store, hash-verified

444 self.mlp = MLP(config, device=device)
445
446 def forward(
447 self, hidden_states, attention_mask, rotary_pos_emb, kv_cache=None, use_cache=True,
448 ):
449 # hidden_states: [s, b, h]
450
451 # Layer norm at the beginning of the transformer layer.
452 layernorm_output = self.input_layernorm(hidden_states)
453 # Self attention.
454 attention_output, kv_cache = self.self_attention(
455 layernorm_output,
456 attention_mask,
457 rotary_pos_emb,
458 kv_cache=kv_cache,
459 use_cache=use_cache
460 )
461
462 # Residual connection.
463 if self.apply_residual_connection_post_layernorm:
464 residual = layernorm_output
465 else:
466 residual = hidden_states
467
468 layernorm_input = torch.nn.functional.dropout(attention_output, p=self.hidden_dropout, training=self.training)
469 layernorm_input = residual + layernorm_input
470
471 # Layer norm post the self attention.
472 layernorm_output = self.post_attention_layernorm(layernorm_input)
473
474 # MLP.
475 mlp_output = self.mlp(layernorm_output)
476
477 # Second residual connection.
478 if self.apply_residual_connection_post_layernorm:
479 residual = layernorm_output
480 else:
481 residual = layernorm_input
482
483 output = torch.nn.functional.dropout(mlp_output, p=self.hidden_dropout, training=self.training)
484 output = residual + output
485
486 return output, kv_cache
487
488
489class GLMTransformer(torch.nn.Module):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected