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

Parameter studies #265

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e697300
Add a packer which tags the axes as uncertain.
nkoskelo Jul 1, 2024
8071f8a
Prototype for interface
nkoskelo Jul 8, 2024
72b9b61
Merge branch 'inducer:main' into uq_inner
nkoskelo Jul 8, 2024
40e33db
Merge branch 'main' into uq_inner
nkoskelo Jul 8, 2024
89ff83c
Move the parameter study definitions to their own folder and update t…
Jul 8, 2024
18bf0c8
Merge branch 'uq_inner' of github.com:nkoskelo/arraycontext into uq_i…
nkoskelo Jul 10, 2024
48a0155
Update terminology.
nkoskelo Jul 10, 2024
4106d32
Update interface.
nkoskelo Jul 10, 2024
1614454
Update interface.
nkoskelo Jul 10, 2024
e99610b
Another iteration on the mapper.
nkoskelo Jul 15, 2024
95e74ef
Add some test cases and a start on the index lambda transform.
nkoskelo Jul 15, 2024
804ed42
Update the Expansion Mapper for index lambda and move the packer to p…
nkoskelo Jul 16, 2024
607db79
Correct most of the type annotations.
nkoskelo Jul 16, 2024
d69cc00
Mypy update.
nkoskelo Jul 17, 2024
904adfc
Add in the mapper for the stack operation.
nkoskelo Jul 18, 2024
6a1e536
Update on concatenate and einsum operations.
nkoskelo Jul 19, 2024
506dbb9
Update the packing and unpacking tests to match the decision to have …
nkoskelo Jul 22, 2024
81b39bc
Add an advection example.
nkoskelo Jul 22, 2024
d0e806b
Fix formatting.
nkoskelo Jul 22, 2024
28e5860
Move the actual transform to pytato.
nkoskelo Jul 30, 2024
a3739b1
Merge branch 'main' into uq_inner
nkoskelo Jul 30, 2024
478fa3b
Update imports to match the location.
nkoskelo Jul 30, 2024
aed5080
Update.
nkoskelo Jul 30, 2024
470a80c
Fix pylint errors.
nkoskelo Jul 30, 2024
30ee1e8
Merge branch 'inducer:main' into uq_inner
nkoskelo Aug 6, 2024
65a9e59
Add in asserts to confirm that multiple single instance programs in s…
nkoskelo Aug 8, 2024
c0a5fc9
Implement packing for array containers.
nkoskelo Aug 15, 2024
28e7b70
Add the requirement to use the correct pytato branch.
nkoskelo Aug 15, 2024
9357fc9
Update the examples.
nkoskelo Aug 15, 2024
ccfc9bc
Trying to get the unpack to work so that you only need to index in wi…
nkoskelo Aug 21, 2024
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
3 changes: 2 additions & 1 deletion arraycontext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
pytest_generate_tests_for_pyopencl_array_context,
)
from .transform_metadata import CommonSubexpressionTag, ElementwiseMapKernelTag

from .parameter_study import pack_for_parameter_study, unpack_parameter_study
from .parameter_study.transform import ParamStudyPytatoPyOpenCLArrayContext

