-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I've totally lost my place, so i'm just going to checkpoint and rebas…
…e later
- Loading branch information
1 parent
0888263
commit 1fd8be2
Showing
42 changed files
with
1,071 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,4 +126,5 @@ dmypy.json | |
|
||
*.o* | ||
|
||
models/ | ||
models/ | ||
*.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import tensorflow as tf | ||
import numpy as np | ||
import re | ||
import sys | ||
import os | ||
from shutil import copyfile | ||
|
||
if len(sys.argv) != 2: | ||
print("Usage: memory_breakdown.py [pretrain-dir]") | ||
exit() | ||
|
||
with tf.Session() as sess: | ||
|
||
# Load all the variables from the checkpoint | ||
total = 0 | ||
embeddings = 0 | ||
attention = 0 | ||
FC = 0 | ||
other = 0 | ||
masks = 0 | ||
cls = 0 | ||
for var_name, _ in tf.train.list_variables(sys.argv[1]): | ||
tensor = tf.contrib.framework.load_variable(sys.argv[1], var_name) | ||
|
||
total += tensor.size | ||
|
||
if var_name.endswith('/mask'): | ||
masks += tensor.size | ||
elif var_name.startswith('cls'): | ||
cls += tensor.size | ||
elif 'embeddings/word_embeddings' in var_name: | ||
embeddings += tensor.size | ||
elif '/attention/' in var_name: | ||
attention += tensor.size | ||
elif '/intermediate' in var_name or '/output/' in var_name: | ||
FC += tensor.size | ||
else: | ||
other += tensor.size | ||
|
||
total -= masks | ||
print(f""" | ||
Embeds: {embeddings} ({int(embeddings/total * 100)}%) | ||
Attention: {attention} ({int(attention/total * 100)}%) | ||
FC: {FC} ({int(FC/total * 100)}%) | ||
cls: {cls} ({int(cls/total * 100)}%) | ||
other: {other} ({int(other/total * 100)}%) | ||
Total: {total} | ||
(masks: {masks}) | ||
""") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import tensorflow as tf | ||
import numpy as np | ||
import re | ||
import sys | ||
import os | ||
from shutil import copyfile | ||
import fire | ||
|
||
def random_masks(model_dir, sparsity: float): | ||
"""Prunes a random [sparsity] of of weights in each matrix of [model_dir]. | ||
Makes a new checkpoint [model_dir]_random_prune_[sparsity]. | ||
""" | ||
model_dir = model_dir.rstrip('/') | ||
|
||
with tf.Session() as sess: | ||
|
||
# Load all the variables from the checkpoint | ||
for var_name, _ in tf.train.list_variables(model_dir): | ||
tensor = tf.contrib.framework.load_variable(model_dir, var_name) | ||
|
||
if var_name.endswith('/mask'): | ||
num_zeros = int(tensor.size * sparsity) | ||
new_mask = np.concatenate((np.zeros(num_zeros), np.ones(tensor.size - num_zeros))) | ||
np.random.shuffle(new_mask) | ||
new_mask = new_mask.reshape(tensor.shape).astype(tensor.dtype) | ||
var = tf.Variable(new_mask, name=var_name) | ||
else: | ||
var = tf.Variable(tensor, name=var_name) | ||
|
||
# Save these new variables | ||
saver = tf.train.Saver() | ||
sess.run(tf.global_variables_initializer()) | ||
output_dir = model_dir + f"_random_prune_{int(sparsity*100)}" | ||
os.mkdir(output_dir) | ||
saver.save(sess, os.path.join(output_dir, 'random_prune.ckpt')) | ||
|
||
if __name__ == '__main__': | ||
fire.Fire(random_masks) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.