File size: 9,971 Bytes
756cf82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python3
"""

CSE 251B NanoGPT Competition — Evaluation Script



Computes perplexity of a submitted model on a tokenized evaluation dataset.



Two modes:

  Local (during development):

    python evaluate.py --model_dir ./my_submission/ --data val.bin



  HuggingFace (verify your submission):

    python evaluate.py --hf_repo your-username/cse251b-group-XX --data val.bin



  Other options:

    --checkpoint_filename   Name of checkpoint file (default: checkpoint.pt)

    --block_size            Context window size (default: 1024)

    --batch_size            Eval batch size (default: 8)

    --device                cuda or cpu (default: cuda)



Your model.py must define:

    load_model(checkpoint_path: str, device: str) -> nn.Module



The returned model must satisfy:

    model(input_ids) -> logits

    - input_ids: LongTensor of shape (batch, seq_len)

    - logits: FloatTensor of shape (batch, seq_len, 50257)

"""

import argparse
import importlib.util
import math
import os
import sys
import tempfile
import time

import numpy as np
import torch
import torch.nn.functional as F


# ============================================================
# Model loading
# ============================================================

def import_load_model(model_dir: str):
    """Dynamically import load_model from the student's model.py."""
    model_py = os.path.join(model_dir, "model.py")
    if not os.path.exists(model_py):
        raise FileNotFoundError(
            f"Expected model.py in {model_dir}. "
            f"Your submission directory must contain a model.py with a load_model() function."
        )
    # Add model_dir to sys.path so relative imports within model.py work
    sys.path.insert(0, os.path.abspath(model_dir))
    spec = importlib.util.spec_from_file_location("student_model", model_py)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    if not hasattr(module, "load_model"):
        raise AttributeError(
            f"model.py in {model_dir} must define a load_model(checkpoint_path, device) function."
        )
    return module.load_model


def download_from_hf(repo_id: str, local_dir: str = None) -> str:
    """Download a model submission from HuggingFace Hub.



    Returns the local directory path containing the downloaded files.

    """
    try:
        from huggingface_hub import snapshot_download
    except ImportError:
        raise ImportError(
            "huggingface_hub is required for --hf_repo mode.\n"
            "Install it with: pip install huggingface_hub"
        )

    if local_dir is None:
        local_dir = tempfile.mkdtemp(prefix="cse251b_eval_")

    print(f"Downloading {repo_id} from HuggingFace Hub...")
    path = snapshot_download(
        repo_id,
        local_dir=local_dir,
        local_dir_use_symlinks=False,
    )
    print(f"Downloaded to {path}")

    if not os.path.exists(os.path.join(path, "model.py")):
        raise FileNotFoundError(
            f"model.py not found in HuggingFace repo {repo_id}. "
            f"Your repo must contain model.py with a load_model() function."
        )
    return path


# ============================================================
# Perplexity computation
# ============================================================

