Dataset Viewer
Auto-converted to Parquet Duplicate
text
stringlengths
0
162
"""
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
import math
import glob
import struct
import inspect
from contextlib import nullcontext
from dataclasses import dataclass
import random
import numpy as np
import torch
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
# Import Muon optimizer
import sys
sys.path.append("/home/aiops/zhangfz/MUON_LLM/modded-nanogpt/optimizers")
from MUON_fix import Muon
# Import GPT model
sys.path.append("/home/aiops/zhangfz/MUON_LLM/modded-nanogpt/models")
import nano_GPT_qkvonorm_pure
from nano_GPT_qkvonorm_pure import GPT, GPTConfig
# Import debug utilities
# from debug_utils import setup_debugpy
# -----------------------------------------------------------------------------
# Our own simple Distributed Data Loader
def _peek_data_shard(filename):
# only reads the header, returns header data
with open(filename, "rb") as f:
# first read the header, which is 256 int32 integers (4 bytes each)
header = np.frombuffer(f.read(256*4), dtype=np.int32)
if header[0] != 20240520:
print("ERROR: magic number mismatch in the data .bin file!")
print("---> HINT: Are you passing in a correct file with --input_bin?")
print("---> HINT: Dataset encoding changed recently, re-run data prepro or refer again to README")
print("---> HINT: For example re-run: `python dev/data/tinyshakespeare.py`, then re-try")
exit(1)
assert header[1] == 1, "unsupported version"
ntok = header[2] # number of tokens (claimed)
return ntok # for now just return the number of tokens
def _load_data_shard(filename):
with open(filename, "rb") as f:
# first read the header, which is 256 int32 integers (4 bytes each)
header = np.frombuffer(f.read(256*4), dtype=np.int32)
assert header[0] == 20240520, "magic number mismatch in the data .bin file"
assert header[1] == 1, "unsupported version"
ntok = header[2] # number of tokens (claimed)
# the rest of it are tokens, stored as uint16
tokens = np.frombuffer(f.read(), dtype=np.uint16)
assert len(tokens) == ntok, "number of tokens read does not match header?"
return tokens
class DistributedDataLoader:
def __init__(self, filename_pattern, B, T, process_rank, num_processes,
shuffle_files=False, random_seed=None):
self.process_rank = process_rank
self.num_processes = num_processes
self.B = B
self.T = T
self.shuffle_files = shuffle_files
self.random_seed = random_seed
self._rng = random.Random(random_seed) if shuffle_files and random_seed is not None else None
End of preview. Expand in Data Studio

No dataset card yet

Downloads last month
15