Coverage for transformer_lens/components/bert_mlm_head.py: 100%
20 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-11-19 14:42 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-11-19 14:42 +0000
1"""Hooked Transformer Bert MLM Head Component.
3This module contains all the component :class:`BertMLMHead`.
4"""
5from typing import Dict, Union
7import torch
8import torch.nn as nn
9from fancy_einsum import einsum
10from jaxtyping import Float
12from transformer_lens.components import LayerNorm
13from transformer_lens.HookedTransformerConfig import HookedTransformerConfig
16class BertMLMHead(nn.Module):
17 """
18 Transforms BERT embeddings into logits. The purpose of this module is to predict masked tokens in a sentence.
19 """
21 def __init__(self, cfg: Union[Dict, HookedTransformerConfig]):
22 super().__init__()
23 self.cfg = HookedTransformerConfig.unwrap(cfg)
24 self.W = nn.Parameter(torch.empty(self.cfg.d_model, self.cfg.d_model, dtype=self.cfg.dtype))
25 self.b = nn.Parameter(torch.zeros(self.cfg.d_model, dtype=self.cfg.dtype))
26 self.act_fn = nn.GELU()
27 self.ln = LayerNorm(self.cfg)
29 def forward(self, resid: Float[torch.Tensor, "batch pos d_model"]) -> torch.Tensor:
30 resid = (
31 einsum(
32 "batch pos d_model_in, d_model_out d_model_in -> batch pos d_model_out",
33 resid,
34 self.W,
35 )
36 + self.b
37 )
38 resid = self.act_fn(resid)
39 resid = self.ln(resid)
40 return resid