Caffe2 - Python API
A deep learning, cross platform ML framework
supported_ops.py
1 import torch.jit
2 # this file is for generating documentation using sphinx autodoc
3 # > help(torch.jit.supported_ops) will also give a nice listed of the
4 # supported ops programmatically
5 
6 
7 def _list_supported_ops():
8  def emit_type(type):
9  return str(type)
10 
11  def emit_arg(indent, i, arg):
12  v = "{} : {}".format(arg.name, emit_type(arg.type))
13  default = arg.default_value
14  if default is not None:
15  v = "{}={}".format(v, str(default))
16  if i > 0:
17  v = "\n{}{}".format(" " * indent, v)
18  return v
19 
20  def emit_args(indent, arguments):
21  return ",".join(emit_arg(indent, i, arg) for i, arg in enumerate(arguments))
22 
23  def emit_ret(ret):
24  return emit_type(ret.type)
25 
26  def emit_rets(returns):
27  if len(returns) == 1:
28  return emit_ret(returns[0])
29  return "Tuple[{}]".format(", ".join(emit_ret(r) for r in returns))
30 
31  def emit_schema(mod, name, schema, arg_start=0):
32  qualified_name = "{}.{}".format(mod, name)
33  schema = "{}({}) -> {}".format(qualified_name,
34  emit_args(len(qualified_name) + 1 + 4, schema.arguments[arg_start:]),
35  emit_rets(schema.returns))
36  return schema
37 
38  def hidden(name):
39  return name.startswith('_') and not name.startswith('__')
40 
41  functions = []
42 
43  for mod in torch.jit._modules_containing_builtins:
44  name = mod.__name__
45  for elem in dir(mod):
46  builtin = torch.jit._find_builtin(getattr(mod, elem))
47  if builtin is not None:
48  schemas = torch._C._jit_get_schemas_for_operator(builtin)
49  for schema in schemas:
50  # remove _tan but not __and__
51  if not hidden(elem):
52  functions.append(emit_schema(name, elem, schema))
53 
55  name = mod.__name__
56  for elem in dir(torch.nn.functional):
57  # weak script functions
58  attr = getattr(mod, elem)
59  if not callable(attr) or elem[0] == '_':
60  # ignore non-functions and internal methods
61  continue
62 
63  # compile weak script fn, get schema
64  scripted = torch.jit._try_compile_weak_script(attr)
65  if scripted is None:
66  continue
67  schema = scripted._get_method('forward').schema()
68  functions.append(emit_schema(name, elem, schema))
69 
70  def is_tensor_method(schema):
71  if len(schema.arguments) == 0:
72  return False
73  self = schema.arguments[0]
74  if self.name != 'self':
75  return False
76  if not self.type.isSubtypeOf(torch._C.TensorType.get()):
77  return False
78  return True
79 
80  methods = []
81  # discover methods
82  for elem in dir(torch.Tensor):
83  if not hidden(elem):
84  schemas = torch._C._jit_get_schemas_for_operator("aten::" + elem)
85  for schema in schemas:
86  if is_tensor_method(schema):
87  methods.append(emit_schema('Tensor', elem, schema, arg_start=1))
88 
89  def emit_block(decls):
90  return '\n::\n\n{}\n'.format(''.join(' {}\n\n'.format(d) for d in decls))
91  body = """
92 Supported Functions
93 ~~~~~~~~~~~~~~~~~~~
94 {}
95 
96 Supported Methods
97 ~~~~~~~~~~~~~~~~~
98 {}
99 """
100  return body.format(emit_block(functions), emit_block(methods))
101 
102 __doc__ = _list_supported_ops()
Module caffe2.python.schema.
def _find_builtin(fn)
Definition: __init__.py:1538
def _try_compile_weak_script(fn)
Definition: __init__.py:713