fastai / nbdev

Create delightful software with Jupyter Notebooks
https://nbdev.fast.ai/
Apache License 2.0
4.93k stars 488 forks source link

`show_doc` rendering issues with TorchScript functions #1180

Open warner-benjamin opened 2 years ago

warner-benjamin commented 2 years ago

show_doc currently has rendering issues with TorchScript functions.

import torch
from nbdev.showdoc import *

@torch.jit.script
def test(p:torch.Tensor):
    "Test torchscript function"
    return p

show_doc(test)

results in the following output, without a source link in the documentation

## ScriptFunction object at 0x7fc550c680e0>
Test torchscript function

switching to show_doc(test.name), where name is a torchscript function property, displays the method name and github source code link, but not the docstring:

## test
test
seeM commented 1 year ago

It looks like @torch.jit.script really clobbers your function e.g. it sets __module__ = 'torch.jit' and deletes both __name__ and __qualname__. I'm not sure how to resolve that...

One workaround might be to show_doc on the original func then call torch.jit.script on it in another cell:

#|export
def test(p:torch.Tensor):
    "Test torchscript function"
    return p

show_doc(test)
test = torch.jit.script(test)