-
-
Notifications
You must be signed in to change notification settings - Fork 668
Update to use fstrings #224
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
| 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, | ||
|
|
@@ -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}")) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'), | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once again, more readable in the code, slightly smaller filesize, and added |
||
| fp.write('\n'.join(all_text)) | ||
|
|
||
| def sample_batch(): | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More efficient, and 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
|
@@ -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'): | ||
|
|
||
There was a problem hiding this comment.
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.