@@ -1003,14 +1003,14 @@ def applys_between(
1003
1003
def truncated_graph_inputs (
1004
1004
outputs : Sequence [Variable ],
1005
1005
ancestors_to_include : Optional [Collection [Variable ]] = None ,
1006
- ) -> List [Variable ]:
1006
+ ) -> list [Variable ]:
1007
1007
"""Get the truncate graph inputs.
1008
1008
1009
1009
Unlike :func:`graph_inputs` this function will return
1010
- the closest nodes to outputs that do not depend on
1010
+ the closest variables to outputs that do not depend on
1011
1011
``ancestors_to_include``. So given all the returned
1012
- variables provided there is no missing node to
1013
- compute the output and all nodes are independent
1012
+ variables provided there is no missing variable to
1013
+ compute the output and all variables are independent
1014
1014
from each other.
1015
1015
1016
1016
Parameters
@@ -1027,7 +1027,7 @@ def truncated_graph_inputs(
1027
1027
1028
1028
Examples
1029
1029
--------
1030
- The returned nodes marked in (parenthesis), ancestors nodes are ``c``, output nodes are ``o``
1030
+ The returned variables marked in (parenthesis), ancestors variables are ``c``, output variables are ``o``
1031
1031
1032
1032
* No ancestors to include
1033
1033
@@ -1047,7 +1047,7 @@ def truncated_graph_inputs(
1047
1047
1048
1048
(c) - (c) - o
1049
1049
1050
- * Additional nodes are present
1050
+ * Additional variables are present
1051
1051
1052
1052
.. code-block::
1053
1053
@@ -1076,58 +1076,60 @@ def truncated_graph_inputs(
1076
1076
1077
1077
"""
1078
1078
# simple case, no additional ancestors to include
1079
- truncated_inputs = list ()
1080
- # blockers have known independent nodes and ancestors to include
1079
+ truncated_inputs : list [ Variable ] = list ()
1080
+ # blockers have known independent variables and ancestors to include
1081
1081
candidates = list (outputs )
1082
1082
if not ancestors_to_include : # None or empty
1083
1083
# just filter out unique variables
1084
- for node in candidates :
1085
- if node not in truncated_inputs :
1086
- truncated_inputs .append (node )
1084
+ for variable in candidates :
1085
+ if variable not in truncated_inputs :
1086
+ truncated_inputs .append (variable )
1087
1087
# no more actions are needed
1088
1088
return truncated_inputs
1089
1089
1090
- blockers : Set [Variable ] = set (ancestors_to_include )
1091
- # enforce O(1) check for node in ancestors to include
1090
+ blockers : set [Variable ] = set (ancestors_to_include )
1091
+ # variables that go here are under check already, do not repeat the loop for them
1092
+ seen : set [Variable ] = set ()
1093
+ # enforce O(1) check for variable in ancestors to include
1092
1094
ancestors_to_include = blockers .copy ()
1093
1095
1094
1096
while candidates :
1095
1097
# on any new candidate
1096
- node = candidates .pop ()
1097
-
1098
- # There was a repeated reference to this node, we have already investigated it
1099
- if node in truncated_inputs :
1098
+ variable = candidates .pop ()
1099
+ # we've looked into this variable already
1100
+ if variable in seen :
1100
1101
continue
1101
-
1102
- # check if the node is independent, never go above blockers;
1103
- # blockers are independent nodes and ancestors to include
1104
- if node in ancestors_to_include :
1105
- # The case where node is in ancestors to include so we check if it depends on others
1102
+ # check if the variable is independent, never go above blockers;
1103
+ # blockers are independent variables and ancestors to include
1104
+ elif variable in ancestors_to_include :
1105
+ # The case where variable is in ancestors to include so we check if it depends on others
1106
1106
# it should be removed from the blockers to check against the rest
1107
- dependent = variable_depends_on (node , ancestors_to_include - {node })
1107
+ dependent = variable_depends_on (variable , ancestors_to_include - {variable })
1108
1108
# ancestors to include that are present in the graph (not disconnected)
1109
1109
# should be added to truncated_inputs
1110
- truncated_inputs .append (node )
1110
+ truncated_inputs .append (variable )
1111
1111
if dependent :
1112
1112
# if the ancestors to include is still dependent we need to go above, the search is not yet finished
1113
- # owner can never be None for a dependent node
1114
- candidates .extend (node .owner .inputs )
1113
+ # owner can never be None for a dependent variable
1114
+ candidates .extend (n for n in variable .owner .inputs if n not in seen )
1115
1115
else :
1116
- # A regular node to check
1117
- dependent = variable_depends_on (node , blockers )
1118
- # all regular nodes fall to blockers
1116
+ # A regular variable to check
1117
+ dependent = variable_depends_on (variable , blockers )
1118
+ # all regular variables fall to blockers
1119
1119
# 1. it is dependent - further search irrelevant
1120
- # 2. it is independent - the search node is inside the closure
1121
- blockers .add (node )
1122
- # if we've found an independent node and it is not in blockers so far
1123
- # it is a new independent node not present in ancestors to include
1120
+ # 2. it is independent - the search variable is inside the closure
1121
+ blockers .add (variable )
1122
+ # if we've found an independent variable and it is not in blockers so far
1123
+ # it is a new independent variable not present in ancestors to include
1124
1124
if dependent :
1125
- # populate search if it's not an independent node
1126
- # owner can never be None for a dependent node
1127
- candidates .extend (node .owner .inputs )
1125
+ # populate search if it's not an independent variable
1126
+ # owner can never be None for a dependent variable
1127
+ candidates .extend (n for n in variable .owner .inputs if n not in seen )
1128
1128
else :
1129
1129
# otherwise, do not search beyond
1130
- truncated_inputs .append (node )
1130
+ truncated_inputs .append (variable )
1131
+ # add variable to seen, no point in checking it once more
1132
+ seen .add (variable )
1131
1133
return truncated_inputs
1132
1134
1133
1135
0 commit comments