This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] support load model/trainer states from s3 (#702)
* support load model/trainer states from s3 * update training script * bug fix * fix lint * rename * move functions to utils/data.py * fix lint and rename * fix import * fix lint * make lint happy..
- Loading branch information
1 parent
0cdb38d
commit e2b2766
Showing
8 changed files
with
196 additions
and
119 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
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
This file was deleted.
Oops, something went wrong.
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,90 @@ | ||
# coding: utf-8 | ||
|
||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint:disable=redefined-outer-name,logging-format-interpolation | ||
"""Utility functions for files.""" | ||
|
||
__all__ = ['mkdir'] | ||
|
||
import os | ||
import warnings | ||
import logging | ||
import tempfile | ||
from .. import _constants as C | ||
|
||
def mkdir(dirname): | ||
"""Create directory. | ||
Parameters | ||
---------- | ||
dirname : str | ||
The name of the target directory to create. | ||
""" | ||
if C.S3_PREFIX in dirname: | ||
warnings.warn('Directory %s is not created because it contains %s' | ||
%(dirname, C.S3_PREFIX)) | ||
return | ||
dirname = os.path.expanduser(dirname) | ||
if not os.path.exists(dirname): | ||
try: | ||
os.makedirs(dirname) | ||
except OSError as e: | ||
# errno 17 means the file already exists | ||
if e.errno != 17: | ||
raise e | ||
|
||
class _TempFilePath(object): | ||
"""A TempFilePath that provides a path to a temporarily file, and automatically | ||
cleans up the temp file at exit. | ||
""" | ||
def __init__(self): | ||
self.temp_dir = os.path.join(tempfile.gettempdir(), str(hash(os.times()))) | ||
if not os.path.exists(self.temp_dir): | ||
os.makedirs(self.temp_dir) | ||
|
||
def __enter__(self): | ||
self.temp_path = os.path.join(self.temp_dir, str(hash(os.times()))) | ||
return self.temp_path | ||
|
||
def __exit__(self, exec_type, exec_value, traceback): | ||
os.remove(self.temp_path) | ||
|
||
def _transfer_file_s3(filename, s3_filename, upload=True): | ||
"""Transfer a file between S3 and local file system.""" | ||
try: | ||
import boto3 | ||
except ImportError: | ||
raise ImportError('boto3 is required to support s3 URI. Please install' | ||
'boto3 via `pip install boto3`') | ||
# parse s3 uri | ||
prefix_len = len(C.S3_PREFIX) | ||
bucket_idx = s3_filename[prefix_len:].index('/') + prefix_len | ||
bucket_name = s3_filename[prefix_len:bucket_idx] | ||
|
||
# filename after the bucket, excluding '/' | ||
key_name = s3_filename[bucket_idx + 1:] | ||
|
||
log_level = logging.getLogger().getEffectiveLevel() | ||
logging.getLogger().setLevel(logging.INFO) | ||
# upload to s3 | ||
s3 = boto3.client('s3') | ||
if upload: | ||
s3.upload_file(filename, bucket_name, key_name) | ||
else: | ||
s3.download_file(bucket_name, key_name, filename) | ||
logging.getLogger().setLevel(log_level) |
Oops, something went wrong.