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 2 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
127 changes: 126 additions & 1 deletion arraycontext/impl/pytato/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Following :mod:`pytato`-based array context are provided:

.. autoclass:: PytatoPyOpenCLArrayContext
.. autoclass:: PytatoPyOpenCLArrayContextUQ
.. autoclass:: PytatoJAXArrayContext


Expand Down Expand Up @@ -50,13 +51,14 @@
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 @@ -684,6 +686,129 @@ def clone(self):
# }}}


@dataclass(frozen=True)
class UQAxisTag(UniqueTag):
nkoskelo marked this conversation as resolved.
Show resolved Hide resolved
nkoskelo marked this conversation as resolved.
Show resolved Hide resolved
"""
A tag for acting on axes of arrays.
nkoskelo marked this conversation as resolved.
Show resolved Hide resolved
"""
uq_instance_num: str
nkoskelo marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

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

It seems that this indirectly identifies the parameter study? Why not identify parameter studies by type.

nkoskelo marked this conversation as resolved.
Show resolved Hide resolved


# {{{ PytatoPyOpenCLArrayContextUQ


class PytatoPyOpenCLArrayContextUQ(PytatoPyOpenCLArrayContext):
Copy link
Owner

Choose a reason for hiding this comment

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

English says descriptive things go first.

"""
A derived class for PytatoPyOpenCLArrayContext updated for the
purpose of enabling parameter studies and uncertainty quantification.

.. automethod:: __init__

.. automethod:: transform_dag

.. automethod:: compile
"""
def compile(self, f: Callable[..., Any]) -> Callable[..., Any]:
# TODO: Update to a new compiler potentially
from .compile import LazilyPyOpenCLCompilingFunctionCaller
return LazilyPyOpenCLCompilingFunctionCaller(self, f)

def transform_dag(self, dag: "pytato.DictOfNamedArrays"
) -> "pytato.DictOfNamedArrays":
import pytato as pt
# TODO: This gets called before generating the placeholders
dag = pt.transform.materialize_with_mpms(dag)
return dag

def pack_for_uq(self,*args) -> dict:
nkoskelo marked this conversation as resolved.
Show resolved Hide resolved
nkoskelo marked this conversation as resolved.
Show resolved Hide resolved
"""
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
["v", v0, v1, v2, ..., vN, "w", w0, w1, w2, ..., wM, \dots]

where "v" and "w" would be the variable names in your program.
If you want to include a constant just pass the var name and then
the value in the next argument.

Returns a dictionary of {var name: stacked array}
"""
from arraycontext.impl.pytato.fake_numpy import PytatoFakeNumpyNamespace

assert len(args) > 0
out = {}
curr_var = str(args[0])

num_calls = 0

for ind in range(1, len(args)):
val = args[ind]
if isinstance(val, str):
# Done with previous.
if val in out.keys():
raise ValueError("Repeated definitions of variable: " + str(val) \
+ " Defined Variables: " + list(out.keys()))
out[curr_var] = PytatoFakeNumpyNamespace.stack(self, out[curr_var])

if out[curr_var].shape[0] > 1:
# Tag the outer axis as uncertain.
tag_name = str(num_calls) + " var: " + str(curr_var)
out[curr_var] = out[curr_var].with_tagged_axis(0, [UQAxisTag(tag_name)])
num_calls += 1
curr_var = val

elif curr_var in out.keys():
out[curr_var].append(val)
else:
out[curr_var] = [val]

# Handle the last variable group.
out[curr_var] = PytatoFakeNumpyNamespace.stack(self, out[curr_var])
nkoskelo marked this conversation as resolved.
Show resolved Hide resolved

if out[curr_var].shape[0] > 1:
# Tag the outer axis as uncertain.
tag_name = str(num_calls) + " var: " + str(curr_var)
out[curr_var] = out[curr_var].with_tagged_axis(0, [UQAxisTag(tag_name)])
num_calls += 1

return out


def unpack(self, data):
"""
Revert data to a sequence of outputs under the assumption that a specific variable
is held constant.

::arg:: data multidimensional array tagged with dimensions that are varying.
UQAxisTag will tag each specific axis that we are going to slice.
"""

ndim = len(data.axes)

out = {}


for i in range(ndim):
axis_tags = data.axes[i].tags_of_type(UQAxisTag)
if axis_tags:
# Now we need to split this data.
for j in range(len(data.axis[i])):
the_slice = [slice(None)] * ndim
the_slice[i] = j
if i in out.keys():
out[i].append(data[the_slice])
else:
out[i] = data[the_slice]
#yield data[the_slice]


return out


# }}}


# {{{ PytatoJAXArrayContext

class PytatoJAXArrayContext(_BasePytatoArrayContext):
Expand Down
48 changes: 48 additions & 0 deletions examples/uncertain_prop.py
nkoskelo marked this conversation as resolved.
Show resolved Hide resolved
nkoskelo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import arraycontext
from dataclasses import dataclass

import numpy as np # for the data types
from pytools.tag import Tag

from arraycontext.impl.pytato.__init__ import (PytatoPyOpenCLArrayContextUQ,
PytatoPyOpenCLArrayContext)

# The goal of this file is to propagate the uncertainty in an array to the output.


my_context = arraycontext.impl.pytato.PytatoPyOpenCLArrayContext
a = my_context.zeros(my_context, shape=(5,5), dtype=np.int32) + 2

b = my_context.zeros(my_context, (5,5), np.int32) + 15

print(a)
print("========================================================")
print(b)
print("========================================================")

# Eq: z = x + y
# Assumptions: x and y are independently uncertain.


x = np.random.random((15,5))
x1 = np.random.random((15,5))
x2 = np.random.random((15,5))

y = np.random.random((15,5))
y1 = np.random.random((15,5))
y2 = np.random.random((15,5))


actx = PytatoPyOpenCLArrayContextUQ
Copy link
Owner

Choose a reason for hiding this comment

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

You'll probably need to instantiate the array context.


out = actx.pack_for_uq(actx,"x", x, x1, x2, "y", y, y1, y2)
Copy link
Owner

Choose a reason for hiding this comment

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

Terminology.

Copy link
Owner

Choose a reason for hiding this comment

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

Is there a good reason to use a batched interface here? Will this result in a type-safe interface?

Copy link
Owner

Choose a reason for hiding this comment

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

Wouldn't you need to supply the tag?

Copy link
Owner

Choose a reason for hiding this comment

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

Why should this be a method on the array context, compared to a separate function?

print("===============OUT======================")
print(out)

x = out["x"]
y = out["y"]

breakpoint()

x + y

Loading