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

refactor[next] Prepare new Field for itir.embedded #1329

Merged
merged 22 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
15 changes: 8 additions & 7 deletions docs/user/cartesian/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
SRCDIR = ../../../src/gt4py
AUTODOCDIR = _source
BUILDDIR = _build
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
SRCDIR = ../../../src/gt4py
SPHINX_APIDOC_OPTS = --private # private modules for gt4py._core
AUTODOCDIR = _source
BUILDDIR = _build

# User-friendly check for sphinx-build
ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1)
Expand Down Expand Up @@ -55,7 +56,7 @@ clean:
autodoc:
@echo
@echo "Running sphinx-apidoc..."
sphinx-apidoc ${SPHINX_OPTS} -o ${AUTODOCDIR} ${SRCDIR}
sphinx-apidoc ${SPHINX_APIDOC_OPTS} -o ${AUTODOCDIR} ${SRCDIR}
@echo
@echo "sphinx-apidoc finished. The generated autodocs are in $(AUTODOCDIR)."

Expand Down
6 changes: 5 additions & 1 deletion docs/user/cartesian/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Internally, gt4py uses the utilities :code:`gt4py.utils.as_numpy` and :code:`gt4
buffers. GT4Py developers are advised to always use those utilities as to guarantee support across gt4py as the
supported interfaces are extended.

.. _cartesian-arrays-dimension-mapping:

Dimension Mapping
^^^^^^^^^^^^^^^^^

Expand All @@ -56,6 +58,8 @@ which implements this lookup.
Note: Support for xarray can be added manually by the user by means of the mechanism described
`here <https://xarray.pydata.org/en/stable/internals/extending-xarray.html>`_.

.. _cartesian-arrays-default-origin:

Default Origin
^^^^^^^^^^^^^^

Expand Down Expand Up @@ -180,4 +184,4 @@ Additionally, these **optional** keyword-only parameters are accepted:
determine the default layout for the storage. Currently supported will be :code:`"I"`,
:code:`"J"`, :code:`"K"` and additional dimensions as string representations of integers,
starting at :code:`"0"`. (This information is not retained in the resulting array, and needs to be specified instead
with the :code:`__gt_dims__` interface. )
with the :code:`__gt_dims__` interface. )
52 changes: 39 additions & 13 deletions src/gt4py/_core/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,13 @@ class DType(Generic[ScalarT]):
"""

scalar_type: Type[ScalarT]
tensor_shape: TensorShape
tensor_shape: TensorShape = dataclasses.field(default=())

def __init__(
self, scalar_type: Type[ScalarT], tensor_shape: Sequence[IntegralScalar] = ()
) -> None:
if not isinstance(scalar_type, type):
raise TypeError(f"Invalid scalar type '{scalar_type}'")
if not is_valid_tensor_shape(tensor_shape):
raise TypeError(f"Invalid tensor shape '{tensor_shape}'")

object.__setattr__(self, "scalar_type", scalar_type)
object.__setattr__(self, "tensor_shape", tensor_shape)
def __post_init__(self) -> None:
if not isinstance(self.scalar_type, type):
raise TypeError(f"Invalid scalar type '{self.scalar_type}'")
if not is_valid_tensor_shape(self.tensor_shape):
raise TypeError(f"Invalid tensor shape '{self.tensor_shape}'")

@functools.cached_property
def kind(self) -> DTypeKind:
Expand All @@ -251,6 +246,14 @@ def lanes(self) -> int:
def subndim(self) -> int:
return len(self.tensor_shape)

def __eq__(self, other: Any) -> bool:
# TODO: discuss (make concrete subclasses equal to instances of this with the same type)
return (
isinstance(other, DType)
and self.scalar_type == other.scalar_type
and self.tensor_shape == other.tensor_shape
)


@dataclasses.dataclass(frozen=True)
class IntegerDType(DType[IntegralT]):
Expand Down Expand Up @@ -322,6 +325,11 @@ class Float64DType(FloatingDType[float64]):
scalar_type: Final[Type[float64]] = dataclasses.field(default=float64, init=False)


@dataclasses.dataclass(frozen=True)
class BoolDType(DType[bool_]):
scalar_type: Final[Type[bool_]] = dataclasses.field(default=bool_, init=False)


DTypeLike = Union[DType, npt.DTypeLike]


Expand All @@ -332,11 +340,29 @@ def dtype(dtype_like: DTypeLike) -> DType:

# -- Custom protocols --
class GTDimsInterface(Protocol):
__gt_dims__: Tuple[str, ...]
"""
A `GTDimsInterface` is an object providing the `__gt_dims__` property, naming the buffer dimensions.

In `gt4py.cartesian` the allowed values are `"I"`, `"J"` and `"K"` with the established semantics.

See :ref:`cartesian-arrays-dimension-mapping` for details.
"""

@property
def __gt_dims__(self) -> Tuple[str, ...]:
...


class GTOriginInterface(Protocol):
__gt_origin__: Tuple[int, ...]
"""
A `GTOriginInterface` is an object providing `__gt_origin__`, describing the origin of a buffer.

See :ref:`cartesian-arrays-default-origin` for details.
"""

@property
def __gt_origin__(self) -> Tuple[int, ...]:
...


# -- Device representation --
Expand Down
1 change: 1 addition & 0 deletions src/gt4py/next/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from . import common, ffront, iterator, program_processors, type_inference
from .common import Dimension, DimensionKind, Field, GridType
from .embedded import nd_array_field
from .ffront import fbuiltins
from .ffront.decorator import field_operator, program, scan_operator
from .ffront.fbuiltins import * # noqa: F403 # fbuiltins defines __all__ and we explicitly want to reexport everything here
Expand Down
Loading