Spaces:
Running on Zero
Running on Zero
Download scripts/train_pi3.py from cyun9286/PURE-Studio: direct link, hf CLI and curl.
- Browser
- Download file 2.51 kB
-
https://huggingface.co/spaces/cyun9286/PURE-Studio/resolve/main/scripts/train_pi3.py
- Command line
-
hf download hf://spaces/cyun9286/PURE-Studio/scripts/train_pi3.py
-
curl -L -o train_pi3.py https://huggingface.co/spaces/cyun9286/PURE-Studio/resolve/main/scripts/train_pi3.py
2.51 kB
| """Train recovered Pi3 on validated precomputed teacher/match caches.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import sys | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | |
| import torch | |
| from pure.models.pi3 import Pi3 | |
| from pure.training.cache import ClipDataset | |
| from pure.training.checkpoint import normalize_model_config | |
| from pure.training.checkpoint import load_model | |
| from pure.training.engine import configure_trainable, fit | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--config", required=True) | |
| parser.add_argument("--data", required=True) | |
| parser.add_argument("--output", required=True) | |
| parser.add_argument("--device", default="cuda") | |
| weights = parser.add_mutually_exclusive_group(required=True) | |
| weights.add_argument( | |
| "--base-checkpoint", | |
| help="Original Pi3 weights: initializes a new convolutional head", | |
| ) | |
| weights.add_argument( | |
| "--checkpoint", help="Full checkpoint from this recovered architecture" | |
| ) | |
| weights.add_argument( | |
| "--resume", help="Resume optimizer, scheduler, RNG, and sample stream" | |
| ) | |
| args = parser.parse_args() | |
| config = normalize_model_config(json.loads(Path(args.config).read_text())) | |
| if config["stage"] == "posttrain" and args.base_checkpoint: | |
| parser.error( | |
| "posttrain requires a head-initialized checkpoint; first run head_init" | |
| ) | |
| torch.manual_seed(config["seed"]) | |
| model = Pi3() | |
| if not args.resume: | |
| report = load_model( | |
| model, | |
| args.base_checkpoint or args.checkpoint, | |
| base=bool(args.base_checkpoint), | |
| ) | |
| print( | |
| json.dumps({"loaded": args.base_checkpoint or args.checkpoint, **report}), | |
| flush=True, | |
| ) | |
| names = configure_trainable( | |
| model, | |
| config["stage"], | |
| train_head=config["train_head"], | |
| decoder_start=config["decoder_start"], | |
| decoder_end=config["decoder_end"], | |
| ) | |
| print( | |
| json.dumps( | |
| { | |
| "trainable_parameters": sum( | |
| p.numel() for p in model.parameters() if p.requires_grad | |
| ), | |
| "trainable_modules": sorted(set(name.split(".")[0] for name in names)), | |
| } | |
| ), | |
| flush=True, | |
| ) | |
| dataset = ClipDataset(args.data) | |
| fit(model, dataset, config, args.output, device=args.device, resume=args.resume) | |
| if __name__ == "__main__": | |
| main() | |