Spaces:
Sleeping
Sleeping
Delete submission/run_context.py
Browse files- submission/run_context.py +0 -110
submission/run_context.py
DELETED
@@ -1,110 +0,0 @@
|
|
1 |
-
# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.
|
2 |
-
#
|
3 |
-
# This work is made available under the Nvidia Source Code License-NC.
|
4 |
-
# To view a copy of this license, visit
|
5 |
-
# https://nvlabs.github.io/stylegan2/license.html
|
6 |
-
|
7 |
-
"""Helpers for managing the run/training loop."""
|
8 |
-
|
9 |
-
import datetime
|
10 |
-
import json
|
11 |
-
import os
|
12 |
-
import pprint
|
13 |
-
import time
|
14 |
-
import types
|
15 |
-
|
16 |
-
from typing import Any
|
17 |
-
|
18 |
-
from . import submit
|
19 |
-
|
20 |
-
# Singleton RunContext
|
21 |
-
_run_context = None
|
22 |
-
|
23 |
-
class RunContext(object):
|
24 |
-
"""Helper class for managing the run/training loop.
|
25 |
-
|
26 |
-
The context will hide the implementation details of a basic run/training loop.
|
27 |
-
It will set things up properly, tell if run should be stopped, and then cleans up.
|
28 |
-
User should call update periodically and use should_stop to determine if run should be stopped.
|
29 |
-
|
30 |
-
Args:
|
31 |
-
submit_config: The SubmitConfig that is used for the current run.
|
32 |
-
config_module: (deprecated) The whole config module that is used for the current run.
|
33 |
-
"""
|
34 |
-
|
35 |
-
def __init__(self, submit_config: submit.SubmitConfig, config_module: types.ModuleType = None):
|
36 |
-
global _run_context
|
37 |
-
# Only a single RunContext can be alive
|
38 |
-
assert _run_context is None
|
39 |
-
_run_context = self
|
40 |
-
self.submit_config = submit_config
|
41 |
-
self.should_stop_flag = False
|
42 |
-
self.has_closed = False
|
43 |
-
self.start_time = time.time()
|
44 |
-
self.last_update_time = time.time()
|
45 |
-
self.last_update_interval = 0.0
|
46 |
-
self.progress_monitor_file_path = None
|
47 |
-
|
48 |
-
# vestigial config_module support just prints a warning
|
49 |
-
if config_module is not None:
|
50 |
-
print("RunContext.config_module parameter support has been removed.")
|
51 |
-
|
52 |
-
# write out details about the run to a text file
|
53 |
-
self.run_txt_data = {"task_name": submit_config.task_name, "host_name": submit_config.host_name, "start_time": datetime.datetime.now().isoformat(sep=" ")}
|
54 |
-
with open(os.path.join(submit_config.run_dir, "run.txt"), "w") as f:
|
55 |
-
pprint.pprint(self.run_txt_data, stream=f, indent=4, width=200, compact=False)
|
56 |
-
|
57 |
-
def __enter__(self) -> "RunContext":
|
58 |
-
return self
|
59 |
-
|
60 |
-
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
|
61 |
-
self.close()
|
62 |
-
|
63 |
-
def update(self, loss: Any = 0, cur_epoch: Any = 0, max_epoch: Any = None) -> None:
|
64 |
-
"""Do general housekeeping and keep the state of the context up-to-date.
|
65 |
-
Should be called often enough but not in a tight loop."""
|
66 |
-
assert not self.has_closed
|
67 |
-
|
68 |
-
self.last_update_interval = time.time() - self.last_update_time
|
69 |
-
self.last_update_time = time.time()
|
70 |
-
|
71 |
-
if os.path.exists(os.path.join(self.submit_config.run_dir, "abort.txt")):
|
72 |
-
self.should_stop_flag = True
|
73 |
-
|
74 |
-
def should_stop(self) -> bool:
|
75 |
-
"""Tell whether a stopping condition has been triggered one way or another."""
|
76 |
-
return self.should_stop_flag
|
77 |
-
|
78 |
-
def get_time_since_start(self) -> float:
|
79 |
-
"""How much time has passed since the creation of the context."""
|
80 |
-
return time.time() - self.start_time
|
81 |
-
|
82 |
-
def get_time_since_last_update(self) -> float:
|
83 |
-
"""How much time has passed since the last call to update."""
|
84 |
-
return time.time() - self.last_update_time
|
85 |
-
|
86 |
-
def get_last_update_interval(self) -> float:
|
87 |
-
"""How much time passed between the previous two calls to update."""
|
88 |
-
return self.last_update_interval
|
89 |
-
|
90 |
-
def close(self) -> None:
|
91 |
-
"""Close the context and clean up.
|
92 |
-
Should only be called once."""
|
93 |
-
if not self.has_closed:
|
94 |
-
# update the run.txt with stopping time
|
95 |
-
self.run_txt_data["stop_time"] = datetime.datetime.now().isoformat(sep=" ")
|
96 |
-
with open(os.path.join(self.submit_config.run_dir, "run.txt"), "w") as f:
|
97 |
-
pprint.pprint(self.run_txt_data, stream=f, indent=4, width=200, compact=False)
|
98 |
-
self.has_closed = True
|
99 |
-
|
100 |
-
# detach the global singleton
|
101 |
-
global _run_context
|
102 |
-
if _run_context is self:
|
103 |
-
_run_context = None
|
104 |
-
|
105 |
-
@staticmethod
|
106 |
-
def get():
|
107 |
-
import dnnlib
|
108 |
-
if _run_context is not None:
|
109 |
-
return _run_context
|
110 |
-
return RunContext(dnnlib.submit_config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|