Skip to content

Commit 99730e2

Browse files
committed
fix: refact FutureLen, add FutureRange
Signed-off-by: zjgemi <[email protected]>
1 parent e10caff commit 99730e2

File tree

10 files changed

+53
-118
lines changed

10 files changed

+53
-118
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ _version.py
136136

137137
# vscode
138138
.vscode/
139+
.idea/
139140

140141
# Mac OS
141142
.DS_store

.idea/.gitignore

-8
This file was deleted.

.idea/dflow.iml

-18
This file was deleted.

.idea/inspectionProfiles/profiles_settings.xml

-6
This file was deleted.

.idea/misc.xml

-4
This file was deleted.

.idea/modules.xml

-8
This file was deleted.

.idea/vcs.xml

-6
This file was deleted.

src/dflow/io.py

-35
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Any, Dict, List, Union
66

77
import jsonpickle
8-
from dflow.client.v1alpha1_sequence import V1alpha1Sequence
98

109
from .common import S3Artifact
1110
from .config import config
@@ -527,9 +526,6 @@ def __getattr__(self, key):
527526
(self.step.id, self.name)
528527
return "outputs.parameters['%s']" % self.name
529528
return ""
530-
if key == "value":
531-
res = FutureLen(self)
532-
return res
533529
return super().__getattr__(key)
534530

535531
def __setattr__(self, key, value):
@@ -939,34 +935,3 @@ def convert_to_argo(self):
939935
for art in self.artifacts.values():
940936
artifacts.append(art.convert_to_argo())
941937
return V1alpha1Outputs(parameters=parameters, artifacts=artifacts)
942-
943-
944-
class FutureLen:
945-
'''
946-
to solve problem that Delayed acquisition length about the output parameters
947-
used in debug module
948-
'''
949-
950-
def __init__(self, par):
951-
self.par = par
952-
self.method = []
953-
954-
def get(self):
955-
v = self.par.value
956-
# preventing recursive calls
957-
if isinstance(v, FutureLen):
958-
raise (
959-
"no parameters named values, use get_len after the output from pre_train has return"
960-
)
961-
for i in self.method:
962-
if i == "argo_len":
963-
self.par = len(v)
964-
if "argo_sequence" in i:
965-
start = i.find("{{") + 2
966-
end = i.find("}}", start)
967-
format = i[start:end]
968-
self.par = V1alpha1Sequence(count=self.par, format=format)
969-
return self.par
970-
971-
def set_method(self, method):
972-
self.method.append(method)

src/dflow/python/python_op_template.py

-2
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,6 @@ def render_script(self):
352352

353353
script += "import json\n"
354354
script += "from dflow import config, s3_config\n"
355-
if config["mode"] == "debug":
356-
script += "config['mode'] = 'debug'\n"
357355
script += "config.update(json.loads('%s'))\n" % json.dumps(config)
358356
script += "s3_config.update(json.loads('%s'))\n" % \
359357
json.dumps(s3_config)

src/dflow/step.py

