ereniko commited on
Commit
a62ab9e
·
verified ·
1 Parent(s): 5405013

Delete transformer.py

Browse files
Files changed (1) hide show
  1. transformer.py +0 -90
transformer.py DELETED
@@ -1,90 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
-
4
- from .config import IvmeConfig
5
- from .rmsnorm import RMSNorm
6
- from .rope import precompute_rope_freqs
7
- from .attention import CausalSelfAttention
8
- from .feedforward import SwiGLU
9
-
10
-
11
- class TransformerBlock(nn.Module):
12
- """One dense transformer layer (Section 3): pre-norm attention + pre-norm SwiGLU,
13
- with residual connections around each. Identical shape repeated n_layers times --
14
- no loops, no weight sharing (Section 3.1, distinguishing this from the shelved
15
- Ivmetron design).
16
- """
17
-
18
- def __init__(self, cfg: IvmeConfig):
19
- super().__init__()
20
- self.attn_norm = RMSNorm(cfg.hidden_dim, eps=cfg.norm_eps)
21
- self.attn = CausalSelfAttention(cfg.hidden_dim, cfg.n_heads, cfg.dropout)
22
- self.ffn_norm = RMSNorm(cfg.hidden_dim, eps=cfg.norm_eps)
23
- self.ffn = SwiGLU(cfg.hidden_dim, cfg.ffn_mult)
24
-
25
- def forward(self, x: torch.Tensor, rope_freqs: torch.Tensor) -> torch.Tensor:
26
- x = x + self.attn(self.attn_norm(x), rope_freqs)
27
- x = x + self.ffn(self.ffn_norm(x))
28
- return x
29
-
30
-
31
- class IvmeConversateV2(nn.Module):
32
- """Ivme-Conversate-v2 (Dense) -- the full model described in Section 4.
33
-
34
- ~20M parameters, 10 layers, hidden_dim 384, 6 heads, RoPE, SwiGLU, RMSNorm,
35
- tied embeddings, 16k vocab, 1024 context. See config.py for the exact spec.
36
- """
37
-
38
- def __init__(self, cfg: IvmeConfig):
39
- super().__init__()
40
- self.cfg = cfg
41
-
42
- self.tok_embed = nn.Embedding(cfg.vocab_size, cfg.hidden_dim)
43
- self.blocks = nn.ModuleList([TransformerBlock(cfg) for _ in range(cfg.n_layers)])
44
- self.final_norm = RMSNorm(cfg.hidden_dim, eps=cfg.norm_eps)
45
-
46
- # Section 4.8: tied embeddings -- output head reuses the input embedding
47
- # table instead of learning a separate one.
48
- self.lm_head = nn.Linear(cfg.hidden_dim, cfg.vocab_size, bias=False)
49
- if cfg.tie_embeddings:
50
- self.lm_head.weight = self.tok_embed.weight
51
-
52
- rope_freqs = precompute_rope_freqs(cfg.head_dim, cfg.context_len, cfg.rope_theta)
53
- self.register_buffer("rope_freqs", rope_freqs, persistent=False)
54
-
55
- self.apply(self._init_weights)
56
-
57
- def _init_weights(self, module: nn.Module):
58
- if isinstance(module, nn.Linear):
59
- nn.init.normal_(module.weight, mean=0.0, std=0.02)
60
- if module.bias is not None:
61
- nn.init.zeros_(module.bias)
62
- elif isinstance(module, nn.Embedding):
63
- nn.init.normal_(module.weight, mean=0.0, std=0.02)
64
-
65
- def forward(self, idx: torch.Tensor, targets: torch.Tensor | None = None):
66
- B, T = idx.shape
67
- assert T <= self.cfg.context_len, (
68
- f"sequence length {T} exceeds context_len {self.cfg.context_len}"
69
- )
70
-
71
- x = self.tok_embed(idx)
72
- for block in self.blocks:
73
- x = block(x, self.rope_freqs)
74
- x = self.final_norm(x)
75
- logits = self.lm_head(x)
76
-
77
- loss = None
78
- if targets is not None:
79
- loss = nn.functional.cross_entropy(
80
- logits.view(-1, logits.size(-1)),
81
- targets.view(-1),
82
- ignore_index=-1,
83
- )
84
- return logits, loss
85
-
86
- def num_params(self, non_embedding: bool = False) -> int:
87
- n = sum(p.numel() for p in self.parameters())
88
- if non_embedding:
89
- n -= self.tok_embed.weight.numel()
90
- return n