Skip to content

Commit

Permalink
Initial exposition of initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Atanas Dimitrov committed Jan 19, 2024
1 parent 10af299 commit de8530e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/spox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import importlib.metadata

from spox._public import argument, build, inline
from spox._public import argument, build, initializer, inline
from spox._type_system import Optional, Sequence, Tensor, Type
from spox._var import Var

Expand All @@ -11,6 +11,7 @@
"Sequence",
"Optional",
"argument",
"initializer",
"build",
"inline",
]
Expand Down
30 changes: 0 additions & 30 deletions src/spox/_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
from typing import Iterable, List, Optional, Union

import numpy as np
import numpy.typing as npt

import spox._node
import spox._value_prop
from spox._graph import initializer as _initializer
from spox._type_system import Tensor
from spox._var import Var

Expand Down Expand Up @@ -42,32 +40,6 @@ def value_prop_backend(backend: ValuePropBackend):
set_value_prop_backend(prev_backend)


def initializer(value: npt.ArrayLike, dtype: npt.DTypeLike = None) -> Var:
"""
Create a Var with a constant value.
Parameters
----------
value
Array-like value for the variable.
dtype
Data type for the given value. If ``None``, it is inferred from the value
using numpy rules (``numpy.array(value)``).
Returns
-------
Var
Variable with the given constant ``value``.
Notes
-----
When the model is built, constants created by this function become initializers.
As such, they are independent of an opset version and are listed separately
in the model. Initializers are also used internally in Spox.
"""
return _initializer(np.array(value, dtype))


class _NumpyLikeOperatorDispatcher:
def __init__(self, op, type_promotion: bool, constant_promotion: bool):
self.op = op
Expand Down Expand Up @@ -210,8 +182,6 @@ def operator_overloading(
"TypeWarningLevel",
"set_type_warning_level",
"type_warning_level",
# Initializer-backed constants
"initializer",
# Value propagation backend
"ValuePropBackend",
"set_value_prop_backend",
Expand Down
33 changes: 31 additions & 2 deletions src/spox/_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
from typing import Dict, List, Optional, Protocol

import numpy as np
import numpy.typing as npt
import onnx
from onnx.numpy_helper import to_array

from . import _internal_op
from ._attributes import AttrType
from ._graph import Argument, initializer, results
from ._graph import Argument
from ._graph import initializer as _initializer
from ._graph import results
from ._inline import _Inline
from ._standard import _strip_dim_symbol
from ._type_system import Type
Expand All @@ -37,6 +40,32 @@ def argument(typ: Type) -> Var:
).outputs.arg


def initializer(value: npt.ArrayLike, dtype: npt.DTypeLike = None) -> Var:
"""
Create a Var with a constant value.
Parameters
----------
value
Array-like value for the variable.
dtype
Data type for the given value. If ``None``, it is inferred from the value
using numpy rules (``numpy.array(value)``).
Returns
-------
Var
Variable with the given constant ``value``.
Notes
-----
When the model is built, constants created by this function become initializers.
As such, they are independent of an opset version and are listed separately
in the model. Initializers are also used internally in Spox.
"""
return _initializer(np.array(value, dtype))


@contextlib.contextmanager
def _temporary_renames(**kwargs: Var):
# The build code can't really special-case variable names that are
Expand Down Expand Up @@ -284,4 +313,4 @@ def inline_inner(*args: Var, **kwargs: Var) -> Dict[str, Var]:
return inline_inner


__all__ = ["argument", "build", "inline"]
__all__ = ["argument", "initializer", "build", "inline"]
2 changes: 1 addition & 1 deletion tests/test_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy
import pytest

from spox._future import initializer
from spox import initializer

TESTED_INITIALIZER_ROWS: List[List[Any]] = [
[0, 1, 2],
Expand Down
3 changes: 1 addition & 2 deletions tests/test_subgraphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import pytest

import spox.opset.ai.onnx.v17 as op
from spox import Var
from spox import Var, initializer
from spox._exceptions import BuildError
from spox._future import initializer
from spox._graph import arguments, results
from spox._type_system import Sequence, Tensor

Expand Down

0 comments on commit de8530e

Please sign in to comment.