Skip to content
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

[IR] Improve from_proto/to_proto typing with overloads #1992

Merged
merged 8 commits into from
Dec 31, 2024
Merged
Changes from 2 commits
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
47 changes: 37 additions & 10 deletions onnxscript/ir/serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import annotations

import functools
import typing

__all__ = [
# Tensors
Expand Down Expand Up @@ -59,7 +60,6 @@
import collections
import logging
import os
import typing
from typing import Any, Callable, List, Mapping, Sequence

import numpy as np
Expand Down Expand Up @@ -121,16 +121,33 @@
return array[::2] + 1j * array[1::2]


@typing.overload
def from_proto(proto: onnx.ModelProto) -> _core.Model: ...
Fixed Show fixed Hide fixed
@typing.overload
def from_proto(proto: onnx.GraphProto) -> _core.Graph: ...
Fixed Show fixed Hide fixed
@typing.overload
def from_proto(proto: onnx.NodeProto) -> _core.Node: ...
Fixed Show fixed Hide fixed
@typing.overload
def from_proto(proto: onnx.TensorProto) -> _core.Tensor: ...
Fixed Show fixed Hide fixed
@typing.overload
def from_proto(proto: onnx.AttributeProto) -> _core.Attr: ...
Fixed Show fixed Hide fixed
@typing.overload
def from_proto(proto: onnx.ValueInfoProto) -> _core.Value: ...
Fixed Show fixed Hide fixed
@typing.overload
def from_proto(proto: onnx.TypeProto) -> _core.TypeAndShape: ...
Fixed Show fixed Hide fixed
@typing.overload
def from_proto(proto: onnx.FunctionProto) -> _core.Function: ...
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
@typing.overload
def from_proto(
proto: onnx.ModelProto
| onnx.GraphProto
| onnx.NodeProto
| onnx.TensorProto
| onnx.AttributeProto
| onnx.ValueInfoProto
| onnx.TypeProto
| onnx.FunctionProto,
) -> Any:
proto: onnx.TensorShapeProto.Dimension,
) -> tuple[int | _core.SymbolicDim, str | None]: ...

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.
@typing.overload
def from_proto(proto: Sequence[onnx.OperatorSetIdProto]) -> dict[str, int]: ...
Fixed Show fixed Hide fixed
@typing.overload
def from_proto(proto: Sequence[onnx.StringStringEntryProto]) -> dict[str, str]: ...
Fixed Show fixed Hide fixed


def from_proto(proto: object) -> object:
"""Deserialize an ONNX proto message to an IR object."""
if isinstance(proto, onnx.ModelProto):
return deserialize_model(proto)
Expand All @@ -151,6 +168,16 @@
)
if isinstance(proto, onnx.FunctionProto):
return deserialize_function(proto)
if isinstance(proto, onnx.TensorShapeProto.Dimension):
return deserialize_dimension(proto)
if isinstance(proto, Sequence) and all(
isinstance(p, onnx.OperatorSetIdProto) for p in proto
):
return deserialize_opset_import(proto)
if isinstance(proto, Sequence) and all(
isinstance(p, onnx.StringStringEntryProto) for p in proto
):
return deserialize_metadata_props(proto)
raise NotImplementedError(
f"Deserialization of {type(proto)} in from_proto is not implemented. "
"Use a specific ir.serde.deserialize* function instead."
Expand Down
Loading