Skip to content
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

Add epilogue lambda guards #67

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Changes from all commits
Commits
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
28 changes: 19 additions & 9 deletions depyf/explain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
if not cpp_guard:
# for old version of pytorch,
# `guard_manager` is a plain python function
guard = guard_manager.code_parts
guard_codes = guard_manager.code_parts

Check warning on line 112 in depyf/explain/utils.py

View check run for this annotation

Codecov / codecov/patch

depyf/explain/utils.py#L112

Added line #L112 was not covered by tests
freevar_names = guard_manager.__code__.co_freevars
freevar_values = [x.cell_contents for x in guard_manager.__closure__]
else:
Expand All @@ -118,28 +118,33 @@
tensor_aliasing_guard_seen = False
def visit(root, ans):
nonlocal tensor_aliasing_guard_seen
for x in root.get_leaf_guards():
if isinstance(guard, torch._C._dynamo.guards.NO_TENSOR_ALIASING):
for leaf_guard in root.get_leaf_guards():
if isinstance(guard_codes, torch._C._dynamo.guards.NO_TENSOR_ALIASING):
if not tensor_aliasing_guard_seen:
tensor_aliasing_guard_seen = True
else:
continue
for verbose_str in x.verbose_code_parts():
verbose_str = verbose_str.strip()
ans.append(verbose_str)
append_guard_code(leaf_guard, ans)
for child in root.get_child_managers():
visit(child, ans)
guard = []
guard_codes = []
root = guard_manager.root
visit(root, guard)

# Add guards in RootGuardManager
visit(root, guard_codes)
# Add guards in epilogue lambda guards
if hasattr(root, "get_epilogue_lambda_guards"):
for lambda_guard in root.get_epilogue_lambda_guards():
append_guard_code(lambda_guard, guard_codes)

if guard_manager.closure_vars is None:
freevar_names = tuple()
freevar_values = []
else:
freevar_names = tuple(guard_manager.closure_vars.keys())
freevar_values = list(guard_manager.closure_vars.values())

self.guard = guard
self.guard = guard_codes
self.freevars = {name: value for name, value in zip(freevar_names, freevar_values)}
code = cache.code

Expand Down Expand Up @@ -285,6 +290,11 @@
indent = len(lines[0]) - len(lines[0].lstrip())
return "".join([line[indent:] + "\n" for line in lines])

def append_guard_code(guard, ans):
for verbose_str in guard.verbose_code_parts():
verbose_str = verbose_str.strip()
ans.append(verbose_str)

from contextlib import contextmanager

@contextmanager
Expand Down