File size: 2,657 Bytes
91ac3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import json
import math
from pathlib import Path

import torch
from transformers import AutoTokenizer

from kawk50m.config import build_llama_config, load_yaml
from kawk50m.sft_dataset import SFTPackedDataset


def main() -> None:
    parser = argparse.ArgumentParser(description="Validate an SFT run before GPU use.")
    parser.add_argument("--config", required=True)
    parser.add_argument("--init-from", required=True)
    args = parser.parse_args()

    config = load_yaml(args.config)
    data = config["data"]
    training = config["training"]
    tokenizer = AutoTokenizer.from_pretrained(
        config["tokenizer_dir"], use_fast=False, legacy=True
    )
    model_config, expected_parameters = build_llama_config(
        load_yaml(config["model_config"]),
        tokenizer_vocab_size=len(tokenizer),
    )
    sequence_length = int(data["sequence_length"])
    train = SFTPackedDataset(data["train_prefixes"], sequence_length)
    validation = SFTPackedDataset(data["validation_prefixes"], sequence_length)
    batch_size = int(training["per_device_batch_size"])
    accumulation = int(training["gradient_accumulation_steps"])
    epochs = float(training["epochs"])
    sequences_per_step = batch_size * accumulation
    max_steps = math.ceil(len(train) * epochs / sequences_per_step)
    init_path = Path(args.init_from)
    errors: list[str] = []
    if not (init_path / "model" / "model.safetensors").is_file():
        errors.append("Initial checkpoint model.safetensors is missing")
    if sequence_length > model_config.max_position_embeddings:
        errors.append("SFT sequence length exceeds model context length")
    result = {
        "tokenizer_vocab_size": len(tokenizer),
        "expected_parameters": expected_parameters,
        "sequence_length": sequence_length,
        "train_sequences": len(train),
        "validation_sequences": len(validation),
        "epochs": epochs,
        "sequences_per_step": sequences_per_step,
        "max_steps": max_steps,
        "planned_capacity_tokens": max_steps * sequences_per_step * sequence_length,
        "init_from": str(init_path.resolve()),
        "gpu": {
            "available": torch.cuda.is_available(),
            "bf16_supported": (
                torch.cuda.is_bf16_supported() if torch.cuda.is_available() else False
            ),
            "name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else None,
        },
        "errors": errors,
    }
    print(json.dumps(result, ensure_ascii=False, indent=2))
    if errors:
        raise SystemExit(1)


if __name__ == "__main__":
    main()