Skip to content

Commit 6d16171

Browse files
authored
Write reward_stat_map atomically (#233)
Write reward_stat_map to separate file and atomically rename to avoid data corruption when backing up an instance/snapshots while a new version of the file is written.
1 parent 4cbdf42 commit 6d16171

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

compiler_opt/rl/train_locally.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ def sequence_example_iterator_fn(seq_ex: List[str]):
164164
t2 = time.time()
165165
logging.info('Last iteration took: %f', t2 - t1)
166166
t1 = t2
167-
with tf.io.gfile.GFile(reward_stat_map_path, 'w') as f:
167+
# Save reward stat map. Writes to separate file and renames to get
168+
# "atomic" updates.
169+
temp_path = f'{reward_stat_map_path}.new'
170+
with tf.io.gfile.GFile(temp_path, 'w') as f:
168171
json.dump(reward_stat_map, f, cls=constant.DataClassJSONEncoder)
172+
tf.io.gfile.rename(temp_path, reward_stat_map_path, overwrite=True)
169173

170174
if best_trajectory_repo is not None:
171175
best_trajectory_repo.sink_to_json_file(best_trajectory_repo_path)

0 commit comments

Comments
 (0)