text stringlengths 0 883 |
|---|
""" |
Reference code for GPT-2 training and inference with Sharpness Analysis. |
Will save the model weights into files, to be read from C as initialization. |
References: |
1) the official GPT-2 TensorFlow implementation released by OpenAI: |
https://github.com/openai/gpt-2/blob/master/src/model.py |
2) huggingface/transformers PyTorch implementation: |
https://github.com/huggingface/transformers/blob/main/src/transformers/models/gpt2/modeling_gpt2.py |
Example launches to only benchmark the speed of bfloat16 compiled GPU training: |
1 GPU: |
python train_gpt2.py --write_tensors=0 --num_iterations=50 --sequence_length=1024 --compile=1 --tensorcores=1 --dtype=bfloat16 |
you can also turn on flash-attention by appending --flash=1 |
4 GPU: |
torchrun --standalone --nproc_per_node=4 train_gpt2.py --write_tensors=0 --num_iterations=50 --sequence_length=1024 --compile=1 --tensorcores=1 --dtype=bfloat16 |
""" |
import sys |
with open(sys.argv[0]) as f: |
code = f.read() # read the code of this file ASAP, for logging |
import os |
os.environ.setdefault("CUBLAS_WORKSPACE_CONFIG", ":4096:8") |
os.environ.setdefault("TORCHINDUCTOR_CACHE_DIR", os.path.expanduser("~/scratch/torchinductor_cache_determ")) |
os.environ.setdefault("TORCHINDUCTOR_FX_GRAPH_CACHE", "1") |
os.environ.setdefault("TORCHINDUCTOR_AUTOGRAD_CACHE", "1") |
os.environ.setdefault("TORCHINDUCTOR_COMPILE_THREADS", "1") |
os.environ.setdefault("MAX_JOBS", "1") |
import math |
import glob |
import struct |
import inspect |
import re |
from datetime import timedelta |
from contextlib import nullcontext |
from dataclasses import dataclass |
import random |
import numpy as np |
import torch |
# Determinism is ON by default (the whole point of this script: bit-exact runs so |
# the only diff between bf16-O / fp32-O / fp16-O is the D-path precision). Set |
# FA_DETERMINISTIC=0 to turn it OFF for speed experiments (lets cudnn.benchmark |
# autotune fast kernels). NOT bit-exact when off -- benchmark use only. |
if os.environ.get("FA_DETERMINISTIC", "1") == "1": |
torch.use_deterministic_algorithms(True) |
torch.backends.cudnn.benchmark = False |
torch.backends.cudnn.deterministic = True |
else: |
torch.backends.cudnn.benchmark = True |
torch.backends.cudnn.deterministic = False |
from torch import Tensor |
import torch.nn as nn |
from torch.nn import functional as F |
import torch._inductor.config as config |
from torch.nn.parallel import DistributedDataParallel as DDP |
from torch.distributed import init_process_group, destroy_process_group |
from torch.distributed.optim import ZeroRedundancyOptimizer |
import torch.distributed as dist |
from torch.amp import autocast |
import copy |
import gc |
import uuid |
import json |
from pathlib import Path |
try: |
import wandb |
except Exception: |
wandb = None |
REPO_ROOT = Path(__file__).resolve().parents[1] |
OPTIMIZER_DIR = REPO_ROOT / "optimizers" |
MODEL_DIR = REPO_ROOT / "models" |
for import_dir in (OPTIMIZER_DIR, MODEL_DIR): |
import_dir_str = str(import_dir) |
if import_dir_str not in sys.path: |
sys.path.insert(0, import_dir_str) |
from MUON_fix import DampedMuon, Muon, MuonDPSK, damped_zeropower_via_ns, zeropower_via_newtonschulz5, zeropower_via_newtonschulz5_dpsk |
from normuon import NorMuon, normuon_update |
import nano_GPT_qkvonorm_pure |
from nano_GPT_qkvonorm_pure import GPT, GPTConfig |
STANDARD_MUON_TYPES = (Muon, NorMuon) |
MUON_FAMILY_TYPES = (Muon, NorMuon, MuonDPSK, DampedMuon) |
def build_standard_muon_optimizer( |
params, |
implementation, |
lr, |
weight_decay, |
momentum, |
nesterov, |
ns_steps, |
rank, |
world_size, |
normuon_beta2, |
): |
End of preview. Expand in Data Studio
No dataset card yet
- Downloads last month
- 170