@torch.no_grad()
def compute_perplexity(

    model: torch.nn.Module,

    data_path: str,

    block_size: int = 1024,

    batch_size: int = 8,

    device: str = "cuda",

) -> dict:
    """

    Compute perplexity on a tokenized .bin file.



    Uses non-overlapping windows of block_size tokens.

    """
    data = np.memmap(data_path, dtype=np.uint16, mode="r")
    data = torch.from_numpy(data.astype(np.int64))

    total_loss = 0.0
    total_tokens = 0
    n_chunks = (len(data) - 1) // block_size
    n_chunks = (n_chunks // batch_size) * batch_size  # full batches only

    if n_chunks == 0:
        raise ValueError(
            f"Data too small: {len(data)} tokens. "
            f"Need at least {block_size * batch_size + 1}."
        )

    for i in range(0, n_chunks, batch_size):
        input_ids = torch.stack([
            data[j * block_size : j * block_size + block_size]
            for j in range(i, i + batch_size)
        ]).to(device)

        targets = torch.stack([
            data[j * block_size + 1 : j * block_size + block_size + 1]
            for j in range(i, i + batch_size)
        ]).to(device)

        logits = model(input_ids)

        # Validate output shape
        if logits.shape[-1] != 50257:
            raise ValueError(
                f"Model output has vocab size {logits.shape[-1]}, expected 50257. "
                f"Your model must produce logits over the GPT-2 vocabulary."
            )

        loss = F.cross_entropy(
            logits.reshape(-1, logits.size(-1)),
            targets.reshape(-1),
            reduction="sum",
        )
        total_loss += loss.item()
        total_tokens += targets.numel()

    avg_loss = total_loss / total_tokens
    perplexity = math.exp(avg_loss)

    return {
        "perplexity": perplexity,
        "avg_loss_nats": avg_loss,
        "total_tokens_evaluated": total_tokens,
    }


# ============================================================
# Main
# ============================================================

def main():
    parser = argparse.ArgumentParser(
        description="CSE 251B — Evaluate a NanoGPT model",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""

Examples:

  Local eval:        python evaluate.py --model_dir ./my_model/ --data val.bin

  HuggingFace eval:  python evaluate.py --hf_repo username/cse251b-group-01 --data val.bin

  CPU fallback:      python evaluate.py --model_dir ./my_model/ --data val.bin --device cpu

        """,
    )
    source = parser.add_mutually_exclusive_group(required=True)
    source.add_argument(
        "--model_dir", type=str,
        help="Local directory containing model.py and checkpoint.pt",
    )
    source.add_argument(
        "--hf_repo", type=str,
        help="HuggingFace repo ID (e.g., 'username/cse251b-group-01'). "
             "Downloads the repo and evaluates. Use this to verify your submission.",
    )
    parser.add_argument(
        "--data", type=str, required=True,
        help="Path to tokenized eval data (.bin file)",
    )
    parser.add_argument(
        "--checkpoint_filename", type=str, default="checkpoint.pt",
        help="Checkpoint filename inside model_dir/repo (default: checkpoint.pt)",
    )
    parser.add_argument("--block_size", type=int, default=1024)
    parser.add_argument("--batch_size", type=int, default=8)
    parser.add_argument("--device", type=str, default="cuda")
    parser.add_argument(
        "--output_json", type=str, default=None,
        help="If provided, write results as JSON to this path (used by TA batch eval script).",
    )
    args = parser.parse_args()

    if args.device == "cuda" and not torch.cuda.is_available():
        print("CUDA not available, falling back to CPU.")
        args.device = "cpu"

    # Resolve model directory (local or download from HF)
    if args.hf_repo:
        model_dir = download_from_hf(args.hf_repo)
        print(f"\n--- Evaluating HuggingFace submission: {args.hf_repo} ---")
    else:
        model_dir = args.model_dir
        print(f"\n--- Evaluating local model: {model_dir} ---")

    # Load model
    load_fn = import_load_model(model_dir)
    ckpt_path = os.path.join(model_dir, args.checkpoint_filename)
    if not os.path.exists(ckpt_path):
        raise FileNotFoundError(
            f"Checkpoint not found: {ckpt_path}\n"
            f"Files in {model_dir}: {os.listdir(model_dir)}"
        )

    print(f"Loading model from {ckpt_path}...")
    model = load_fn(ckpt_path, args.device)
    model.eval()

    # Count and report parameters
    total_params = sum(p.numel() for p in model.parameters())
    trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print(f"Total parameters:     {total_params:>12,}")
    print(f"Trainable parameters: {trainable_params:>12,}")

    if total_params > 100_000_000:
        print(f"\n*** WARNING: Model has {total_params:,} parameters (> 100M limit). ***")
        print(f"*** This submission would be DISQUALIFIED. ***\n")

    # Evaluate
    print(f"Evaluating on {args.data} (block_size={args.block_size})...")
    t0 = time.time()
    results = compute_perplexity(
        model, args.data,
        block_size=args.block_size,
        batch_size=args.batch_size,
        device=args.device,
    )
    elapsed = time.time() - t0

    print(f"\n{'=' * 50}")
    print(f"  Perplexity:          {results['perplexity']:.4f}")
    print(f"  Avg loss (nats):     {results['avg_loss_nats']:.6f}")
    print(f"  Tokens evaluated:    {results['total_tokens_evaluated']:,}")
    print(f"  Total parameters:    {total_params:,}")
    print(f"  Eval time:           {elapsed:.1f}s")
    print(f"{'=' * 50}")

    if args.hf_repo:
        print(f"\nThis is what the TAs will see when evaluating your submission.")
        print(f"If the above looks correct, your submission is ready.")

    if args.output_json:
        import json
        output = {
            "perplexity": results["perplexity"],
            "avg_loss_nats": results["avg_loss_nats"],
            "total_tokens_evaluated": results["total_tokens_evaluated"],
            "total_params": total_params,
            "eval_time_sec": round(elapsed, 2),
        }
        with open(args.output_json, "w") as f:
            json.dump(output, f, indent=2)
        print(f"\nResults written to {args.output_json}")


if __name__ == "__main__":
    main()