Skip to content

Commit 17915ed

Browse files
Some updates to the tutorial documentation (#478)
Some updates to the tutorial documentation --------- Signed-off-by: Ganesan Ramalingam <[email protected]> Co-authored-by: Justin Chu <[email protected]>
1 parent 2952f41 commit 17915ed

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

docs/examples/06_plot_model_local_funs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def sum(z):
3636

3737
@script()
3838
def l2norm(x: FLOAT["N"], y: FLOAT["N"]) -> FLOAT[1]: # noqa: F821
39-
return op.sqrt(sum(diff_square(x, y)))
39+
return op.Sqrt(sum(diff_square(x, y)))
4040

4141

4242
#%%

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ executes in an eager-evaluation mode.
9595
:maxdepth: 1
9696

9797
tutorial/index
98-
api/index
9998
auto_examples/index
99+
api/index
100100

101101
**License**
102102

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from onnxscript import opset15 as op
2+
from onnxscript import script
3+
4+
5+
@script()
6+
def LeakyRelu(X, alpha: float):
7+
alpha_value = op.Constant(value_float=alpha)
8+
return op.Where(X < 0.0, alpha_value * X, X)

docs/tutorial/index.rst

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,19 @@ values for the attributes ``start`` and ``end``.
4444

4545
.. literalinclude:: examples/firstdim.py
4646

47-
In the translation of a call to an ONNX operator, keyword arguments (aka named arguments)
48-
of Python are translated into attribute parameters (of ONNX), while positional arguments
47+
In the translation of a call to an ONNX operator, the translator makes use of the
48+
``OpSchema`` specification of the operator to map the actual parameters to appropriate input
49+
parameters and attribute parameters. Since the ONNX specification does not indicate any
50+
ordering for attribute parameters, it is recommended that attribute parameters be specified
51+
using keyword arguments (aka named arguments).
52+
53+
If the translator does not have an opschema for the called op, it uses the following
54+
strategy to map the actual parameters to appropriate input parameters and attribute parameters:
55+
Keyword arguments of Python are translated into attribute parameters (of ONNX), while positional arguments
4956
are translated into normal value-parameters.
50-
Thus, ``X`` is treated as a normal value-parameter (in ONNX) for this particular call, while
51-
``start`` and ``end`` are treated as attribute-parameters.
52-
This is a limitation of the current converter and is proposed to be relaxed
53-
when schema information is available for the callee indicating which are
54-
value-parameters and which are attribute-parameters.
57+
Thus, in the above example, ``X`` is treated as a normal value-parameter for this particular call, while
58+
``start`` and ``end`` are treated as attribute-parameters (when an opschema is unavailable).
59+
5560

5661
**Specifying tensor-valued attributes**
5762

@@ -106,13 +111,48 @@ The converter uses the type annotation on the formal input parameters to make th
106111
Thus, in the example below, ``alpha`` is treated as an attribute parameter (because of its ``float``
107112
type annotation).
108113

109-
.. literalinclude:: examples/leakyrelu.py
114+
.. literalinclude:: examples/leaky_relu.py
115+
116+
The (ONNX) types of attributes supported and their corresponding (Python) type annotations are shown
117+
in the table below. Other types of ONNX attributes are not yet supported.
118+
119+
====================== ======================
120+
ONNX Type Python Type Annotation
121+
====================== ======================
122+
AttributeProto.FLOAT float
123+
AttributeProto.INT int, bool
124+
AttributeProto.STRING str
125+
AttributeProto.FLOATS Sequence[float]
126+
AttributeProto.INTS Sequence[int]
127+
AttributeProto.STRINGS Sequence[str]
128+
====================== ======================
129+
130+
**Automatic promotion of attribute-parameters to values**
110131

111132
As illustrated in the above example, when an attribute-parameter is used in a context
112133
requiring a value-parameter, the converter will automatically convert the attribute
113134
into a tensor-value. Specifically, in the sub-expression ``alpha * X``, the attribute
114135
parameter ``alpha`` is used as a value-parameter of the call to the ``Mul`` op (denoted
115-
by the ``*``) and is automatically converted.
136+
by the ``*``) and is automatically converted. Thus,
137+
138+
.. literalinclude:: examples/leaky_relu.py
139+
140+
is expanded to the following:
141+
142+
.. literalinclude:: examples/leaky_relu_attr_promoted.py
143+
144+
**Automatic casts for constant values**
145+
146+
The converter also automatically introduces casts (via the ONNX ``CastLike`` op)
147+
when constants are used in a context where they are constrained to be of the
148+
same type as some other (non-constant) operand. For example, the expression
149+
``2 * X`` is expanded to ``op.CastLike(2, X) * X``, which allows the same
150+
code to work for different types of ``X``.
151+
152+
*Control-Flow*
153+
154+
The support for control-flow constructs in |onnxscript| is limited by
155+
requirements of ONNX control-flow ops.
116156

117157
**Conditional statements**
118158

0 commit comments

Comments
 (0)