Skip to content

Commit 83fb974

Browse files
c00wpytorchmergebot
authored andcommitted
scriptfunction: Make sure we have valid __name__ and __qualname__ (pytorch#147906)
It's not fully clear why these are not being created, but you can definitely reproduce this in code. `__name__` is fun, since there appears to be no way to explicitly set it on the pybind11 layer or c++ layer. I've set this in the python wrapper code (which works correctly). But let me know if people feel strongly and want us to go explicitly cast to python within the cpp functions and set it there. Pull Request resolved: pytorch#147906 Approved by: https://github.com/jansel ghstack dependencies: pytorch#147894
1 parent 1ae7cc4 commit 83fb974

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

test/test_jit.py

+7
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,13 @@ def fn(x: torch.Tensor) -> torch.Tensor:
458458

459459
pkl_fn = pickle.dumps(fn, protocol=0)
460460

461+
def test_script_fn_valid_name(self):
462+
@torch.jit.script
463+
def fn(x: torch.Tensor) -> torch.Tensor:
464+
return x
465+
self.assertIsNotNone(fn.__name__)
466+
self.assertIsNotNone(fn.__qualname__)
467+
461468
def test_restore_device(self):
462469
class M(torch.jit.ScriptModule):
463470
def __init__(self, cpu_device_str):

torch/jit/_script.py

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
Functionally equivalent to a :class:`ScriptModule`, but represents a single
6464
function and does not have any attributes or Parameters.
6565
"""
66+
ScriptFunction.__name__ = "ScriptFunction"
67+
ScriptFunction.__qualname__ = "torch.jit.ScriptFunction"
6668
set_module(ScriptFunction, "torch.jit")
6769

6870

@@ -1214,6 +1216,8 @@ def _script_impl(
12141216
)
12151217
# Forward docstrings
12161218
fn.__doc__ = obj.__doc__
1219+
fn.__name__ = "ScriptFunction"
1220+
fn.__qualname__ = "torch.jit.ScriptFunction"
12171221
# Allow torch.compile() to inline
12181222
fn._torchdynamo_inline = obj # type: ignore[attr-defined]
12191223
_set_jit_function_cache(obj, fn)

0 commit comments

Comments
 (0)