openagi-agi commited on
Commit
03bdd0f
·
verified ·
1 Parent(s): 4714480

Upload 13 files

Browse files
Files changed (14) hide show
  1. .gitattributes +1 -0
  2. README.md +23 -0
  3. banner.png +3 -0
  4. chat.py +99 -0
  5. ckpt.pt +3 -0
  6. ckpt_000000.pt +3 -0
  7. collect.py +40 -0
  8. configurator.py +47 -0
  9. meta.pkl +3 -0
  10. model.py +330 -0
  11. test.py +7 -0
  12. train.bin +3 -0
  13. train.py +377 -0
  14. val.bin +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ banner.png filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,3 +1,26 @@
1
  ---
2
  license: gpl-3.0
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: gpl-3.0
3
+ datasets:
4
+ - lmsys/lmsys-chat-1m
5
+ language:
6
+ - en
7
+ pipeline_tag: text-generation
8
  ---
9
+
10
+ <img src="ShaNet.png" alt="ShaNet Banner" width="100%">
11
+
12
+ # ShaNet
13
+
14
+ ShaNet is a Generative Pre-trained Transformer (GPT) trained on conversational data designed to understand and generate human-like text based on the input it receives. This model can be used for various applications such as chatbots, content generation, and more.
15
+
16
+ ## Features
17
+ - **Conversational Understanding**: Trained on a diverse dataset to understand context and nuances in conversations.
18
+ - **Text Generation**: Capable of generating coherent and contextually relevant text.
19
+ - **Customizable**: Can be fine-tuned for specific applications or domains.
20
+ - **Open Source**: Available for use and modificatio under the permissive GPL-3.0 license.
21
+
22
+ ## Installation
23
+ To install ShaNet, you can downlaod all files and run the `chat.py` script.
24
+
25
+ Note:
26
+ - The train and config code are modified versions of train.py and configurator.py from [NanoGPT](https://github.com/karpathy/nanoGPT).
banner.png ADDED

Git LFS Details

  • SHA256: 30e72893f2b2bd0112c72a528fedd18a5a625cdb85434e687ac0d6901e846e80
  • Pointer size: 132 Bytes
  • Size of remote file: 1.98 MB
chat.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import os
3
+ import torch
4
+ import pickle
5
+ from model import GPTConfig, GPT
6
+ import tiktoken
7
+ from rich.traceback import install
8
+
9
+ install()
10
+
11
+ # ----- CONFIG -----
12
+ ckpt_path = 'out/ckpt.pt'
13
+ meta_path = 'data/mydata/meta.pkl'
14
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
15
+ tokenizer_name = 'o200k_base'
16
+ max_new_tokens = 1024
17
+ temperature = 0.8
18
+ top_k = 100
19
+ special_tokens = {"<|endoftext|>", "<|im_start|>", "<|im_end|>", "<|user|>", "<|assistant|>"}
20
+
21
+ # ----- LOAD TOKENIZER -----
22
+ enc = tiktoken.get_encoding(tokenizer_name)
23
+ encode = enc.encode
24
+ decode = enc.decode
25
+
26
+ # ----- LOAD METADATA -----
27
+ with open(meta_path, 'rb') as f:
28
+ meta = pickle.load(f)
29
+ vocab_size = meta['vocab_size']
30
+
31
+ # ----- LOAD CHECKPOINT -----
32
+ checkpoint = torch.load(ckpt_path, map_location=device)
33
+ model_args = checkpoint['model_args']
34
+ model_args['vocab_size'] = vocab_size
35
+ block_size = model_args.get('block_size', 1024)
36
+
37
+ # ----- INITIALIZE MODEL -----
38
+ model = GPT(GPTConfig(**model_args))
39
+ model.load_state_dict(checkpoint['model'])
40
+ model.to(device)
41
+ model.eval()
42
+
43
+ @torch.no_grad()
44
+ def generate_stream(model, input_ids, max_new_tokens, temperature=1.0, top_k=None):
45
+ model.eval()
46
+ special_token_id = encode("<|endoftext|>", allowed_special=special_tokens)[0]
47
+
48
+ for _ in range(max_new_tokens):
49
+ if input_ids.size(1) > block_size:
50
+ input_ids = input_ids[:, -block_size:]
51
+
52
+ logits, _ = model(input_ids)
53
+ logits = logits[:, -1, :] / temperature
54
+
55
+ if top_k is not None:
56
+ v, _ = torch.topk(logits, top_k)
57
+ logits[logits < v[:, [-1]]] = -float('Inf')
58
+
59
+ probs = torch.nn.functional.softmax(logits, dim=-1)
60
+ next_token = torch.multinomial(probs, num_samples=1)
61
+ next_token_id = next_token.item()
62
+
63
+ input_ids = torch.cat((input_ids, next_token), dim=1)
64
+
65
+ decoded_token = decode([next_token_id])
66
+ print(decoded_token, end='', flush=True) if decoded_token not in special_tokens else None
67
+
68
+ if next_token_id == special_token_id:
69
+ break
70
+
71
+ print() # Ensure newline after generation
72
+ return input_ids
73
+
74
+ def main():
75
+ print("🤖 AI Assistant is ready. Type 'exit' or press Ctrl+C to quit.\n")
76
+ try:
77
+ while True:
78
+ user_input = input("You: ")
79
+ if user_input.lower() in {"exit", "quit"}:
80
+ print("👋 Exiting assistant.")
81
+ break
82
+
83
+ prompt = f"""
84
+ <|user|>
85
+ {user_input}
86
+ <|assistant|>
87
+
88
+ """
89
+ input_ids = torch.tensor(encode(prompt, allowed_special=special_tokens), dtype=torch.long, device=device)[None, ...]
90
+
91
+ print("🤖 Assistant:", end=' ', flush=True)
92
+ generate_stream(model, input_ids, max_new_tokens, temperature, top_k)
93
+ print("-" * 50)
94
+
95
+ except KeyboardInterrupt:
96
+ print("\n👋 Exiting assistant.")
97
+
98
+ if __name__ == "__main__":
99
+ main()
ckpt.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d055faee79090cf2fb9709e23b07598a4be78c851696eb2ff72c7c7ffd5a265b
3
+ size 2787314743
ckpt_000000.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
3
+ size 0
collect.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Download, transform LMSYS-Chat-1M into plain text for LLM completion models
4
+ in the format:
5
+ <|im_start|>role
6
+ message<|endoftext|>
7
+ <|im_stop|>
8
+
9
+ with 6 newlines between conversations.
10
+ """
11
+
12
+ from datasets import load_dataset
13
+ import sys
14
+
15
+ def main(output_path="lmsys_chat_1m.txt", split="train"):
16
+ ds = load_dataset("lmsys/lmsys-chat-1m", split=split)
17
+
18
+ with open(output_path, "w", encoding="utf-8") as out:
19
+ for i, sample in enumerate(ds):
20
+ conv = sample["conversation"] # list of messages
21
+
22
+ for msg in conv:
23
+ role = msg["role"]
24
+ content = msg["content"].strip()
25
+ out.write(f"<|im_start|>{role}\n{content}<|endoftext|>\n<|im_stop|>\n")
26
+
27
+ out.write("\n" * 6) # 6 newlines between conversations
28
+
29
+ if (i + 1) % 10000 == 0:
30
+ print(f"Processed {i + 1} conversations", file=sys.stderr)
31
+
32
+ print(f"✔ Saved plain-text to: {output_path}")
33
+
34
+ if __name__ == "__main__":
35
+ import argparse
36
+ p = argparse.ArgumentParser(description="Convert LMSYS-Chat-1M to LLM-friendly text format")
37
+ p.add_argument("--output", "-o", default="lmsys_chat_1m.txt", help="Output file path")
38
+ p.add_argument("--split", "-s", default="train", help="Dataset split (e.g. 'train')")
39
+ args = p.parse_args()
40
+ main(output_path=args.output, split=args.split)
configurator.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Poor Man's Configurator. Probably a terrible idea. Example usage:
3
+ $ python train.py config/override_file.py --batch_size=32
4
+ this will first run config/override_file.py, then override batch_size to 32
5
+
6
+ The code in this file will be run as follows from e.g. train.py:
7
+ >>> exec(open('configurator.py').read())
8
+
9
+ So it's not a Python module, it's just shuttling this code away from train.py
10
+ The code in this script then overrides the globals()
11
+
12
+ I know people are not going to love this, I just really dislike configuration
13
+ complexity and having to prepend config. to every single variable. If someone
14
+ comes up with a better simple Python solution I am all ears.
15
+ """
16
+
17
+ import sys
18
+ from ast import literal_eval
19
+
20
+ for arg in sys.argv[1:]:
21
+ if '=' not in arg:
22
+ # assume it's the name of a config file
23
+ assert not arg.startswith('--')
24
+ config_file = arg
25
+ print(f"Overriding config with {config_file}:")
26
+ with open(config_file) as f:
27
+ print(f.read())
28
+ exec(open(config_file).read())
29
+ else:
30
+ # assume it's a --key=value argument
31
+ assert arg.startswith('--')
32
+ key, val = arg.split('=')
33
+ key = key[2:]
34
+ if key in globals():
35
+ try:
36
+ # attempt to eval it it (e.g. if bool, number, or etc)
37
+ attempt = literal_eval(val)
38
+ except (SyntaxError, ValueError):
39
+ # if that goes wrong, just use the string
40
+ attempt = val
41
+ # ensure the types match ok
42
+ assert type(attempt) == type(globals()[key])
43
+ # cross fingers
44
+ print(f"Overriding: {key} = {attempt}")
45
+ globals()[key] = attempt
46
+ else:
47
+ raise ValueError(f"Unknown config key: {key}")
meta.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d54fb1091f16a3f10c2e3c39ad6138ce3c61fa7c59871982c413c20dbf9a1ee4
3
+ size 205
model.py ADDED
@@ -0,0 +1,330 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Full definition of a GPT Language Model, all of it in this single file.
3
+ References:
4
+ 1) the official GPT-2 TensorFlow implementation released by OpenAI:
5
+ https://github.com/openai/gpt-2/blob/master/src/model.py
6
+ 2) huggingface/transformers PyTorch implementation:
7
+ https://github.com/huggingface/transformers/blob/main/src/transformers/models/gpt2/modeling_gpt2.py
8
+ """
9
+
10
+ import math
11
+ import inspect
12
+ from dataclasses import dataclass
13
+
14
+ import torch
15
+ import torch.nn as nn
16
+ from torch.nn import functional as F
17
+
18
+ class LayerNorm(nn.Module):
19
+ """ LayerNorm but with an optional bias. PyTorch doesn't support simply bias=False """
20
+
21
+ def __init__(self, ndim, bias):
22
+ super().__init__()
23
+ self.weight = nn.Parameter(torch.ones(ndim))
24
+ self.bias = nn.Parameter(torch.zeros(ndim)) if bias else None
25
+
26
+ def forward(self, input):
27
+ return F.layer_norm(input, self.weight.shape, self.weight, self.bias, 1e-5)
28
+
29
+ class CausalSelfAttention(nn.Module):
30
+
31
+ def __init__(self, config):
32
+ super().__init__()
33
+ assert config.n_embd % config.n_head == 0
34
+ # key, query, value projections for all heads, but in a batch
35
+ self.c_attn = nn.Linear(config.n_embd, 3 * config.n_embd, bias=config.bias)
36
+ # output projection
37
+ self.c_proj = nn.Linear(config.n_embd, config.n_embd, bias=config.bias)
38
+ # regularization
39
+ self.attn_dropout = nn.Dropout(config.dropout)
40
+ self.resid_dropout = nn.Dropout(config.dropout)
41
+ self.n_head = config.n_head
42
+ self.n_embd = config.n_embd
43
+ self.dropout = config.dropout
44
+ # flash attention make GPU go brrrrr but support is only in PyTorch >= 2.0
45
+ self.flash = hasattr(torch.nn.functional, 'scaled_dot_product_attention')
46
+ if not self.flash:
47
+ print("WARNING: using slow attention. Flash Attention requires PyTorch >= 2.0")
48
+ # causal mask to ensure that attention is only applied to the left in the input sequence
49
+ self.register_buffer("bias", torch.tril(torch.ones(config.block_size, config.block_size))
50
+ .view(1, 1, config.block_size, config.block_size))
51
+
52
+ def forward(self, x):
53
+ B, T, C = x.size() # batch size, sequence length, embedding dimensionality (n_embd)
54
+
55
+ # calculate query, key, values for all heads in batch and move head forward to be the batch dim
56
+ q, k, v = self.c_attn(x).split(self.n_embd, dim=2)
57
+ k = k.view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
58
+ q = q.view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
59
+ v = v.view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
60
+
61
+ # causal self-attention; Self-attend: (B, nh, T, hs) x (B, nh, hs, T) -> (B, nh, T, T)
62
+ if self.flash:
63
+ # efficient attention using Flash Attention CUDA kernels
64
+ y = torch.nn.functional.scaled_dot_product_attention(q, k, v, attn_mask=None, dropout_p=self.dropout if self.training else 0, is_causal=True)
65
+ else:
66
+ # manual implementation of attention
67
+ att = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(k.size(-1)))
68
+ att = att.masked_fill(self.bias[:,:,:T,:T] == 0, float('-inf'))
69
+ att = F.softmax(att, dim=-1)
70
+ att = self.attn_dropout(att)
71
+ y = att @ v # (B, nh, T, T) x (B, nh, T, hs) -> (B, nh, T, hs)
72
+ y = y.transpose(1, 2).contiguous().view(B, T, C) # re-assemble all head outputs side by side
73
+
74
+ # output projection
75
+ y = self.resid_dropout(self.c_proj(y))
76
+ return y
77
+
78
+ class MLP(nn.Module):
79
+
80
+ def __init__(self, config):
81
+ super().__init__()
82
+ self.c_fc = nn.Linear(config.n_embd, 4 * config.n_embd, bias=config.bias)
83
+ self.gelu = nn.GELU()
84
+ self.c_proj = nn.Linear(4 * config.n_embd, config.n_embd, bias=config.bias)
85
+ self.dropout = nn.Dropout(config.dropout)
86
+
87
+ def forward(self, x):
88
+ x = self.c_fc(x)
89
+ x = self.gelu(x)
90
+ x = self.c_proj(x)
91
+ x = self.dropout(x)
92
+ return x
93
+
94
+ class Block(nn.Module):
95
+
96
+ def __init__(self, config):
97
+ super().__init__()
98
+ self.ln_1 = LayerNorm(config.n_embd, bias=config.bias)
99
+ self.attn = CausalSelfAttention(config)
100
+ self.ln_2 = LayerNorm(config.n_embd, bias=config.bias)
101
+ self.mlp = MLP(config)
102
+
103
+ def forward(self, x):
104
+ x = x + self.attn(self.ln_1(x))
105
+ x = x + self.mlp(self.ln_2(x))
106
+ return x
107
+
108
+ @dataclass
109
+ class GPTConfig:
110
+ block_size: int = 1024
111
+ vocab_size: int = 50304 # GPT-2 vocab_size of 50257, padded up to nearest multiple of 64 for efficiency
112
+ n_layer: int = 12
113
+ n_head: int = 12
114
+ n_embd: int = 768
115
+ dropout: float = 0.0
116
+ bias: bool = True # True: bias in Linears and LayerNorms, like GPT-2. False: a bit better and faster
117
+
118
+ class GPT(nn.Module):
119
+
120
+ def __init__(self, config):
121
+ super().__init__()
122
+ assert config.vocab_size is not None
123
+ assert config.block_size is not None
124
+ self.config = config
125
+
126
+ self.transformer = nn.ModuleDict(dict(
127
+ wte = nn.Embedding(config.vocab_size, config.n_embd),
128
+ wpe = nn.Embedding(config.block_size, config.n_embd),
129
+ drop = nn.Dropout(config.dropout),
130
+ h = nn.ModuleList([Block(config) for _ in range(config.n_layer)]),
131
+ ln_f = LayerNorm(config.n_embd, bias=config.bias),
132
+ ))
133
+ self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
134
+ # with weight tying when using torch.compile() some warnings get generated:
135
+ # "UserWarning: functional_call was passed multiple values for tied weights.
136
+ # This behavior is deprecated and will be an error in future versions"
137
+ # not 100% sure what this is, so far seems to be harmless. TODO investigate
138
+ self.transformer.wte.weight = self.lm_head.weight # https://paperswithcode.com/method/weight-tying
139
+
140
+ # init all weights
141
+ self.apply(self._init_weights)
142
+ # apply special scaled init to the residual projections, per GPT-2 paper
143
+ for pn, p in self.named_parameters():
144
+ if pn.endswith('c_proj.weight'):
145
+ torch.nn.init.normal_(p, mean=0.0, std=0.02/math.sqrt(2 * config.n_layer))
146
+
147
+ # report number of parameters
148
+ print("number of parameters: %.2fM" % (self.get_num_params()/1e6,))
149
+
150
+ def get_num_params(self, non_embedding=True):
151
+ """
152
+ Return the number of parameters in the model.
153
+ For non-embedding count (default), the position embeddings get subtracted.
154
+ The token embeddings would too, except due to the parameter sharing these
155
+ params are actually used as weights in the final layer, so we include them.
156
+ """
157
+ n_params = sum(p.numel() for p in self.parameters())
158
+ if non_embedding:
159
+ n_params -= self.transformer.wpe.weight.numel()
160
+ return n_params
161
+
162
+ def _init_weights(self, module):
163
+ if isinstance(module, nn.Linear):
164
+ torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
165
+ if module.bias is not None:
166
+ torch.nn.init.zeros_(module.bias)
167
+ elif isinstance(module, nn.Embedding):
168
+ torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
169
+
170
+ def forward(self, idx, targets=None):
171
+ device = idx.device
172
+ b, t = idx.size()
173
+ assert t <= self.config.block_size, f"Cannot forward sequence of length {t}, block size is only {self.config.block_size}"
174
+ pos = torch.arange(0, t, dtype=torch.long, device=device) # shape (t)
175
+
176
+ # forward the GPT model itself
177
+ tok_emb = self.transformer.wte(idx) # token embeddings of shape (b, t, n_embd)
178
+ pos_emb = self.transformer.wpe(pos) # position embeddings of shape (t, n_embd)
179
+ x = self.transformer.drop(tok_emb + pos_emb)
180
+ for block in self.transformer.h:
181
+ x = block(x)
182
+ x = self.transformer.ln_f(x)
183
+
184
+ if targets is not None:
185
+ # if we are given some desired targets also calculate the loss
186
+ logits = self.lm_head(x)
187
+ loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1)
188
+ else:
189
+ # inference-time mini-optimization: only forward the lm_head on the very last position
190
+ logits = self.lm_head(x[:, [-1], :]) # note: using list [-1] to preserve the time dim
191
+ loss = None
192
+
193
+ return logits, loss
194
+
195
+ def crop_block_size(self, block_size):
196
+ # model surgery to decrease the block size if necessary
197
+ # e.g. we may load the GPT2 pretrained model checkpoint (block size 1024)
198
+ # but want to use a smaller block size for some smaller, simpler model
199
+ assert block_size <= self.config.block_size
200
+ self.config.block_size = block_size
201
+ self.transformer.wpe.weight = nn.Parameter(self.transformer.wpe.weight[:block_size])
202
+ for block in self.transformer.h:
203
+ if hasattr(block.attn, 'bias'):
204
+ block.attn.bias = block.attn.bias[:,:,:block_size,:block_size]
205
+
206
+ @classmethod
207
+ def from_pretrained(cls, model_type, override_args=None):
208
+ assert model_type in {'gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl'}
209
+ override_args = override_args or {} # default to empty dict
210
+ # only dropout can be overridden see more notes below
211
+ assert all(k == 'dropout' for k in override_args)
212
+ from transformers import GPT2LMHeadModel
213
+ print("loading weights from pretrained gpt: %s" % model_type)
214
+
215
+ # n_layer, n_head and n_embd are determined from model_type
216
+ config_args = {
217
+ 'gpt2': dict(n_layer=12, n_head=12, n_embd=768), # 124M params
218
+ 'gpt2-medium': dict(n_layer=24, n_head=16, n_embd=1024), # 350M params
219
+ 'gpt2-large': dict(n_layer=36, n_head=20, n_embd=1280), # 774M params
220
+ 'gpt2-xl': dict(n_layer=48, n_head=25, n_embd=1600), # 1558M params
221
+ }[model_type]
222
+ print("forcing vocab_size=50257, block_size=1024, bias=True")
223
+ config_args['vocab_size'] = 50257 # always 50257 for GPT model checkpoints
224
+ config_args['block_size'] = 1024 # always 1024 for GPT model checkpoints
225
+ config_args['bias'] = True # always True for GPT model checkpoints
226
+ # we can override the dropout rate, if desired
227
+ if 'dropout' in override_args:
228
+ print(f"overriding dropout rate to {override_args['dropout']}")
229
+ config_args['dropout'] = override_args['dropout']
230
+ # create a from-scratch initialized minGPT model
231
+ config = GPTConfig(**config_args)
232
+ model = GPT(config)
233
+ sd = model.state_dict()
234
+ sd_keys = sd.keys()
235
+ sd_keys = [k for k in sd_keys if not k.endswith('.attn.bias')] # discard this mask / buffer, not a param
236
+
237
+ # init a huggingface/transformers model
238
+ model_hf = GPT2LMHeadModel.from_pretrained(model_type)
239
+ sd_hf = model_hf.state_dict()
240
+
241
+ # copy while ensuring all of the parameters are aligned and match in names and shapes
242
+ sd_keys_hf = sd_hf.keys()
243
+ sd_keys_hf = [k for k in sd_keys_hf if not k.endswith('.attn.masked_bias')] # ignore these, just a buffer
244
+ sd_keys_hf = [k for k in sd_keys_hf if not k.endswith('.attn.bias')] # same, just the mask (buffer)
245
+ transposed = ['attn.c_attn.weight', 'attn.c_proj.weight', 'mlp.c_fc.weight', 'mlp.c_proj.weight']
246
+ # basically the openai checkpoints use a "Conv1D" module, but we only want to use a vanilla Linear
247
+ # this means that we have to transpose these weights when we import them
248
+ assert len(sd_keys_hf) == len(sd_keys), f"mismatched keys: {len(sd_keys_hf)} != {len(sd_keys)}"
249
+ for k in sd_keys_hf:
250
+ if any(k.endswith(w) for w in transposed):
251
+ # special treatment for the Conv1D weights we need to transpose
252
+ assert sd_hf[k].shape[::-1] == sd[k].shape
253
+ with torch.no_grad():
254
+ sd[k].copy_(sd_hf[k].t())
255
+ else:
256
+ # vanilla copy over the other parameters
257
+ assert sd_hf[k].shape == sd[k].shape
258
+ with torch.no_grad():
259
+ sd[k].copy_(sd_hf[k])
260
+
261
+ return model
262
+
263
+ def configure_optimizers(self, weight_decay, learning_rate, betas, device_type):
264
+ # start with all of the candidate parameters
265
+ param_dict = {pn: p for pn, p in self.named_parameters()}
266
+ # filter out those that do not require grad
267
+ param_dict = {pn: p for pn, p in param_dict.items() if p.requires_grad}
268
+ # create optim groups. Any parameters that is 2D will be weight decayed, otherwise no.
269
+ # i.e. all weight tensors in matmuls + embeddings decay, all biases and layernorms don't.
270
+ decay_params = [p for n, p in param_dict.items() if p.dim() >= 2]
271
+ nodecay_params = [p for n, p in param_dict.items() if p.dim() < 2]
272
+ optim_groups = [
273
+ {'params': decay_params, 'weight_decay': weight_decay},
274
+ {'params': nodecay_params, 'weight_decay': 0.0}
275
+ ]
276
+ num_decay_params = sum(p.numel() for p in decay_params)
277
+ num_nodecay_params = sum(p.numel() for p in nodecay_params)
278
+ print(f"num decayed parameter tensors: {len(decay_params)}, with {num_decay_params:,} parameters")
279
+ print(f"num non-decayed parameter tensors: {len(nodecay_params)}, with {num_nodecay_params:,} parameters")
280
+ # Create AdamW optimizer and use the fused version if it is available
281
+ fused_available = 'fused' in inspect.signature(torch.optim.AdamW).parameters
282
+ use_fused = fused_available and device_type == 'cuda'
283
+ extra_args = dict(fused=True) if use_fused else dict()
284
+ optimizer = torch.optim.AdamW(optim_groups, lr=learning_rate, betas=betas, **extra_args)
285
+ print(f"using fused AdamW: {use_fused}")
286
+
287
+ return optimizer
288
+
289
+ def estimate_mfu(self, fwdbwd_per_iter, dt):
290
+ """ estimate model flops utilization (MFU) in units of A100 bfloat16 peak FLOPS """
291
+ # first estimate the number of flops we do per iteration.
292
+ # see PaLM paper Appendix B as ref: https://arxiv.org/abs/2204.02311
293
+ N = self.get_num_params()
294
+ cfg = self.config
295
+ L, H, Q, T = cfg.n_layer, cfg.n_head, cfg.n_embd//cfg.n_head, cfg.block_size
296
+ flops_per_token = 6*N + 12*L*H*Q*T
297
+ flops_per_fwdbwd = flops_per_token * T
298
+ flops_per_iter = flops_per_fwdbwd * fwdbwd_per_iter
299
+ # express our flops throughput as ratio of A100 bfloat16 peak flops
300
+ flops_achieved = flops_per_iter * (1.0/dt) # per second
301
+ flops_promised = 312e12 # A100 GPU bfloat16 peak flops is 312 TFLOPS
302
+ mfu = flops_achieved / flops_promised
303
+ return mfu
304
+
305
+ @torch.no_grad()
306
+ def generate(self, idx, max_new_tokens, temperature=1.0, top_k=None):
307
+ """
308
+ Take a conditioning sequence of indices idx (LongTensor of shape (b,t)) and complete
309
+ the sequence max_new_tokens times, feeding the predictions back into the model each time.
310
+ Most likely you'll want to make sure to be in model.eval() mode of operation for this.
311
+ """
312
+ for _ in range(max_new_tokens):
313
+ # if the sequence context is growing too long we must crop it at block_size
314
+ idx_cond = idx if idx.size(1) <= self.config.block_size else idx[:, -self.config.block_size:]
315
+ # forward the model to get the logits for the index in the sequence
316
+ logits, _ = self(idx_cond)
317
+ # pluck the logits at the final step and scale by desired temperature
318
+ logits = logits[:, -1, :] / temperature
319
+ # optionally crop the logits to only the top k options
320
+ if top_k is not None:
321
+ v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
322
+ logits[logits < v[:, [-1]]] = -float('Inf')
323
+ # apply softmax to convert logits to (normalized) probabilities
324
+ probs = F.softmax(logits, dim=-1)
325
+ # sample from the distribution
326
+ idx_next = torch.multinomial(probs, num_samples=1)
327
+ # append sampled index to the running sequence and continue
328
+ idx = torch.cat((idx, idx_next), dim=1)
329
+
330
+ return idx
test.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ try:
4
+ ckpt = torch.load("out/ckpt.pt", map_location="cpu")
5
+ print("✅ Checkpoint has been loaded successfully")
6
+ except Exception as e:
7
+ print("❌ Failed to load the checkpoint:", e)
train.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a3068e542d16822e95ea1a80651e73fa062ed3e0995186dbea359d2db11efd94
3
+ size 1052529120
train.py ADDED
@@ -0,0 +1,377 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import os
3
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
4
+ import time
5
+ import math
6
+ import pickle
7
+ from contextlib import nullcontext
8
+ # note from ag: you may need to manually change the name of the trained model to match the name expected in the test.py chat.py and other scripts, also really impressive work here.
9
+ import numpy as np
10
+ import torch
11
+ torch.set_float32_matmul_precision("medium")
12
+ from torch.nn.parallel import DistributedDataParallel as DDP
13
+ from torch.distributed import init_process_group, destroy_process_group
14
+ try:
15
+ _ = torch.empty((4_000_000_000,), dtype=torch.float32) # ~4 GB
16
+ print("Memory alloc OK")
17
+ except Exception as e:
18
+ print(f"OOM triggered early: {e}")
19
+
20
+ import tiktoken
21
+ from rich.traceback import install
22
+ install()
23
+ from model import GPTConfig, GPT
24
+
25
+ # -------------------------------------------------------------------------------
26
+ # SPECIAL TOKENS for tokenizer (edit here as needed)
27
+ SPECIAL_TOKENS = {'<|im_start|>', '<|im_end|>', '<|system|>', '<|user|>', '<|assistant|>', "<|im_start|>", "<|endoftext|>", "<|endofprompt|>"}
28
+ print(f"ℹ️ Using special tokens: {SPECIAL_TOKENS}")
29
+
30
+ # -------------------------------------------------------------------------------
31
+ # DEFAULT CONFIG — override via CLI or `configurator.py`
32
+ out_dir = 'out'
33
+ eval_interval = 95
34
+ log_interval = 1
35
+ eval_iters = 5
36
+ eval_only = False # if True, exit after first eval
37
+ always_save_checkpoint = True # forces save every eval
38
+
39
+ init_from = 'resume' # 'scratch' | 'resume' | 'gpt2*'
40
+
41
+ wandb_log = False
42
+ wandb_project = 'shanet-2'
43
+ wandb_run_name= 'run' + str(time.time())
44
+ cmpl_backend = 'eager'
45
+
46
+ # Data / Tokenization
47
+ dataset = 'mydata' # subfolder under data/
48
+ data_file = '../data.txt'
49
+ tokenizer_name = 'o200k_base'
50
+ token_dtype = 'uint32' # must hold up to tokenizer.n_vocab
51
+
52
+ # Model architecture
53
+ n_layer = 2 # reduced to 3 layers
54
+ n_head = 12 # keep heads high for representation capacity
55
+ n_embd = 1032 # increased from 1280 → 1024 for stability and efficiency
56
+ dropout = 0.05 # lower dropout since underfitting may occur
57
+ bias = True
58
+
59
+ # Optimizer
60
+ learning_rate = 3e-4
61
+ max_iters = 50000
62
+ weight_decay = 0.05 # use 0.1 if batch size is large
63
+ beta1 = 0.9
64
+ beta2 = 0.98
65
+ grad_clip = 1.0
66
+
67
+ # LR schedule
68
+ decay_lr = True
69
+ warmup_iters = 100 # faster warmup for shallow models
70
+ lr_decay_iters = 10000 # align with max_iters for sharper decay
71
+ min_lr = 1e-5
72
+
73
+ # Batch & block sizes
74
+ batch_size = 16 # increase batch size if GPU RAM allows
75
+ gradient_accumulation_steps = 8 # adjust accordingly to match effective batch size
76
+ block_size = 256 # keep same for compatibility
77
+
78
+
79
+ # DDP
80
+ backend = 'nccl'
81
+
82
+ # Precision / compilation
83
+ device = 'cuda'
84
+ dtype = 'bfloat16' if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else 'float16'
85
+ compile = True
86
+
87
+ # Checkpointing
88
+ save_interval = 1000 # also save every N steps
89
+ checkpoint_limit = None # keep only last N checkpoints (None == keep all)
90
+ # -------------------------------------------------------------------------------
91
+
92
+ # allow overrides via CLI / configurator.py
93
+ config_keys = [k for k,v in globals().items()
94
+ if not k.startswith('_') and isinstance(v, (int,float,bool,str,list))]
95
+ exec(open('configurator.py').read()) # override from CLI or config
96
+ config = {k: globals()[k] for k in config_keys}
97
+
98
+ # -----------------------------------------------------------------------------
99
+ # AUTO-PREPROCESSING: data.txt → train.bin / val.bin + meta.pkl
100
+ data_dir = os.path.join('data', dataset)
101
+ train_bin_path = os.path.join(data_dir, 'train.bin')
102
+ val_bin_path = os.path.join(data_dir, 'val.bin')
103
+ meta_path = os.path.join(data_dir, 'meta.pkl')
104
+ dtype_token = np.dtype(token_dtype)
105
+
106
+ if not (os.path.exists(train_bin_path) and os.path.exists(val_bin_path) and os.path.exists(meta_path)):
107
+ print(f"ℹ️ Preprocessing raw text from {data_file} ...")
108
+ raw_text = open(data_file, 'r', encoding='utf-8').read()
109
+ enc = tiktoken.get_encoding(tokenizer_name)
110
+ encode = enc.encode
111
+ vocab_size= enc.n_vocab
112
+
113
+ # ensure dtype can hold vocab_size
114
+ if np.issubdtype(dtype_token, np.integer):
115
+ info = np.iinfo(dtype_token)
116
+ if info.max < vocab_size:
117
+ raise ValueError(f"token_dtype={token_dtype} max={info.max} < vocab_size={vocab_size}")
118
+
119
+ tokens = np.array(encode(raw_text, allowed_special=SPECIAL_TOKENS), dtype=dtype_token)
120
+ n = tokens.shape[0]
121
+ split = int(0.9 * n)
122
+ train_tokens = tokens[:split]
123
+ val_tokens = tokens[split:]
124
+
125
+ os.makedirs(data_dir, exist_ok=True)
126
+ train_tokens.tofile(train_bin_path)
127
+ val_tokens.tofile(val_bin_path)
128
+ with open(meta_path, 'wb') as f:
129
+ pickle.dump({
130
+ 'vocab_size': vocab_size,
131
+ 'tokenizer': tokenizer_name,
132
+ 'token_dtype': token_dtype,
133
+ 'special_tokens': SPECIAL_TOKENS,
134
+ }, f)
135
+ print(f"✅ Wrote {train_bin_path} ({train_tokens.nbytes} bytes), "
136
+ f"{val_bin_path} ({val_tokens.nbytes} bytes), and {meta_path}")
137
+
138
+ # -----------------------------------------------------------------------------
139
+ # DDP or single-GPU
140
+ ddp = int(os.environ.get('RANK', -1)) != -1
141
+ if ddp:
142
+ init_process_group(backend=backend)
143
+ ddp_rank = int(os.environ['RANK'])
144
+ ddp_local_rank = int(os.environ['LOCAL_RANK'])
145
+ ddp_world_size = int(os.environ['WORLD_SIZE'])
146
+ device = f'cuda:{ddp_local_rank}'
147
+ torch.cuda.set_device(device)
148
+ master_process = (ddp_rank == 0)
149
+ seed_offset = ddp_rank
150
+ assert gradient_accumulation_steps % ddp_world_size == 0
151
+ gradient_accumulation_steps //= ddp_world_size
152
+ else:
153
+ master_process = True
154
+ seed_offset = 0
155
+ ddp_world_size = 1
156
+
157
+ tokens_per_iter = gradient_accumulation_steps * ddp_world_size * batch_size * block_size
158
+ print(f"ℹ️ tokens per iteration = {tokens_per_iter:,}")
159
+
160
+ if master_process:
161
+ os.makedirs(out_dir, exist_ok=True)
162
+ torch.manual_seed(1337 + seed_offset)
163
+ torch.backends.cuda.matmul.allow_tf32 = True
164
+ torch.backends.cudnn.allow_tf32 = True
165
+ device_type = 'cuda' if 'cuda' in device else 'cpu'
166
+ ptdtype = {'float32':torch.float32, 'bfloat16':torch.bfloat16, 'float16':torch.float16}[dtype]
167
+ ctx = nullcontext() if device_type=='cpu' else torch.amp.autocast(device_type=device_type, dtype=ptdtype)
168
+
169
+ # -----------------------------------------------------------------------------
170
+ # BATCH LOADER
171
+ def get_batch(split):
172
+ data = np.memmap(os.path.join(data_dir, f'{split}.bin'),
173
+ dtype=dtype_token, mode='r')
174
+ ix = torch.randint(len(data) - block_size, (batch_size,))
175
+ x = torch.stack([torch.from_numpy(data[i:i+block_size].astype(np.int64)) for i in ix])
176
+ y = torch.stack([torch.from_numpy(data[i+1:i+1+block_size].astype(np.int64)) for i in ix])
177
+ if device_type == 'cuda':
178
+ x,y = x.pin_memory().to(device, non_blocking=True), y.pin_memory().to(device, non_blocking=True)
179
+ else:
180
+ x,y = x.to(device), y.to(device)
181
+ return x, y
182
+
183
+ # -----------------------------------------------------------------------------
184
+ # MODEL INIT / RESUME
185
+ iter_num = 0
186
+ best_val_loss = 1e9
187
+
188
+ meta = pickle.load(open(meta_path,'rb'))
189
+ vocab_size = meta['vocab_size']
190
+
191
+ model_args = dict(
192
+ n_layer = n_layer,
193
+ n_head = n_head,
194
+ n_embd = n_embd,
195
+ block_size = block_size,
196
+ bias = bias,
197
+ vocab_size = vocab_size,
198
+ dropout = dropout,
199
+ )
200
+
201
+ if init_from == 'scratch':
202
+ print("ℹ️ Initializing new model from scratch")
203
+ model = GPT(GPTConfig(**model_args))
204
+
205
+ elif init_from == 'resume':
206
+ print(f"ℹ️ Resuming from {out_dir}")
207
+ ckpt = torch.load(os.path.join(out_dir,'ckpt.pt'), map_location=device)
208
+ for k in ['n_layer','n_head','n_embd','block_size','bias','vocab_size']:
209
+ model_args[k] = ckpt['model_args'][k]
210
+ model = GPT(GPTConfig(**model_args))
211
+ state = ckpt['model']
212
+ for key in list(state.keys()):
213
+ if key.startswith('_orig_mod.'):
214
+ state[key[len('_orig_mod.'):]] = state.pop(key)
215
+ model.load_state_dict(state)
216
+ iter_num = ckpt['iter_num']
217
+ best_val_loss = ckpt['best_val_loss']
218
+
219
+ elif init_from.startswith('gpt2'):
220
+ print(f"ℹ️ Initializing from OpenAI GPT-2 weights: {init_from}")
221
+ override = dict(dropout=dropout)
222
+ model = GPT.from_pretrained(init_from, override)
223
+ for k in ['n_layer','n_head','n_embd','block_size','bias','vocab_size']:
224
+ model_args[k] = getattr(model.config, k)
225
+
226
+ if block_size < model.config.block_size:
227
+ model.crop_block_size(block_size)
228
+ model_args['block_size'] = block_size
229
+
230
+ model.to(device)
231
+ scaler = torch.cuda.amp.GradScaler(enabled=(dtype=='float16'))
232
+ optimizer = model.configure_optimizers(weight_decay, learning_rate, (beta1,beta2), device_type)
233
+ if init_from == 'resume':
234
+ optimizer.load_state_dict(ckpt['optimizer'])
235
+
236
+ # -----------------------------------------------------------------------------
237
+ # COMPILE & DDP WRAP
238
+ if compile:
239
+ print("ℹ️ Compiling the model...")
240
+ model = torch.compile(model, backend=cmpl_backend)
241
+ if ddp:
242
+ model = DDP(model, device_ids=[ddp_local_rank])
243
+
244
+ raw_model = model.module if ddp else model
245
+
246
+ # -----------------------------------------------------------------------------
247
+ # INITIAL CHECKPOINT at step 0
248
+ if master_process:
249
+ ckpt = {
250
+ 'model': raw_model.state_dict(),
251
+ 'optimizer': optimizer.state_dict(),
252
+ 'model_args': model_args,
253
+ 'iter_num': iter_num,
254
+ 'best_val_loss': best_val_loss,
255
+ 'config': config,
256
+ }
257
+ ckpt_path = os.path.join(out_dir, f'ckpt_{iter_num:06d}.pt')
258
+ print(f"💾 Saving initial checkpoint to {ckpt_path}")
259
+ torch.save(ckpt, ckpt_path)
260
+
261
+ # -----------------------------------------------------------------------------
262
+ # LOSS ESTIMATE
263
+ @torch.no_grad()
264
+ def estimate_loss():
265
+ out = {}
266
+ model.eval()
267
+ for split in ('train','val'):
268
+ losses = torch.zeros(eval_iters)
269
+ for k in range(eval_iters):
270
+ X,Y = get_batch(split)
271
+ with ctx:
272
+ _, loss = model(X,Y)
273
+ torch.cuda.empty_cache()
274
+ torch.cuda.synchronize()
275
+ losses[k] = loss.item()
276
+ out[split] = losses.mean().item()
277
+ model.train()
278
+ return out
279
+
280
+ def get_lr(it):
281
+ if it < warmup_iters:
282
+ return learning_rate * (it+1) / (warmup_iters+1)
283
+ if it > lr_decay_iters:
284
+ return min_lr
285
+ decay_ratio = (it - warmup_iters) / (lr_decay_iters - warmup_iters)
286
+ coeff = 0.5 * (1 + math.cos(math.pi * decay_ratio))
287
+ return min_lr + coeff * (learning_rate - min_lr)
288
+
289
+ if wandb_log and master_process:
290
+ import wandb
291
+ wandb.init(project=wandb_project, name=wandb_run_name, config=config)
292
+
293
+ # -----------------------------------------------------------------------------
294
+ # TRAINING LOOP
295
+ X, Y = get_batch('train')
296
+ t0 = time.time()
297
+ local_iter = 0
298
+ while True:
299
+ lr = get_lr(iter_num) if decay_lr else learning_rate
300
+ for pg in optimizer.param_groups:
301
+ pg['lr'] = lr
302
+
303
+ if iter_num % eval_interval == 0 and master_process:
304
+ losses = estimate_loss()
305
+ print(f"step {iter_num}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}")
306
+ if wandb_log:
307
+ wandb.log({"iter":iter_num, "train/loss":losses['train'], "val/loss":losses['val'], "lr":lr})
308
+
309
+ should_save = (
310
+ losses['val'] < best_val_loss
311
+ or always_save_checkpoint
312
+ or (iter_num % save_interval == 0)
313
+ )
314
+ if should_save and iter_num > 0:
315
+ best_val_loss = min(best_val_loss, losses['val'])
316
+ ckpt = {
317
+ 'model': raw_model.state_dict(),
318
+ 'optimizer': optimizer.state_dict(),
319
+ 'model_args': model_args,
320
+ 'iter_num': iter_num,
321
+ 'best_val_loss': best_val_loss,
322
+ 'config': config,
323
+ }
324
+ ckpt_path = os.path.join(out_dir, f'ckpt_{iter_num:06d}.pt')
325
+ print(f"💾 Saving checkpoint to {ckpt_path}")
326
+ torch.save(ckpt, ckpt_path)
327
+ if checkpoint_limit is not None:
328
+ all_ckpts = sorted(f for f in os.listdir(out_dir)
329
+ if f.startswith('ckpt_') and f.endswith('.pt'))
330
+ for old in all_ckpts[:-checkpoint_limit]:
331
+ os.remove(os.path.join(out_dir, old))
332
+
333
+ if iter_num == 0 and eval_only:
334
+ break
335
+
336
+ # Preload a full big batch once
337
+ X_full, Y_full = get_batch('train')
338
+ X_chunks = X_full.chunk(gradient_accumulation_steps, dim=0)
339
+ Y_chunks = Y_full.chunk(gradient_accumulation_steps, dim=0)
340
+
341
+ for micro in range(gradient_accumulation_steps):
342
+ X, Y = X_chunks[micro], Y_chunks[micro]
343
+ if ddp:
344
+ model.require_backward_grad_sync = (micro == gradient_accumulation_steps - 1)
345
+ with ctx:
346
+ logits, loss = model(X, Y)
347
+ loss = loss / gradient_accumulation_steps
348
+ scaler.scale(loss).backward()
349
+
350
+ torch.cuda.empty_cache()
351
+
352
+ if grad_clip != 0.0:
353
+ scaler.unscale_(optimizer)
354
+ torch.nn.utils.clip_grad_norm_(model.parameters(), grad_clip)
355
+ scaler.step(optimizer)
356
+ scaler.update()
357
+ optimizer.zero_grad(set_to_none=True)
358
+
359
+ dt = time.time() - t0
360
+ t0 = time.time()
361
+ if iter_num % log_interval == 0 and master_process:
362
+ lossf = loss.item() * gradient_accumulation_steps
363
+ if local_iter >= 5:
364
+ mfu = raw_model.estimate_mfu(batch_size * gradient_accumulation_steps, dt)
365
+ print(f"iter {iter_num}: loss {lossf:.4f}, time {dt*1000:.2f}ms, mfu {mfu*100:.2f}%")
366
+ else:
367
+ print(f"iter {iter_num}: loss {lossf:.4f}, time {dt*1000:.2f}ms")
368
+
369
+ iter_num += 1
370
+ local_iter += 1
371
+ if iter_num > max_iters:
372
+ break
373
+
374
+
375
+
376
+ if ddp:
377
+ destroy_process_group()
val.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dfd2ab42d40ddf7f031adcce55e31b76d1fe08655637d413557975e2da2182cb
3
+ size 116947684