API Reference
Contents
API Reference#
Einsum Nodes#
- class feinsum.einsum.FusedEinsum(arg_shapes, value_to_dtype, access_descriptors, use_matrix, index_names)[source]#
A fused einsum expression.
- shape#
- ndim#
- class feinsum.einsum.VeryLongAxis[source]#
Describes a dimension length which is to be assumed to be very large.
- class feinsum.einsum.EinsumAxisAccess[source]#
Base class for axis access types in an einsum expression.
- class feinsum.einsum.FreeAxis(output_index)[source]#
Records the axis of an einsum argument over which contraction is not performed.
- output_index#
Position of the corresponding index in the einsum’s output.
- class feinsum.einsum.SummationAxis(index)[source]#
Records an index in an einsum expression over which reduction is performed. Sometimes also referred to as an axis with a corresponding “dummy index” in Ricci Calculus.
- index#
An integer which is unique to a reduction index of an einsum.
- class feinsum.einsum.Argument[source]#
An abstract class denoting an argument to an einsum in
ContractionSchedule. SeeContractionSchedule.arguments.
- class feinsum.einsum.EinsumOperand(ioperand)[source]#
An
Argumentrepresenting the ioperand-th argument that was passed to the parent einsum whoseContractionScheduleis being specified.
- class feinsum.einsum.IntermediateResult(name)[source]#
An
Argumentrepresenting an intermediate result available during the current contraction.
- class feinsum.einsum.ContractionSchedule(subscripts, result_names, arguments)[source]#
Records the schedule in which contractions are to be performed in an einsum as a series of einsums with the i-th einsum having subscript
subscript[i]operating onarguments[i]and writing its result toresult_names[i].- result_names#
Names of the result generated by each step.
- nsteps#
Helper routines#
- feinsum.einsum.get_trivial_contraction_schedule(einsum)[source]#
Returns the
ContractionSchedulefor einsum scheduled as a single contraction.- Return type
- feinsum.einsum.get_opt_einsum_contraction_schedule(expr, **opt_einsum_kwargs)[source]#
Returns a
ContractionScheduleas computed byopt_einsum.contract_path().- Parameters
opt_einsum_kwargs (
Any) – kwargs to be passed toopt_einsum.contract_path().
Note
The following defaults are populated in opt_einsum_kwargs, if left unspecified:
optimize="optimal"use_blas=False
- Return type
Routines for empirical evaluation#
- feinsum.measure.timeit(einsum, *, transform, cl_ctx, long_dim_length=100000, schedule=None)[source]#
Returns the runtime in seconds for executing einsum on OpenCL context cl_ctx.
- Parameters
transform (
TransformT) – The transformation to be applied toloopy.TranslationUnitlowered from einsum.- Return type
- feinsum.measure.measure_giga_op_rate(expr, *, transform, cl_ctx, long_dim_length=100000, schedule=None)[source]#
Returns the arithmetic operations rate (in Giga Ops per second) by arithmetic operation’s result dtypes.
- Return type
Map
- feinsum.measure.stringify_comparison_vs_roofline(expr, *, schedule=None, transform, cl_ctx, long_dim_length=100000, ignore_unknown_device=False)[source]#
Returns the prettified comparison of expr transformed with transform wrt roofline. The roofline model assumes that kernel’s performance is limited by either the device’s global memory bandwidth or the device’s floating point units being saturated to their maximum throughput.
Helpers to build feinsum.einsum.FusedEinsum#
- feinsum.make_einsum.einsum(subscripts, *operands, arg_names=None)[source]#
Returns a
FusedEinsumwith an interface similar tonumpy.einsum().
- feinsum.make_einsum.fused_einsum(subscripts, operand_shapes, use_matrix, dtypes=None, value_to_dtype=None)[source]#
Returns a
FusedEinsumwith an interface similar tonumpy.einsum().- Parameters
subscripts (
str) – Astrdescribing the Einstein summation as accepted bynumpy.einsum().dtypes (
Union[dtype[Any],None,Type[Any],_SupportsDType[dtype[Any]],str,Tuple[Any,int],Tuple[Any,Union[SupportsIndex,Sequence[SupportsIndex]]],List[Any],_DTypeDict,Tuple[Any,Any]]) – The dtype of all the value the operands use. Cannot be provide both value_to_dtype and dtypes.value_to_dtype (
Optional[Mapping[str,Union[dtype[Any],None,Type[Any],_SupportsDType[dtype[Any]],str,Tuple[Any,int],Tuple[Any,Union[SupportsIndex,Sequence[SupportsIndex]]],List[Any],_DTypeDict,Tuple[Any,Any]]]]) – A mapping from the values the operands of the einsum depend on to their dtypes. Cannot be provide both value_to_dtype and dtypes.operand_shapes (
Sequence[Any]) – A sequence of shapes of the operands of the Einstein Summation.use_matrix (
Any) – A 2Dnumpyarray-like object, wherei,j-th entry corresponds to thefrozensetofstrof value names that thej-th operand of thei-th einsum accesses.
- Return type
Interacting with code-transformation databases#
- feinsum.sql_utils.query(einsum, cl_ctx, *, database='/home/docs/checkouts/readthedocs.org/user_builds/feinsum/conda/latest/lib/python3.10/site-packages/feinsum/../../data/transform_archive_v3.sqlite', err_if_no_results=False)[source]#
Returns facts of previous recorded runs of einsum on cl_ctx.
- Parameters
err_if_no_results (
bool) – If True, raises aRuntimeErrorif no recorded runs corresponding to einsum are available in database. Defaults to False.- Return type
Some helper loopy transformations relevant for Einstein Summations#
loopy helpers. The aim is to keep this module as lean as possible by
upstreaming the heavy lifting parts to loopy itself.
- feinsum.loopy_utils.get_a_matched_einsum(t_unit, kernel_name=None, insn_match=None, argument_substitutions=None, long_dim_length=500)[source]#
Returns a tuple of the form
(matched_einsum, subst_map)where,matched_einsumis the batched einsum having a memory access pattern similar to the instructions in insn_match of t_unit, and,subst_mapis a mapping from the variables in t_unit to the entities (i.e. indices, arguments) of match_einsum.- Parameters
t_unit (
TranslationUnit) – The subject translation unit which is being matched against ref_einsum.insn_match (
Optional[Any]) – A match expression as understood byloopy.match.parse_match()representing the subset of the kernel’s instructions raised to a batched einsum.argument_substitutions (
Optional[FrozenSet[str]]) – Afrozensetof substitutions that are to be matched as an Einstein-summation’s argument. If None, any substitution rule invocation is treated as accessing an Einstein Summation’s argument.long_dim_length (
int) – Axis length above which can be assumed to be long enough to be invariant of having any noticeable effects on the kernel’s performance.
Note
See xxx (TODO) for a description of a translation unit with a einsum-like memory access pattern.
- Return type
Tuple[FusedEinsum,frozenbidict[str,str]]
- feinsum.loopy_utils.match_t_unit_to_einsum(t_unit, einsum, *, kernel_name=None, insn_match=None, argument_substitutions=None, long_dim_length=500)[source]#
Returns a mapping from the entities of einsum to the variables of the corresponding matched einsum in t_unit. See
get_a_matched_einsum()for a subset of grammar ofloopykernels to be a matched as a batched einsum.
Lowering to loopy#
- feinsum.codegen.loopy.generate_loopy(einsum, schedule=None)[source]#
Returns a
loopy.TranslationUnitwith the reductions scheduled by contract_path.- Parameters
schedule (
Optional[ContractionSchedule]) – An optional instance ofContractionSchedule. Defaults to the trivial contraction schedule if not provided.- Return type
- feinsum.codegen.loopy.generate_loopy_with_opt_einsum_schedule(expr, **opt_einsum_kwargs)[source]#
Returns a
loopy.TranslationUnitwith theContractionSchedulespecified viaopt_einsum.contract_path().- Parameters
opt_einsum_kwargs (
Any) – kwargs to be passed toget_opt_einsum_contraction_schedule().- Return type
Canonicalization#
Miscellaneous Utilities#
- feinsum.utils.has_similar_subscript(einsum, subscript)[source]#
Returns True only if einsum’s expression being applied to its operands is equivalently represented by subscript.
>>> import feinsum as f >>> ensm = f.einsum("ij,j->i", ... f.array((10, 4), "float64"), ... f.array(4, "float64")) >>> has_similar_subscript(ensm, "ij,j->i") True >>> has_similar_subscript(ensm, "ik,k->i") True >>> has_similar_subscript(ensm, "ik,kj->ij") False >>> ensm = f.einsum("ik,kj->ij", ... f.array((10, 4), "float64"), ... f.array((4, 100), "float64")) >>> has_similar_subscript(ensm, "ij,j->i") False >>> has_similar_subscript(ensm, "ik,kj->ij") True >>> has_similar_subscript(ensm, "pq,qr->pr") True
- Return type
- feinsum.utils.is_any_redn_dim_parametric(einsum)[source]#
>>> import feinsum as f >>> ensm = f.einsum("ij,j->i", ... f.array((10, 4), "float64"), ... f.array(4, "float64")) >>> is_any_redn_dim_parametric(ensm) False >>> ensm = f.einsum("ij,jk->i", ... f.array((10, 4), "float64"), ... f.array((4, 20), "float64")) >>> is_any_redn_dim_parametric(ensm) False >>> ensm = f.einsum("i,j->i", ... f.array(125, "float64"), ... f.array(np.inf, "float64")) >>> is_any_redn_dim_parametric(ensm) True
- Return type