|
| 1 | +================================= |
| 2 | +Exported ONNX with Dynamic Shapes |
| 3 | +================================= |
| 4 | + |
| 5 | +The following script shows the exported program for many short cases |
| 6 | +and various l-plot-export-with-dynamic-shape to retrieve an ONNX model equivalent |
| 7 | +to the original model. |
| 8 | + |
| 9 | +.. runpython:: |
| 10 | + :showcode: |
| 11 | + :rst: |
| 12 | + :toggle: code |
| 13 | + :warningout: UserWarning |
| 14 | + |
| 15 | + import inspect |
| 16 | + import textwrap |
| 17 | + import pandas |
| 18 | + from onnx_diagnostic.helpers import string_type |
| 19 | + from onnx_diagnostic.helpers.onnx_helper import pretty_onnx |
| 20 | + from onnx_diagnostic.torch_export_patches.eval import discover, run_exporter |
| 21 | + from onnx_diagnostic.ext_test_case import unit_test_going |
| 22 | + |
| 23 | + cases = discover() |
| 24 | + print() |
| 25 | + print(":ref:`Summary <ledx-summary-exported-program>`") |
| 26 | + print() |
| 27 | + sorted_cases = sorted(cases.items()) |
| 28 | + if unit_test_going(): |
| 29 | + sorted_cases = sorted_cases[:3] |
| 30 | + for name, cls_model in sorted_cases: |
| 31 | + print(f"* :ref:`{name} <ledx-model-case-export-{name}>`") |
| 32 | + print() |
| 33 | + print() |
| 34 | + |
| 35 | + obs = [] |
| 36 | + for name, cls_model in sorted(cases.items()): |
| 37 | + print() |
| 38 | + print(f".. _ledx-model-case-export-{name}:") |
| 39 | + print() |
| 40 | + print(name) |
| 41 | + print("=" * len(name)) |
| 42 | + print() |
| 43 | + print("forward") |
| 44 | + print("+++++++") |
| 45 | + print() |
| 46 | + print(".. code-block:: python") |
| 47 | + print() |
| 48 | + src = inspect.getsource(cls_model.forward) |
| 49 | + if src: |
| 50 | + print(textwrap.indent(textwrap.dedent(src), " ")) |
| 51 | + else: |
| 52 | + print(" # code is missing") |
| 53 | + print() |
| 54 | + print() |
| 55 | + for exporter in ("custom", "dynamo-ir"): |
| 56 | + expname = exporter.replace("export-", "") |
| 57 | + print() |
| 58 | + print(expname) |
| 59 | + print("+" * len(expname)) |
| 60 | + print() |
| 61 | + res = run_exporter(exporter, cls_model, True, quiet=True) |
| 62 | + case_ref = f":ref:`{name} <ledx-model-case-export-{name}>`" |
| 63 | + expo = exporter.split("-", maxsplit=1)[-1] |
| 64 | + if "inputs" in res: |
| 65 | + print(f"* **inputs:** ``{string_type(res['inputs'], with_shape=True)}``") |
| 66 | + if "dynamic_shapes" in res: |
| 67 | + print(f"* **shapes:** ``{string_type(res['dynamic_shapes'])}``") |
| 68 | + print() |
| 69 | + print() |
| 70 | + if "onx" in res: |
| 71 | + print(".. code-block:: text") |
| 72 | + print() |
| 73 | + print(textwrap.indent(pretty_onnx(res["onx"]), " ")) |
| 74 | + print() |
| 75 | + print() |
| 76 | + if "error" not in res: |
| 77 | + obs.append(dict(case=case_ref, error="", exporter=expo)) |
| 78 | + if "error" in res: |
| 79 | + print("**FAILED**") |
| 80 | + print() |
| 81 | + print(".. code-block:: text") |
| 82 | + print() |
| 83 | + err = str(res["error"]) |
| 84 | + if err: |
| 85 | + print(textwrap.indent(err, " ")) |
| 86 | + else: |
| 87 | + print(" # no error found for the failure") |
| 88 | + print() |
| 89 | + print() |
| 90 | + obs.append(dict(case=case_ref, error="FAIL", exporter=expo)) |
| 91 | + |
| 92 | + print() |
| 93 | + print(".. _ledx-summary-exported-program:") |
| 94 | + print() |
| 95 | + print("Summary") |
| 96 | + print("+++++++") |
| 97 | + print() |
| 98 | + df = pandas.DataFrame(obs) |
| 99 | + piv = df.pivot(index="case", columns="exporter", values="error") |
| 100 | + print(piv.to_markdown(tablefmt="rst")) |
| 101 | + print() |
0 commit comments