• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# mypy: allow-untyped-defs
2from textwrap import dedent
3from typing import Any, Dict
4
5import torch.jit
6
7
8def execWrapper(code, glob, loc):
9    exec(code, glob, loc)
10
11
12def _gen_unsupported_methods_properties():
13    tensor_attrs = set(filter(lambda x: x[0] != "_", dir(torch.Tensor)))
14    tensor = torch.tensor([2])
15    funcs_template = dedent(
16        """
17    def func(x):
18        return x.{op}()
19    """
20    )
21
22    deprecated_apis = {
23        "volatile",
24        "resize",
25        "reinforce",
26        "new",
27        "name",
28        "map2_",
29        "has_names",
30        "grad_fn",
31        "resize_as",
32    }
33    tensor_attrs = tensor_attrs - deprecated_apis
34
35    properties = []
36    methods = []
37    sorted_tensor_attrs = sorted(tensor_attrs, key=lambda x: x.lower())
38    for attr in sorted_tensor_attrs:
39        funcs_str = funcs_template.format(op=attr)
40        scope: Dict[str, Any] = {}
41        execWrapper(funcs_str, globals(), scope)
42        try:
43            cu = torch.jit.CompilationUnit(funcs_str)
44        except Exception as e:
45            if "nonexistent attribute" not in repr(e):
46                continue
47            attr_repr = repr(getattr(tensor, attr))
48            if "bound method" in attr_repr or "built-in method" in attr_repr:
49                methods.append(attr)
50            else:
51                properties.append(attr)
52
53    mapped_methods = ("\t*  :meth:`~torch.Tensor." + x + r"`" for x in methods)
54    mapped_properties = ("\t*  :attr:`~torch.Tensor." + x + r"`" for x in properties)
55    return "\n".join(mapped_methods), "\n".join(mapped_properties)
56
57
58def _list_unsupported_tensor_ops():
59    header = """\n\n
60Unsupported Tensor Methods
61~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62    """
63    methods, properties = _gen_unsupported_methods_properties()
64    return (
65        header
66        + "\n"
67        + methods
68        + """
69
70Unsupported Tensor Properties
71~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72    """
73        + "\n"
74        + properties
75    )
76
77
78__doc__ = _list_unsupported_tensor_ops()
79