Skip to content
Open
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
37 changes: 17 additions & 20 deletions gpt_2_simple/gpt_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def download_file_with_progress(url_base, sub_dir, model_name, file_name):

# set to download 1MB at a time. This could be much larger with no issue
DOWNLOAD_CHUNK_SIZE = 1024 * 1024
r = requests.get(url_base + "/models/" + model_name + "/" + file_name, stream=True)
r = requests.get(f"{url_base}/models/{model_name}/{file_name}", stream=True)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes nothing, besides increasing the efficiency of the code.

with open(os.path.join(sub_dir, file_name), 'wb') as f:
file_size = int(r.headers["content-length"])
with tqdm(ncols=100, desc="Fetching " + file_name,
Expand Down Expand Up @@ -275,8 +275,7 @@ def save():
maketree(checkpoint_path)
print(
'Saving',
os.path.join(checkpoint_path,
'model-{}').format(counter-1))
os.path.join(checkpoint_path, f"model-{counter-1}"))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, changes nothing besides a slight efficiency increase.

saver.save(
sess,
os.path.join(checkpoint_path, 'model'),
Expand All @@ -294,15 +293,13 @@ def generate_samples():
feed_dict={context: batch_size * [context_tokens]})
for i in range(min(sample_num - index, batch_size)):
text = enc.decode(out[i])
text = '======== SAMPLE {} ========\n{}\n'.format(
index + 1, text)
text = f'======== SAMPLE {index + 1} ========\n{text}\n'
Comment on lines -297 to +296

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More readable in the code, slightly smaller filesize

all_text.append(text)
index += 1
print(text)
maketree(os.path.join(SAMPLE_DIR, run_name))
with open(
os.path.join(SAMPLE_DIR, run_name,
'samples-{}').format(counter), 'w') as fp:
os.path.join(SAMPLE_DIR, run_name, f"samples-{counter}.txt", 'w') as fp:
Comment on lines -304 to +302

@JadynHax JadynHax Jul 31, 2020

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again, more readable in the code, slightly smaller filesize, and added .txt file extension for accessibility in Colaboratory.

fp.write('\n'.join(all_text))

def sample_batch():
Expand Down Expand Up @@ -348,12 +345,12 @@ def sample_batch():
avg_loss[1] * 0.99 + 1.0)

print(
'[{counter} | {time:2.2f}] loss={loss:2.2f} avg={avg:2.2f}'
'[{} | {2.2f}] loss={2.2f} avg={2.2f}'
.format(
counter=counter,
time=time.time() - start_time,
loss=v_loss,
avg=avg_loss[0] / avg_loss[1]))
counter,
time.time() - start_time,
v_loss,
avg_loss[0] / avg_loss[1]))
Comment on lines -351 to +353

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really no particular reason here, other than the fact that the identifiers were unnecessary (and personal preference).


counter += 1
except KeyboardInterrupt:
Expand Down Expand Up @@ -573,32 +570,32 @@ def copy_checkpoint_to_gdrive(run_name='run1', copy_folder=False):
"""Copies the checkpoint folder to a mounted Google Drive."""
is_mounted()

checkpoint_folder = os.path.join('checkpoint', run_name)
checkpoint_folder = f'checkpoint/{run_name}'
Comment on lines -576 to +573

@JadynHax JadynHax Jul 31, 2020

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More efficient, and os.path.join() was unneeded, since this is a function that can only be run on Colaboratory with the /content/XYZ references later in the function.

EDIT: called the function a "command" lmao


if copy_folder:
shutil.copytree(checkpoint_folder, "/content/drive/My Drive/" + checkpoint_folder)
shutil.copytree(checkpoint_folder, f"/content/drive/My Drive/{checkpoint_folder}")
Comment on lines -579 to +576

@JadynHax JadynHax Jul 31, 2020

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you get the point by now. Fstrings are just overall better in these scenarios.

I'll stop commenting on every single one of these, as you can probably guess by now why I used them in each scenario.

else:
file_path = get_tarfile_name(checkpoint_folder)

# Reference: https://stackoverflow.com/a/17081026
with tarfile.open(file_path, 'w') as tar:
tar.add(checkpoint_folder)

shutil.copyfile(file_path, "/content/drive/My Drive/" + file_path)
shutil.copyfile(file_path, f"/content/drive/My Drive/{file_path}")


def copy_checkpoint_from_gdrive(run_name='run1', copy_folder=False):
"""Copies the checkpoint folder from a mounted Google Drive."""
is_mounted()

checkpoint_folder = os.path.join('checkpoint', run_name)
checkpoint_folder = f'checkpoint/{run_name}'

if copy_folder:
shutil.copytree("/content/drive/My Drive/" + checkpoint_folder, checkpoint_folder)
shutil.copytree(f"/content/drive/My Drive/{checkpoint_folder}", checkpoint_folder)
else:
file_path = get_tarfile_name(checkpoint_folder)

shutil.copyfile("/content/drive/My Drive/" + file_path, file_path)
shutil.copyfile(f"/content/drive/My Drive/{file_path}", file_path)

with tarfile.open(file_path, 'r') as tar:
tar.extractall()
Expand All @@ -608,14 +605,14 @@ def copy_file_to_gdrive(file_path):
"""Copies a file to a mounted Google Drive."""
is_mounted()

shutil.copyfile(file_path, "/content/drive/My Drive/" + file_path)
shutil.copyfile(file_path, f"/content/drive/My Drive/{file_path}")


def copy_file_from_gdrive(file_path):
"""Copies a file from a mounted Google Drive."""
is_mounted()

shutil.copyfile("/content/drive/My Drive/" + file_path, file_path)
shutil.copyfile(f"/content/drive/My Drive/{file_path}", file_path)


def is_gpt2_downloaded(model_dir='models', model_name='124M'):
Expand Down