[dynamo] Use UUID for compiled function variable names. (#154148)

Summary:
We previously assign each compiled function variable a name based on in-process global counter. This works fine within the same process but when we're trying to serialize the states with precompile, we need a way to load back these compiled functions without causing collision to the existing global scope.

Changing the counter to a true global uuid seems to resolve this issue.

For example, the new variable name will look like:
```
__compiled_fn_0_7ce7d872_4fe8_4174_b8fd_2496b09b8b43
```

Test Plan: CI

Differential Revision: D75244901

Pull Request resolved: https://github.com/pytorch/pytorch/pull/154148
Approved by: https://github.com/jansel
This commit is contained in:
Zhengxu Chen
2025-05-24 01:08:42 +00:00
committed by PyTorch MergeBot
parent 7ba6fb69e6
commit 308beeeb56
3 changed files with 8 additions and 4 deletions

View File

@ -22,6 +22,7 @@ import functools
import itertools
import sys
import types
import uuid
from collections.abc import Iterator, Sequence
from typing import Any, Callable, cast, Optional, Union
@ -1547,8 +1548,11 @@ def _cached_cleaned_instructions(code, safe=False) -> Sequence[Instruction]:
_unique_id_counter = itertools.count()
def unique_id(name) -> str:
return f"{name}_{next(_unique_id_counter)}"
def unique_id(name, with_uuid=False) -> str:
ret = f"{name}_{next(_unique_id_counter)}"
if with_uuid:
ret += f"_{uuid.uuid4()}".replace("-", "_")
return ret
def is_generator(code: types.CodeType) -> bool: