Implementations of the Array Context Abstraction¶
Array context based on numpy¶
A numpy-based array context.
- class arraycontext.NumpyArrayContext[source]¶
A
ArrayContextthat usesnumpy.ndarrayto represent arrays.
Array context based on pyopencl.array¶
- class arraycontext.PyOpenCLArrayContext(queue: cl.CommandQueue, allocator: cl_array.Allocator | None = None, wait_event_queue_length: int | None = None, force_device_scalars: bool | None = None)[source]¶
A
ArrayContextthat usespyopencl.array.Arrayinstances for its base array class.- context¶
- queue¶
- allocator¶
A PyOpenCL memory allocator. Can also be None (default) or False to use the default allocator. Please note that running with the default allocator allocates and deallocates OpenCL buffers directly. If lots of arrays are created (e.g. as results of computation), the associated cost may become significant. Using e.g.
pyopencl.tools.MemoryPoolas the allocator can help avoid this cost.
- transform_loopy_program(t_unit: lp.TranslationUnit) lp.TranslationUnit[source]¶
- class arraycontext.impl.pyopencl.taggable_cl_array.TaggableCLArray(cq, shape, dtype, order='C', allocator=None, data=None, offset=0, strides=None, events=None, _flags=None, _fast=False, _size=None, _context=None, _queue=None, axes=None, tags=frozenset({}))[source]¶
A
pyopencl.array.Arraywith additional metadata. This is used byPytatoPyOpenCLArrayContextto preserve tags for data while frozen, and also in a similar capacity byPyOpenCLArrayContext.- tags¶
A
frozensetofpytools.tag.Tag. Typically intended to record application-specific metadata to drive the optimizations inarraycontext.PyOpenCLArrayContext.transform_loopy_program().
- class arraycontext.impl.pyopencl.taggable_cl_array.Axis(tags: frozenset[Tag])[source]¶
Records the tags corresponding to a dimension of
TaggableCLArray.
- arraycontext.impl.pyopencl.taggable_cl_array.to_tagged_cl_array(ary: Array, axes: tuple[Axis, ...] | None = None, tags: frozenset[Tag] = frozenset({})) TaggableCLArray[source]¶
Returns a
TaggableCLArraythat is constructed from the data in ary along with the metadata from axes and tags. If ary is already aTaggableCLArray, the new tags and axes are added to the existing ones.- Parameters:
axes – An instance of
Axisfor each dimension of the array. If passed None, then initialized to apytato.Axiswith no tags attached for each dimension.
Lazy/Deferred evaluation array context based on pytato¶
A pytato-based array context defers the evaluation of an array until it is
frozen. The execution contexts for the evaluations are specific to an
ArrayContext type. For example,
PytatoPyOpenCLArrayContext uses pyopencl to
JIT-compile and execute the array expressions.
The following pytato-based array contexts are provided:
- class arraycontext.PytatoPyOpenCLArrayContext(queue: cl.CommandQueue, allocator: cl_array.Allocator | None = None, *, use_memory_pool: bool | None = None, compile_trace_callback: Callable[[Any, str, Any], None] | None = None, profile_kernels: bool = False, _force_svm_arg_limit: int | None = None)[source]¶
An
ArrayContextthat usespytatodata types to represent the arrays targeting OpenCL for offloading operations.- queue¶
- allocator¶
A
pyopenclmemory allocator. Can also be None (default) or False to use the default allocator.
- __init__(queue: cl.CommandQueue, allocator: cl_array.Allocator | None = None, *, use_memory_pool: bool | None = None, compile_trace_callback: Callable[[Any, str, Any], None] | None = None, profile_kernels: bool = False, _force_svm_arg_limit: int | None = None) None[source]¶
- Parameters:
compile_trace_callback – A function of three arguments (what, stage, ir), where what identifies the object being compiled, stage is a string describing the compilation pass, and ir is an object containing the intermediate representation. This interface should be considered unstable.
- transform_dag(dag: pytato.AbstractResultWithNamedArrays) pytato.AbstractResultWithNamedArrays[source]¶
Returns a transformed version of dag. Sub-classes are supposed to override this method to implement context-specific transformations on dag (most likely to perform domain-specific optimizations). Every
pytatoDAG that is compiled to a GPU-kernel is passed through this routine.- Parameters:
dag – An instance of
pytato.DictOfNamedArrays- Returns:
A transformed version of dag.
- compile(f: Callable[P, ArrayOrArithContainerOrScalarT]) Callable[P, ArrayOrArithContainerOrScalarT][source]¶
Compiles f for repeated use on this array context. f is expected to be a pure function performing an array computation.
Control flow statements (
if,while) that might take different paths depending on the data lead to undefined behavior and are illegal. Any data-dependent control flow must be expressed via array functions, such asactx.np.where.f may be called on placeholder data, to obtain a representation of the computation performed, or it may be called as part of the actual computation, on actual data. If f is called on placeholder data, it may be called only once (or a few times).
- Parameters:
f – the function executing the computation.
- Returns:
a function with the same signature as f.
- class arraycontext.PytatoJAXArrayContext(*, compile_trace_callback: Callable[[Any, str, Any], None] | None = None)[source]¶
An arraycontext that uses
pytatoto represent the thawed state of the arrays and compiles the expressions usingpytato.target.python.JAXPythonTarget.- transform_dag(dag: pytato.AbstractResultWithNamedArrays) pytato.AbstractResultWithNamedArrays[source]¶
Returns a transformed version of dag. Sub-classes are supposed to override this method to implement context-specific transformations on dag (most likely to perform domain-specific optimizations). Every
pytatoDAG that is compiled to a GPU-kernel is passed through this routine.- Parameters:
dag – An instance of
pytato.DictOfNamedArrays- Returns:
A transformed version of dag.
Compiling a Python callable (Internal)¶
- class arraycontext.impl.pytato.compile.BaseLazilyCompilingFunctionCaller(actx: _BasePytatoArrayContext, f: Callable[..., Any], program_cache: dict[Mapping[tuple[Hashable, ...], AbstractInputDescriptor], CompiledFunction] = <factory>)[source]¶
Records a side-effect-free callable
fthat can be specialized for the input types with which__call__()is invoked.- __call__(*args: Array | ObjectArray[tuple[int, ...], ArrayOrContainerOrScalar] | _UserDefinedArrayContainer | int | integer | float | complex | inexact | bool | bool, **kwargs: Array | ObjectArray[tuple[int, ...], ArrayOrContainerOrScalar] | _UserDefinedArrayContainer | int | integer | float | complex | inexact | bool | bool) Array | ObjectArray[tuple[int, ...], ArrayOrContainerOrScalar] | _UserDefinedArrayContainer | int | integer | float | complex | inexact | bool | bool[source]¶
Returns the result of
f’s function application on args.Before applying
f, it is compiled to apytatoDAG that would applyfwith args in a lazy-sense. The intermediary pytato DAG for args is memoized in self.
- class arraycontext.impl.pytato.compile.LazilyPyOpenCLCompilingFunctionCaller(actx: _BasePytatoArrayContext, f: Callable[..., Any], program_cache: dict[Mapping[tuple[Hashable, ...], AbstractInputDescriptor], CompiledFunction] = <factory>)[source]¶
- class arraycontext.impl.pytato.compile.LazilyJAXCompilingFunctionCaller(actx: _BasePytatoArrayContext, f: Callable[..., Any], program_cache: dict[Mapping[tuple[Hashable, ...], AbstractInputDescriptor], CompiledFunction] = <factory>)[source]¶
- class arraycontext.impl.pytato.compile.CompiledFunction[source]¶
A callable which captures the
pytato.target.BoundProgramresulting from callingfwith a given set of input types, and generatingloopyIR from it.- pytato_program¶
- input_id_to_name_in_program¶
A mapping from input id to the placeholder name in
CompiledFunction.pytato_program. Input id is represented as the position off’s argument augmented with the leaf array’s key if the argument is an array container.
- abstractmethod __call__(arg_id_to_arg) Any[source]¶
- Parameters:
arg_id_to_arg – Mapping from input id to the passed argument. See
CompiledFunction.input_id_to_name_in_programfor input id’s representation.
Utils¶
- arraycontext.impl.pytato.utils.transfer_from_numpy(expr: ArrayOrNamesTc, actx: ArrayContext) ArrayOrNamesTc[source]¶
Transfer arrays contained in
DataWrapperinstances to be device arrays, usingfrom_numpy().
- arraycontext.impl.pytato.utils.transfer_to_numpy(expr: ArrayOrNamesTc, actx: ArrayContext) ArrayOrNamesTc[source]¶
Transfer arrays contained in
DataWrapperinstances to benumpy.ndarrayinstances, usingto_numpy().
References¶
- class arraycontext.impl.pytato.utils.ArrayOrNamesTc¶
alias of TypeVar(‘ArrayOrNamesTc’, ~pytato.array.Array, ~pytato.array.AbstractResultWithNamedArrays, ~pytato.array.DictOfNamedArrays)
Array context based on jax.numpy¶
- class arraycontext.EagerJAXArrayContext[source]¶
A
ArrayContextthat usesjax.Arrayinstances for its base array class and performs all array operations eagerly. SeePytatoJAXArrayContextfor a lazier version.Note
JAX stores a global configuration state in
jax.config. Callers are expected to maintain those. Most important for scientific computing workloads beingjax_enable_x64.
numpy coverage¶
This is a list of functionality implemented by arraycontext.ArrayContext.np.
Note
Only functions and methods that have at least one implementation are listed.
Array creation routines¶
Function |
||||
|---|---|---|---|---|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
No |
No |
Array manipulation routines¶
Function |
||||
|---|---|---|---|---|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
No |
Yes |
Yes |
Yes |
|
No |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
Linear algebra¶
Function |
||||
|---|---|---|---|---|
Yes |
Yes |
No |
No |
Logic Functions¶
Function |
||||
|---|---|---|---|---|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
Mathematical functions¶
Function |
||||
|---|---|---|---|---|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |
|
Yes |
Yes |
Yes |
Yes |