Skip to content
Open
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions einops/_torch_specific.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ def reshape(x, shape: list[int]):
def apply_for_scriptable_torch(
recipe: TransformRecipe, tensor: torch.Tensor, reduction_type: str, axes_dims: list[tuple[str, int]]
) -> torch.Tensor:
"""
Apply an einops transformation recipe to a tensor using only torchscript-compatible operations.

This function mirrors ``einops.einops._apply_recipe`` but uses ``TorchJitBackend``
so that the result can be scripted with ``torch.jit.script``.

Args:
recipe: A parsed ``TransformRecipe`` describing the transformation.
tensor: Input tensor to transform.
reduction_type: Reduction operation name (e.g. ``'min'``, ``'max'``, ``'sum'``,
``'mean'``, ``'prod'``). Ignored when the recipe contains no reduced axes.
axes_dims: List of ``(axis_name, axis_length)`` pairs providing concrete
dimension sizes for named axes.

Returns:
Transformed tensor after reshape, transpose, reduction, and added-axes steps.
"""
backend = TorchJitBackend
(
init_shapes,
Expand Down