PyTorch
File size: 1,047 Bytes
bd992b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import torch
import torch.nn as nn

from benchmarks.config import load_config
from benchmarks.registry import config_tester, model_init
from net import VanillaDecoderTransformer


@config_tester(
    model_name="VanillaDecoderTransformer",
    n_layers=8,
    d_model=512,
    head_amount=8,
    d_ffn=2048,
)
def count_params(vocab_size: int, seq_len: int, n_layers: int, d_model: int, head_amount: int, d_ffn: int):
    with torch.device("meta"):
        model = VanillaDecoderTransformer(
            vocab_size=vocab_size,
            n_layers=n_layers,
            n_ctx=seq_len,
            d_model=d_model,
            head_amount=head_amount,
            d_ffn=d_ffn
        )
    return sum(p.numel() for p in model.parameters())


@model_init(
    model_name="VanillaDecoderTransformer"
)
def make_model(n_params: int, vocab_size: int, seq_len: int) -> nn.Module:
    cfg = load_config("VanillaDecoderTransformer", n_params)
    return VanillaDecoderTransformer(
        vocab_size=vocab_size,
        n_ctx=seq_len,
        **cfg
    )