Skip to content

First documentation of the light API #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
minorchanges
  • Loading branch information
xadupre committed Nov 7, 2023
commit 20bbeddc65a8e9d9d9252871ea605204d6027abe
122 changes: 61 additions & 61 deletions _doc/tutorial/onnx_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,6 @@ an existing onnx models or to merge models coming from different packages.
Sometimes, they are just not available, only onnx is.
Let's see how it looks like a very simply example.

Use torch or tensorflow
=======================

:epkg:`pytorch` offers the possibility to convert any function
implemented with pytorch function into onnx with :epkg:`torch.onnx`.
A couple of examples.

.. code-block:: python

import torch
import torch.nn


class MyModel(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(2, 2)

def forward(self, x, bias=None):
out = self.linear(x)
out = out + bias
return out

model = MyModel()
kwargs = {"bias": 3.}
args = (torch.randn(2, 2, 2),)

export_output = torch.onnx.dynamo_export(
model,
*args,
**kwargs).save("my_simple_model.onnx")

.. code-block:: python

from typing import Dict, Tuple
import torch
import torch.onnx


def func_with_nested_input_structure(
x_dict: Dict[str, torch.Tensor],
y_tuple: Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]],
):
if "a" in x_dict:
x = x_dict["a"]
elif "b" in x_dict:
x = x_dict["b"]
else:
x = torch.randn(3)

y1, (y2, y3) = y_tuple

return x + y1 + y2 + y3

x_dict = {"a": torch.tensor(1.)}
y_tuple = (torch.tensor(2.), (torch.tensor(3.), torch.tensor(4.)))
export_output = torch.onnx.dynamo_export(func_with_nested_input_structure, x_dict, y_tuple)

print(export_output.adapt_torch_inputs_to_onnx(x_dict, y_tuple))

Euclidian distance
==================

Expand Down Expand Up @@ -298,6 +238,66 @@ Here are some of the questions which may need to be answered.
* easy error messages
* is it actively maintained?

Use torch or tensorflow
+++++++++++++++++++++++

:epkg:`pytorch` offers the possibility to convert any function
implemented with pytorch function into onnx with :epkg:`torch.onnx`.
A couple of examples.

.. code-block:: python

import torch
import torch.nn


class MyModel(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(2, 2)

def forward(self, x, bias=None):
out = self.linear(x)
out = out + bias
return out

model = MyModel()
kwargs = {"bias": 3.}
args = (torch.randn(2, 2, 2),)

export_output = torch.onnx.dynamo_export(
model,
*args,
**kwargs).save("my_simple_model.onnx")

.. code-block:: python

from typing import Dict, Tuple
import torch
import torch.onnx


def func_with_nested_input_structure(
x_dict: Dict[str, torch.Tensor],
y_tuple: Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]],
):
if "a" in x_dict:
x = x_dict["a"]
elif "b" in x_dict:
x = x_dict["b"]
else:
x = torch.randn(3)

y1, (y2, y3) = y_tuple

return x + y1 + y2 + y3

x_dict = {"a": torch.tensor(1.)}
y_tuple = (torch.tensor(2.), (torch.tensor(3.), torch.tensor(4.)))
export_output = torch.onnx.dynamo_export(func_with_nested_input_structure, x_dict, y_tuple)

print(export_output.adapt_torch_inputs_to_onnx(x_dict, y_tuple))

onnxscript
++++++++++

Expand Down Expand Up @@ -539,7 +539,7 @@ produces different onnx graphs depending on the execution path.
model = jitted_myloss.get_onnx()
print(onnx_simple_text_plot(model))

light API
Light API
+++++++++

See :ref:`l-light-api`. This API was created to be able to write an onnx graph
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy