3 from __future__
import absolute_import
4 from __future__
import division
5 from __future__
import print_function
6 from __future__
import unicode_literals
11 from future.utils
import viewitems, viewvalues
13 OpSchema = workspace.C.OpSchema
20 def upload(self, text):
24 class DocGenerator(object):
25 def __init__(self, formatter, uploader):
26 self.formatter = formatter
27 self.uploader = uploader
28 self.content_body =
"" 30 def create_body(self):
34 self.uploader.upload(self.content_body)
38 def getOperatorDoc(self, name, schema, priority):
41 def getOperatorEngine(self, name):
44 def getOperators(self):
51 if x ==
"caffe2/caffe2/operators":
53 if 'contrib' in x.split(
'/'):
55 if 'experiments' in x.split(
'/'):
59 for name
in core._GetRegisteredOperators():
60 schema = OpSchema.get(name)
62 priority = filePriority(os.path.dirname(schema.file))
67 elif name.find(
"_ENGINE_") != -1:
69 if engine.base_op_name
in self.
engines:
70 self.
engines[engine.base_op_name].append(engine)
72 self.
engines[engine.base_op_name] = [engine]
79 for name, engines
in viewitems(self.
engines):
86 key=
lambda op: (op.priority, op.name)
92 for operator
in operators:
99 def __init__(self, name):
101 self.base_op_name, self.
engine = name.split(
"_ENGINE_", 1)
103 def getDeviceImpl(self):
105 for device, impl
in [(
'CPU', OpSchema.get_cpu_impl(self.
op_name)),
106 (
'CUDA', OpSchema.get_cuda_impl(self.
op_name))]:
109 deviceImplList.append((device, impl))
110 return deviceImplList
112 def generateDoc(self, formatter):
115 '{engine} on {device}: {impl}'.format(engine=self.
engine,
121 def __init__(self, name, schema, priority):
125 print(
"Gathering docs for {}...".format(self.
name))
128 def addEngines(self, engines):
131 def generateDoc(self, formatter):
133 formatter.parseAndAdd(self.schema.doc)
134 formatter.addLinebreak()
136 formatter.addLine(
"No documentation yet.")
138 def generateTable(self, formatter, tuples, title_row, title):
141 formatter.addHeader(title, 3)
145 for name, doc
in tuples:
146 table.append([name, doc
or ''])
147 formatter.addTable(table, (table == []))
149 def generateInterface(self, formatter):
150 def makeDesc(title, args):
151 f = formatter.clone()
152 f.addEmphasis(title, 1)
153 out = [(f.dump(),
'')]
155 f = formatter.clone()
156 if isinstance(arg, tuple):
159 description = arg[1]
or '' 164 description = arg.description
or '' 165 f.addCode(name, inline=
True)
166 out.append((f.dump(), description
or ''))
172 tuples += makeDesc(
'Arguments', self.schema.args)
174 if self.schema.input_desc:
175 tuples += makeDesc(
'Inputs', self.schema.input_desc)
177 if self.schema.output_desc:
178 tuples += makeDesc(
'Outputs', self.schema.output_desc)
181 print(
"Generated interface for {}".format(self.
name))
183 def generateCodeLink(self, formatter):
184 formatter.addHeader(
"Code", 3)
185 formatter.addLinebreak()
186 formatter.addCodeLink(self.schema.file)
188 def getInfo(self, formatter, name, impl):
191 def generateDevices(self, formatter):
192 formatter.addHeader(
"Devices", 3)
194 self.getInfo(formatter,
195 'CPU', OpSchema.get_cpu_impl(self.name)),
196 self.getInfo(formatter,
197 'GPU', OpSchema.get_cuda_impl(self.name)),
199 formatter.addList([i
for i
in devices
if i])
201 def generateEngines(self, formatter):
202 if not len(self.engines):
204 formatter.addHeader(
"Engines", 3)
205 for engine
in self.engines:
206 engine.generateDoc(formatter)
208 def generateSchema(self, formatter):
209 formatter.addHeader(self.name, 2)
211 self.generateDoc(formatter)
212 self.generateInterface(formatter)
213 self.generateCodeLink(formatter)
214 self.generateDevices(formatter)
215 self.generateEngines(formatter)
218 formatter.addLine(
"No schema documented yet.")
219 self.generateDevices(formatter)
222 if __name__ ==
"__main__":
223 parser = argparse.ArgumentParser(description=
"Operators catalog generator.")
224 parser.add_argument(
'catalog_path', type=str,
225 help=
'operators-catalogue.md to write out to')
226 args = parser.parse_args()
228 with open(args.catalog_path,
'w')
as fp:
229 ops = OpDocGenerator(Markdown(), DocUploader())
231 fp.write(ops.content_body)
def getOperatorDoc(self, name, schema, priority)
def getOperatorEngine(self, name)
def generateTable(self, formatter, tuples, title_row, title)