8000 [dynamo] Use the new `get_unique_name_wrt` helper when applicable by StrongerXi · Pull Request #146950 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[dynamo] Use the new get_unique_name_wrt helper when applicable #146950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
10000
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions torch/_dynamo/output_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,15 +1542,7 @@ def dedup_pass(self):
return dict()

def install_subgraph(self, name, sub_gm):
next_name = None
i = 0
while not next_name:
candidate = f"{name}_{i}"
if candidate in self.nn_modules:
i += 1
else:
next_name = candidate

next_name = get_unique_name_wrt(name, self.nn_modules, requires_suffix=True)
sub_gm.__name__ = next_name
sub_gm.torchdynamo_force_dynamic = False
# This graph module is not present in the user space, so it can't be
Expand Down Expand Up @@ -2256,14 +2248,7 @@ def create_graph_input(
TracingContext.extract_stack()
)

# unique
if name in self.input_name_to_proxy:
for i in itertools.count():
candidate_name = f"{name}_{i}"
if candidate_name not in self.input_name_to_proxy:
name = candidate_name
break

name = get_unique_name_wrt(name, self.input_name_to_proxy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to get this to always append a number, like before?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to get this to always append a number, like before? I'm not sure that it matters but it's nice for the invariant to be "the name is foobar_i" where i is a number

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah lemme try that, otherwise there's too many tests to update lol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sad -- either way we'll need to update a bunch of test so I added a requires_suffix=False flag to preserve the existing behavior.

if self.input_name_to_proxy:
prev_name = next(reversed(self.input_name_to_proxy))
node = self.input_name_to_proxy[prev_name].node
Expand Down
26 changes: 14 additions & 12 deletions torch/_dynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2574,23 +2574,25 @@ def get_safe_global_name(tx, root, obj):
return f"{root}_{id(obj)}_c{tx.output.compile_id}"


def get_unique_name_wrt(prefix: str, *containers) -> str:
def is_in(item: Any, *containers) -> bool:
for container in containers:
if item in container:
return True
return False


def get_unique_name_wrt(prefix: str, *containers, requires_suffix=False) -> str:
"""
Return a name that starts with `prefix` and is not in any of the
`containers` (e.g., map, set).
"""
name = prefix
if not requires_suffix and not is_in(prefix, *containers):
return prefix

for i in itertools.count():
found = False
for container in containers:
if name in container:
found = True
break

if not found:
return name
# else update and retry
name = f"{prefix}_{i}"
candidate = f"{prefix}_{i}"
if not is_in(candidate, *containers):
return candidate

raise AssertionError("unreachable")

Expand Down
Loading
0