|
14 | 14 | from collections.abc import Sequence |
15 | 15 |
|
16 | 16 | import thunder.core.baseutils as baseutils |
| 17 | +from thunder.core.baseutils import BoundSymbolInterface, TagBase |
17 | 18 | import thunder.core.codeutils as codeutils |
18 | 19 | from thunder.core.codeutils import Printable, Positions |
19 | | -from thunder.core.baseutils import BoundSymbolInterface, TagBase |
20 | | -from thunder.core.utils import FrozenDict, make_hashable |
21 | | -from thunder.core.pytree import tree_flatten_with_dataclass, tree_unflatten, tree_map |
22 | | -from thunder.core.proxies import Proxy, TensorProxy, variableify, CollectionProxy, ProxyTag |
23 | 20 | from thunder.core.compile_data import get_compile_data |
| 21 | +import thunder.core.prims as prims |
| 22 | +from thunder.core.proxies import Proxy, TensorProxy, variableify, CollectionProxy, ProxyTag |
| 23 | +from thunder.core.pytree import tree_flatten, tree_flatten_with_dataclass, tree_unflatten, tree_map |
| 24 | +from thunder.core.trace import get_tracectx, VariableInterface |
| 25 | +from thunder.core.utils import FrozenDict, make_hashable |
24 | 26 |
|
25 | | -from thunder.core.trace import ( |
26 | | - get_tracectx, |
27 | | - VariableInterface, |
28 | | -) |
29 | 27 |
|
30 | 28 | # |
31 | 29 | # Support for querying "traceable" functions |
@@ -314,6 +312,17 @@ def __call__(self, *args, **kwargs): |
314 | 312 | else: |
315 | 313 | trace.push_scope(subsymbols) |
316 | 314 | result = self.meta(*args, **kwargs) |
| 315 | + |
| 316 | + # To avoid passing an arg directly to output, we make a shallow_copy |
| 317 | + flat_results, spec = tree_flatten(result) |
| 318 | + flat_args, _ = tree_flatten((args, kwargs)) |
| 319 | + for i, result_ in enumerate(flat_results): |
| 320 | + for arg in flat_args: |
| 321 | + if arg is result_ and isinstance(arg, Proxy): |
| 322 | + flat_results[i] = prims.shallow_copy(arg) |
| 323 | + |
| 324 | + result = tree_unflatten(flat_results, spec) |
| 325 | + |
317 | 326 | trace.pop_scope() |
318 | 327 |
|
319 | 328 | cd = get_compile_data() |
|
0 commit comments