|
3 | 3 | import textwrap |
4 | 4 | import onnx |
5 | 5 | import onnx.defs # noqa |
6 | | -from onnx.defs import OpSchema |
7 | 6 |
|
8 | 7 |
|
9 | 8 | def _get_doc_template(): |
@@ -115,154 +114,6 @@ def get_domain_list(): |
115 | 114 | ) |
116 | 115 |
|
117 | 116 |
|
118 | | -def get_rst_doc(op_name=None): |
119 | | - """ |
120 | | - Returns a documentation in RST format |
121 | | - for all :class:`OnnxOperator`. |
122 | | -
|
123 | | - :param op_name: operator name of None for all |
124 | | - :return: string |
125 | | -
|
126 | | - The function relies on module *jinja2* or replaces it |
127 | | - with a simple rendering if not present. |
128 | | - """ |
129 | | - if op_name is None: |
130 | | - schemas = onnx.defs.get_all_schemas_with_history() |
131 | | - elif isinstance(op_name, str): |
132 | | - schemas = [ |
133 | | - schema |
134 | | - for schema in onnx.defs.get_all_schemas_with_history() |
135 | | - if schema.name == op_name |
136 | | - ] |
137 | | - if len(schemas) > 1: |
138 | | - raise RuntimeError( |
139 | | - "Multiple operators have the same name '{}'.".format(op_name) |
140 | | - ) |
141 | | - elif not isinstance(op_name, list): |
142 | | - schemas = [op_name] |
143 | | - if len(schemas) == 0: |
144 | | - raise ValueError("Unable to find any operator with name '{}'.".format(op_name)) |
145 | | - |
146 | | - # from onnx.backend.sample.ops import collect_sample_implementations |
147 | | - # from onnx.backend.test.case import collect_snippets |
148 | | - # SNIPPETS = collect_snippets() |
149 | | - # SAMPLE_IMPLEMENTATIONS = collect_sample_implementations() |
150 | | - def format_name_with_domain(sch): |
151 | | - if sch.domain: |
152 | | - return "{} ({})".format(sch.name, sch.domain) |
153 | | - return sch.name |
154 | | - |
155 | | - def get_is_homogeneous(obj): |
156 | | - try: |
157 | | - return obj.is_homogeneous |
158 | | - except AttributeError: |
159 | | - try: |
160 | | - return obj.isHomogeneous |
161 | | - except AttributeError: |
162 | | - return False |
163 | | - |
164 | | - def format_option(obj): |
165 | | - opts = [] |
166 | | - if OpSchema.FormalParameterOption.Optional == obj.option: |
167 | | - opts.append("optional") |
168 | | - elif OpSchema.FormalParameterOption.Variadic == obj.option: |
169 | | - opts.append("variadic") |
170 | | - if get_is_homogeneous(obj): |
171 | | - opts.append("heterogeneous") |
172 | | - if opts: |
173 | | - return " (%s)" % ", ".join(opts) |
174 | | - return "" |
175 | | - |
176 | | - def getconstraint(const, ii): |
177 | | - if const.type_param_str: |
178 | | - name = const.type_param_str |
179 | | - else: |
180 | | - name = str(ii) |
181 | | - if const.allowed_type_strs: |
182 | | - name += " " + ", ".join(const.allowed_type_strs) |
183 | | - return name |
184 | | - |
185 | | - def getname(obj, i): |
186 | | - name = obj.name |
187 | | - if len(name) == 0: |
188 | | - return str(i) |
189 | | - return name |
190 | | - |
191 | | - def process_documentation(doc): |
192 | | - if doc is None: |
193 | | - doc = "" |
194 | | - doc = textwrap.dedent(doc) |
195 | | - main_docs_url = "https://github.com/onnx/onnx/blob/main/" |
196 | | - rep = { |
197 | | - "[the doc](IR.md)": "`ONNX <{0}docs/IR.md>`_", |
198 | | - "[the doc](Broadcasting.md)": ( |
199 | | - "`Broadcasting in ONNX <{0}docs/Broadcasting.md>`_" |
200 | | - ), |
201 | | - "<dl>": "", |
202 | | - "</dl>": "", |
203 | | - "<dt>": "* ", |
204 | | - "<dd>": " ", |
205 | | - "</dt>": "", |
206 | | - "</dd>": "", |
207 | | - "<tt>": "``", |
208 | | - "</tt>": "``", |
209 | | - "<br>": "\n", |
210 | | - } |
211 | | - for k, v in rep.items(): |
212 | | - doc = doc.replace(k, v.format(main_docs_url)) |
213 | | - move = 0 |
214 | | - lines = [] |
215 | | - for line in doc.split("\n"): |
216 | | - if line.startswith("```"): |
217 | | - if move > 0: |
218 | | - move -= 4 |
219 | | - lines.append("\n") |
220 | | - else: |
221 | | - lines.append("::\n") |
222 | | - move += 4 |
223 | | - elif move > 0: |
224 | | - lines.append(" " * move + line) |
225 | | - else: |
226 | | - lines.append(line) |
227 | | - return "\n".join(lines) |
228 | | - |
229 | | - def build_doc_url(sch): |
230 | | - doc_url = "https://github.com/onnx/onnx/blob/main/docs/Operators" |
231 | | - if "ml" in sch.domain: |
232 | | - doc_url += "-ml" |
233 | | - doc_url += ".md" |
234 | | - doc_url += "#" |
235 | | - if sch.domain not in (None, "", "ai.onnx"): |
236 | | - doc_url += sch.domain + "." |
237 | | - return doc_url |
238 | | - |
239 | | - def get_type_str(inou): |
240 | | - try: |
241 | | - return inou.type_str |
242 | | - except AttributeError: |
243 | | - return inou.typeStr |
244 | | - |
245 | | - fnwd = format_name_with_domain |
246 | | - tmpl = _template_operator |
247 | | - docs = tmpl.render( |
248 | | - schemas=schemas, |
249 | | - OpSchema=OpSchema, |
250 | | - len=len, |
251 | | - getattr=getattr, |
252 | | - sorted=sorted, |
253 | | - format_option=format_option, |
254 | | - getconstraint=getconstraint, |
255 | | - getname=getname, |
256 | | - enumerate=enumerate, |
257 | | - format_name_with_domain=fnwd, |
258 | | - process_documentation=process_documentation, |
259 | | - build_doc_url=build_doc_url, |
260 | | - str=str, |
261 | | - get_type_str=get_type_str, |
262 | | - ) |
263 | | - return docs |
264 | | - |
265 | | - |
266 | 117 | def _get_doc_template_sklearn(): |
267 | 118 | try: |
268 | 119 | from jinja2 import Template |
|
0 commit comments