티스토리 뷰
Deep Learning
2. Neural Networks / L4. GPU Workspaces Demo - GPU Workspaces for iterator wrapper
chrisysl 2018. 7. 18. 19:28GPU Workspaces for iterator wrapper
- GPU 사용 환경에서 지속적으로 세션을 유지할 경우 아래의 두 명령어를 이용하여 유지할 수 있음.
import signal from contextlib import contextmanager import requests DELAY = INTERVAL = 4 * 60 # interval time in seconds MIN_DELAY = MIN_INTERVAL = 2 * 60 KEEPALIVE_URL = "https://nebula.udacity.com/api/v1/remote/keep-alive" TOKEN_URL = "http://metadata.google.internal/computeMetadata/v1/instance/attributes/keep_alive_token" TOKEN_HEADERS = {"Metadata-Flavor":"Google"} def _request_handler(headers): def _handler(signum, frame): requests.request("POST", KEEPALIVE_URL, headers=headers) return _handler @contextmanager def active_session(delay=DELAY, interval=INTERVAL): """ Example: from workspace_utils import active session with active_session(): # do long-running work here """ token = requests.request("GET", TOKEN_URL, headers=TOKEN_HEADERS).text headers = {'Authorization': "STAR " + token} delay = max(delay, MIN_DELAY) interval = max(interval, MIN_INTERVAL) original_handler = signal.getsignal(signal.SIGALRM) try: signal.signal(signal.SIGALRM, _request_handler(headers)) signal.setitimer(signal.ITIMER_REAL, delay, interval) yield finally: signal.signal(signal.SIGALRM, original_handler) signal.setitimer(signal.ITIMER_REAL, 0) def keep_awake(iterable, delay=DELAY, interval=INTERVAL): """ Example: from workspace_utils import keep_awake for i in keep_awake(range(5)): # do iteration with lots of work here """ with active_session(delay, interval): yield from iterable
- active_session 과 keep_awake 는 동일한 역할을 함 둘중에 더 편한걸 사용하면 되고,
- 긴 연산이 필요한경우 해당 함수 내에 처리시켜주면 됨. (네트워크의 결과를 도출해내는경우 등..)
'Deep Learning' 카테고리의 다른 글
댓글