Download scripts/preflight_sft.py from Infinity08/KAWK50M-Project: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/datasets/Infinity08/KAWK50M-Project/resolve/main/scripts/preflight_sft.py
- Command line
-
hf download hf://datasets/Infinity08/KAWK50M-Project/scripts/preflight_sft.py
-
curl -L -o preflight_sft.py https://huggingface.co/datasets/Infinity08/KAWK50M-Project/resolve/main/scripts/preflight_sft.py
2.66 kB
| 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() | |