Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize encode/decode for datasets #415

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ __pycache__/
*.pt
*.pyc
input.txt
*.venv
*.code-workspace
out/
out-*/
wandb/*
data/*/samples/*
8 changes: 4 additions & 4 deletions data/shakespeare_char/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
encoder and decoder and some other related info.
"""
import os
import pickle
import dill
import requests
import numpy as np

Expand Down Expand Up @@ -54,11 +54,11 @@ def decode(l):
# save the meta information as well, to help us encode/decode later
meta = {
'vocab_size': vocab_size,
'itos': itos,
'stoi': stoi,
'encode': encode,
'decode': decode,
}
with open(os.path.join(os.path.dirname(__file__), 'meta.pkl'), 'wb') as f:
pickle.dump(meta, f)
dill.dump(meta, f, recurse=True)

# length of dataset in characters: 1115394
# all the unique characters:
Expand Down
9 changes: 3 additions & 6 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Sample from a trained model
"""
import os
import pickle
import dill
from contextlib import nullcontext
import torch
import tiktoken
Expand Down Expand Up @@ -61,11 +61,8 @@
if load_meta:
print(f"Loading meta from {meta_path}...")
with open(meta_path, 'rb') as f:
meta = pickle.load(f)
# TODO want to make this more general to arbitrary encoder/decoder schemes
stoi, itos = meta['stoi'], meta['itos']
encode = lambda s: [stoi[c] for c in s]
decode = lambda l: ''.join([itos[i] for i in l])
meta = dill.load(f)
encode, decode = meta['encode'], meta['decode']
else:
# ok let's assume gpt-2 encodings by default
print("No meta.pkl found, assuming GPT-2 encodings...")
Expand Down
4 changes: 2 additions & 2 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import os
import time
import math
import pickle
import dill
from contextlib import nullcontext

import numpy as np
Expand Down Expand Up @@ -136,7 +136,7 @@ def get_batch(split):
meta_vocab_size = None
if os.path.exists(meta_path):
with open(meta_path, 'rb') as f:
meta = pickle.load(f)
meta = dill.load(f)
meta_vocab_size = meta['vocab_size']
print(f"found vocab_size = {meta_vocab_size} (inside {meta_path})")

Expand Down