MCPcopy Create free account
hub / github.com/FreedomIntelligence/CMB / forward

Method forward

workers/chatglm3_modeling.py:536–576  ·  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

534 self.mlp = MLP(config, device=device)
535
536 def forward(
537 self, hidden_states, attention_mask, rotary_pos_emb, kv_cache=None, use_cache=True,
538 ):
539 # hidden_states: [s, b, h]
540
541 # Layer norm at the beginning of the transformer layer.
542 layernorm_output = self.input_layernorm(hidden_states)
543 # Self attention.
544 attention_output, kv_cache = self.self_attention(
545 layernorm_output,
546 attention_mask,
547 rotary_pos_emb,
548 kv_cache=kv_cache,
549 use_cache=use_cache
550 )
551
552 # Residual connection.
553 if self.apply_residual_connection_post_layernorm:
554 residual = layernorm_output
555 else:
556 residual = hidden_states
557
558 layernorm_input = torch.nn.functional.dropout(attention_output, p=self.hidden_dropout, training=self.training)
559 layernorm_input = residual + layernorm_input
560
561 # Layer norm post the self attention.
562 layernorm_output = self.post_attention_layernorm(layernorm_input)
563
564 # MLP.
565 mlp_output = self.mlp(layernorm_output)
566
567 # Second residual connection.
568 if self.apply_residual_connection_post_layernorm:
569 residual = layernorm_output
570 else:
571 residual = layernorm_input
572
573 output = torch.nn.functional.dropout(mlp_output, p=self.hidden_dropout, training=self.training)
574 output = residual + output
575
576 return output, kv_cache
577
578
579class GLMTransformer(torch.nn.Module):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected