-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathoss.py
More file actions
299 lines (243 loc) · 9.86 KB
/
Copy pathoss.py
File metadata and controls
299 lines (243 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# Copyright 2020 The SQLFlow Authors. All rights reserved.
# Licensed 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.
import os
import pickle
import sys
import oss2
import tensorflow as tf
from runtime.diagnostics import SQLFlowDiagnostic
from runtime.tensorflow import is_tf_estimator
# NOTE(typhoonzero): hard code bucket name "sqlflow-models" as the bucket to
# save models trained.
SQLFLOW_MODELS_BUCKET = "sqlflow-models"
def remove_bucket_prefix(oss_uri):
return oss_uri.replace("oss://%s/" % SQLFLOW_MODELS_BUCKET, "")
def get_models_bucket():
return get_bucket(SQLFLOW_MODELS_BUCKET)
def get_bucket(name, ak=None, sk=None, endpoint=None):
if ak is None:
ak = os.getenv("SQLFLOW_OSS_AK", "")
if sk is None:
sk = os.getenv("SQLFLOW_OSS_SK", "")
if endpoint is None:
endpoint = os.getenv("SQLFLOW_OSS_MODEL_ENDPOINT", "")
if ak == "" or sk == "":
raise ValueError("must configure SQLFLOW_OSS_AK and SQLFLOW_OSS_SK "
"when submitting to PAI")
if endpoint == "":
raise ValueError(
"must configure SQLFLOW_OSS_MODEL_ENDPOINT when submitting to PAI")
auth = oss2.Auth(ak, sk)
bucket = oss2.Bucket(auth, endpoint, name)
return bucket
def copyfileobj(source, dest, ak, sk, endpoint, bucket_name):
'''
copy_file_to_oss copies alocal file (source) to an object on OSS (dest),
overwrite if the oss object exists.
'''
auth = oss2.Auth(ak, sk)
bucket = oss2.Bucket(auth, endpoint, bucket_name)
bucket.put_object_from_file(dest, source)
def get_oss_path_from_uri(oss_model_dir, file_name):
# oss_model_dir is of format: oss://bucket/path/to/dir/
assert (oss_model_dir.startswith("oss://"))
oss_file_path = "/".join([oss_model_dir.rstrip("/"), file_name])
return oss_file_path
def mkdir(bucket, oss_dir):
assert (oss_dir.startswith("oss://"))
if not oss_dir.endswith("/"):
oss_dir = oss_dir + "/"
path = remove_bucket_prefix(oss_dir)
has_dir = True
try:
bucket.get_object_meta(path)
except oss2.exceptions.NoSuchKey:
has_dir = False
except Exception as e:
raise e
if not has_dir:
bucket.put_object(path, "")
def delete_oss_dir_recursive(bucket, directory):
"""
Recursively delete a directory on the OSS
Args:
bucket: bucket on OSS
directory (str): the directory to delete
Returns:
None.
"""
if not directory.endswith("/"):
raise SQLFlowDiagnostic("dir to delete must end with /")
loc = bucket.list_objects(prefix=directory, delimiter="/")
object_path_list = []
for obj in loc.object_list:
object_path_list.append(obj.key)
# delete sub dir first
if len(loc.prefix_list) > 0:
for sub_prefix in loc.prefix_list:
delete_oss_dir_recursive(bucket, sub_prefix)
# empty list param will raise error
if len(object_path_list) > 0:
bucket.batch_delete_objects(object_path_list)
def save_dir(oss_model_dir, local_dir):
'''
Recursively upload local_dir under oss_model_dir
'''
bucket = get_models_bucket()
for (root, dirs, files) in os.walk(local_dir, topdown=True):
dst_dir = "/".join([oss_model_dir.rstrip("/"), root])
mkdir(bucket, dst_dir)
for file_name in files:
curr_file_path = os.path.join(root, file_name)
remote_file_path = "/".join([dst_dir.rstrip("/"), file_name])
remote_file_path = remove_bucket_prefix(remote_file_path)
bucket.put_object_from_file(remote_file_path, curr_file_path)
def load_dir(oss_model_dir):
bucket = get_models_bucket()
path = remove_bucket_prefix(oss_model_dir)
prefix = "/".join(path.split("/")[:-1]) + "/"
for obj in oss2.ObjectIterator(bucket, prefix=path):
# remove prefix when writing to local, e.g.
# remote: path/to/my/dir/
# local: dir/
if obj.key.endswith("/"):
try:
os.makedirs(obj.key.replace(prefix, ""))
except Exception as e:
sys.stderr.write("mkdir exception: %s\n" % str(e))
else:
bucket.get_object_to_file(obj.key, obj.key.replace(prefix, ""))
def save_file(oss_model_dir, local_file_name, oss_file_name=None):
"""
Save the local file (file_name is a file under current directory)
to OSS directory.
Args:
oss_model_dir (str): the OSS model directory. It is in the format
of oss://bucket/path/to/dir/.
local_file_name (str): the local file path.
oss_file_name (str): the OSS file path to save. If None,
use local_file_name as oss_file_name.
Returns:
None.
"""
if oss_file_name is None:
oss_file_name = local_file_name
bucket = get_models_bucket()
oss_path = get_oss_path_from_uri(oss_model_dir, oss_file_name)
oss_path = remove_bucket_prefix(oss_path)
mkdir(bucket, oss_model_dir)
bucket.put_object_from_file(oss_path, local_file_name)
def save_string(oss_file_path, data):
'''
Save a string into an oss_file_path
'''
bucket = get_models_bucket()
oss_dir = "/".join(oss_file_path.split("/")[:-1])
mkdir(bucket, oss_dir)
oss_file_path = remove_bucket_prefix(oss_file_path)
bucket.put_object(oss_file_path, data)
def load_file(oss_model_dir, local_file_name, oss_file_name=None):
"""
Load file from OSS to local directory.
Args:
oss_model_dir (str): the OSS model directory. It is in the format
of oss://bucket/path/to/dir/.
local_file_name (str): the local file path.
oss_file_name (str): the OSS file path to load. If None,
use local_file_name as oss_file_name.
Returns:
None.
"""
if oss_file_name is None:
oss_file_name = local_file_name
oss_file_path = "/".join([oss_model_dir.rstrip("/"), oss_file_name])
oss_file_path = remove_bucket_prefix(oss_file_path)
bucket = get_models_bucket()
bucket.get_object_to_file(oss_file_path, local_file_name)
def load_string(oss_file_path):
data = load_bytes(oss_file_path)
return data.decode("utf-8")
def load_bytes(oss_file_path):
bucket = get_models_bucket()
oss_file_path = remove_bucket_prefix(oss_file_path)
return bucket.get_object(oss_file_path).read()
def save_metas(oss_model_dir, num_workers, file_name, *meta):
'''
Save model descriptions like the training SQL statements to OSS directory.
Data are saved using pickle.
it will report "can't pickle weakref objects" when using pickle.
Args:
oss_model_dir: OSS URI that the model will be saved to.
*meta: python objects to be saved.
Return:
None
'''
if num_workers > 1:
FLAGS = tf.app.flags.FLAGS
if FLAGS.task_index != 0:
print("skip saving model desc on workers other than worker 0")
return
oss_path = get_oss_path_from_uri(oss_model_dir, file_name)
serialized = pickle.dumps(list(meta))
save_string(oss_path, serialized)
# write a file "file_name_estimator" to store the estimator name,
# so we can determine if the estimator is BoostedTrees* when
# explaining the model.
estimator_file_name = "_".join([file_name, "estimator"])
oss_path = get_oss_path_from_uri(oss_model_dir, estimator_file_name)
save_string(oss_path, meta[0])
def load_metas(oss_model_dir, file_name):
'''Load model meta which are saved by save_metas from OSS
Args:
oss_model_dir: OSS URI that the model meta saved to.
file_name: meta data file name
Returns:
A list contains the saved python objects
'''
oss_path = "/".join([oss_model_dir.rstrip("/"), file_name])
serialized = load_bytes(oss_path)
return pickle.loads(serialized)
def load_oss_model(oss_model_dir, estimator):
is_estimator = is_tf_estimator(estimator)
# Keras single node is using h5 format to save the model, no need to deal
# with export model format. Keras distributed mode will use estimator, so
# this is also needed.
if is_estimator:
load_file(oss_model_dir, "exported_path")
# NOTE(typhoonzero): directory "model_save" is hardcoded in
# codegen/tensorflow/codegen.go
load_dir(os.path.join(oss_model_dir, "model_save"))
def save_oss_model(oss_model_dir, model_name, is_estimator,
feature_column_names, feature_column_names_map,
feature_metas, label_meta, model_params,
feature_columns_code, num_workers):
# Keras single node is using h5 format to save the model, no need to deal
# with export model format. Keras distributed mode will use estimator, so
# this is also needed.
if is_estimator:
with open("exported_path", "rb") as fn:
saved_model_path = fn.read()
if isinstance(saved_model_path, bytes):
saved_model_path = saved_model_path.decode("utf-8")
save_dir(oss_model_dir, saved_model_path)
save_file(oss_model_dir, "exported_path")
else:
if num_workers > 1:
FLAGS = tf.app.flags.FLAGS
if FLAGS.task_index == 0:
save_file(oss_model_dir, "exported_path")
else:
save_dir(oss_model_dir, "model_save")
save_metas(oss_model_dir, num_workers, "tensorflow_model_desc", model_name,
feature_column_names, feature_column_names_map, feature_metas,
label_meta, model_params, feature_columns_code)