| 
11 | 11 | # All configuration values have a default; values that are commented out  | 
12 | 12 | # serve to show the default.  | 
13 | 13 | 
 
  | 
 | 14 | +import inspect  | 
 | 15 | + | 
14 | 16 | # If extensions (or modules to document with autodoc) are in another directory,  | 
15 | 17 | # add these directories to sys.path here. If the directory is relative to the  | 
16 | 18 | # documentation root, use os.path.abspath to make it absolute, like shown here.  | 
 | 
62 | 64 |     "sphinx_copybutton",  | 
63 | 65 |     "sphinx_panels",  | 
64 | 66 |     "myst_parser",  | 
 | 67 | +    "sphinx.ext.linkcode",  | 
65 | 68 | ]  | 
66 | 69 | 
 
  | 
67 | 70 | # build the templated autosummary files  | 
 | 
3378 | 3381 |     html_title = " ".join((project, version, "documentation"))  | 
3379 | 3382 |     release = version  | 
3380 | 3383 | 
 
  | 
 | 3384 | + | 
 | 3385 | +# Use the linkcode extension to override [SOURCE] links to point  | 
 | 3386 | +# to the repo. Use the torch_version variable defined above to  | 
 | 3387 | +# determine link  | 
 | 3388 | +def linkcode_resolve(domain, info):  | 
 | 3389 | +    if domain != "py":  | 
 | 3390 | +        return None  | 
 | 3391 | +    if not info["module"]:  | 
 | 3392 | +        return None  | 
 | 3393 | + | 
 | 3394 | +    try:  | 
 | 3395 | +        module = __import__(info["module"], fromlist=[""])  | 
 | 3396 | +        obj = module  | 
 | 3397 | +        for part in info["fullname"].split("."):  | 
 | 3398 | +            obj = getattr(obj, part)  | 
 | 3399 | +        # Get the source file and line number  | 
 | 3400 | +        obj = inspect.unwrap(obj)  | 
 | 3401 | +        fn = inspect.getsourcefile(obj)  | 
 | 3402 | +        source, lineno = inspect.getsourcelines(obj)  | 
 | 3403 | +    except Exception:  | 
 | 3404 | +        return None  | 
 | 3405 | + | 
 | 3406 | +    # Determine the tag based on the torch_version  | 
 | 3407 | +    if RELEASE:  | 
 | 3408 | +        version_parts = torch_version.split(  | 
 | 3409 | +            "."  | 
 | 3410 | +        )  # For release versions, format as "vX.Y.Z" for correct path in repo  | 
 | 3411 | +        patch_version = (  | 
 | 3412 | +            version_parts[2].split("+")[0].split("a")[0]  | 
 | 3413 | +        )  # assuming a0 always comes after release version in versions.txt  | 
 | 3414 | +        version_path = f"v{version_parts[0]}.{version_parts[1]}.{patch_version}"  | 
 | 3415 | +    else:  | 
 | 3416 | +        version_path = torch.version.git_version  | 
 | 3417 | +    fn = os.path.relpath(fn, start=os.path.dirname(torch.__file__))  | 
 | 3418 | +    return (  | 
 | 3419 | +        f"https://github.com/pytorch/pytorch/blob/{version_path}/torch/{fn}#L{lineno}"  | 
 | 3420 | +    )  | 
 | 3421 | + | 
 | 3422 | + | 
3381 | 3423 | # The language for content autogenerated by Sphinx. Refer to documentation  | 
3382 | 3424 | # for a list of supported languages.  | 
3383 | 3425 | #  | 
 | 
0 commit comments