Upload code/train.py with huggingface_hub
Browse files12 unmodified lines13141516171819202122232425262728190 unmodified lines21922022122222322412 unmodified linesfrom huggingface_hub import HfApi, hf_hub_download, create_repo, loginfrom datetime import timedeltadef setup_ddp():dist.init_process_group(backend="nccl", timeout=timedelta(minutes=60))rank = dist.get_rank()local_rank = int(os.environ["LOCAL_RANK"])world_size = dist.get_world_size()torch.cuda.set_device(local_rank)return rank, local_rank, world_sizerank, local_rank, world_size = setup_ddp()DEVICE = f"cuda:{local_rank}"IS_MAIN = rank == 0def log(msg):190 unmodified linesdef save_checkpoint(model, optimizer, step, val_loss, filename, blocking=False, shard_cursor=None):if not IS_MAIN:returnstate_dict = extract_clean_state_dict(model)data = {"model": state_dict, "step": step, "val_loss": val_loss}if shard_cursor is not None:12 unmodified lines13141516171819202122232425262728293031323334353637383940190 unmodified lines23123223323423523623723823912 unmodified linesfrom huggingface_hub import HfApi, hf_hub_download, create_repo, loginfrom datetime import timedeltadef setup_distributed():if "RANK" in os.environ and "WORLD_SIZE" in os.environ:local_rank = int(os.environ.get("LOCAL_RANK", 0))torch.cuda.set_device(local_rank)try:dist.init_process_group(backend="nccl",device_id=torch.device(f"cuda:{local_rank}"),timeout=timedelta(minutes=60))except TypeError:dist.init_process_group(backend="nccl", timeout=timedelta(minutes=60))rank = dist.get_rank()world_size = dist.get_world_size()return rank, local_rank, world_size, Truelocal_rank = 0if torch.cuda.is_available():torch.cuda.set_device(local_rank)return 0, 0, 1, Falserank, local_rank, world_size, is_ddp = setup_distributed()DEVICE = f"cuda:{local_rank}" if torch.cuda.is_available() else "cpu"IS_MAIN = rank == 0def log(msg):190 unmodified linesdef save_checkpoint(model, optimizer, step, val_loss, filename, blocking=False, shard_cursor=None):if not IS_MAIN:return# Wait for any prior background upload thread to finish to prevent race condition# where an older step upload finishes later and overwrites ckpt_latest.pt!wait_for_uploads()state_dict = extract_clean_state_dict(model)data = {"model": state_dict, "step": step, "val_loss": val_loss}if shard_cursor is not None:
code/train.py+25-10