Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/requirements/lintrunner/pylin…
Browse files Browse the repository at this point in the history
…t-3.1.0
  • Loading branch information
justinchuby authored Apr 2, 2024
2 parents 9f803e6 + e87e05d commit 6cc1f66
Show file tree
Hide file tree
Showing 65 changed files with 5,370 additions and 368 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ jobs:
- name: Upload coverage to Codecov
if: always()
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/01_plot_selu.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def Selu(X, alpha: float, gamma: float):
neg = gammaX * (alphaX * op.Exp(X) - alphaX)
pos = gammaX * X
zero = op.CastLike(0, X)
return op.Where(X <= zero, neg, pos)
return op.Where(zero >= X, neg, pos)


# %%
Expand Down
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"jinja2",
"numpy==1.24.4",
"typing_extensions",
"beartype!=0.16.0",
"beartype==0.17.2",
"types-PyYAML",
"expecttest==0.1.6",
"hypothesis",
Expand All @@ -26,7 +26,7 @@
"pytest!=7.1.0",
"pyyaml",
)
ONNX = "onnx==1.14.1"
ONNX = "onnx==1.15"
ONNX_RUNTIME = "onnxruntime==1.16.1"
PYTORCH = "torch==2.1.0"
TORCHVISON = "torchvision==0.16"
Expand Down
2 changes: 1 addition & 1 deletion onnxscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
from ._internal.utils import external_tensor
from .values import OnnxFunction, TracedOnnxFunction

try:
try: # noqa: SIM105
__version__ = importlib.metadata.version("onnxscript")
except importlib.metadata.PackageNotFoundError:
# package is not installed
Expand Down
2 changes: 1 addition & 1 deletion onnxscript/_internal/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

def _get_loop_var(for_stmt: ast.For, formatter: sourceinfo.Formatter) -> str:
if not isinstance(for_stmt.target, ast.Name):
raise ValueError(formatter(for_stmt, "For loop target must be a single variable."))
raise TypeError(formatter(for_stmt, "For loop target must be a single variable."))
return for_stmt.target.id


Expand Down
64 changes: 7 additions & 57 deletions onnxscript/_internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
from __future__ import annotations

import numbers
from typing import Any, Iterable, Optional, Sequence
from typing import Optional, Sequence

import numpy as np
import onnx
import onnx.helper
from onnx import FunctionProto, ModelProto, TensorProto, ValueInfoProto

from onnxscript import tensor

# pylint: enable=unused-import, ungrouped-imports


def external_tensor(
name: str,
Expand All @@ -26,7 +23,7 @@ def external_tensor(
length: Optional[int] = None,
checksum: Optional[str] = None,
basepath: Optional[str] = None,
) -> TensorProto:
) -> onnx.TensorProto:
"""Create a TensorProto referencing externally stored tensor-data.
Args:
Expand All @@ -44,11 +41,11 @@ def external_tensor(
See https://github.com/onnx/onnx/blob/main/docs/ExternalData.md for more details.
"""
tensor_proto = TensorProto()
tensor_proto = onnx.TensorProto()
tensor_proto.name = name
tensor_proto.data_type = data_type
tensor_proto.dims.extend(dims)
tensor_proto.data_location = TensorProto.EXTERNAL
tensor_proto.data_location = onnx.TensorProto.EXTERNAL

def add(k, v):
entry = tensor_proto.external_data.add()
Expand All @@ -74,17 +71,17 @@ def value_to_type_proto(val):
shape = val.shape
return onnx.helper.make_tensor_type_proto(elem_type, shape)
if isinstance(val, int):
return onnx.helper.make_tensor_type_proto(TensorProto.INT32, [])
return onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT32, [])
if isinstance(val, (float, np.float32)):
return onnx.helper.make_tensor_type_proto(TensorProto.FLOAT, [])
return onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [])
if isinstance(val, list):
if len(val) > 0:
return onnx.helper.make_sequence_type_proto(value_to_type_proto(val[0]))
# Edge-case. Cannot determine a suitable ONNX type for an empty list.
# Should be using a typed-value instead.
# Treated as a sequence of tensors of float-type.
return onnx.helper.make_sequence_type_proto(
onnx.helper.make_tensor_type_proto(TensorProto.FLOAT, None)
onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, None)
)
if isinstance(val, numbers.Number):
nparray = np.array(val)
Expand All @@ -102,50 +99,3 @@ def values_to_value_infos(name_values):
for (name, val) in name_values
if val is not None
]


def make_model_from_function_proto(
function_proto: FunctionProto,
function_opset_version: int,
input_value_infos: Sequence[ValueInfoProto],
output_value_infos: Sequence[ValueInfoProto],
**attrs: Any,
) -> ModelProto:
"""Creates a model containing a single call to a given
function with input and output value_infos, etc.
Args:
function_proto (FunctionProto): function proto
representing a single call
function_opset_version (int): function_proto's version
input_value_infos (list of ValueInfoProto): function's input
output_value_infos (list of ValueInfoProto): function's output
**attrs (dict): the attributes of the node for the function
Returns:
ModelProto
"""

input_names = [vi.name for vi in input_value_infos]
output_names = [vi.name for vi in output_value_infos]
node = onnx.helper.make_node(
function_proto.name,
input_names,
output_names,
domain=function_proto.domain,
**attrs,
)
graph = onnx.helper.make_graph([node], "node_graph", input_value_infos, output_value_infos)
model_proto_opset: Iterable[onnx.OperatorSetIdProto] = function_proto.opset_import
if all(o.domain != function_proto.domain for o in model_proto_opset):
model_proto_opset = [
*model_proto_opset,
onnx.helper.make_opsetid(function_proto.domain, function_opset_version),
]
model = onnx.helper.make_model(
graph,
functions=[function_proto],
producer_name="onnxscript",
opset_imports=model_proto_opset,
)
return model
3 changes: 3 additions & 0 deletions onnxscript/_thirdparty/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Third-party

Third-party vendored libraries. License in respective packages.
Loading

0 comments on commit 6cc1f66

Please sign in to comment.