Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
13 changes: 10 additions & 3 deletions thunder/core/update_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def insert_alias_updates(computation_trace: Trace, alias_tensor_indices: list[li
# Third pass: insert alias updates
for bsym in computation_trace.bound_symbols:
if _is_inplace_op(bsym) or _is_view_creation_op(bsym) or _involves_viewed_args(bsym, viewed):
bsym = bsym.from_bsym_swap_proxies(swap_map, skip_output=True)
in_tensors = list(map(variableify, filter(lambda p: isinstance(p, TensorProxy), bsym.flat_proxy_args)))
if _is_inplace_op(bsym) and in_tensors:
in_tensors = {in_tensors[0]}
Expand All @@ -175,9 +176,15 @@ def insert_alias_updates(computation_trace: Trace, alias_tensor_indices: list[li
out_tensors = set(map(variableify, filter(lambda p: isinstance(p, TensorProxy), bsym.flat_proxy_outs)))
encountered.update(in_tensors)
group = set(reduce(set.union, filter(lambda g: any(g.intersection(in_tensors)), view_groups), set()))
if not group or not (views_encountered := group.intersection(encountered)):
# If group is empty, this is a view creation with operands that are not involved in any inplace ops.
bsyms.append(bsym.from_bsym_swap_proxies(swap_map, skip_output=True))
views_encountered = group.intersection(encountered)

if _is_inplace_op(bsym):
# This is a hack to insert fusion break because nvFuser doesn't support mutation on intermediates
views_encountered.update(in_tensors)

if not views_encountered:
Comment thread
shino16 marked this conversation as resolved.
# This is a view creation with operands that are not involved in any inplace ops.
bsyms.append(bsym)
continue

new_aliases = _get_new_aliases(views_encountered, computation_trace)
Expand Down
30 changes: 30 additions & 0 deletions thunder/tests/test_update_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,33 @@ def f(x, y, z):
torch.testing.assert_close(a, a_)
torch.testing.assert_close(b, b_)
torch.testing.assert_close(c, c_)


@instantiate(
dtypes=(dtypes.float32,),
)
def test_update_aliases_count(executor, device, dtype):
def f(x):
x.sin_()
return x * x * x * x

def g(x):
x.sin_()
x.cos_()
return x * x * x * x

expected_num_update_aliases = {
f: 1, # before sin_
g: 2, # before sin_ and cos_; latter is a hack to cause fusion break
}

for fn in [f, g]:
a = make_tensor((2, 3), dtype=dtypes.to_torch_dtype(dtype), device=device)
a_ = a.clone().detach()
jfn = executor.make_callable(fn)
actual = jfn(a)
expected = fn(a_)
torch.testing.assert_close(actual, expected)
extrace = thunder.last_traces(jfn)[-1]
actual_num_update_aliases = len([bsym for bsym in extrace.bound_symbols if bsym.sym.name == "update_aliases"])
assert actual_num_update_aliases == expected_num_update_aliases[fn]
Loading