Spaces:
Runtime error
Runtime error
Update train.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126# Copyright Pathway Technology, Inc.import osfrom contextlib import nullcontextimport bdhimport numpy as npimport requestsimport torchimport torch.nn as nnimport torch.nn.functional as Fdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")# On a Mac you can also try# device=torch.device('mps')dtype = ("bfloat16"if torch.cuda.is_available() and torch.cuda.is_bf16_supported()else "float16") # 'float32', 'bfloat16', or 'float16', the latter will auto implement a GradScalerptdtype = {"float32": torch.float32,"bfloat16": torch.bfloat16,"float16": torch.float16,}[dtype]ctx = (torch.amp.autocast(device_type=device.type, dtype=ptdtype)if "cuda" in device.typeelse nullcontext())scaler = torch.amp.GradScaler(device=device.type, enabled=(dtype == "float16"))torch.manual_seed(1337)torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmultorch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnnprint(f"Using device: {device} with dtype {dtype}")# ConfigurationBDH_CONFIG = bdh.BDHConfig()BLOCK_SIZE = 512BATCH_SIZE = 32MAX_ITERS = 3000LEARNING_RATE = 1e-3WEIGHT_DECAY = 0.1LOG_FREQ = 100input_file_path = os.path.join(os.path.dirname(__file__), "input.txt")# Fetch the tiny Shakespeare datasetdef fetch_data():if not os.path.exists(input_file_path):data_url = "https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt"with open(input_file_path, "w") as f:f.write(requests.get(data_url).text)def get_batch(split):# treat the file as bytesdata = np.memmap(input_file_path, dtype=np.uint8, mode="r")if split == "train":data = data[: int(0.9 * len(data))]else:data = data[int(0.9 * len(data)) :]ix = torch.randint(len(data) - BLOCK_SIZE, (BATCH_SIZE,))x = torch.stack([torch.from_numpy((data[i : i + BLOCK_SIZE]).astype(np.int64)) for i in ix])y = torch.stack([torch.from_numpy((data[i + 1 : i + 1 + BLOCK_SIZE]).astype(np.int64))for i in ix])if torch.cuda.is_available():# pin arrays x,y, which allows us to move them to GPU asynchronously (non_blocking=True)x, y = x.pin_memory().to(device, non_blocking=True), y.pin_memory().to(device, non_blocking=True)else:x, y = x.to(device), y.to(device)return x, ydef eval(model):model.eval()if __name__ == "__main__":fetch_data()model = bdh.BDH(BDH_CONFIG).to(device)model = torch.compile(model)optimizer = torch.optim.AdamW(model.parameters(), lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)x, y = get_batch("train")loss_acc = 0loss_steps = 0for step in range(MAX_ITERS):with ctx:logits, loss = model(x, y)x, y = get_batch("train")loss_acc += lossloss_steps += 1scaler.scale(loss).backward()scaler.step(optimizer)scaler.update()optimizer.zero_grad()if step % LOG_FREQ == 0:print(f"Step: {step}/{MAX_ITERS} loss {loss_acc.item() / loss_steps:.3}")loss_acc = 0loss_steps = 0print("Training done, now generating a sample ")model.eval()prompt = torch.tensor(bytearray("To be or ", "utf-8"), dtype=torch.long, device=device).unsqueeze(0)ret = model.generate(prompt, max_new_tokens=100, top_k=3)ret_decoded = bytes(ret.to(torch.uint8).to("cpu").squeeze(0)).decode(errors="backslashreplace")print(ret_decoded)
train.py+126