File size: 1,151 Bytes
59d5a80
3d66068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
# model_utils.py -Handles model loading and metadata extraction:

from transformers import (
    AutoTokenizer,
    AutoModel,
    AutoModelForCausalLM,
)
import torch


MODEL_OPTIONS = {
    "BERT (bert-base-uncased)": "bert-base-uncased",
    "DistilBERT": "distilbert-base-uncased",
    "RoBERTa": "roberta-base",
    "GPT-2": "gpt2",
    "Electra": "google/electra-small-discriminator",
    "ALBERT": "albert-base-v2",
    "XLNet": "xlnet-base-cased",
}


def load_model(model_name):
    if "gpt2" in model_name or "causal" in model_name:
        model = AutoModelForCausalLM.from_pretrained(model_name, output_attentions=True)
    else:
        model = AutoModel.from_pretrained(model_name, output_attentions=True)

    tokenizer = AutoTokenizer.from_pretrained(model_name)
    return tokenizer, model


def get_model_info(model):
    config = model.config
    return {
        "Model Type": config.model_type,
        "Number of Layers": getattr(config, "num_hidden_layers", "N/A"),
        "Number of Attention Heads": getattr(config, "num_attention_heads", "N/A"),
        "Total Parameters": sum(p.numel() for p in model.parameters()),
    }