patchbanks commited on
Commit
ce9d604
·
verified ·
1 Parent(s): b46b3cd

Upload 14 files

Browse files
app.py ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pickle
3
+ import torch
4
+ import random
5
+ import subprocess
6
+ import re
7
+ import pretty_midi
8
+ import gradio as gr
9
+ from contextlib import nullcontext
10
+ from model import GPTConfig, GPT
11
+ from pedalboard import Pedalboard, Reverb, Compressor, Gain, Limiter
12
+ from pedalboard.io import AudioFile
13
+
14
+ in_space = os.getenv("SYSTEM") == "spaces"
15
+
16
+ temp_dir = 'temp'
17
+ os.makedirs(temp_dir, exist_ok=True)
18
+
19
+ init_from = 'resume'
20
+ out_dir = 'checkpoints'
21
+ ckpt_load = 'model.pt'
22
+
23
+ start = "000000000000\n"
24
+ num_samples = 1
25
+ max_new_tokens = 564
26
+
27
+ seed = random.randint(1, 100000)
28
+ torch.manual_seed(seed)
29
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
30
+ dtype = 'bfloat16' if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else 'float16'
31
+ compile = False
32
+ exec(open('configurator.py').read())
33
+
34
+ torch.manual_seed(seed)
35
+ torch.cuda.manual_seed(seed)
36
+ torch.backends.cuda.matmul.allow_tf32 = True
37
+ torch.backends.cudnn.allow_tf32 = True
38
+ device_type = 'cpu' if 'cuda' in device else 'cpu'
39
+ ptdtype = {'float32': torch.float32, 'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype]
40
+ ctx = nullcontext() if device_type == 'cpu' else torch.amp.autocast(device_type=device_type, dtype=ptdtype)
41
+
42
+ if init_from == 'resume':
43
+ ckpt_path = os.path.join(out_dir, ckpt_load)
44
+ checkpoint = torch.load(ckpt_path, map_location=device, weights_only=True)
45
+ gptconf = GPTConfig(**checkpoint['model_args'])
46
+ model = GPT(gptconf)
47
+ state_dict = checkpoint['model']
48
+ unwanted_prefix = '_orig_mod.'
49
+ for k, v in list(state_dict.items()):
50
+ if k.startswith(unwanted_prefix):
51
+ state_dict[k[len(unwanted_prefix):]] = state_dict.pop(k)
52
+ model.load_state_dict(state_dict)
53
+ elif init_from.startswith('gpt2'):
54
+ model = GPT.from_pretrained(init_from, dict(dropout=0.0))
55
+
56
+ model.eval()
57
+ model.to(device)
58
+ if compile:
59
+ model = torch.compile(model)
60
+
61
+ tokenizer = re.compile(r'000000000000|\d{1}|\n')
62
+
63
+ meta_path = os.path.join('', 'meta.pkl')
64
+ with open(meta_path, 'rb') as f:
65
+ meta = pickle.load(f)
66
+ stoi = meta.get('stoi', None)
67
+ itos = meta.get('itos', None)
68
+
69
+ def encode(text):
70
+ matches = tokenizer.findall(text)
71
+ return [stoi[c] for c in matches]
72
+
73
+ def decode(encoded):
74
+ return ''.join([itos[i] for i in encoded])
75
+
76
+ def clear_midi(dir):
77
+ for file in os.listdir(dir):
78
+ if file.endswith('.mid'):
79
+ os.remove(os.path.join(dir, file))
80
+
81
+ clear_midi(temp_dir)
82
+
83
+
84
+ def generate_midi(temperature, top_k):
85
+ start_ids = encode(start)
86
+ x = (torch.tensor(start_ids, dtype=torch.long, device=device)[None, ...])
87
+
88
+ midi_events = []
89
+ seq_count = 0
90
+
91
+ with torch.no_grad():
92
+ for _ in range(num_samples):
93
+ sequence = []
94
+ y = model.generate(x, max_new_tokens, temperature=temperature, top_k=top_k)
95
+ tkn_seq = decode(y[0].tolist())
96
+ lines = tkn_seq.splitlines()
97
+
98
+ for event in lines:
99
+ if event.startswith(start.strip()):
100
+ if sequence:
101
+ midi_events.append(sequence)
102
+ sequence = []
103
+ seq_count += 1
104
+ elif event.strip() == "":
105
+ continue
106
+ else:
107
+ try:
108
+ p = int(event[0:2])
109
+ v = int(event[2:4])
110
+ s = int(event[4:8])
111
+ e = int(event[8:12])
112
+ except ValueError:
113
+ p, v, s, e = 0, 0, 0, 0
114
+ sequence.append({'file_name': f'nanompc_{seq_count:02d}', 'pitch': p, 'velocity': v, 'start': s, 'end': e})
115
+
116
+ if sequence:
117
+ midi_events.append(sequence)
118
+
119
+ round_bars = []
120
+
121
+ for sequence in midi_events:
122
+ filtered_sequence = []
123
+ for event in sequence:
124
+ if event['start'] < 768 and event['end'] <= 768:
125
+ filtered_sequence.append(event)
126
+ if filtered_sequence:
127
+ round_bars.append(filtered_sequence)
128
+
129
+ midi_events = round_bars
130
+
131
+ return midi_events
132
+
133
+
134
+ def write_midi(midi_events, bpm):
135
+ midi_data = pretty_midi.PrettyMIDI(initial_tempo=bpm, resolution=96)
136
+ midi_data.time_signature_changes.append(pretty_midi.containers.TimeSignature(4, 4, 0))
137
+ instrument = pretty_midi.Instrument(0)
138
+ midi_data.instruments.append(instrument)
139
+
140
+ for sequence in midi_events:
141
+ for event in sequence:
142
+ pitch = event['pitch']
143
+ velocity = event['velocity']
144
+ start = midi_data.tick_to_time(event['start'])
145
+ end = midi_data.tick_to_time(event['end'])
146
+ note = pretty_midi.Note(pitch=pitch, velocity=velocity, start=start, end=end)
147
+ instrument.notes.append(note)
148
+
149
+ midi_path = os.path.join(temp_dir, 'output.mid')
150
+ midi_data.write(midi_path)
151
+ print(f"Generated: {midi_path}")
152
+
153
+
154
+ def render_wav(midi_file):
155
+
156
+ sf2_dir = 'sf2_kits'
157
+ audio_format = 's16'
158
+ sample_rate = '44100'
159
+ gain = '2.0'
160
+
161
+ sf2_files = [f for f in os.listdir(sf2_dir) if f.endswith('.sf2')]
162
+ if not sf2_files:
163
+ raise ValueError("No SoundFont (.sf2) file found in directory.")
164
+
165
+ sf2_file = os.path.join(sf2_dir, random.choice(sf2_files))
166
+ print(sf2_file)
167
+ output_wav = os.path.join(temp_dir, 'output.wav')
168
+
169
+ with open(os.devnull, 'w') as devnull:
170
+ command = [
171
+ 'fluidsynth', '-ni', sf2_file, midi_file, '-F', output_wav, '-r', str(sample_rate),
172
+ '-o', f'audio.file.format={audio_format}', '-g', str(gain)
173
+ ]
174
+ subprocess.call(command, stdout=devnull, stderr=devnull)
175
+
176
+ return output_wav
177
+
178
+
179
+ def render_sfx(wav_raw, settings):
180
+ wav_fx = wav_raw
181
+
182
+ for setting in settings:
183
+ board = setting['board']
184
+
185
+ with AudioFile(wav_raw) as f:
186
+ with AudioFile(wav_fx, 'w', f.samplerate, f.num_channels) as o:
187
+ while f.tell() < f.frames:
188
+ chunk = f.read(int(f.samplerate))
189
+ effected = board(chunk, f.samplerate, reset=False)
190
+ o.write(effected)
191
+
192
+ return wav_fx
193
+
194
+
195
+ def generate_and_return_files(bpm, temperature, top_k):
196
+ midi_events = generate_midi(temperature, top_k)
197
+ if not midi_events:
198
+ return "Error generating MIDI.", None, None
199
+
200
+ write_midi(midi_events, bpm)
201
+
202
+ midi_file = os.path.join(temp_dir, 'output.mid')
203
+ wav_raw = render_wav(midi_file)
204
+ wav_fx = os.path.join(temp_dir, 'output_fx.wav')
205
+
206
+ sfx_settings = [
207
+ {
208
+ 'board': Pedalboard([
209
+ Reverb(room_size=0.01, wet_level=random.uniform(0.005, 0.01), dry_level=0.75, width=1.0),
210
+ Compressor(threshold_db=-3.0, ratio=8.0, attack_ms=0.0, release_ms=300.0),
211
+ ])
212
+ }
213
+ ]
214
+
215
+ for setting in sfx_settings:
216
+ board = setting['board']
217
+
218
+ with AudioFile(wav_raw) as f:
219
+ with AudioFile(wav_fx, 'w', f.samplerate, f.num_channels) as o:
220
+ while f.tell() < f.frames:
221
+ chunk = f.read(int(f.samplerate))
222
+ effected = board(chunk, f.samplerate, reset=False)
223
+ o.write(effected)
224
+
225
+ return midi_file, wav_fx
226
+
227
+
228
+
229
+ iface = gr.Interface(
230
+ fn=generate_and_return_files,
231
+ inputs=[
232
+ gr.Slider(minimum=60, maximum=200, step=1, value=90, label="bpm"),
233
+ gr.Slider(minimum=0.1, maximum=2.0, step=0.1, value=1.0, label="temperature"),
234
+ gr.Slider(minimum=4, maximum=128, step=1, value=16, label="top_k")
235
+ ],
236
+ outputs=[
237
+ gr.File(label="MIDI File"),
238
+ gr.Audio(label="Generated Audio", type="filepath")
239
+ ],
240
+ title="<h1 style='font-weight: bold; text-align: center;'>nanoMPC Lite - AI Midi Drum Sequencer</h1>",
241
+ description="<p style='text-align:center;'>nanoMPC is a tiny transformer model that generates MIDI drum beats inspired by Lo-Fi, Boom Bap and other styles of Hip Hop.</p>"
242
+ )
243
+
244
+ iface.launch()
checkpoints/model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3fbfb97479ba42ba92b04b532e8fb98b1f00d8ac1a7a70c312b3d0608f33764a
3
+ size 25067441
config/eval_gpt2.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # evaluate the base gpt2
2
+ # n_layer=12, n_head=12, n_embd=768
3
+ # 124M parameters
4
+ batch_size = 8
5
+ eval_iters = 500 # use more iterations to get good estimate
6
+ eval_only = True
7
+ wandb_log = False
8
+ init_from = 'gpt2'
config/eval_gpt2_large.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # evaluate the base gpt2
2
+ # n_layer=36, n_head=20, n_embd=1280
3
+ # 774M parameters
4
+ batch_size = 8
5
+ eval_iters = 500 # use more iterations to get good estimate
6
+ eval_only = True
7
+ wandb_log = False
8
+ init_from = 'gpt2-large'
config/eval_gpt2_medium.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # evaluate the base gpt2
2
+ # n_layer=24, n_head=16, n_embd=1024
3
+ # 350M parameters
4
+ batch_size = 8
5
+ eval_iters = 500 # use more iterations to get good estimate
6
+ eval_only = True
7
+ wandb_log = False
8
+ init_from = 'gpt2-medium'
config/eval_gpt2_xl.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # evaluate the base gpt2
2
+ # n_layer=48, n_head=25, n_embd=1600
3
+ # 1558M parameters
4
+ batch_size = 8
5
+ eval_iters = 500 # use more iterations to get good estimate
6
+ eval_only = True
7
+ wandb_log = False
8
+ init_from = 'gpt2-xl'
config/finetune_shakespeare.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ out_dir = 'out-shakespeare'
4
+ eval_interval = 5
5
+ eval_iters = 40
6
+ wandb_log = False # feel free to turn on
7
+ wandb_project = 'shakespeare'
8
+ wandb_run_name = 'ft-' + str(time.time())
9
+
10
+ dataset = 'shakespeare'
11
+ init_from = 'gpt2-xl' # this is the largest GPT-2 model
12
+
13
+ # only save checkpoints if the validation loss improves
14
+ always_save_checkpoint = False
15
+
16
+ # the number of examples per iter:
17
+ # 1 batch_size * 32 grad_accum * 1024 tokens = 32,768 tokens/iter
18
+ # shakespeare has 301,966 tokens, so 1 epoch ~= 9.2 iters
19
+ batch_size = 1
20
+ gradient_accumulation_steps = 32
21
+ max_iters = 20
22
+
23
+ # finetune at constant LR
24
+ learning_rate = 3e-5
25
+ decay_lr = False
config/train_gpt2.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # config for training GPT-2 (124M) down to very nice loss of ~2.85 on 1 node of 8X A100 40GB
2
+ # launch as the following (e.g. in a screen session) and wait ~5 days:
3
+ # $ torchrun --standalone --nproc_per_node=8 train.py config/train_gpt2.py
4
+
5
+ wandb_log = True
6
+ wandb_project = 'owt'
7
+ wandb_run_name='gpt2-124M'
8
+
9
+ # these make the total batch size be ~0.5M
10
+ # 12 batch size * 1024 block size * 5 gradaccum * 8 GPUs = 491,520
11
+ batch_size = 12
12
+ block_size = 1024
13
+ gradient_accumulation_steps = 5 * 8
14
+
15
+ # this makes total number of tokens be 300B
16
+ max_iters = 600000
17
+ lr_decay_iters = 600000
18
+
19
+ # eval stuff
20
+ eval_interval = 1000
21
+ eval_iters = 200
22
+ log_interval = 10
23
+
24
+ # weight decay
25
+ weight_decay = 1e-1
config/train_shakespeare_char.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # train a miniature character-level shakespeare model
2
+ # good for debugging and playing on macbooks and such
3
+
4
+ out_dir = 'out-shakespeare-char'
5
+ eval_interval = 250 # keep frequent because we'll overfit
6
+ eval_iters = 200
7
+ log_interval = 10 # don't print too too often
8
+
9
+ # we expect to overfit on this small dataset, so only save when val improves
10
+ always_save_checkpoint = False
11
+
12
+ wandb_log = False # override via command line if you like
13
+ wandb_project = 'shakespeare-char'
14
+ wandb_run_name = 'mini-gpt'
15
+
16
+ dataset = 'shakespeare_char'
17
+ gradient_accumulation_steps = 1
18
+ batch_size = 64
19
+ block_size = 256 # context of up to 256 previous characters
20
+
21
+ # baby GPT model :)
22
+ n_layer = 6
23
+ n_head = 6
24
+ n_embd = 384
25
+ dropout = 0.2
26
+
27
+ learning_rate = 1e-3 # with baby networks can afford to go a bit higher
28
+ max_iters = 5000
29
+ lr_decay_iters = 5000 # make equal to max_iters usually
30
+ min_lr = 1e-4 # learning_rate / 10 usually
31
+ beta2 = 0.99 # make a bit bigger because number of tokens per iter is small
32
+
33
+ warmup_iters = 100 # not super necessary potentially
34
+
35
+ # on macbook also add
36
+ # device = 'cpu' # run on cpu only
37
+ # compile = False # do not torch compile the model
configurator.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import argparse
20
+
21
+ parser = argparse.ArgumentParser(description="nanoMPC")
22
+ parser.add_argument("--bpm", type=int, default=90, help="Beats per minute")
23
+ parser.add_argument("--num_samples", type=int, default=1, help="Number of samples")
24
+ args, unknown_args = parser.parse_known_args() # Capture unknown args for configurator
25
+
26
+ bpm = args.bpm
27
+ num_samples = args.num_samples
28
+
29
+ for arg in unknown_args:
30
+ if arg.startswith('--'):
31
+ print(f"Skipping command-line argument: {arg}")
32
+ continue
33
+
34
+ if '=' not in arg:
35
+ config_file = arg
36
+ print(f"Overriding config with {config_file}:")
37
+ with open(config_file) as f:
38
+ print(f.read())
39
+ exec(open(config_file).read())
40
+ else:
41
+ key, val = arg.split('=')
42
+ key = key[2:]
43
+
44
+ if key in globals():
45
+ if key in ['bpm', 'num_samples']:
46
+ continue
47
+ try:
48
+ attempt = literal_eval(val)
49
+ except (SyntaxError, ValueError):
50
+ attempt = val
51
+ assert type(attempt) == type(globals()[key])
52
+ print(f"Overriding: {key} = {attempt}")
53
+ globals()[key] = attempt
54
+ else:
55
+ raise ValueError(f"Unknown config key: {key}")
56
+
57
+
meta.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a7e6f8758266a9d6ea2ee9abb6295dad8ef38dde1640b9de66d98f09fb293494
3
+ size 184
model.py ADDED
@@ -0,0 +1,297 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import inspect
2
+ from dataclasses import dataclass
3
+ from torch.utils.data import DataLoader, Dataset
4
+ from torch.nn import functional as F
5
+ import torch.nn as nn
6
+ import torch
7
+ import math
8
+ import numpy as np
9
+
10
+
11
+ class RMSNorm(torch.nn.Module):
12
+ def __init__(self, dim: int, eps: float):
13
+ super().__init__()
14
+ self.eps = eps
15
+ self.weight = nn.Parameter(torch.ones(dim))
16
+
17
+ def _norm(self, x):
18
+ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
19
+
20
+ def forward(self, x):
21
+ output = self._norm(x.float()).type_as(x)
22
+ return output * self.weight
23
+
24
+
25
+ class CausalSelfAttention(nn.Module):
26
+ def __init__(self, config):
27
+ super().__init__()
28
+ assert config.n_embd % config.n_head == 0
29
+ self.config = config # Store the config object
30
+
31
+ self.n_head = config.n_head
32
+ self.n_embd = config.n_embd
33
+ self.dropout = config.dropout
34
+
35
+ self.c_attn = nn.Linear(config.n_embd, 3 * config.n_embd, bias=config.bias)
36
+ self.c_proj = nn.Linear(config.n_embd, config.n_embd, bias=config.bias)
37
+ self.attn_dropout = nn.Dropout(config.dropout)
38
+ self.resid_dropout = nn.Dropout(config.dropout)
39
+
40
+ self.rel_attn_bias = nn.Embedding(config.block_size * 2 - 1, config.n_head)
41
+
42
+ def forward(self, x):
43
+ B, T, C = x.size()
44
+ q, k, v = self.c_attn(x).split(self.n_embd, dim=2)
45
+ q = q.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
46
+ k = k.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
47
+ v = v.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
48
+
49
+ if hasattr(torch.nn.functional, 'scaled_dot_product_attention'):
50
+ attn_logits = 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)
51
+ else:
52
+ attn_logits = (q @ k.transpose(-2, -1)) / math.sqrt(C // self.n_head)
53
+ max_rpe = self.config.block_size // 2 # Use config object
54
+ rpe_matrix = self.generate_rpe(T, max_rpe).to(x.device)
55
+ rpe_embeddings = self.rel_attn_bias(rpe_matrix).transpose(1, 2).unsqueeze(0)
56
+ attn_logits = attn_logits + rpe_embeddings
57
+ attn_logits = attn_logits.masked_fill(self.bias[:,:,:T,:T] == 0, float('-inf'))
58
+ attn_logits = F.softmax(attn_logits, dim=-1)
59
+ attn_logits = self.attn_dropout(attn_logits)
60
+ attn_logits = attn_logits @ v
61
+
62
+ y = attn_logits.transpose(1, 2).contiguous().view(B, T, C)
63
+ y = self.resid_dropout(self.c_proj(y))
64
+ return y
65
+
66
+ def generate_rpe(self, length, max_rpe):
67
+ range_vec = torch.arange(length)
68
+ range_mat = range_vec.unsqueeze(0) - range_vec.unsqueeze(1)
69
+ range_mat_clipped = torch.clamp(range_mat, -max_rpe, max_rpe)
70
+ final_mat = range_mat_clipped + max_rpe
71
+ return final_mat
72
+
73
+
74
+ class MLP(nn.Module):
75
+
76
+ def __init__(self, config):
77
+ super().__init__()
78
+ self.c_fc = nn.Linear(config.n_embd, 4 * config.n_embd, bias=config.bias)
79
+ self.gelu = nn.GELU()
80
+ self.c_proj = nn.Linear(4 * config.n_embd, config.n_embd, bias=config.bias)
81
+ self.dropout = nn.Dropout(config.dropout)
82
+
83
+ def forward(self, x):
84
+ x = self.c_fc(x)
85
+ x = self.gelu(x)
86
+ x = self.c_proj(x)
87
+ x = self.dropout(x)
88
+ return x
89
+
90
+ class Block(nn.Module):
91
+ def __init__(self, config):
92
+ super().__init__()
93
+ self.ln_1 = RMSNorm(config.n_embd, eps=1e-5)
94
+ self.attn = CausalSelfAttention(config)
95
+ self.ln_2 = RMSNorm(config.n_embd, eps=1e-5)
96
+ self.mlp = MLP(config)
97
+
98
+ def forward(self, x):
99
+ x = x + self.attn(self.ln_1(x))
100
+ x = x + self.mlp(self.ln_2(x))
101
+ return x
102
+
103
+ @dataclass
104
+ class GPTConfig:
105
+ block_size: int = 1024
106
+ vocab_size: int = 50304
107
+ n_layer: int = 12
108
+ n_head: int = 12
109
+ n_embd: int = 768
110
+ dropout: float = 0.0
111
+ bias: bool = True
112
+
113
+ class GPT(nn.Module):
114
+ def __init__(self, config):
115
+ super().__init__()
116
+ assert config.vocab_size is not None
117
+ assert config.block_size is not None
118
+ self.config = config
119
+
120
+ self.transformer = nn.ModuleDict(dict(
121
+ wte = nn.Embedding(config.vocab_size, config.n_embd),
122
+ wpe = nn.Embedding(config.block_size, config.n_embd),
123
+ drop = nn.Dropout(config.dropout),
124
+ h = nn.ModuleList([Block(config) for _ in range(config.n_layer)]),
125
+ ln_f = RMSNorm(config.n_embd, eps=1e-5),
126
+ ))
127
+ self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
128
+ self.transformer.wte.weight = self.lm_head.weight
129
+
130
+ self.apply(self._init_weights)
131
+ for pn, p in self.named_parameters():
132
+ if pn.endswith('c_proj.weight'):
133
+ torch.nn.init.normal_(p, mean=0.0, std=0.02/math.sqrt(2 * config.n_layer))
134
+
135
+ #print("number of parameters: %.2fM" % (self.get_num_params()/1e6,))
136
+
137
+ def get_num_params(self, non_embedding=True):
138
+ n_params = sum(p.numel() for p in self.parameters())
139
+ if non_embedding:
140
+ n_params -= self.transformer.wpe.weight.numel()
141
+ return n_params
142
+
143
+ def _init_weights(self, module):
144
+ if isinstance(module, nn.Linear):
145
+ torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
146
+ if module.bias is not None:
147
+ torch.nn.init.zeros_(module.bias)
148
+ elif isinstance(module, nn.Embedding):
149
+ torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
150
+
151
+ def forward(self, idx, targets=None, noise_pct=0.1):
152
+ device = idx.device
153
+ b, t = idx.size()
154
+ assert t <= self.config.block_size, f"Cannot forward sequence of length {t}, block size is only {self.config.block_size}"
155
+ pos = torch.arange(0, t, dtype=torch.long, device=device)
156
+
157
+ tok_emb = self.transformer.wte(idx)
158
+ pos_emb = self.transformer.wpe(pos)
159
+
160
+ # add noise to the input
161
+ if noise_pct > 0.0:
162
+ noise_std = torch.std(tok_emb) * noise_pct
163
+ noise = torch.randn_like(tok_emb) * noise_std
164
+ tok_emb = tok_emb + noise
165
+
166
+ x = self.transformer.drop(tok_emb + pos_emb)
167
+ for block in self.transformer.h:
168
+ x = block(x)
169
+ x = self.transformer.ln_f(x)
170
+
171
+ if targets is not None:
172
+
173
+ logits = self.lm_head(x)
174
+ loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1)
175
+ else:
176
+
177
+ logits = self.lm_head(x[:, [-1], :])
178
+ loss = None
179
+
180
+ return logits, loss
181
+
182
+
183
+ def crop_block_size(self, block_size):
184
+ assert block_size <= self.config.block_size
185
+ self.config.block_size = block_size
186
+ self.transformer.wpe.weight = nn.Parameter(self.transformer.wpe.weight[:block_size])
187
+ for block in self.transformer.h:
188
+ if hasattr(block.attn, 'bias'):
189
+ block.attn.bias = block.attn.bias[:,:,:block_size,:block_size]
190
+
191
+ @classmethod
192
+ def from_pretrained(cls, model_type, override_args=None):
193
+ assert model_type in {'gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl'}
194
+ override_args = override_args or {}
195
+
196
+ assert all(k == 'dropout' for k in override_args)
197
+ from transformers import GPT2LMHeadModel
198
+ print("loading weights from pretrained gpt: %s" % model_type)
199
+
200
+ config_args = {
201
+ 'gpt2': dict(n_layer=12, n_head=12, n_embd=768),
202
+ 'gpt2-medium': dict(n_layer=24, n_head=16, n_embd=1024),
203
+ 'gpt2-large': dict(n_layer=36, n_head=20, n_embd=1280),
204
+ 'gpt2-xl': dict(n_layer=48, n_head=25, n_embd=1600),
205
+ }[model_type]
206
+ print("forcing vocab_size=50257, block_size=1024, bias=True")
207
+ config_args['vocab_size'] = 50257
208
+ config_args['block_size'] = 1024
209
+ config_args['bias'] = True
210
+
211
+ if 'dropout' in override_args:
212
+ print(f"overriding dropout rate to {override_args['dropout']}")
213
+ config_args['dropout'] = override_args['dropout']
214
+
215
+ config = GPTConfig(**config_args)
216
+ model = GPT(config)
217
+ sd = model.state_dict()
218
+ sd_keys = sd.keys()
219
+ sd_keys = [k for k in sd_keys if not k.endswith('.attn.bias')]
220
+
221
+ model_hf = GPT2LMHeadModel.from_pretrained(model_type)
222
+ sd_hf = model_hf.state_dict()
223
+
224
+ sd_keys_hf = sd_hf.keys()
225
+ sd_keys_hf = [k for k in sd_keys_hf if not k.endswith('.attn.masked_bias')]
226
+ sd_keys_hf = [k for k in sd_keys_hf if not k.endswith('.attn.bias')]
227
+ transposed = ['attn.c_attn.weight', 'attn.c_proj.weight', 'mlp.c_fc.weight', 'mlp.c_proj.weight']
228
+
229
+ assert len(sd_keys_hf) == len(sd_keys), f"mismatched keys: {len(sd_keys_hf)} != {len(sd_keys)}"
230
+ for k in sd_keys_hf:
231
+ if any(k.endswith(w) for w in transposed):
232
+
233
+ assert sd_hf[k].shape[::-1] == sd[k].shape
234
+ with torch.no_grad():
235
+ sd[k].copy_(sd_hf[k].t())
236
+ else:
237
+
238
+ assert sd_hf[k].shape == sd[k].shape
239
+ with torch.no_grad():
240
+ sd[k].copy_(sd_hf[k])
241
+
242
+ return model
243
+
244
+ def configure_optimizers(self, weight_decay, learning_rate, betas, device_type):
245
+
246
+ param_dict = {pn: p for pn, p in self.named_parameters()}
247
+
248
+ param_dict = {pn: p for pn, p in param_dict.items() if p.requires_grad}
249
+
250
+ decay_params = [p for n, p in param_dict.items() if p.dim() >= 2]
251
+ nodecay_params = [p for n, p in param_dict.items() if p.dim() < 2]
252
+ optim_groups = [
253
+ {'params': decay_params, 'weight_decay': weight_decay},
254
+ {'params': nodecay_params, 'weight_decay': 0.0}
255
+ ]
256
+ num_decay_params = sum(p.numel() for p in decay_params)
257
+ num_nodecay_params = sum(p.numel() for p in nodecay_params)
258
+ print(f"num decayed parameter tensors: {len(decay_params)}, with {num_decay_params:,} parameters")
259
+ print(f"num non-decayed parameter tensors: {len(nodecay_params)}, with {num_nodecay_params:,} parameters")
260
+
261
+ fused_available = 'fused' in inspect.signature(torch.optim.AdamW).parameters
262
+ use_fused = fused_available and device_type == 'cuda'
263
+ extra_args = dict(fused=True) if use_fused else dict()
264
+ optimizer = torch.optim.AdamW(optim_groups, lr=learning_rate, betas=betas, **extra_args)
265
+ print(f"using fused AdamW: {use_fused}")
266
+
267
+ return optimizer
268
+
269
+ def estimate_mfu(self, fwdbwd_per_iter, dt):
270
+ """ estimate model flops utilization (MFU) in units of A100 bfloat16 peak FLOPS """
271
+
272
+ N = self.get_num_params()
273
+ cfg = self.config
274
+ L, H, Q, T = cfg.n_layer, cfg.n_head, cfg.n_embd//cfg.n_head, cfg.block_size
275
+ flops_per_token = 6*N + 12*L*H*Q*T
276
+ flops_per_fwdbwd = flops_per_token * T
277
+ flops_per_iter = flops_per_fwdbwd * fwdbwd_per_iter
278
+
279
+ flops_achieved = flops_per_iter * (1.0/dt)
280
+ flops_promised = 312e12
281
+ mfu = flops_achieved / flops_promised
282
+ return mfu
283
+
284
+ @torch.no_grad()
285
+ def generate(self, idx, max_new_tokens, temperature=1.0, top_k=None):
286
+ for _ in range(max_new_tokens):
287
+ idx_cond = idx if idx.size(1) <= self.config.block_size else idx[:, -self.config.block_size:]
288
+ logits, _ = self(idx_cond)
289
+ logits = logits[:, -1, :] / temperature
290
+ if top_k is not None:
291
+ v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
292
+ logits[logits < v[:, [-1]]] = -float('Inf')
293
+ probs = F.softmax(logits, dim=-1)
294
+ idx_next = torch.multinomial(probs, num_samples=1)
295
+ idx = torch.cat((idx, idx_next), dim=1)
296
+
297
+ return idx
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fluidsynth
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ pretty_midi==0.2.10
2
+ pedalboard==0.9.3
3
+ torch
4
+ gradio