A straightforward method for training your LLM, from downloading data to generating text.
feat(scripts): --int8 for generate_text.py and chat.py
FareedKhan-dev143b5199d3eb
diff --git a/scripts/chat.py b/scripts/chat.pyindex 9a1f66a..67e3fb1 100644--- a/scripts/chat.py+++ b/scripts/chat.py@@ -8,6 +8,7 @@ One-shot:python scripts/chat.py --ckpt models/sft.pt --prompt "What is 13 + 29?"python scripts/chat.py --ckpt models/grpo.pt --prompt "..." --greedypython scripts/chat.py --ckpt models/base_pretrained.pt --raw --prompt "Once upon a time"+ python scripts/chat.py --ckpt models/sft.pt --int8 --prompt "..." # int8 linear weightsInteractive REPL (no --prompt):python scripts/chat.py --ckpt models/sft.pt"""@@ -21,8 +22,8 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1])) # run from the repimport argparse-import torch-+from src.device import resolve_device+from src.inference.quantize import quantize_int8from src.post_training.inference import generate_reply, load_model_from_ckpt@@ -37,11 +38,15 @@ def main():p.add_argument("--top_p", type=float, default=0.95)p.add_argument("--top_k", type=int, default=None)p.add_argument("--greedy", action="store_true", help="deterministic argmax decoding")- p.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")+ p.add_argument("--device", default="auto", help="auto (CUDA, then Apple MPS, then CPU), cuda, mps or cpu")+ p.add_argument("--int8", action="store_true", help="store the linear weights as int8 (about 4x smaller)")args = p.parse_args()+ args.device = resolve_device(args.device)model = load_model_from_ckpt(args.ckpt, args.device)n = sum(p.numel() for p in model.parameters())+ if args.int8:+ model = quantize_int8(model)print(f"loaded {args.ckpt} ({n/1e6:.0f}M params) on {args.device} | "f"mode={'raw' if args.raw else 'chat'} {'greedy' if args.greedy else f'T={args.temperature} top_p={args.top_p}'}")diff --git a/scripts/generate_text.py b/scripts/generate_text.pyindex e1ef9b3..2845ba0 100644--- a/scripts/generate_text.py+++ b/scripts/generate_text.py@@ -8,6 +8,7 @@ and the window it was trained on. So you only pass the checkpoint path:python scripts/generate_text.py --model_path models/tiny.ptpython scripts/generate_text.py --model_path models/student.pt --input_text "The little dog" \--max_new_tokens 200 --temperature 0.7 --top_k 40 --num_samples 3+ python scripts/generate_text.py --model_path models/tiny.pt --int8 # int8 linear weightsVery old checkpoints that stored only the weights fall back to the sizes in config/config.py."""@@ -30,6 +31,7 @@ from src.checkpoint import ( # noqa: E402model_state_from_checkpoint,)from src.device import resolve_device # noqa: E402+from src.inference.quantize import Int8Linear, quantize_int8 # noqa: E402from src.models.factory import build_model # noqa: E402from src.tokenizer import safe_decode, tokenizer_from_spec # noqa: E402@@ -83,13 +85,17 @@ def main() -> None:parser.add_argument("--num_samples", type=int, default=1, help="How many continuations to print.")parser.add_argument("--device", default="auto", help="auto, cuda, mps or cpu.")parser.add_argument("--seed", type=int, default=None, help="Random seed for reproducible samples.")+ parser.add_argument("--int8", action="store_true", help="Store the linear weights as int8 (about 4x smaller).")args = parser.parse_args()if args.seed is not None:torch.manual_seed(args.seed)device = resolve_device(args.device)model, tokenizer, window, model_cfg = load_trained_model(args.model_path, device)- n_params = sum(p.numel() for p in model.parameters())+ if args.int8:+ model = quantize_int8(model)+ n_params = sum(p.numel() for p in model.parameters()) + sum(+ m.weight_int8.numel() for m in model.modules() if isinstance(m, Int8Linear))print(f"Loaded {args.model_path}: {n_params:,} parameters, {model_cfg.get('arch', 'classic')} "f"architecture, window {window} tokens, on {device}")