+52-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
from multiprocessing.sharedctypes import Value
32
import re
43
from copy import deepcopy
54
from typing import Any, Dict, List, Union
@@ -10,7 +9,7 @@
109
from .config import config
1110
from .context_syntax import GLOBAL_CONTEXT
1211
from .executor import Executor
13-
from .io import (PVC, ArgoVar, FutureLen, InputArtifact, InputParameter, OutputArtifact,
12+
from .io import (PVC, ArgoVar, InputArtifact, InputParameter, OutputArtifact,
1413
OutputParameter)
1514
from .op_template import OPTemplate, PythonScriptOPTemplate, ShellOPTemplate
1615
from .resource import Resource
@@ -30,6 +29,36 @@
3029
uploaded_python_packages = []
3130

3231

32+
class FutureLen:
33+
'''
34+
To solve the problem of delayed acquisition for length of output
35+
parameters in the debug mode
36+
'''
37+
38+
def __init__(self, par):
39+
self.par = par
40+
41+
def get(self):
42+
return len(self.par.value)
43+
44+
45+
class FutureRange:
46+
def __init__(self, *args):
47+
self.args = args
48+
49+
def get(self):
50+
args = []
51+
for i in self.args:
52+
if isinstance(i, FutureLen):
53+
args.append(i.get())
54+
elif isinstance(i, (InputParameter, OutputParameter)):
55+
args.append(i.value)
56+
else:
57+
args.append(i)
58+
args = tuple(args)
59+
return list(range(*args))
60+
61+
3362
def argo_range(
3463
*args,
3564
) -> ArgoVar:
@@ -40,12 +69,7 @@ def argo_range(
4069
Each argument can be Argo parameter
4170
"""
4271
if config["mode"] == "debug":
43-
args = tuple(i.value if isinstance(i, (InputParameter, OutputParameter
44-
)) else i for i in args)
45-
for i in range(len(args)):
46-
if isinstance(args[i], (InputParameter, OutputParameter)):
47-
args[i] = args[i].value
48-
return list(range(*args))
72+
return FutureRange(*args)
4973
start = 0
5074
step = 1
5175
if len(args) == 1:
@@ -86,19 +110,13 @@ def argo_sequence(
86110
with count, can be an Argo parameter
87111
format: a printf format string to format the value in the sequence
88112
"""
89-
if isinstance(count,FutureLen):
90-
method = argo_sequence.__name__
91-
if format != None:
92-
#the writing style may not be compliant
93-
method = method + ".{{%s}}" % format
94-
count.set_method(method=method)
95-
return count
96-
if isinstance(count, ArgoVar):
97-
count = "{{=%s}}" % count.expr
98-
if isinstance(start, ArgoVar):
99-
start = "{{=%s}}" % start.expr
100-
if isinstance(end, ArgoVar):
101-
end = "{{=%s}}" % end.expr
113+
if config["mode"] != "debug":
114+
if isinstance(count, ArgoVar):
115+
count = "{{=%s}}" % count.expr
116+
if isinstance(start, ArgoVar):
117+
start = "{{=%s}}" % start.expr
118+
if isinstance(end, ArgoVar):
119+
end = "{{=%s}}" % end.expr
102120
return V1alpha1Sequence(count=count, start=start, end=end, format=format)
103121

104122

@@ -112,11 +130,7 @@ def argo_len(
112130
param: the Argo parameter which is a list
113131
"""
114132
if config["mode"] == "debug":
115-
par = param.value
116-
if isinstance(par,FutureLen):
117-
par.set_method(argo_len.__name__)
118-
return par
119-
return len(par)
133+
return FutureLen(param)
120134
if isinstance(param, S3Artifact):
121135
try:
122136
path_list = catalog_of_artifact(param)
@@ -533,8 +547,7 @@ def __init__(
533547
self.inputs.parameters["dflow_with_param"] = InputParameter(
534548
value=self.with_param)
535549
self.with_param = None
536-
if self.with_sequence is not None and \
537-
not isinstance(self.with_sequence,FutureLen):
550+
if self.with_sequence is not None:
538551
if self.with_sequence.start is not None:
539552
steps.inputs.parameters["dflow_sequence_start"] = \
540553
InputParameter()
@@ -683,6 +696,7 @@ def run(self, context):
683696

684697
import os
685698
from copy import copy
699+
686700
from .dag import DAG
687701
from .steps import Steps
688702

@@ -750,25 +764,32 @@ def run(self, context):
750764
return
751765

752766
if self.with_param is not None or self.with_sequence is not None:
753-
if isinstance(self.with_param, (InputParameter, OutputParameter)):
767+
if isinstance(self.with_param, FutureRange):
768+
item_list = self.with_param.get()
769+
elif isinstance(self.with_param, (InputParameter,
770+
OutputParameter)):
754771
item_list = self.with_param.value
755772
elif isinstance(self.with_param, list):
756773
item_list = self.with_param
757774
elif self.with_sequence is not None:
758-
if isinstance(self.with_sequence,FutureLen):
759-
self.with_sequence = self.with_sequence.get()
760775
start = 0
761776
if self.with_sequence.start is not None:
762777
start = self.with_sequence.start
778+
if isinstance(start, FutureLen):
779+
start = start.get()
763780
if isinstance(start, (InputParameter, OutputParameter)):
764781
start = start.value
765782
if self.with_sequence.count is not None:
766783
count = self.with_sequence.count
784+
if isinstance(count, FutureLen):
785+
count = count.get()
767786
if isinstance(count, (InputParameter, OutputParameter)):
768787
count = count.value
769788
sequence = list(range(start, start + count))
770789
if self.with_sequence.end is not None:
771790
end = self.with_sequence.end
791+
if isinstance(end, FutureLen):
792+
end = end.get()
772793
if isinstance(end, (InputParameter, OutputParameter)):
773794
end = end.value
774795
sequence = list(range(start, end + 1))

0 commit comments

Comments
 (0)