Skip to content

Commit b412f27

Browse files
committed
*): Update for coding style.
1 parent cec96af commit b412f27

File tree

10 files changed

+38
-38
lines changed

10 files changed

+38
-38
lines changed

datasets/openwebtext/create_tfrecords.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def create_file(args):
5050
return
5151
if os.path.exists(os.path.join(output_dir, s)): # Unfinished file, remove
5252
os.remove(os.path.join(output_dir, s))
53-
53+
5454
with tf.python_io.TFRecordWriter(os.path.join(output_dir, s)) as writer:
5555
good_files = 0
5656
current = None
@@ -85,4 +85,4 @@ def create_file(args):
8585

8686
end = time.time()
8787

88-
print("Done! In {:.2f}s, {} / {} good files.".format(end-start, str(good), str(len(files))))
88+
print("Done! In {:.2f}s, {} / {} good files.".format(end-start, str(good), str(len(files))))

download_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@
4949
# 1k for chunk_size, since Ethernet packet size is around 1500 bytes
5050
for chunk in r.iter_content(chunk_size=chunk_size):
5151
f.write(chunk)
52-
pbar.update(chunk_size)
52+
pbar.update(chunk_size)

experimental/experiments.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def get_value(self):
7575
return val
7676
else:
7777
raise RuntimeError("{} ran out of values!".format(self.name))
78-
78+
7979
# Sample randomly from a list of values
8080
elif self.distribution == "sample":
8181
return random.sample(self.values)
@@ -111,7 +111,7 @@ def generate_experiments(base, parameters, number):
111111
ex[p.name] = p.get_value()
112112

113113
experiments.append(ex)
114-
114+
115115
return experiments
116116

