Skip to content

Commit d26798f

Browse files
author
Ian Schweer
committed
Make module references FIFO
1 parent d14c258 commit d26798f

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

pytensor/link/pytorch/dispatch/basic.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inspect
22
import sys
3+
from collections import defaultdict
34
from functools import singledispatch
45
from types import NoneType
56

@@ -58,23 +59,19 @@ def pytorch_funcify_FunctionGraph(
5859
fgraph_name="pytorch_funcified_fgraph",
5960
**kwargs,
6061
):
62+
fnames_counter = defaultdict(int)
63+
6164
def conversion_func_register(*args, **kwargs):
6265
last_frame = inspect.currentframe().f_back
6366
functor = pytorch_funcify(*args, **kwargs)
6467
if last_frame:
6568
module = sys.modules[last_frame.f_globals["__name__"]]
6669
name = functor.__name__
67-
i = 0
68-
prefix = ""
69-
while True:
70-
name = functor.__name__ + prefix
71-
if name in dir(module):
72-
i += 1
73-
prefix = f"_{i}"
74-
else:
75-
print("Registering functor", name)
76-
setattr(module, name, functor)
77-
break
70+
if fnames_counter[name]:
71+
name = f"{name}_{fnames_counter[name]}"
72+
print("Registering functor", name)
73+
setattr(module, name, functor)
74+
fnames_counter[functor.__name__] += 1
7875
return functor
7976

8077
return fgraph_to_python(

0 commit comments

Comments
 (0)