Internals

Generic Code Generation Functionality

class pytato.codegen.CodeGenPreprocessor(target: pytato.target.Target)[source]

A mapper that preprocesses graphs to simplify code generation.

The following node simplifications are performed:

class pytato.codegen.PreprocessResult(outputs: 'DictOfNamedArrays', compute_order: 'Tuple[str, ...]', bound_arguments: 'Dict')[source]
pytato.codegen.preprocess(outputs: pytato.array.DictOfNamedArrays, target: pytato.target.Target) pytato.codegen.PreprocessResult[source]

Preprocess a computation for code generation.

pytato.codegen.normalize_outputs(result: Union[pytato.array.Array, pytato.array.DictOfNamedArrays, Dict[str, pytato.array.Array]]) pytato.array.DictOfNamedArrays[source]

Convert outputs of a computation to the canonical form.

Performs a conversion to DictOfNamedArrays if necessary.

Parameters

result – Outputs of the computation.

loopy Code Generation

class pytato.target.loopy.codegen.PersistentExpressionContext(state: pytato.target.loopy.codegen.CodeGenState, _depends_on: typing.FrozenSet[str] = <factory>)[source]

Mutable state used while generating loopy expressions for a ImplementedResult. Wraps CodeGenState with more expression-specific information.

This data is passed through InlinedExpressionGenMapper via arguments, and is also used by ImplementedResult.to_loopy_expression() to retrieve contextual data.

state

The CodeGenState.

depends_on

The set of statement IDs that need to be included in loopy.InstructionBase.depends_on.

update_depends_on(other: FrozenSet[str]) None[source]
class pytato.target.loopy.codegen.LocalExpressionContext(num_indices: int, local_namespace: Mapping[str, pytato.array.Array], reduction_bounds: Mapping[str, Tuple[Union[numbers.Number, int, numpy.bool_, bool, pymbolic.primitives.Expression], Union[numbers.Number, int, numpy.bool_, bool, pymbolic.primitives.Expression]]])[source]

Records context being to be conveyed from a parent expression to its sub-expressions.

local_namespace

A (read-only) local name mapping used for name lookup when generating code.

num_indices

The number of indices of the form _0, _1, allowed in the expression.

lookup(name: str) pytato.array.Array[source]
class pytato.target.loopy.codegen.ImplementedResult[source]

Generated code for a node in the computation graph (i.e., an array expression).

abstract to_loopy_expression(indices: Tuple[Union[int, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, pymbolic.primitives.Expression], ...], expr_context: pytato.target.loopy.codegen.PersistentExpressionContext) Union[numbers.Number, int, numpy.bool_, bool, pymbolic.primitives.Expression][source]

Return a loopy expression for this result.

Parameters
  • indices – symbolic expressions for the indices of the array

  • expr_context

    the associated expression context. The fields are treated as follows:

    • depends_on is populated with any dependencies needed for the generated expression.

class pytato.target.loopy.codegen.StoredResult(name: str, num_indices: int, depends_on: FrozenSet[str])[source]

An array expression generated as a loopy array.

See also: pytato.tags.ImplStored.

class pytato.target.loopy.codegen.InlinedResult(expr: Union[numbers.Number, int, numpy.bool_, bool, pymbolic.primitives.Expression], num_indices: int, depends_on: FrozenSet[str])[source]

An array expression generated as a loopy expression containing inlined sub-expressions.

See also: pytato.tags.ImplInlined.

class pytato.target.loopy.codegen.CodeGenState(_program: Union[loopy.translation_unit.TranslationUnit, loopy.kernel.LoopKernel], results: Dict[pytato.array.Array, pytato.target.loopy.codegen.ImplementedResult])[source]

A container for data kept by CodeGenMapper.

_program

The partial loopy.LoopKernel or loopy.TranslationUnit being built.

results

A mapping from pytato.Array instances to instances of ImplementedResult.

var_name_gen
insn_id_gen
update_kernel(kernel: loopy.kernel.LoopKernel) None[source]
class pytato.target.loopy.codegen.CodeGenMapper(array_tag_t_to_not_propagate: FrozenSet[Type[pytools.tag.Tag]], axis_tag_t_to_not_propagate: FrozenSet[Type[pytools.tag.Tag]])[source]

A mapper for generating code for nodes in the computation graph.

class pytato.target.loopy.codegen.InlinedExpressionGenMapper(codegen_mapper: pytato.target.loopy.codegen.CodeGenMapper)[source]

A mapper for generating loopy expressions with inlined sub-expressions.

The inputs to this mapper are scalar expression as found in pytato.array.IndexLambda, or expressions that are compatible (e.g., shape expressions).

The outputs of this mapper are scalar expressions suitable for wrapping in InlinedResult.

pytato.target.loopy.codegen.domain_for_shape(dim_names: Tuple[str, ...], shape: Tuple[Union[numbers.Number, int, numpy.bool_, bool, pymbolic.primitives.Expression], ...], reductions: Dict[str, Tuple[Union[numbers.Number, int, numpy.bool_, bool, pymbolic.primitives.Expression], Union[numbers.Number, int, numpy.bool_, bool, pymbolic.primitives.Expression]]]) islpy._isl.BasicSet[source]

Create an islpy.BasicSet that expresses an appropriate index domain for an array of (potentially symbolic) shape shape having reduction dimensions reductions.

Parameters
  • dim_names – A tuple of strings, the names of the axes. These become set dimensions in the returned domain.

  • shape – A tuple of constant or quasi-affine pymbolic expressions. The variables in these expressions become parameter dimensions in the returned set. Must have the same length as dim_names.

  • reductions – A map from reduction inames to (lower, upper) bounds (as half-open integer ranges). The variables in the bounds become parameter dimensions in the returned set.

pytato.target.loopy.codegen.get_loopy_temporary(name: str, expr: pytato.array.Array, cgen_mapper: pytato.target.loopy.codegen.CodeGenMapper, state: pytato.target.loopy.codegen.CodeGenState) loopy.kernel.data.TemporaryVariable[source]
pytato.target.loopy.codegen.add_store(name: str, expr: pytato.array.Array, result: pytato.target.loopy.codegen.ImplementedResult, state: pytato.target.loopy.codegen.CodeGenState, cgen_mapper: pytato.target.loopy.codegen.CodeGenMapper, output_to_temporary: bool = False) str[source]

Add an instruction that stores to a variable in the kernel.

Parameters
  • name – name of the output array, which is created

  • expr – the Array to store

  • result – the corresponding ImplementedResult

  • state – code generation state

  • output_to_temporary – whether to generate an output argument (default) or a temporary variable

Returns

the id of the generated instruction

pytato.target.loopy.codegen.normalize_outputs(result: Union[pytato.array.Array, pytato.array.DictOfNamedArrays, Dict[str, pytato.array.Array]]) pytato.array.DictOfNamedArrays[source]

Convert outputs of a computation to the canonical form.

Performs a conversion to DictOfNamedArrays if necessary.

Parameters

result – Outputs of the computation.

pytato.target.loopy.codegen.get_initial_codegen_state(target: pytato.target.loopy.LoopyTarget, options: loopy.options.Options) pytato.target.loopy.codegen.CodeGenState[source]