__all__ = (
"Array",
Expand Down
10 changes: 8 additions & 2 deletions arraycontext/impl/pytato/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@
import numpy as np

from pytools import memoize_method
from pytools.tag import Tag, ToTagSetConvertible, normalize_tags
from pytools.tag import Tag, ToTagSetConvertible, normalize_tags, UniqueTag

from arraycontext.container.traversal import rec_map_array_container, with_array_context
from arraycontext.context import Array, ArrayContext, ArrayOrContainer, ScalarLike
from arraycontext.metadata import NameHint

from dataclasses import dataclass

if TYPE_CHECKING:
import pyopencl as cl
Expand Down Expand Up @@ -562,7 +563,10 @@ def _to_frozen(key: Tuple[Any, ...], ary) -> TaggableCLArray:
evt, out_dict = pt_prg(self.queue,
allocator=self.allocator,
**bound_arguments)
evt.wait()
if isinstance(evt, list):
[_evt.wait() for _evt in evt]
else:
evt.wait()
assert len(set(out_dict) & set(key_to_frozen_subary)) == 0

key_to_frozen_subary = {
Expand Down Expand Up @@ -699,6 +703,8 @@ def clone(self):
# }}}




# {{{ PytatoJAXArrayContext

class PytatoJAXArrayContext(_BasePytatoArrayContext):
Expand Down
5 changes: 4 additions & 1 deletion arraycontext/impl/pytato/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,10 @@ def __call__(self, arg_id_to_arg) -> ArrayContainer:
# FIXME Kernels (for now) allocate tons of memory in temporaries. If we
# race too far ahead with enqueuing, there is a distinct risk of
# running out of memory. This mitigates that risk a bit, for now.
evt.wait()
if isinstance(evt, list):
[_evt.wait() for _evt in evt]
else:
evt.wait()

def to_output_template(keys, _):
name_in_program = self.output_id_to_name_in_program[keys]
Expand Down
146 changes: 146 additions & 0 deletions arraycontext/parameter_study/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
"""
.. currentmodule:: arraycontext

A :mod:`pytato`-based array context defers the evaluation of an array until its
frozen. The execution contexts for the evaluations are specific to an
:class:`~arraycontext.ArrayContext` type. For ex.
:class:`~arraycontext.ParamStudyPytatoPyOpenCLArrayContext`
uses :mod:`pyopencl` to JIT-compile and execute the array expressions.

Following :mod:`pytato`-based array context are provided:

.. autoclass:: ParamStudyPytatoPyOpenCLArrayContext

The compiled function is stored as.
.. autoclass:: ParamStudyLazyPyOpenCLFunctionCaller


Compiling a Python callable (Internal) for multiple distinct instances of
execution.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. automodule:: arraycontext.parameter_study
"""
__copyright__ = """
Copyright (C) 2020-1 University of Illinois Board of Trustees
"""

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import sys
from typing import (
TYPE_CHECKING,
Any,
Dict,
List,
Mapping,
Tuple,
Type,
)

import numpy as np

import pytato as pt
from pytato.array import Array

from arraycontext.context import ArrayContext
from arraycontext.parameter_study.transform import ParameterStudyAxisTag


ParamStudyTagT = Type[ParameterStudyAxisTag]

if TYPE_CHECKING:
import pyopencl as cl
import pytato

if getattr(sys, "_BUILDING_SPHINX_DOCS", False):
import pyopencl as cl

import logging


logger = logging.getLogger(__name__)


def pack_for_parameter_study(actx: ArrayContext,
study_name_tag_type: ParamStudyTagT,
newshape: Tuple[int, ...],
*args: Array) -> Array:
"""
Args is a list of variable names and the realized input data that needs
to be packed for a parameter study or uncertainty quantification.

Args needs to be in the format
[v0, v1, v2, ..., vN] where N is the total number of instances you want to
try. Note these may be across multiple parameter studies on the same inputs.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you looked at this docstring rendered?

"""

assert len(args) > 0
assert len(args) == np.prod(newshape)

orig_shape = args[0].shape
out = actx.np.stack(args, axis=len(args[0].shape))
outshape = tuple([*list(orig_shape), newshape])

# if len(newshape) > 1:
# # Reshape the object
# out = out.reshape(outshape)

for i in range(len(orig_shape), len(outshape)):
out = out.with_tagged_axis(i, [study_name_tag_type(i - len(orig_shape),
newshape[i-len(orig_shape)])])
return out


def unpack_parameter_study(data: Array,
study_name_tag_type: ParamStudyTagT) -> Mapping[int,
List[Array]]:
"""
Split the data array along the axes which vary according to
a ParameterStudyAxisTag whose name tag is an instance study_name_tag_type.

output[i] corresponds to the values associated with the ith parameter study that
uses the variable name :arg: `study_name_tag_type`.
"""

ndim: int = len(data.shape)
out: Dict[int, List[Array]] = {}

study_count = 0
for i in range(ndim):
axis_tags = data.axes[i].tags_of_type(study_name_tag_type)
if axis_tags:
# Now we need to split this data.
breakpoint()
for j in range(data.shape[i]):
tmp: List[Any] = [slice(None)] * ndim
tmp[i] = j
the_slice = tuple(tmp)
# Needs to be a tuple of slices not list of slices.
if study_count in out.keys():
out[study_count].append(data[the_slice])
else:
out[study_count] = [data[the_slice]]
if study_count in out.keys():
study_count += 1
# yield data[the_slice]

return out
Loading
Loading