Skip to content

Commit 844989e

Browse files
authored
refine codes in runtime (#2765)
1 parent 1970b41 commit 844989e

11 files changed

Lines changed: 172 additions & 151 deletions

File tree

python/runtime/alps/train.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,17 @@
1616

1717
import os
1818

19-
import tensorflow as tf
2019
from alps.client.base import run_experiment, submit_experiment
21-
from alps.conf.closure import Closure
22-
from alps.framework.column.column import (DenseColumn, GroupedSparseColumn,
23-
SparseColumn)
2420
from alps.framework.engine import (KubemakerEngine, LocalEngine, ResourceConf,
2521
YarnEngine)
26-
from alps.framework.experiment import (EstimatorBuilder, EvalConf, Experiment,
27-
RuntimeConf, TrainConf)
22+
from alps.framework.experiment import (EvalConf, Experiment, RuntimeConf,
23+
TrainConf)
2824
from alps.framework.exporter import ExportStrategy
2925
from alps.framework.exporter.arks_exporter import ArksExporter
3026
from alps.framework.exporter.base import Goal, MetricComparator
31-
from alps.framework.train.training import build_run_config
3227
from alps.io import DatasetX
33-
from alps.io.base import FeatureMap, OdpsConf
28+
from alps.io.base import FeatureMap
3429
from alps.io.reader.odps_reader import OdpsReader
35-
from alps.util.remote_module import RemoteModule
36-
from tensorflow.python.saved_model.signature_constants import \
37-
DEFAULT_SERVING_SIGNATURE_DEF_KEY
3830

3931
# for debug usage.
4032
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
@@ -109,7 +101,7 @@ def train(model_builder,
109101
else:
110102
runtime_conf = None
111103

112-
if max_steps == None:
104+
if max_steps is None:
113105
keep_checkpoint_max = 100
114106
else:
115107
keep_checkpoint_max = int(max_steps / 100)

python/runtime/alps/train_example.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import shutil
1616

1717
import tensorflow as tf
18-
from alps.framework.column.column import (DenseColumn, GroupedSparseColumn,
19-
SparseColumn)
20-
from alps.framework.engine import LocalEngine
18+
from alps.framework.column.column import DenseColumn, SparseColumn
2119
from alps.framework.experiment import EstimatorBuilder
2220
from alps.io.base import OdpsConf
2321
from runtime.alps.train import train
@@ -65,7 +63,8 @@ def _build(self, experiment, run_config):
6563
odps_conf = OdpsConf(
6664
accessid=os.getenv("SQLFLOW_TEST_DB_MAXCOMPUTE_AK"),
6765
accesskey=os.getenv("SQLFLOW_TEST_DB_MAXCOMPUTE_SK"),
68-
# endpoint should looks like: "https://service.cn.maxcompute.aliyun.com/api"
66+
# endpoint should looks like:
67+
# "https://service.cn.maxcompute.aliyun.com/api"
6968
endpoint=os.getenv("SQLFLOW_TEST_DB_MAXCOMPUTE_ENDPOINT"),
7069
project=odps_project)
7170
features = [

python/runtime/alps/train_test.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
import os
1514
import subprocess
1615
import unittest
1716
from unittest import TestCase
@@ -20,8 +19,11 @@
2019

2120

2221
class TestALPSTrain(TestCase):
23-
'''NOTE: we must test tensorflow training and predicting in separated processes, or
24-
TensorFlow will raise error "Graph is finalized.'''
22+
'''
23+
NOTE: we must test tensorflow training and predicting
24+
in separated processes, or TensorFlow will raise error
25+
"Graph is finalized.
26+
'''
2527
@unittest.skipUnless(testing.get_submitter() == "alps",
2628
"skip non alps tests")
2729
def test_train(self):
@@ -33,7 +35,7 @@ def test_train(self):
3335
env={"PYTHONPATH": "python"},
3436
check=True)
3537
self.assertEqual(ret.returncode, 0)
36-
except:
38+
except: # noqa: E722
3739
self.fail("%s" % ret.stderr)
3840

3941

python/runtime/local/xgboost/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
from runtime.local.xgboost.train import train
14+
from runtime.local.xgboost.train import train # noqa: F401

python/runtime/local/xgboost/train_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from runtime.xgboost.dataset import xgb_dataset
2222

2323
# iris dataset features meta
24-
# TODO(yancey1989): implement runtime.feature_derivation API to generate the following
25-
# feature metas
24+
# TODO(yancey1989): implement runtime.feature_derivation API to generate
25+
# the following feature metas
2626
feature_metas = {
2727
"sepal_length": {
2828
"feature_name": "sepal_length",

python/runtime/optimize/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
from runtime.optimize.optflow import run_optimize_on_optflow
14+
from runtime.optimize.optflow import run_optimize_on_optflow # noqa: F401
1515

1616
# Step images which submit job to OptFlow may not require pyomo module
1717
try:
18-
from runtime.optimize.local import (generate_model_with_data_frame,
19-
run_optimize_locally)
20-
except:
18+
from runtime.optimize.local import generate_model_with_data_frame # noqa: F401, E501
19+
from runtime.optimize.local import run_optimize_locally # noqa: F401, E501
20+
except: # noqa: E722
2121
pass

python/runtime/optimize/local.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@
1919
import runtime.db as db
2020
import runtime.verifier as verifier
2121
import six
22-
from pyomo.environ import (Binary, Integers, NegativeIntegers, NegativeReals,
23-
NonNegativeIntegers, NonNegativeReals,
24-
NonPositiveIntegers, NonPositiveReals,
25-
PositiveIntegers, PositiveReals, Reals, maximize,
26-
minimize)
2722
from runtime.optimize.model_generation import (
28-
generate_objective_and_constraint_expression,
29-
generate_unique_result_value_name)
23+
generate_objective_and_constraint_expr, generate_unique_result_value_name)
3024

3125
# FIXME(sneaxiy): do not know why Pyomo requires that the data frame must be
3226
# a global variable
@@ -47,25 +41,35 @@ def generate_model_with_data_frame(data_frame, variables, variable_type,
4741
result_value_name (str): the result value name to be optimized.
4842
objective (list[str]): the objective string token list.
4943
direction (str): "maximize" or "minimize".
50-
constraints (dict): the constraint expression containing the token list and GROUP BY column name.
44+
constraints (dict): the constraint expression containing the token list
45+
and GROUP BY column name.
5146
5247
Returns:
5348
A Pyomo ConcreteModel.
5449
"""
5550
direction = direction.lower()
56-
if direction not in ['maximize', 'minimize']:
51+
if direction == 'maximize':
52+
direction = pyomo_env.maximize
53+
elif direction == 'minimize':
54+
direction = pyomo_env.minimize
55+
else:
5756
raise ValueError("direction must be one of 'maximize' or 'minimize'")
5857

58+
if not hasattr(pyomo_env, variable_type):
59+
raise ValueError("cannot find variable type %s" % variable_type)
60+
61+
variable_type = getattr(pyomo_env, variable_type)
62+
5963
model = pyomo_env.ConcreteModel()
6064
var_num = len(data_frame)
61-
model.x = pyomo_env.Var(list(range(var_num)), within=eval(variable_type))
65+
model.x = pyomo_env.Var(list(range(var_num)), within=variable_type)
6266

6367
columns = data_frame.columns
6468

6569
variable_str = "model.x"
6670
data_str = "DATA_FRAME"
6771

68-
obj_expr, c_exprs = generate_objective_and_constraint_expression(
72+
obj_expr, c_exprs = generate_objective_and_constraint_expr(
6973
columns=columns,
7074
objective=objective,
7175
constraints=constraints,
@@ -79,21 +83,23 @@ def generate_model_with_data_frame(data_frame, variables, variable_type,
7983
global DATA_FRAME
8084
DATA_FRAME = data_frame
8185
obj_func = eval("lambda model: %s" % obj_expr)
82-
model.objective = pyomo_env.Objective(rule=obj_func,
83-
sense=eval(direction))
86+
model.objective = pyomo_env.Objective(rule=obj_func, sense=direction)
8487

8588
for i, (expr, for_range, iter_vars) in enumerate(c_exprs):
8689
attr_name = "constraint_%d" % i
8790

8891
if for_range:
89-
assert iter_vars, "for_range and iter_vars must be both non-empty"
92+
assert iter_vars, "for_range and iter_vars must be " \
93+
"both non-empty"
9094
setattr(model, attr_name, pyomo_env.ConstraintList())
9195
constraint_list = getattr(model, attr_name)
92-
add_constraint_str = "lambda model, constraint_list: [constraint_list.add(%s) for %s in %s]" % (
93-
expr, ",".join(iter_vars), for_range)
96+
template = "lambda model, constraint_list: [constraint_list.add(%s) for %s in %s]" # noqa: E501
97+
add_constraint_str = template % (expr, ",".join(iter_vars),
98+
for_range)
9499
eval(add_constraint_str)(model, constraint_list)
95100
else:
96-
assert not iter_vars, "for_range and iter_vars must be both empty"
101+
assert not iter_vars, \
102+
"for_range and iter_vars must be both empty"
97103
func = eval('lambda model: %s' % expr)
98104
constraint = pyomo_env.Constraint(rule=func)
99105
setattr(model, attr_name, constraint)
@@ -137,8 +143,8 @@ def solve_model(model, solver):
137143
if pyomo_dtype is None:
138144
pyomo_dtype = type(model.x[idx])
139145

140-
assert pyomo_dtype == type(
141-
model.x[idx]), "all variables must be of the same data type"
146+
assert isinstance(model.x[idx], pyomo_dtype), \
147+
"all variables must be of the same data type"
142148

143149
if has_error:
144150
msg = 'Solve model error. Termination condition: {}.'\
@@ -187,7 +193,8 @@ def save_solved_result_in_db(solved_result, data_frame, variables,
187193
Save the solved result of the Pyomo model into the database.
188194
189195
Args:
190-
solved_result (numpy.ndarray): a numpy array which indicates the solved result.
196+
solved_result (numpy.ndarray): a numpy array which indicates
197+
the solved result.
191198
data_frame (panda.DataFrame): the input table data.
192199
variables (list[str]): the variable names to be optimized.
193200
result_value_name (str): the result value name to be optimized.
@@ -244,7 +251,8 @@ def run_optimize_locally(datasource, select, variables, variable_type,
244251
result_value_name (str): the result value name to be optimized.
245252
objective (list[str]): the objective string token list.
246253
direction (str): "maximize" or "minimize".
247-
constraints (dict): the constraint expression containing the token list and GROUP BY column name.
254+
constraints (dict): the constraint expression containing the token list
255+
and GROUP BY column name.
248256
solver (str): the solver used to solve the model.
249257
result_table (str): the table name to save the solved results.
250258

0 commit comments

Comments
 (0)