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

Avoid BoundSymbol repeating itself in its subsymbols after functionalization #802

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions thunder/core/transform_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,12 @@ def computation(x):
new_bsyms.append(new_functional_bsym)
bsym_inplace_to_functional[new_bsym] = new_functional_bsym

if (
len(new_functional_bsym.subsymbols) == 1
and new_functional_bsym.rhs == new_functional_bsym.subsymbols[0].rhs
Copy link
Contributor

@nikitaved nikitaved Jul 22, 2024

Choose a reason for hiding this comment

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

Could you please explain why name/id/other description not involving explicit construction of BoundSymbolRHS is not sufficient? My rationale: I am biased in favor of using as less of hash(BoundSymbol...) as possible...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I might be just paranoid but I want to make sure args/kwargs are the same, not only sym of them.

):
new_functional_bsym.subsymbols = new_functional_bsym.subsymbols[0].subsymbols

required_copy_bsyms, swap_map_for_return = collect_required_copy_bsyms_for_args(
intermediate_trace,
removed_copy_bsyms,
Expand Down
29 changes: 29 additions & 0 deletions thunder/tests/test_inplace_functionalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,32 @@ def f(a, b):
b_out = jitted(a, b)
torch.testing.assert_close(b_out, b__out)
assert b.data_ptr() == b_out.data_ptr()


@instantiate(
dtypes=NOTHING,
)
def test_no_self_repeat_in_subsymbols(executor, device, _):

def f(a, b, c):
a.add_(b, alpha=c)
return a.add_(b, alpha=c)

def functional_f(a, b, c):
d = a.add(b, alpha=c)
return d.add(b, alpha=c)

a = make_tensor((2, 2), device=device, dtype=torch.float32)
b = make_tensor((2, 2), device=device, dtype=torch.float32)
c = make_tensor((1,), device=device, dtype=torch.float32)

a_out_ref = executor.make_callable(functional_f)(a, b, c)

jitted = executor.make_callable(f)
a_out = jitted(a, b, c)
torch.testing.assert_close(a_out, a_out_ref)

traces = thunder.last_traces(jitted)
for t in filter(lambda t: t._provenance is not None and "Functionalize in-place ops" in t._provenance.pss, traces):
for bsym in filter(lambda b: b.subsymbols, t.bound_symbols):
assert bsym.rhs != bsym.subsymbols[0].rhs, bsym
nikitaved marked this conversation as resolved.
Show resolved Hide resolved
Loading