117117
parameters = [

experimental/overrunner.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def save(runners):
3939
states = []
4040
for r in runners:
4141
states.append(r.dump_dict())
42-
42+
4343
with open("logs/state.json", "w") as f:
4444
json.dump(states, f)
4545

@@ -51,7 +51,7 @@ def save(runners):
5151
if ts.done:
5252
runners.remove(ts)
5353
continue
54-
54+
5555
ts.update_state()
5656
logging.info("{} - TPU State: {} - Process Running: {}".format(ts.prefix, ts.state, ts.task_running))
5757

@@ -86,7 +86,7 @@ def save(runners):
8686

8787
if ts.running_time > 60*60*24: # Make a hard checkpoint save every day
8888
logging.info("Backing up {}".format(ts.prefix))
89-
subprocess.call(["gsutil", "cp", "-r", ts.params["model_dir"],
89+
subprocess.call(["gsutil", "cp", "-r", ts.params["model_dir"],
9090
os.path.join(backup_path, ts.params["model_dir"].split("/")[-1] + "-" + str(ts.current_save))])
9191
ts.current_save += 1
9292

@@ -113,4 +113,4 @@ def save(runners):
113113
ts.kill_current_task()
114114
except Exception as e:
115115
logging.error(e)
116-
save(all_runners)
116+
save(all_runners)

experimental/tpu_survival.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(self, project=None, location=None, id=None, params=None, d=None):
4242
self.done = d["done"]
4343

4444

45-
45+
4646
# current running job
4747
self.current_process = None
4848
self.state = None
@@ -250,4 +250,4 @@ def delete_tpu(project, location, tpu_name):
250250
request = service.projects().locations().nodes().delete(
251251
name=name)
252252

253-
return request.execute()
253+
return request.execute()

inputs.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def generic_text(params):
4444
datasets = [bpe_text(params["batch_size"], dataset[0], amount=params["n_ctx"], iterations=params["iterations"], stitch=params["stitch"], batch=False)
4545
for dataset in params["dataset"]]
4646
weights = [dataset[1] for dataset in params["dataset"]]
47-
47+
4848
dataset = tf.data.experimental.sample_from_datasets(datasets, weights=weights)
4949
dataset = dataset.batch(params["batch_size"], drop_remainder=True).prefetch(params["iterations"] * 2)
5050

@@ -65,9 +65,9 @@ def _parse_function(example_proto):
6565
dataset = dataset.map(_parse_function, num_parallel_calls=1).shuffle(1000 * stitch)
6666

6767
# Since samples can be less than the correct length, and TPUs don't like variable lengths, this function stitches together enough samples
68-
# to have a text at least 1024 tokens long. For this to work the stitch parameter must be correctly tuned so that
68+
# to have a text at least 1024 tokens long. For this to work the stitch parameter must be correctly tuned so that
6969
# stitch * min(characters_in_text) >= amount
70-
def _stitch_text(x, y):
70+
def _stitch_text(x, y):
7171
x = tf.sparse.to_dense(x)
7272

7373
def _get_x(i):
@@ -76,7 +76,7 @@ def _get_x(i):
7676
out = _get_x(0)
7777
for i in range(1, stitch):
7878
out = tf.concat([out, [50256], _get_x(i)], axis=0) # text1<|endoftext|>text2
79-
79+
8080
return out
8181

8282
# Hack-y way to stitch together multiple texts
@@ -113,6 +113,6 @@ def gpt2_pred_input(params, text=None):
113113
tokens = enc.encode(text)
114114
if len(tokens) > 1024:
115115
tokens = tokens[:1024]
116-
t = tf.broadcast_to(tokens, [params["batch_size"], len(tokens)])
117-
dataset = tf.data.Dataset.from_tensors(t)
118-
return dataset
116+
t = tf.broadcast_to(tokens, [params["batch_size"], len(tokens)])
117+
dataset = tf.data.Dataset.from_tensors(t)
118+
return dataset

main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# This program was designed to function with multiple kinds of models, but currently only GPT2 is supported
1616
# The first element in the tupel is the model function, the second is the function called when predicting
1717
models = {
18-
"GPT2": (gpt2_model, gpt2_predict)
18+
"GPT2": (gpt2_model, gpt2_predict)
1919
}
2020

2121
inputs = {
@@ -46,8 +46,8 @@
4646
elif args.predict_file is not None and args.predict_text is not None:
4747
print("ERROR: Specify exactly one of --predict_file and --predict_text!")
4848
sys.exit()
49-
50-
49+
50+
5151
# Setup logging
5252
Path("logs").mkdir(exist_ok=True)
5353
tf.logging.set_verbosity(logging.INFO)
@@ -91,7 +91,7 @@
9191
cluster=tpu_cluster_resolver,
9292
save_checkpoints_secs=60*30,
9393
session_config=tf.ConfigProto(
94-
# allow_soft_placement=True,
94+
# allow_soft_placement=True,
9595
# log_device_placement=True
9696
),
9797
tpu_config=tf.contrib.tpu.TPUConfig(iterations_per_loop=params["iterations"])
@@ -138,15 +138,15 @@
138138
network.train(
139139
input_fn=partial(input_fn, eval=False),
140140
steps=params["train_steps"])
141-
141+
142142

143143
end = time.time()
144144
logger.info("\nTrain loop took {:.2f}s\n".format(end-start))
145145

146146
eval_result = network.evaluate(
147147
input_fn=partial(input_fn, eval=True),
148148
steps=params["eval_steps"])
149-
149+
150150
logger.info("\nEval Results: {}\n".format(str(eval_result)))
151151

152152
if network.get_variable_value("global_step") > params["max_steps"]:

model_fns.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ def gpt2_model(features, labels, mode, params):
1818
train=mode==tf.estimator.ModeKeys.TRAIN)
1919

2020
output["logits"] = tf.cast(output["logits"], tf.float32)
21-
21+
2222
else:
2323
output = gpt2.model(X=features, params=params,
2424
labels=labels,
2525
past=None, reuse=tf.AUTO_REUSE,
2626
train=mode==tf.estimator.ModeKeys.TRAIN)
27-
27+
2828
loss_batch = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=output["logits"], labels=labels)
2929
loss = tf.reduce_mean(loss_batch)
3030

3131
if mode == tf.estimator.ModeKeys.TRAIN:
3232
train_op = create_train_op(loss, params)
33-
33+
3434
if params["use_tpu"]:
3535
return tf.contrib.tpu.TPUEstimatorSpec(mode, loss=loss, train_op=train_op)
3636
else:
@@ -42,10 +42,10 @@ def gpt2_model(features, labels, mode, params):
4242

4343
if params["use_tpu"]:
4444
# Metric inputs are transferred to CPU and must preserve batch dimension
45-
return tf.contrib.tpu.TPUEstimatorSpec(mode=mode,
45+
return tf.contrib.tpu.TPUEstimatorSpec(mode=mode,
4646
loss=loss, eval_metrics=(perplexity_metric, {"loss": loss_batch}))
4747
else:
48-
return tf.estimator.EstimatorSpec(mode=mode,
48+
return tf.estimator.EstimatorSpec(mode=mode,
4949
loss=loss, eval_metric_ops=perplexity_metric(loss_batch))
5050

5151

@@ -61,12 +61,12 @@ def gpt2_model(features, labels, mode, params):
6161
batch_size=params["batch_size"],
6262
temperature=1.0, top_k=params["top_k"]
6363
)
64-
64+
6565
predictions = {
6666
"tokens": output
6767
}
6868

6969
if params["use_tpu"]:
7070
return tf.contrib.tpu.TPUEstimatorSpec(mode, predictions=predictions)
7171
else:
72-
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
72+
return tf.estimator.EstimatorSpec(mode, predictions=predictions)

models/gpt2/sample.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def step(params, tokens, past=None):
3535
lm_output = gpt2.model(params=params, X=tokens, past=past, reuse=tf.AUTO_REUSE)
3636

3737
lm_output["logits"] = tf.cast(lm_output["logits"], tf.float32)
38-
38+
3939
else:
4040
lm_output = lm_output = gpt2.model(params=params, X=tokens, past=past, reuse=tf.AUTO_REUSE)
41-
41+
4242

4343
logits = lm_output['logits'][:, :, :params["n_vocab"]]
4444
presents = lm_output['present']
@@ -65,7 +65,7 @@ def body(past, prev, output):
6565

6666
def cond(*args):
6767
return True
68-
68+
6969
_, _, tokens = tf.while_loop(
7070
cond=cond, body=body,
7171
maximum_iterations=length,
@@ -82,4 +82,4 @@ def cond(*args):
8282
back_prop=False,
8383
)
8484

85-
return tokens
85+
return tokens

optimizers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
def create_train_op(loss, params):
66
lr = params["lr"]
77
if "warmup_steps" in params.keys():
8-
lr = cosine_decay_with_warmup(tf.train.get_global_step(), lr,
8+
lr = cosine_decay_with_warmup(tf.train.get_global_step(), lr,
99
params["max_steps"], warmup_steps=params["warmup_steps"])
1010

1111
if params["opt_name"] == "adam":
@@ -51,7 +51,7 @@ def create_train_op(loss, params):
5151

5252
else:
5353
raise ValueError("Unknown optimizer type!")
54-
54+
5555
if params["use_tpu"]:
5656
optimizer = tf.contrib.tpu.CrossShardOptimizer(optimizer)
5757

@@ -352,4 +352,4 @@ def cast_like(x, y):
352352
pass
353353
tf.logging.warning("Cast for %s may induce copy from '%s' to '%s'", x_name,
354354
x.device, cast_x.device)
355-
return cast_x
355+
return cast_x

0 commit comments

Comments
 (0)